diff --git a/src/main/java/clusterer/ClusterGenerator.java b/src/main/java/clusterer/ClusterGenerator.java index 23cb1f1..69404ed 100644 --- a/src/main/java/clusterer/ClusterGenerator.java +++ b/src/main/java/clusterer/ClusterGenerator.java @@ -104,20 +104,22 @@ public static void main(String[] args) { */ File mapFile = new File(options.classFieldMapFileName); - Map> fieldsOfType = new HashMap>(); + Map> fieldsOfType = new HashMap>(); 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()); - } - 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()); + } + fieldsOfType.get(key).add(sf); } } } @@ -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>> result = new ArrayList<>(); // index: field-name -> declaring class name final Map index = new HashMap<>(); - for(Collection each : fieldsOfType.values()){ - final Corpus corpus = Corpus.ofStrings(); + for (Collection each : fieldsOfType.values()) { + final Corpus corpus = Corpus.ofStrings(); each.forEach(e -> { corpus.add(e.getName()); index.put(e.getName(), e.getDeclaringClass().getName()); }); - final Map, List> relevantMaps = Introspector.generateRelevantMapping(corpus, tokenizer); - if(relevantMaps.isEmpty()) continue; + final Map, List> relevantMaps = Introspector.generateRelevantMapping(corpus, + tokenizer); + if (relevantMaps.isEmpty()) + continue; final List a = Iterables.get(relevantMaps.keySet(), 0); final List b = Iterables.get(relevantMaps.values(), 0); - final List wordList = b.isEmpty() ? a/*frequent words*/ : b/*typical words*/; + final List wordList = b.isEmpty() ? a + /* frequent words */ : b/* typical words */; - final Set relevant = wordList.stream().map(Word::element).collect(Collectors.toSet()); - final Set universe = corpus.dataSet(); + final Set relevant = wordList.stream().map(Word::element).collect(Collectors.toSet()); + final Set universe = corpus.dataSet(); Map> 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); } @@ -215,11 +220,11 @@ private static void writeToJson(Map> clusters, File outfi } } - private static void writeFieldsToJson(Map> classToFields, File outfile) { + private static void writeFieldsToJson(Map> classToFields, File outfile) { try (PrintWriter writer = new PrintWriter(outfile, "UTF-8")) { writer.println("{\n\t\"mappings\": ["); boolean first = true; - for (Entry> entry : classToFields.entrySet()) { + for (Entry> entry : classToFields.entrySet()) { if (first) { first = false; } else { @@ -240,7 +245,7 @@ private static void writeFieldsToJson(Map> 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}"); } @@ -254,12 +259,13 @@ private static void writeFieldsToJson(Map> clas } } - private static void writeMappingsToJson(List>> wordToFields, Map index, File outfile) { + private static void writeMappingsToJson(List>> wordToFields, Map index, + File outfile) { try (PrintWriter writer = new PrintWriter(outfile, "UTF-8")) { writer.println("{\n\t\"mappings\": ["); boolean first = true; for (Map> map : wordToFields) { - for(String eachKey : map.keySet()){ + for (String eachKey : map.keySet()) { final List eachValue = map.get(eachKey); if (first) { @@ -272,14 +278,14 @@ private static void writeMappingsToJson(List>> 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); @@ -294,7 +300,6 @@ private static void writeMappingsToJson(List>> wordToFi writer.print("\n\t\t}"); } - } writer.println("\n\t]\n}"); } catch (IOException e) { @@ -302,7 +307,6 @@ private static void writeMappingsToJson(List>> wordToFi } } - /** * map from FunFactory to "fun;factory" unless super class contains * "factory", then only map to "fun".