Skip to content

Commit 5d298ed

Browse files
committed
Rename Method data members
1 parent 525bfa8 commit 5d298ed

14 files changed

+140
-147
lines changed

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/data/Method.java

Lines changed: 83 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.fraunhofer.iem.swan.data;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.fasterxml.jackson.annotation.JsonValue;
35
import org.apache.commons.lang3.StringUtils;
46
import soot.SootMethod;
57
import soot.Type;
@@ -8,6 +10,7 @@
810
import java.util.HashSet;
911
import java.util.List;
1012
import java.util.Set;
13+
import java.util.stream.Collectors;
1114

1215
/**
1316
* Class representing a single method
@@ -17,51 +20,65 @@
1720
public class Method {
1821

1922
// Inherited by SootMethodAndClass (from Soot Infoflow)
20-
private final String methodName;
21-
private final String className;
22-
private final String returnType;
23-
private final List<String> parameters;
24-
25-
private String subSignature = null;
26-
private String signature = null;
23+
private String name;
24+
private String className;
25+
@JsonProperty("return")
26+
private String returnType;
27+
private List<String> parameters;
28+
private String subSignature;
29+
private String signature;
2730
private int hashCode = 0;
2831

29-
public enum SecLevel {
30-
HIGH, LOW, NEUTRAL;
31-
}
32+
public enum SecurityLevel {
33+
HIGH("high"), LOW("low"), NEUTRAL("neutral"), NONE("none");
34+
35+
private final String level;
36+
37+
SecurityLevel(String level ) {
38+
this.level = level;
39+
}
3240

33-
private String framework = "";
34-
private String link = "";
35-
private String comment = "";
36-
private String discovery = "";
37-
private SecLevel secLevel = SecLevel.NEUTRAL;
38-
private RelevantPart dataIn = new RelevantPart();
39-
private RelevantPart dataOut = new RelevantPart();
40-
// private Set<CWE> cwes = new HashSet<CWE>();
41-
private Set<Category> categoriesTrained = new HashSet<Category>();
42-
private Set<Category> categoriesClassified = new HashSet<Category>();
41+
@JsonValue
42+
public String getLevel() {
43+
return level;
44+
}
45+
}
4346

44-
private Category categoryClassified = null;
47+
private String framework;
48+
private String link;
49+
private String comment;
50+
private String discovery;
51+
private SecurityLevel securityLevel;
52+
private RelevantPart dataIn ;
53+
private RelevantPart dataOut;
54+
@JsonProperty("type")
55+
private Set<Category> srm;
56+
private Set<Category> cwe;
4557

4658
private Javadoc javadoc = new Javadoc();
47-
private String sourceJar = "";
59+
@JsonProperty("jar")
60+
private String sourceJar;
4861

49-
public Method(String methodName, String returnType, String className) {
50-
this.methodName = methodName;
62+
public Method() {
63+
cwe = new HashSet<>();
64+
}
65+
66+
public Method(String name, String returnType, String className) {
67+
this.name = name;
5168
this.className = className;
5269
this.returnType = returnType;
53-
this.parameters = new ArrayList<String>();
70+
this.parameters = new ArrayList<>();
5471
}
5572

56-
public Method(String methodName, List<String> parameters, String returnType, String className) {
57-
this.methodName = methodName;
73+
public Method(String name, List<String> parameters, String returnType, String className) {
74+
this.name = name;
5875
this.className = className;
5976
this.returnType = returnType;
6077
this.parameters = parameters;
6178
}
6279

6380
public Method(SootMethod sm) {
64-
this.methodName = sm.getName();
81+
this.name = sm.getName();
6582
this.className = sm.getDeclaringClass().getName();
6683
this.returnType = sm.getReturnType().toString();
6784
this.parameters = new ArrayList<String>();
@@ -70,42 +87,34 @@ public Method(SootMethod sm) {
7087
}
7188

7289
public Method(Method methodAndClass) {
73-
this.methodName = methodAndClass.methodName;
90+
this.name = methodAndClass.name;
7491
this.className = methodAndClass.className;
7592
this.returnType = methodAndClass.returnType;
7693
this.parameters = new ArrayList<String>(methodAndClass.parameters);
7794
}
7895

7996
public Method deriveWithNewClass(String className) {
80-
Method m = new Method(this.getMethodName(), this.getParameters(), this.getReturnType(), className);
97+
Method m = new Method(this.getName(), this.getParameters(), this.getReturnType(), className);
8198
m.setFramework(this.framework);
8299
m.setLink(this.link);
83100
m.setComment(this.comment);
84101
m.setDiscovery(this.discovery);
85-
m.setSecLevel(m.secLevel);
102+
m.setSecurityLevel(m.securityLevel);
86103
m.setDataIn(this.dataIn);
87104
m.setDataOut(this.dataOut);
88105
// m.setCwes(this.cwes);
89-
m.setCategoriesTrained(this.categoriesTrained);
90-
m.setCategoryClassified(this.categoryClassified);
106+
m.setSrm(this.srm);
91107
return m;
92108
}
93109

94-
@Override
95-
public String toString() {
96-
if (this.categoryClassified == null)
97-
return "";
98-
return getSignature() + " ->_" + this.categoryClassified.toString().toUpperCase() + "_";
99-
}
100-
101110
/**
102111
* Gets whether this method has been annotated as a source, sink, neither,
103112
* sanitizer or authentication nor.
104113
*
105114
* @return True if there is an annotation for this method, otherwise false.
106115
*/
107116
public boolean isAnnotated() {
108-
return !this.categoriesTrained.isEmpty();
117+
return !this.srm.isEmpty();
109118
}
110119

