From 82d1902cad917996a46f5054f72783a7b5c29b38 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Thu, 4 Feb 2021 20:09:43 +0100 Subject: [PATCH 1/3] Correctly handle directories in Slicer Fixes #44 --- .../java/es/upv/mist/slicing/cli/Slicer.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/sdg-cli/src/main/java/es/upv/mist/slicing/cli/Slicer.java b/sdg-cli/src/main/java/es/upv/mist/slicing/cli/Slicer.java index cc34c1c..1967614 100644 --- a/sdg-cli/src/main/java/es/upv/mist/slicing/cli/Slicer.java +++ b/sdg-cli/src/main/java/es/upv/mist/slicing/cli/Slicer.java @@ -22,6 +22,7 @@ import java.io.PrintWriter; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; public class Slicer { protected static final String HELP_HEADER = "Java SDG Slicer: extract a slice from a Java program. At least" + @@ -217,8 +218,13 @@ public class Slicer { // Build the SDG NodeList units = new NodeList<>(); try { - for (File directory : dirIncludeSet) - units.add(StaticJavaParser.parse(directory)); + for (File file : dirIncludeSet) { + if (file.isDirectory()) + for (File f : (Iterable) findAllJavaFiles(file)::iterator) + units.add(StaticJavaParser.parse(f)); + else + units.add(StaticJavaParser.parse(file)); + } CompilationUnit scUnit = StaticJavaParser.parse(scFile); if (!units.contains(scUnit)) units.add(scUnit); @@ -258,6 +264,24 @@ public class Slicer { } } + protected Stream findAllJavaFiles(File directory) { + Stream.Builder builder = Stream.builder(); + findAllJavaFiles(directory, builder); + return builder.build(); + } + + protected void findAllJavaFiles(File directory, Stream.Builder builder) { + File[] files = directory.listFiles(); + if (files == null) + return; + for (File f : files) { + if (f.isDirectory()) + findAllJavaFiles(f, builder); + else if (f.getName().endsWith(".java")) + builder.add(f); + } + } + protected String getDisclaimer(CompilationUnit.Storage s) { return String.format("\n\tThis file was automatically generated as part of a slice with criterion" + "\n\tfile: %s, line: %d, variable(s): %s\n\tOriginal file: %s\n", -- GitLab From 3299397b48c1ba0cb499637838b70fa8d7efb15b Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Thu, 4 Feb 2021 20:22:09 +0100 Subject: [PATCH 2/3] Remove decl>vertex map, as it caused errors. Replaced by linear lookup. --- .../java/es/upv/mist/slicing/graphs/CallGraph.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/sdg-core/src/main/java/es/upv/mist/slicing/graphs/CallGraph.java b/sdg-core/src/main/java/es/upv/mist/slicing/graphs/CallGraph.java index d6a2e6b..f0cdb2c 100644 --- a/sdg-core/src/main/java/es/upv/mist/slicing/graphs/CallGraph.java +++ b/sdg-core/src/main/java/es/upv/mist/slicing/graphs/CallGraph.java @@ -40,7 +40,6 @@ import java.util.stream.Stream; */ public class CallGraph extends DirectedPseudograph> implements Buildable> { private final Map, CFG> cfgMap; - private final Map, Vertex> vertexDeclarationMap = ASTUtils.newIdentityHashMap(); private final ClassGraph classGraph; private boolean built = false; @@ -116,27 +115,21 @@ public class CallGraph extends DirectedPseudograph() { @Override public void visit(MethodDeclaration n, Void arg) { - addDeclaration(n); + addVertex(new Vertex(n)); super.visit(n, arg); } @Override public void visit(ConstructorDeclaration n, Void arg) { - addDeclaration(n); + addVertex(new Vertex(n)); super.visit(n, arg); } }, null); } - protected void addDeclaration(CallableDeclaration n) { - Vertex v = new Vertex(n); - vertexDeclarationMap.put(n, v); - addVertex(v); - } - protected boolean addEdge(CallableDeclaration source, CallableDeclaration target, Resolvable call) { Edge edge = new Edge<>(call, findGraphNode(call, source)); - return addEdge(vertexDeclarationMap.get(source), vertexDeclarationMap.get(target), edge); + return addEdge(findVertexByDeclaration(source), findVertexByDeclaration(target), edge); } /** Find the calls to methods and constructors (edges) in the given list of compilation units. */ -- GitLab From 64195ec71c9886003a8ee2921cfca2bf38edff6d Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Thu, 4 Feb 2021 20:41:49 +0100 Subject: [PATCH 3/3] Guarantee that each file is read just once --- .../src/main/java/es/upv/mist/slicing/cli/Slicer.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sdg-cli/src/main/java/es/upv/mist/slicing/cli/Slicer.java b/sdg-cli/src/main/java/es/upv/mist/slicing/cli/Slicer.java index 1967614..460161a 100644 --- a/sdg-cli/src/main/java/es/upv/mist/slicing/cli/Slicer.java +++ b/sdg-cli/src/main/java/es/upv/mist/slicing/cli/Slicer.java @@ -13,6 +13,7 @@ import es.upv.mist.slicing.graphs.sdg.SDG; import es.upv.mist.slicing.slicing.FileLineSlicingCriterion; import es.upv.mist.slicing.slicing.Slice; import es.upv.mist.slicing.slicing.SlicingCriterion; +import es.upv.mist.slicing.utils.NodeHashSet; import es.upv.mist.slicing.utils.StaticTypeSolver; import org.apache.commons.cli.*; @@ -216,7 +217,7 @@ public class Slicer { StaticJavaParser.getConfiguration().setAttributeComments(false); // Build the SDG - NodeList units = new NodeList<>(); + Set units = new NodeHashSet<>(); try { for (File file : dirIncludeSet) { if (file.isDirectory()) @@ -225,9 +226,7 @@ public class Slicer { else units.add(StaticJavaParser.parse(file)); } - CompilationUnit scUnit = StaticJavaParser.parse(scFile); - if (!units.contains(scUnit)) - units.add(scUnit); + units.add(StaticJavaParser.parse(scFile)); } catch (FileNotFoundException e) { throw new ParseException(e.getMessage()); } @@ -241,7 +240,7 @@ public class Slicer { default: throw new IllegalArgumentException("Unknown type of graph. Available graphs are SDG, ASDG, PSDG, ESSDG"); } - sdg.build(units); + sdg.build(new NodeList<>(units)); // Slice the SDG SlicingCriterion sc = new FileLineSlicingCriterion(scFile, scLine); -- GitLab