Skip to content

Commit 525bfa8

Browse files
committed
Refactor source file loader
1 parent 2864b5e commit 525bfa8

File tree

5 files changed

+117
-105
lines changed

5 files changed

+117
-105
lines changed

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/SwanPipeline.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ public void run() throws IOException, InterruptedException {
135135
for (int x = 0; x < 10; x++)
136136
predictions.put(Integer.toString(x), new HashSet<>());
137137

138-
runClassEvaluation(options.getSrmClasses(), feature, learnerMode );
138+
//Load methods from the test set
139+
logger.info("Loading test JARs in {}", options.getTestDataDir());
140+
SourceFileLoader testDataset = new SourceFileLoader(options.getTestDataDir());
141+
testDataset.load(dataset.getMethods());
139142

140143
// Save data from last classification.
141144
loader.resetMethods();

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/features/code/soot/Loader.java

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package de.fraunhofer.iem.swan.features.code.soot;
2+
3+
import de.fraunhofer.iem.swan.data.Method;
4+
import de.fraunhofer.iem.swan.features.code.type.AbstractSootFeature;
5+
import de.fraunhofer.iem.swan.util.SootUtils;
6+
import de.fraunhofer.iem.swan.util.Util;
7+
import soot.Scene;
8+
import soot.SootClass;
9+
import soot.SootMethod;
10+
11+
import java.io.IOException;
12+
import java.util.HashSet;
13+
import java.util.Set;
14+
15+
public class SourceFileLoader {
16+
17+
private final String classpath;
18+
private Set<Method> methods = new HashSet<>();
19+
Set<String> testClasses;
20+
21+
public SourceFileLoader(String sourceFileDir) throws IOException {
22+
23+
testClasses = Util.getAllClassesFromDirectory(sourceFileDir);
24+
this.classpath = Util.buildCP(sourceFileDir);
25+
}
26+
27+
public void load(final Set<Method> trainingSet) {
28+
loadMethodsFromTestLib();
29+
Util.createSubclassAnnotations(methods, classpath);
30+
methods = Util.sanityCheck(methods, trainingSet);
31+
}
32+
33+
public void loadMethodsFromTestLib() {
34+
35+
new AbstractSootFeature(classpath) {
36+
37+
@Override
38+
public Type appliesInternal(Method method) {
39+
40+
for (String className : testClasses) {
41+
SootClass sc = Scene.v().forceResolve(className, SootClass.HIERARCHY);
42+
43+
if (sc == null || !testClasses.contains(sc.getName()))
44+
continue;
45+
46+
if (!sc.isInterface() && !sc.isPrivate())
47+
for (SootMethod sm : sc.getMethods()) {
48+
if (sm.isConcrete()) {
49+
// This is done by hand here because of the cases where the
50+
// character ' is in the signature. This is not supported by the
51+
// current Soot.
52+
// TODO: Get Soot to support the character '
53+
methods.add(SootUtils.convertSootSignature(sm.getSignature()));
54+
}
55+
}
56+
}
57+
return Type.NOT_SUPPORTED;
58+
}
59+
60+
}.applies(new Method("a", "void", "x.y"));
61+
}
62+
63+
public Set<Method> getMethods() {
64+
return methods;
65+
}
66+
67+
public String getClasspath() {
68+
return classpath;
69+
}
70+
}

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/training/TrainingSetUpdater.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ public static Set<Method> extractMethodData(HashSet<String> classes, HashSet<Str
133133
methodSet.put(t.substring(0, t.indexOf("|")), t.substring(t.indexOf("|") + 1));
134134
}
135135

136-
Loader loader = new Loader(classpath);
137-
loader.loadMethodsFromTestLib(testClasses);
136+
SourceFileLoader sourceFileLoader = new SourceFileLoader(classpath);
137+
sourceFileLoader.loadMethodsFromTestLib();
138138

139-
Util.createSubclassAnnotations(loader.methods(), classpath);
139+
Util.createSubclassAnnotations(sourceFileLoader.getMethods(), classpath);
140140

141141
HashSet<Method> trainingMethods = new HashSet<>();
142142

143143

144-
for (Method m : loader.methods()) {
144+
for (Method m : sourceFileLoader.getMethods()) {
145145

146146
String sig = m.getClassName() + "." + m.getMethodName();
147147

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package de.fraunhofer.iem.swan.util;
2+
3+
import de.fraunhofer.iem.swan.data.Method;
4+
import java.util.Arrays;
5+
6+
public class SootUtils {
7+
8+
public static Method convertSootSignature(String sootSignature){
9+
10+
//Ignore < and > at the beginning and end of the signature
11+
String[] parts = sootSignature.substring(1, sootSignature.length()-1).split(":");
12+
13+
String className = parts[0];
14+
String[] method = parts[1].trim().split(" ");
15+
String returnType = method[0];
16+
String methodName = method[1].substring(0, method[1].indexOf("("));
17+
String[] paramList = method[1].substring(method[1].indexOf("(") + 1, method[1].indexOf(")")).split(",");
18+
19+
//TODO correctly handle <init> and <clinit>
20+
if (methodName.contains("<init>"))
21+
methodName = getSimpleName(className);
22+
23+
return new Method(methodName, Arrays.asList(paramList), returnType, className);
24+
}
25+
26+
/**
27+
* Returns last part of a fully qualified name string.
28+
*
29+
* @param value Namespace of object
30+
* @return returns simple name
31+
*/
32+
public static String getSimpleName(String value) {
33+
34+
if (!value.contains("."))
35+
return value;
36+
else
37+
return value.substring(value.lastIndexOf(".") + 1);
38+
}
39+
}

0 commit comments

Comments
 (0)