Skip to content

Commit 3860ac7

Browse files
committed
MULTIPLE COMMITS 4 EXPERIMENTS
ObjectTree: make fields into arrays of Strings. - The previous approach used a single string, with fields separated by dots. - The inclusion of packages in class names thwarts the previous approach.
1 parent eb2d825 commit 3860ac7

File tree

15 files changed

+374
-170
lines changed

15 files changed

+374
-170
lines changed

sdg-cli/src/main/java/es/upv/mist/slicing/cli/Slicer.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package es.upv.mist.slicing.cli;
22

3-
import com.github.javaparser.JavaParser;
4-
import com.github.javaparser.ParseResult;
5-
import com.github.javaparser.ParserConfiguration;
63
import com.github.javaparser.Problem;
4+
import com.github.javaparser.StaticJavaParser;
75
import com.github.javaparser.ast.CompilationUnit;
86
import com.github.javaparser.ast.NodeList;
97
import com.github.javaparser.ast.comments.BlockComment;
108
import com.github.javaparser.ast.nodeTypes.NodeWithName;
11-
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
12-
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
139
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
14-
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
1510
import es.upv.mist.slicing.graphs.augmented.ASDG;
1611
import es.upv.mist.slicing.graphs.augmented.PSDG;
1712
import es.upv.mist.slicing.graphs.exceptionsensitive.ESSDG;
@@ -21,12 +16,15 @@
2116
import es.upv.mist.slicing.slicing.Slice;
2217
import es.upv.mist.slicing.slicing.SlicingCriterion;
2318
import es.upv.mist.slicing.utils.NodeHashSet;
19+
import es.upv.mist.slicing.utils.StaticTypeSolver;
2420
import org.apache.commons.cli.*;
2521

2622
import java.io.File;
2723
import java.io.FileNotFoundException;
2824
import java.io.PrintWriter;
2925
import java.util.*;
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
3028
import java.util.regex.Matcher;
3129
import java.util.regex.Pattern;
3230
import java.util.stream.Stream;
@@ -175,24 +173,21 @@ public String getScVar() {
175173

176174
public void slice() throws ParseException {
177175
// Configure JavaParser
178-
ParserConfiguration parserConfig = new ParserConfiguration();
179-
parserConfig.setAttributeComments(false);
180-
CombinedTypeSolver cts = new CombinedTypeSolver();
181-
cts.add(new ReflectionTypeSolver(true));
176+
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, "Configuring JavaParser");
177+
StaticTypeSolver.addTypeSolverJRE();
182178
for (File directory : dirIncludeSet)
183-
if (directory.isDirectory())
184-
cts.add(new JavaParserTypeSolver(directory));
185-
parserConfig.setSymbolResolver(new JavaSymbolSolver(cts));
186-
JavaParser parser = new JavaParser(parserConfig);
179+
StaticTypeSolver.addTypeSolver(new JavaParserTypeSolver(directory));
180+
StaticJavaParser.getConfiguration().setAttributeComments(false);
187181

188182
// Build the SDG
183+
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, "Parsing files");
189184
Set<CompilationUnit> units = new NodeHashSet<>();
190185
List<Problem> problems = new LinkedList<>();
191186
boolean scFileFound = false;
192187
for (File file : (Iterable<File>) findAllJavaFiles(dirIncludeSet)::iterator)
193-
scFileFound |= parse(parser, file, units, problems);
188+
scFileFound |= parse(file, units, problems);
194189
if (!scFileFound)
195-
parse(parser, scFile, units, problems);
190+
parse(scFile, units, problems);
196191
if (!problems.isEmpty()) {
197192
for (Problem p : problems)
198193
System.out.println(" * " + p.getVerboseMessage());
@@ -209,16 +204,20 @@ public void slice() throws ParseException {
209204
default:
210205
throw new IllegalArgumentException("Unknown type of graph. Available graphs are SDG, ASDG, PSDG, ESSDG");
211206
}
207+
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, "Building the SDG");
212208
sdg.build(new NodeList<>(units));
213209

214210
// Slice the SDG
211+
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, "Searching for criterion and slicing");
215212
SlicingCriterion sc = new FileLineSlicingCriterion(scFile, scLine, scVar);
216213
Slice slice = sdg.slice(sc);
217214