111120
public String getFramework() {
@@ -140,12 +149,12 @@ public void setDiscovery(String discovery) {
140149
this.discovery = discovery;
141150
}
142151

143-
public SecLevel getSecLevel() {
144-
return secLevel;
152+
public SecurityLevel getSecurityLevel() {
153+
return securityLevel;
145154
}
146155

147-
public void setSecLevel(SecLevel secLevel) {
148-
this.secLevel = secLevel;
156+
public void setSecurityLevel(SecurityLevel securityLevel) {
157+
this.securityLevel = securityLevel;
149158
}
150159

151160
public RelevantPart getDataIn() {
@@ -164,45 +173,42 @@ public void setDataOut(RelevantPart dataOut) {
164173
this.dataOut = dataOut;
165174
}
166175

167-
public Category getCategoryClassified() {
168-
return categoryClassified;
169-
}
170-
171-
public void setCategoryClassified(Category category) {
172-
this.categoryClassified = category;
176+
public Set<Category> getSrm() {
177+
return srm;
173178
}
174179

175-
public Set<Category> getCategoriesTrained() {
176-
return categoriesTrained;
177-
}
180+
public Set<Category> getAuthSrm() {
178181

179-
public void setCategoriesTrained(Set<Category> categoriesTrained) {
180-
this.categoriesTrained = categoriesTrained;
182+
return srm.stream().filter(Category::isAuthentication).collect(Collectors.toSet());
181183
}
182184

183-
public void addCategoriesTrained(Set<Category> categories) {
184-
this.categoriesTrained.addAll(categories);
185+
public void setSrm(Set<Category> srm) {
186+
this.srm = srm;
185187
}
186188

187-
public void addCategoryTrained(Category category) {
188-
this.categoriesTrained.add(category);
189+
public void setCwe(Set<Category> categories) {
190+
this.cwe = categories;
189191
}
190192

191-
public Set<Category> getCategoriesClassified() {
192-
return this.categoriesClassified;
193+
public Set<Category> getCwe() {
194+
return this.cwe;
193195
}
194196

195197
public void addCategoryClassified(Category category) {
196-
this.categoriesClassified.add(category);
198+
this.cwe.add(category);
197199
}
198200

199201
// Inherited from SootMethodAndClass (from Soot Infoflow)
200-
public String getMethodName() {
201-
return this.methodName;
202+
public String getName() {
203+
return this.name;
202204
}
203205

204206
public String getClassName() {
205-
return this.className;
207+
208+
if(name.contains("."))
209+
return name.substring(0, name.lastIndexOf("."));
210+
else
211+
return name;
206212
}
207213

208214
public String getReturnType() {
@@ -218,12 +224,12 @@ public String getSubSignature() {
218224
return subSignature;
219225

220226
StringBuilder sb = new StringBuilder(
221-
10 + this.returnType.length() + this.methodName.length() + (this.parameters.size() * 30));
227+
10 + this.returnType.length() + this.name.length() + (this.parameters.size() * 30));
222228
if (!this.returnType.isEmpty()) {
223229
sb.append(this.returnType);
224230
sb.append(" ");
225231
}
226-
sb.append(this.methodName);
232+
sb.append(trimProperty(this.name));
227233
sb.append("(");
228234

229235
for (int i = 0; i < this.parameters.size(); i++) {
@@ -247,16 +253,16 @@ public String getSignature() {
247253
if (signature != null)
248254
return signature;
249255

250-
StringBuilder sb = new StringBuilder(10 + this.className.length() + this.returnType.length()
251-
+ this.methodName.length() + (this.parameters.size() * 30));
256+
StringBuilder sb = new StringBuilder(10 + getClassName().length() + this.returnType.length()
257+
+ this.name.length() + (this.parameters.size() * 30));
252258
sb.append("<");
253-
sb.append(this.className);
259+
sb.append(getClassName());
254260
sb.append(": ");
255261
if (!this.returnType.isEmpty()) {
256262
sb.append(this.returnType);
257263
sb.append(" ");
258264
}
259-
sb.append(this.methodName);
265+
sb.append(trimProperty(getName()));
260266
sb.append("(");
261267

262268
for (int i = 0; i < this.parameters.size(); i++) {
@@ -276,8 +282,7 @@ public String getSignature() {
276282
*/
277283
public String getSimpleSignature() {
278284

279-
return trimProperty(getReturnType()) + " " + trimProperty(getMethodName()) + " (" + StringUtils.join(getParameters(true), ", ") + ")";
280-
285+
return trimProperty(getReturnType()) + " " + trimProperty(getName()) + " (" + StringUtils.join(getParameters(true), ", ") + ")";
281286
}
282287

283288
/**
@@ -316,12 +321,10 @@ private String trimProperty(String property) {
316321
*/
317322
public String getJavaSignature() {
318323

319-
320-
String methodName = getMethodName();
321-
if (getMethodName().equals("<init>"))
324+
String methodName = getName();
325+
if (getName().equals("<init>"))
322326
methodName = getClassName().substring(getClassName().lastIndexOf(".") + 1);
323327

324-
325328
return this.returnType + " " + this.className + "." + methodName + "(" + StringUtils.join(this.parameters, ", ") + ")";
326329
}
327330

@@ -354,7 +357,7 @@ public boolean equals(Object another) {
354357
return false;
355358
Method otherMethod = (Method) another;
356359

357-
if (!this.methodName.equals(otherMethod.methodName))
360+
if (!this.name.equals(otherMethod.name))
358361
return false;
359362
if (!this.parameters.equals(otherMethod.parameters))
360363
return false;
@@ -366,7 +369,7 @@ public boolean equals(Object another) {
366369
@Override
367370
public int hashCode() {
368371
if (this.hashCode == 0)
369-
this.hashCode = this.methodName.hashCode() + this.className.hashCode() * 5;
372+
this.hashCode = this.name.hashCode() * 5;
370373
// The parameter list is available from the outside, so we can't cache it
371374
return this.hashCode + this.parameters.hashCode() * 7;
372375
}

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/features/code/type/AbstractSootFeature.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ protected SootMethod getSootMethod(Method method, boolean lookInHierarchy) {
4747
Scene.v().forceResolve(method.getClassName(), SootClass.BODIES);
4848

4949
if (c == null || c.isPhantom()) {
50-
System.err.println("Class " + method.getClassName() + " not found");
5150
return null;
5251
}
5352

@@ -57,9 +56,10 @@ protected SootMethod getSootMethod(Method method, boolean lookInHierarchy) {
5756
while (c != null) {
5857
// Does the current class declare the method we are looking for?
5958
if (method.getReturnType().isEmpty()) {
60-
if (c.declaresMethodByName(method.getMethodName()))
61-
return c.getMethodByName(method.getMethodName());
59+
if (c.declaresMethodByName(method.getName()))
60+
return c.getMethodByName(method.getName());
6261
} else {
62+
//System.out.println(method.getSubSignature());
6363
if (c.declaresMethod(method.getSubSignature()))
6464
return c.getMethod(method.getSubSignature());
6565
}

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/features/code/type/IsImplicitMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class IsImplicitMethod extends WeightedFeature implements IFeature {
1212

1313
@Override
1414
public Type applies(Method method) {
15-
return (method.getMethodName().contains("$") ? Type.TRUE : Type.FALSE);
15+
return (method.getName().contains("$") ? Type.TRUE : Type.FALSE);
1616
}
1717

1818
@Override

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/features/code/type/IsThreadRunFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public IsThreadRunFeature(String cp) {
1717

1818
@Override
1919
public Type appliesInternal(Method method) {
20-
if (!method.getMethodName().equals("run"))
20+
if (!method.getName().equals("run"))
2121
return Type.FALSE;
2222

2323
SootMethod sm = getSootMethod(method);

swan-pipeline/src/main/java/de/fraunhofer/iem/swan/features/code/type/MethodIsConstructor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class MethodIsConstructor extends WeightedFeature implements IFeature {
1212

1313
@Override
1414
public Type applies(Method method) {
15-
if (method.getMethodName().equals("<init>")
16-
|| method.getMethodName().equals("<clinit>"))
15+
if (method.getName().equals("<init>")
16+
|| method.getName().equals("<clinit>"))
1717
return Type.TRUE;
1818
return Type.FALSE;
1919
}

0 commit comments

Comments
 (0)