From b0b8e498602cf54f31409a7448e3b2ae13041b1a Mon Sep 17 00:00:00 2001 From: David Olmos Garrido Date: Thu, 26 Nov 2020 23:00:33 +0100 Subject: [PATCH 1/2] Implement criterion which don't need a file. --- .../slicing/ClassFileLineCriterion.java | 31 +++++++++++++++++++ .../slicing/FileLineSlicingCriterion.java | 9 +----- .../slicing/slicing/LineNumberCriterion.java | 13 +++++++- .../es/upv/mist/slicing/slicing/Slice.java | 5 +-- 4 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 sdg-core/src/main/java/es/upv/mist/slicing/slicing/ClassFileLineCriterion.java diff --git a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/ClassFileLineCriterion.java b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/ClassFileLineCriterion.java new file mode 100644 index 0000000..a258093 --- /dev/null +++ b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/ClassFileLineCriterion.java @@ -0,0 +1,31 @@ +package es.upv.mist.slicing.slicing; + +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.NodeList; +import java.util.Optional; + +public class ClassFileLineCriterion extends LineNumberCriterion { + private final String fullyQualifiedClassName; + + public ClassFileLineCriterion(final String fullyQualifiedClassName, int lineNumber, String variable) { + super(lineNumber, variable); + this.fullyQualifiedClassName = fullyQualifiedClassName; + } + + /** Locates the compilation unit that corresponds to this criterion's file. */ + protected Optional findCompilationUnit(NodeList cus) { + for (CompilationUnit cu : cus) { + boolean hasType = cu.getTypes().stream() + .anyMatch((type) -> type.getFullyQualifiedName().equals(Optional.of(fullyQualifiedClassName))); + if (hasType) { + return Optional.of(cu); + } + } + return Optional.empty(); + } + + @Override + public String toString() { + return fullyQualifiedClassName + "#" + lineNumber + ":" + variable; + } +} diff --git a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/FileLineSlicingCriterion.java b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/FileLineSlicingCriterion.java index 584f454..41a7775 100644 --- a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/FileLineSlicingCriterion.java +++ b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/FileLineSlicingCriterion.java @@ -18,15 +18,8 @@ public class FileLineSlicingCriterion extends LineNumberCriterion { this.file = file; } - @Override - public Optional> findNode(SDG graph) { - Optional optCu = findCompilationUnit(graph.getCompilationUnits()); - if (optCu.isEmpty()) - return Optional.empty(); - return optCu.get().findFirst(Statement.class, this::matchesLine).flatMap(graph::findNodeByASTNode); - } - /** Locates the compilation unit that corresponds to this criterion's file. */ + @Override protected Optional findCompilationUnit(NodeList cus) { for (CompilationUnit cu : cus) { Optional optStorage = cu.getStorage(); diff --git a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/LineNumberCriterion.java b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/LineNumberCriterion.java index 18598ae..59973ab 100644 --- a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/LineNumberCriterion.java +++ b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/LineNumberCriterion.java @@ -1,7 +1,10 @@ package es.upv.mist.slicing.slicing; import com.github.javaparser.Position; +import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.stmt.Statement; import es.upv.mist.slicing.graphs.cfg.CFG; import es.upv.mist.slicing.graphs.pdg.PDG; import es.upv.mist.slicing.graphs.sdg.SDG; @@ -46,7 +49,15 @@ public class LineNumberCriterion extends SlicingCriterion { @Override public Optional> findNode(SDG graph) { - return Optional.empty(); + Optional optCu = findCompilationUnit(graph.getCompilationUnits()); + if (optCu.isEmpty()) + return Optional.empty(); + return optCu.get().findFirst(Statement.class, this::matchesLine).flatMap(graph::findNodeByASTNode); + } + + /** Locates the compilation unit that corresponds to this criterion's file. */ + protected Optional findCompilationUnit(NodeList cus) { + return cus.stream().findFirst(); } /** Check if a node matches the criterion's line. */ diff --git a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/Slice.java b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/Slice.java index 67babbc..63ec4d1 100644 --- a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/Slice.java +++ b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/Slice.java @@ -73,8 +73,9 @@ public class Slice { CloneVisitor cloneVisitor = new CloneVisitor(); for (Map.Entry> entry : cuMap.entrySet()) { CompilationUnit clone = (CompilationUnit) entry.getKey().accept(cloneVisitor, null); - assert entry.getKey().getStorage().isPresent(); - clone.setStorage(entry.getKey().getStorage().get().getPath()); + if (entry.getKey().getStorage().isPresent()) { + clone.setStorage(entry.getKey().getStorage().get().getPath()); + } clone.accept(sliceVisitor, entry.getValue()); cus.add(clone); } -- GitLab From 2861afaa6bc1cc9a85bdb72d6edcd02847917924 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Fri, 27 Nov 2020 09:18:33 +0100 Subject: [PATCH 2/2] small changes --- .../mist/slicing/slicing/ClassFileLineCriterion.java | 11 +++++++---- .../slicing/slicing/FileLineSlicingCriterion.java | 11 +++++------ .../upv/mist/slicing/slicing/LineNumberCriterion.java | 2 +- .../main/java/es/upv/mist/slicing/slicing/Slice.java | 6 +++--- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/ClassFileLineCriterion.java b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/ClassFileLineCriterion.java index a258093..a1b4828 100644 --- a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/ClassFileLineCriterion.java +++ b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/ClassFileLineCriterion.java @@ -2,6 +2,9 @@ package es.upv.mist.slicing.slicing; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.body.TypeDeclaration; + +import java.util.Objects; import java.util.Optional; public class ClassFileLineCriterion extends LineNumberCriterion { @@ -15,11 +18,11 @@ public class ClassFileLineCriterion extends LineNumberCriterion { /** Locates the compilation unit that corresponds to this criterion's file. */ protected Optional findCompilationUnit(NodeList cus) { for (CompilationUnit cu : cus) { - boolean hasType = cu.getTypes().stream() - .anyMatch((type) -> type.getFullyQualifiedName().equals(Optional.of(fullyQualifiedClassName))); - if (hasType) { + if (cu.getTypes().stream() + .map(TypeDeclaration::getFullyQualifiedName) + .map(o -> o.orElse(null)) + .anyMatch(type -> Objects.equals(type, fullyQualifiedClassName))) return Optional.of(cu); - } } return Optional.empty(); } diff --git a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/FileLineSlicingCriterion.java b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/FileLineSlicingCriterion.java index 41a7775..8a1afdb 100644 --- a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/FileLineSlicingCriterion.java +++ b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/FileLineSlicingCriterion.java @@ -2,9 +2,6 @@ package es.upv.mist.slicing.slicing; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.NodeList; -import com.github.javaparser.ast.stmt.Statement; -import es.upv.mist.slicing.graphs.sdg.SDG; -import es.upv.mist.slicing.nodes.GraphNode; import java.io.File; import java.util.Optional; @@ -22,9 +19,11 @@ public class FileLineSlicingCriterion extends LineNumberCriterion { @Override protected Optional findCompilationUnit(NodeList cus) { for (CompilationUnit cu : cus) { - Optional optStorage = cu.getStorage(); - if (optStorage.isPresent() && optStorage.get().getFileName().equals(file.getName()) - && optStorage.get().getDirectory().toAbsolutePath().equals(file.toPath().toAbsolutePath().getParent())) + if (cu.getStorage().isEmpty()) + continue; + CompilationUnit.Storage storage = cu.getStorage().get(); + if (storage.getDirectory().toAbsolutePath().equals(file.toPath().toAbsolutePath().getParent()) + && storage.getFileName().equals(file.getName())) return Optional.of(cu); } return Optional.empty(); diff --git a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/LineNumberCriterion.java b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/LineNumberCriterion.java index 59973ab..84fd948 100644 --- a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/LineNumberCriterion.java +++ b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/LineNumberCriterion.java @@ -57,7 +57,7 @@ public class LineNumberCriterion extends SlicingCriterion { /** Locates the compilation unit that corresponds to this criterion's file. */ protected Optional findCompilationUnit(NodeList cus) { - return cus.stream().findFirst(); + return cus.getFirst(); } /** Check if a node matches the criterion's line. */ diff --git a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/Slice.java b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/Slice.java index 63ec4d1..4dc751a 100644 --- a/sdg-core/src/main/java/es/upv/mist/slicing/slicing/Slice.java +++ b/sdg-core/src/main/java/es/upv/mist/slicing/slicing/Slice.java @@ -73,9 +73,9 @@ public class Slice { CloneVisitor cloneVisitor = new CloneVisitor(); for (Map.Entry> entry : cuMap.entrySet()) { CompilationUnit clone = (CompilationUnit) entry.getKey().accept(cloneVisitor, null); - if (entry.getKey().getStorage().isPresent()) { - clone.setStorage(entry.getKey().getStorage().get().getPath()); - } + if (entry.getKey().getStorage().isPresent()) + clone.setStorage(entry.getKey().getStorage().get().getPath(), + entry.getKey().getStorage().get().getEncoding()); clone.accept(sliceVisitor, entry.getValue()); cus.add(clone); } -- GitLab