218215
// Convert the slice to code and output the result to `outputDir`
216+
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, "Printing slice to files");
219217
for (CompilationUnit cu : slice.toAst()) {
220218
if (cu.getStorage().isEmpty())
221219
throw new IllegalStateException("A synthetic CompilationUnit was discovered, with no file associated to it.");
220+
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, "Printing slice for " + cu.getStorage().get().getFileName());
222221
String packagePath = cu.getPackageDeclaration().map(NodeWithName::getNameAsString).orElse("").replace(".", "/");
223222
File packageDir = new File(outputDir, packagePath);
224223
packageDir.mkdirs();
@@ -232,13 +231,9 @@ public void slice() throws ParseException {
232231
}
233232
}
234233

235-
private boolean parse(JavaParser parser, File file, Set<CompilationUnit> units, List<Problem> problems) {
234+
private boolean parse(File file, Set<CompilationUnit> units, List<Problem> problems) {
236235
try {
237-
ParseResult<CompilationUnit> result = parser.parse(file);
238-
if (result.isSuccessful())
239-
result.ifSuccessful(units::add);
240-
else
241-
problems.addAll(result.getProblems());
236+
units.add(StaticJavaParser.parse(file));
242237
} catch (FileNotFoundException e) {
243238
problems.add(new Problem(e.getLocalizedMessage(), null, e));
244239
}

sdg-core/src/main/java/es/upv/mist/slicing/arcs/pdg/FlowDependencyArc.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package es.upv.mist.slicing.arcs.pdg;
22

33
import es.upv.mist.slicing.arcs.Arc;
4+
import es.upv.mist.slicing.utils.Utils;
45

56
/** Represents a data dependency in an object-oriented SDG or PDG. */
67
public class FlowDependencyArc extends Arc {
@@ -12,4 +13,8 @@ public FlowDependencyArc(String variable) {
1213
super(variable);
1314
}
1415

16+
public FlowDependencyArc(String[] member) {
17+
super(Utils.arrayJoin(member, "."));
18+
}
19+
1520
}

sdg-core/src/main/java/es/upv/mist/slicing/graphs/CallGraph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public CallableDeclaration<?> getDeclaration() {
271271

272272
@Override
273273
public int hashCode() {
274-
return Objects.hash(declaration, declaration.getRange());
274+
return Objects.hash(declaration.getSignature(), declaration.getRange());
275275
}
276276

277277
@Override

sdg-core/src/main/java/es/upv/mist/slicing/graphs/ClassGraph.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.javaparser.ast.CompilationUnit;
44
import com.github.javaparser.ast.NodeList;
55
import com.github.javaparser.ast.body.*;
6+
import com.github.javaparser.ast.nodeTypes.NodeWithName;
67
import com.github.javaparser.ast.type.TypeParameter;
78
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
89
import com.github.javaparser.resolution.UnsolvedSymbolException;
@@ -19,6 +20,8 @@
1920
import java.util.stream.Collectors;
2021
import java.util.stream.Stream;
2122

23+
import static es.upv.mist.slicing.nodes.ObjectTree.ROOT_NODE;
24+
2225
public class ClassGraph extends DirectedPseudograph<ClassGraph.Vertex<?>, ClassGraph.ClassArc> implements Buildable<NodeList<CompilationUnit>> {
2326
private static ClassGraph instance = null;
2427

@@ -191,10 +194,10 @@ public ObjectTree generateObjectTreeFor(ResolvedReferenceType type) {
191194
protected ObjectTree generateObjectTreeFor(Vertex<? extends TypeDeclaration<?>> classVertex) {
192195
if (classVertex == null)
193196
return new ObjectTree();
194-
return generatePolyObjectTreeFor(classVertex, new ObjectTree(), ObjectTree.ROOT_NAME, 0);
197+
return generatePolyObjectTreeFor(classVertex, new ObjectTree(), ROOT_NODE, 0);
195198
}
196199

197-
protected ObjectTree generatePolyObjectTreeFor(Vertex<? extends TypeDeclaration<?>> classVertex, ObjectTree tree, String level, int depth) {
200+
protected ObjectTree generatePolyObjectTreeFor(Vertex<? extends TypeDeclaration<?>> classVertex, ObjectTree tree, String[] level, int depth) {
198201
if (depth >= StaticConfig.K_LIMIT)
199202
return tree;
200203
Set<? extends TypeDeclaration<?>> types = subclassesOf(classVertex);
@@ -205,19 +208,25 @@ protected ObjectTree generatePolyObjectTreeFor(Vertex<? extends TypeDeclaration<
205208
Vertex<? extends TypeDeclaration<?>> subclassVertex = classDeclarationMap.get(mapKey(type));
206209
if (!findAllFieldsOf(subclassVertex).isEmpty()) {
207210
ObjectTree newType = tree.addType(ASTUtils.resolvedTypeDeclarationToResolvedType(type.resolve()), level);
208-
generateObjectTreeFor(subclassVertex, tree, level + '.' + newType.getMemberNode().getLabel(), depth + 1);
211+
String[] newLevel = new String[level.length + 1];
212+
System.arraycopy(level, 0, newLevel, 0, level.length);
213+
newLevel[level.length] = newType.getMemberNode().getLabel();
214+
generateObjectTreeFor(subclassVertex, tree, newLevel, depth + 1);
209215
}
210216
}
211217
}
212218
return tree;
213219
}
214220

215-
protected void generateObjectTreeFor(Vertex<? extends TypeDeclaration<?>> classVertex, ObjectTree tree, String level, int depth) {
221+
protected void generateObjectTreeFor(Vertex<? extends TypeDeclaration<?>> classVertex, ObjectTree tree, String[] level, int depth) {
216222
Map<String, Vertex<? extends TypeDeclaration<?>>> classFields = findAllFieldsOf(classVertex);
217223
for (var entry : classFields.entrySet()) {
218-
tree.addField(level + '.' + entry.getKey());
224+
String[] newLevel = new String[level.length + 1];
225+
System.arraycopy(level, 0, newLevel, 0, level.length);
226+
newLevel[level.length] = entry.getKey();
227+
tree.addField(newLevel);
219228
if (entry.getValue() != null)
220-
generatePolyObjectTreeFor(entry.getValue(), tree, level + '.' + entry.getKey(), depth);
229+
generatePolyObjectTreeFor(entry.getValue(), tree, newLevel, depth);
221230
}
222231
}
223232

@@ -441,7 +450,9 @@ public T getDeclaration() {
441450

442451
@Override
443452
public int hashCode() {
444-
return Objects.hash(declaration, declaration.getRange());
453+
if (declaration instanceof NodeWithName<?>)
454+
return Objects.hash(((NodeWithName<?>) declaration).getNameAsString(), declaration.getRange());
455+
return Objects.hash(String.valueOf(declaration), declaration.getRange());
445456
}
446457

447458
@Override

sdg-core/src/main/java/es/upv/mist/slicing/graphs/cfg/CFG.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,11 @@ protected boolean findLastVarActionsFrom(Set<GraphNode<?>> visited, List<Variabl
9898
stream = stream.takeWhile(va -> va != var);
9999
List<VariableAction> list = stream.filter(var::matches).filter(filter).collect(Collectors.toList());
100100
if (!list.isEmpty()) {
101-
boolean found = false;
102-
for (int i = list.size() - 1; i >= 0 && !found; i--) {
101+
for (int i = list.size() - 1; i >= 0; i--) {
103102
result.add(list.get(i));
104103
if (!list.get(i).isOptional())
105-
found = true;
104+
return true;
106105
}
107-
if (found)
108-
return true;
109106
}
110107

111108
// Not found: traverse backwards!

sdg-core/src/main/java/es/upv/mist/slicing/graphs/cfg/CFGBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import es.upv.mist.slicing.nodes.io.MethodExitNode;
1616
import es.upv.mist.slicing.nodes.io.OutputNode;
1717
import es.upv.mist.slicing.utils.ASTUtils;
18+
import es.upv.mist.slicing.utils.Logger;
1819

1920
import java.util.*;
2021

@@ -80,6 +81,7 @@ protected <T extends Node> GraphNode<T> connectTo(T n) {
8081
* @see #connectTo(GraphNode)
8182
*/
8283
protected <T extends Node> GraphNode<T> connectTo(T n, String text) {
84+
Logger.log("Connecting new node: " + text);
8385
GraphNode<T> dest = graph.addVertex(text, n);
8486
connectTo(dest);
8587
return dest;

sdg-core/src/main/java/es/upv/mist/slicing/graphs/jsysdg/JSysCFG.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import es.upv.mist.slicing.nodes.VariableAction;
2222
import es.upv.mist.slicing.nodes.io.MethodExitNode;
2323
import es.upv.mist.slicing.utils.ASTUtils;
24+
import es.upv.mist.slicing.utils.Logger;
2425
import es.upv.mist.slicing.utils.NodeHashSet;
2526
import es.upv.mist.slicing.utils.NodeNotFoundException;
2627

@@ -65,7 +66,7 @@ protected CFGBuilder newCFGBuilder() {
6566

6667
/** Given a usage of an object member, find the last definitions of that member.
6768
* This method returns a list of variable actions, where the caller can find the member. */
68-
public List<VariableAction> findLastDefinitionOfObjectMember(VariableAction usage, String member) {
69+
public List<VariableAction> findLastDefinitionOfObjectMember(VariableAction usage, String[] member) {
6970
return findLastVarActionsFrom(usage, def -> def.isDefinition() && def.hasTreeMember(member));
7071
}
7172

@@ -111,7 +112,7 @@ protected void findAllFutureVarActionsFor(Set<GraphNode<?>> visited, List<Variab
111112

112113
/** Given an action that defines a member, locates the previous total definition that gave
113114
* it value. */
114-
public List<VariableAction> findLastTotalDefinitionOf(VariableAction action, String member) {
115+
public List<VariableAction> findLastTotalDefinitionOf(VariableAction action, String[] member) {
115116
return findLastVarActionsFrom(action, def ->
116117
(def.isDeclaration() && def.hasTreeMember(member))
117118
|| (def.isDefinition() && def.asDefinition().isTotallyDefinedMember(member)));
@@ -120,7 +121,7 @@ public List<VariableAction> findLastTotalDefinitionOf(VariableAction action, Str
120121
/** Given a definition of a given member, locate all definitions of the same object until a definition
121122
* containing the given member is found (not including that last one). If the member is found in the
122123
* given definition, it will return a list with only the given definition. */
123-
public List<VariableAction> findNextObjectDefinitionsFor(VariableAction definition, String member) {
124+
public List<VariableAction> findNextObjectDefinitionsFor(VariableAction definition, String[] member) {
124125
if (!this.containsVertex(definition.getGraphNode()))
125126
throw new NodeNotFoundException(definition.getGraphNode(), this);
126127
if (definition.hasTreeMember(member))
@@ -135,7 +136,7 @@ public List<VariableAction> findNextObjectDefinitionsFor(VariableAction definiti
135136
* the given argument. This search stops after finding a matching action in each branch. */
136137
protected boolean findNextVarActionsFor(Set<GraphNode<?>> visited, List<VariableAction> result,
137138
GraphNode<?> currentNode, VariableAction var,
138-
Predicate<VariableAction> filter, String memberName) {
139+
Predicate<VariableAction> filter, String[] memberName) {
139140
// Base case
140141
if (visited.contains(currentNode))
141142
return true;
@@ -181,6 +182,7 @@ protected Builder(JSysCFG jSysCFG) {
181182

182183
@Override
183184
protected <T extends Node> GraphNode<T> connectTo(T n, String text) {
185+
Logger.log("Connecting new node: " + text);
184186
GraphNode<T> dest;
185187
dest = new GraphNode<>(text, n);
186188
if (methodInsertedInstructions.contains(n) ||

sdg-core/src/main/java/es/upv/mist/slicing/graphs/jsysdg/JSysDG.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
import es.upv.mist.slicing.slicing.SlicingAlgorithm;
2222
import es.upv.mist.slicing.utils.NodeHashSet;
2323

24+
import java.util.NoSuchElementException;
25+
import java.util.logging.Level;
26+
import java.util.logging.Logger;
27+
2428
public class JSysDG extends ESSDG {
2529
@Override
2630
protected SlicingAlgorithm createSlicingAlgorithm() {
@@ -71,6 +75,13 @@ public Visitable visit(EnumDeclaration n, Object arg) {
7175

7276
@Override
7377
protected void buildCFG(CallableDeclaration<?> declaration, CFG cfg) {
78+
String origin;
79+
try {
80+
origin = " from " + declaration.findCompilationUnit().get().getStorage().get().getFileName() + ".";
81+
} catch (NoSuchElementException ignore) {
82+
origin = " (location unknown, may be synthetic).";
83+
}
84+
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, "Building CFG for method " + declaration.getSignature() + origin);
7485
((JSysCFG) cfg).build(declaration, newlyInsertedConstructors, ClassGraph.getInstance());
7586
}
7687

0 commit comments

Comments
 (0)