Skip to content

Commit

Permalink
removed restriction that clusterer only considers fields of Applicati…
Browse files Browse the repository at this point in the history
…onClass type. Now classes with library types and primitive types can also be clustered
  • Loading branch information
martinschaef committed Nov 29, 2016
1 parent 0ae4b59 commit d2f4674
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions src/main/java/clusterer/ClusterGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,22 @@ public static void main(String[] args) {
*/
File mapFile = new File(options.classFieldMapFileName);

Map<SootClass, Collection<SootField>> fieldsOfType = new HashMap<SootClass, Collection<SootField>>();
Map<String, Collection<SootField>> fieldsOfType = new HashMap<String, Collection<SootField>>();

for (SootClass sc : Scene.v().getApplicationClasses()) {
if (sc.resolvingLevel() >= SootClass.SIGNATURES) {
for (SootField sf : sc.getFields()) {
// ignore this referneces.
if (sf.getType() instanceof RefType && !sf.getName().startsWith("this")) {
SootClass declClass = ((RefType) sf.getType()).getSootClass();
if (declClass.isApplicationClass()) {
if (!fieldsOfType.containsKey(declClass)) {
fieldsOfType.put(declClass, new LinkedList<SootField>());
}
fieldsOfType.get(declClass).add(sf);
if (!sf.getName().startsWith("this")) {
String key = sf.getType().toString();
if (sf.getType() instanceof RefType) {
SootClass declClass = ((RefType) sf.getType()).getSootClass();
key = declClass.toString();
}
if (!fieldsOfType.containsKey(key)) {
fieldsOfType.put(key, new LinkedList<SootField>());
}
fieldsOfType.get(key).add(sf);
}
}
}
Expand All @@ -127,42 +129,45 @@ public static void main(String[] args) {

writeFieldsToJson(fieldsOfType, mapFile);


if (options.wordFieldMapFileName != null) {

final WordsTokenizer tokenizer = Tokenizers.tokenizeString();
final WordsTokenizer tokenizer = Tokenizers.tokenizeString();

final List<Map<String, List<String>>> result = new ArrayList<>();

// index: field-name -> declaring class name
final Map<String, String> index = new HashMap<>();

for(Collection<SootField> each : fieldsOfType.values()){
final Corpus<String> corpus = Corpus.ofStrings();
for (Collection<SootField> each : fieldsOfType.values()) {
final Corpus<String> corpus = Corpus.ofStrings();

each.forEach(e -> {
corpus.add(e.getName());
index.put(e.getName(), e.getDeclaringClass().getName());
});

final Map<List<Word>, List<Word>> relevantMaps = Introspector.generateRelevantMapping(corpus, tokenizer);
if(relevantMaps.isEmpty()) continue;
final Map<List<Word>, List<Word>> relevantMaps = Introspector.generateRelevantMapping(corpus,
tokenizer);
if (relevantMaps.isEmpty())
continue;

final List<Word> a = Iterables.get(relevantMaps.keySet(), 0);
final List<Word> b = Iterables.get(relevantMaps.values(), 0);

final List<Word> wordList = b.isEmpty() ? a/*frequent words*/ : b/*typical words*/;
final List<Word> wordList = b.isEmpty() ? a
/* frequent words */ : b/* typical words */;

final Set<String> relevant = wordList.stream().map(Word::element).collect(Collectors.toSet());
final Set<String> universe = corpus.dataSet();
final Set<String> relevant = wordList.stream().map(Word::element).collect(Collectors.toSet());
final Set<String> universe = corpus.dataSet();

Map<String, List<String>> wordFieldsMap = Recommend.mappingOfLabels(relevant, universe);
if(wordFieldsMap.isEmpty()) continue;
if (wordFieldsMap.isEmpty())
continue;

// removes entries where a label is mapped to list with less than two elements (e.g., food -> ())
wordFieldsMap = wordFieldsMap.entrySet().stream()
.filter(e -> e.getValue().size()>1)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
// removes entries where a label is mapped to list with less
// than two elements (e.g., food -> ())
wordFieldsMap = wordFieldsMap.entrySet().stream().filter(e -> e.getValue().size() > 1)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));

result.add(wordFieldsMap);
}
Expand Down Expand Up @@ -215,11 +220,11 @@ private static void writeToJson(Map<String, Set<SootClass>> clusters, File outfi
}
}

private static void writeFieldsToJson(Map<SootClass, Collection<SootField>> classToFields, File outfile) {
private static void writeFieldsToJson(Map<String, Collection<SootField>> classToFields, File outfile) {
try (PrintWriter writer = new PrintWriter(outfile, "UTF-8")) {
writer.println("{\n\t\"mappings\": [");
boolean first = true;
for (Entry<SootClass, Collection<SootField>> entry : classToFields.entrySet()) {
for (Entry<String, Collection<SootField>> entry : classToFields.entrySet()) {
if (first) {
first = false;
} else {
Expand All @@ -240,7 +245,7 @@ private static void writeFieldsToJson(Map<SootClass, Collection<SootField>> clas
}
writer.println("\n\t\t ],");
writer.println("\t\t \"class\":[");
writer.println("\t\t\t\"" + entry.getKey().getName() + "\"");
writer.println("\t\t\t\"" + entry.getKey() + "\"");
writer.println("\t\t ]");
writer.print("\n\t\t}");
}
Expand All @@ -254,12 +259,13 @@ private static void writeFieldsToJson(Map<SootClass, Collection<SootField>> clas
}
}

private static void writeMappingsToJson(List<Map<String, List<String>>> wordToFields, Map<String, String> index, File outfile) {
private static void writeMappingsToJson(List<Map<String, List<String>>> wordToFields, Map<String, String> index,
File outfile) {
try (PrintWriter writer = new PrintWriter(outfile, "UTF-8")) {
writer.println("{\n\t\"mappings\": [");
boolean first = true;
for (Map<String, List<String>> map : wordToFields) {
for(String eachKey : map.keySet()){
for (String eachKey : map.keySet()) {
final List<String> eachValue = map.get(eachKey);

if (first) {
Expand All @@ -272,14 +278,14 @@ private static void writeMappingsToJson(List<Map<String, List<String>>> wordToFi
writer.println("\t\t \"fields\":[");
boolean firstSignature = true;

for(String eachField : eachValue){
for (String eachField : eachValue) {
if (firstSignature) {
firstSignature = false;
} else {
writer.println(",");
}
writer.print("\t\t\t\"");
if(index.containsKey(eachField)) {
if (index.containsKey(eachField)) {
writer.print(index.get(eachField) + "." + eachField);
} else {
writer.print(eachField);
Expand All @@ -294,15 +300,13 @@ private static void writeMappingsToJson(List<Map<String, List<String>>> wordToFi
writer.print("\n\t\t}");
}


}
writer.println("\n\t]\n}");
} catch (IOException e) {
e.printStackTrace(System.err);
}
}


/**
* map from FunFactory to "fun;factory" unless super class contains
* "factory", then only map to "fun".
Expand Down

0 comments on commit d2f4674

Please sign in to comment.