diff --git a/language-api/src/main/java/de/jplag/AbstractParser.java b/language-api/src/main/java/de/jplag/AbstractParser.java index a7100c475..6aa10f954 100644 --- a/language-api/src/main/java/de/jplag/AbstractParser.java +++ b/language-api/src/main/java/de/jplag/AbstractParser.java @@ -6,10 +6,16 @@ /** * Abstract parser class. Counts errors and manages an error consumer. * @author Emeric Kwemou + * @version $Id: $Id */ public abstract class AbstractParser { public final Logger logger; + /** + *

+ * Constructor for AbstractParser. + *

+ */ protected AbstractParser() { this.logger = LoggerFactory.getLogger(this.getClass()); } diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/CpgAdapter.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/CpgAdapter.java index 8ed458134..aeb596582 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/CpgAdapter.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/CpgAdapter.java @@ -26,7 +26,7 @@ public class CpgAdapter { private List tokenList; private boolean reorderingEnabled = true; - public CpgAdapter(GraphTransformation... transformations) { + public CpgAdapter(GraphTransformation... transformations) { addTransformations(transformations); } @@ -85,11 +85,11 @@ private void setTokenList(List tokenList) { * Registers the given transformations to be applied in the transformation step. * @param transformations the transformations */ - public void addTransformations(GraphTransformation[] transformations) { + public void addTransformations(GraphTransformation[] transformations) { Arrays.stream(transformations).forEach(this::addTransformation); } - public void addTransformation(GraphTransformation transformation) { + public void addTransformation(GraphTransformation transformation) { switch (transformation.getPhase()) { case OBLIGATORY -> PrepareTransformationPass.registerTransformation(transformation); case AST_TRANSFORM -> AstTransformationPass.registerTransformation(transformation); diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/JavaCpgLanguage.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/JavaCpgLanguage.java index 2d7f8bf22..b675d58f7 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/JavaCpgLanguage.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/JavaCpgLanguage.java @@ -32,7 +32,7 @@ public JavaCpgLanguage() { * Adds the given {@link GraphTransformation} to the list to apply to the submissions. * @param transformation the transformation */ - public void addTransformation(GraphTransformation transformation) { + public void addTransformation(GraphTransformation transformation) { this.cpgAdapter.addTransformation(transformation); } @@ -40,7 +40,7 @@ public void addTransformation(GraphTransformation transformation) { * Adds the given {@link GraphTransformation}s to the list to apply to the submissions. * @param transformations the transformations */ - public void addTransformations(GraphTransformation[] transformations) { + public void addTransformations(GraphTransformation[] transformations) { this.cpgAdapter.addTransformations(transformations); } @@ -78,7 +78,7 @@ public void resetTransformations() { * Returns the set of transformations required to ensure that the tokenization works properly. * @return the array of obligatory transformations */ - private GraphTransformation[] obligatoryTransformations() { + private GraphTransformation[] obligatoryTransformations() { return new GraphTransformation[] {wrapThenStatement, wrapElseStatement, wrapForStatement, wrapWhileStatement, wrapDoStatement}; } @@ -86,7 +86,7 @@ private GraphTransformation[] obligatoryTransformations() { * Returns a set of transformations suggested for use. * @return the array of recommended transformations */ - public GraphTransformation[] standardTransformations() { + public GraphTransformation[] standardTransformations() { return new GraphTransformation[] {removeOptionalOfCall, // 3 removeOptionalGetCall, // 4 moveConstantToOnlyUsingClass, // 6 @@ -100,7 +100,7 @@ public GraphTransformation[] standardTransformations() { * Returns a set of all transformations. * @return the array of all transformations */ - public GraphTransformation[] allTransformations() { + public GraphTransformation[] allTransformations() { return new GraphTransformation[] {ifWithNegatedConditionResolution, // 1 forStatementToWhileStatement, // 2 removeOptionalOfCall, // 3 diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/ATransformationPass.kt b/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/ATransformationPass.kt index 37ee8b30a..aff5b0b59 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/ATransformationPass.kt +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/ATransformationPass.kt @@ -23,20 +23,20 @@ abstract class ATransformationPass(ctx: TranslationContext) : TranslationResultP override fun accept(t: TranslationResult) { val detector = CpgIsomorphismDetector() - for (transformation: GraphTransformation<*> in getPhaseSpecificTransformations()) { + for (transformation: GraphTransformation in getPhaseSpecificTransformations()) { detector.loadGraph(t) instantiate(transformation, detector) } } - abstract fun getPhaseSpecificTransformations(): List> + abstract fun getPhaseSpecificTransformations(): List /** * Applies the given transformation to all the matches that the detector can find. * @param The concrete node type of the target node/GraphTransformation/Match */ - private fun instantiate(transformation: GraphTransformation, detector: CpgIsomorphismDetector) { + private fun instantiate(transformation: GraphTransformation, detector: CpgIsomorphismDetector) { val sourcePattern: GraphPattern = transformation.sourcePattern var count = 0 diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/AstTransformationPass.kt b/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/AstTransformationPass.kt index e987a97e9..b3ba756cd 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/AstTransformationPass.kt +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/AstTransformationPass.kt @@ -3,7 +3,6 @@ package de.jplag.java_cpg.passes import de.fraunhofer.aisec.cpg.TranslationContext import de.fraunhofer.aisec.cpg.passes.EvaluationOrderGraphPass import de.fraunhofer.aisec.cpg.passes.ImportResolver -import de.fraunhofer.aisec.cpg.passes.TranslationResultPass import de.fraunhofer.aisec.cpg.passes.order.DependsOn import de.fraunhofer.aisec.cpg.passes.order.ExecuteBefore import de.jplag.java_cpg.transformation.GraphTransformation @@ -16,21 +15,21 @@ import de.jplag.java_cpg.transformation.GraphTransformation class AstTransformationPass(ctx: TranslationContext) : ATransformationPass(ctx) { - override fun getPhaseSpecificTransformations(): List> { + override fun getPhaseSpecificTransformations(): List { return transformations.toList() } companion object { @JvmStatic - val transformations: MutableList> = ArrayList() + val transformations: MutableList = ArrayList() @JvmStatic - fun registerTransformation(transformation: GraphTransformation<*>) { + fun registerTransformation(transformation: GraphTransformation) { transformations.add(transformation) } @JvmStatic - fun registerTransformations(newTransformations: Array>) { + fun registerTransformations(newTransformations: Array) { transformations.addAll(newTransformations) } diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/CpgTransformationPass.kt b/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/CpgTransformationPass.kt index 8f9a03787..27ad9a809 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/CpgTransformationPass.kt +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/CpgTransformationPass.kt @@ -12,21 +12,21 @@ import de.jplag.java_cpg.transformation.GraphTransformation @ExecuteBefore(TokenizationPass::class) class CpgTransformationPass(ctx: TranslationContext) : ATransformationPass(ctx) { - override fun getPhaseSpecificTransformations(): List> { + override fun getPhaseSpecificTransformations(): List { return transformations.toList() } companion object { @JvmStatic - val transformations: MutableList> = ArrayList() + val transformations: MutableList = ArrayList() @JvmStatic - fun registerTransformation(transformation: GraphTransformation<*>) { + fun registerTransformation(transformation: GraphTransformation) { transformations.add(transformation) } @JvmStatic - fun registerTransformations(newTransformations: Array>) { + fun registerTransformations(newTransformations: Array) { transformations.addAll(newTransformations) } diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/PrepareTransformationPass.kt b/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/PrepareTransformationPass.kt index 8495cf80c..3dc835ca9 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/PrepareTransformationPass.kt +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/passes/PrepareTransformationPass.kt @@ -13,21 +13,21 @@ import de.jplag.java_cpg.transformation.GraphTransformation @ExecuteBefore(AstTransformationPass::class) class PrepareTransformationPass(ctx: TranslationContext) : ATransformationPass(ctx) { - override fun getPhaseSpecificTransformations(): List> { + override fun getPhaseSpecificTransformations(): List { return transformations.toList(); } companion object { @JvmStatic - val transformations: MutableList> = ArrayList() + val transformations: MutableList = ArrayList() @JvmStatic - fun registerTransformation(transformation: GraphTransformation<*>) { + fun registerTransformation(transformation: GraphTransformation) { transformations.add(transformation) } @JvmStatic - fun registerTransformations(newTransformations: Array>) { + fun registerTransformations(newTransformations: Array) { transformations.addAll(newTransformations) } diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/GraphTransformation.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/GraphTransformation.java index 5042b958f..302e45ffe 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/GraphTransformation.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/GraphTransformation.java @@ -22,9 +22,8 @@ /** * This saves all information related to a transformation on a graph. Note that the source and target patterns have to * have compatible root types, otherwise the transformed graph may not be semantically correct. - * @param the common root {@link Node} type of the source and target {@link NodePattern}s. */ -public interface GraphTransformation { +public interface GraphTransformation { /** * Applies the transformation to the Graph represented by the given {@link Match} which indicates which {@link Node}s @@ -79,7 +78,7 @@ enum ExecutionOrder { DESCENDING_LOCATION } - class GraphTransformationImpl implements GraphTransformation { + class GraphTransformationImpl implements GraphTransformation { private final static Logger logger = LoggerFactory.getLogger(GraphTransformationImpl.class); protected final GraphPattern sourcePattern; protected final GraphPattern targetPattern; @@ -211,7 +210,7 @@ public static Builder from(MultiGraphPattern sourcePattern, MultiGraphPatt return new Builder<>(sourcePattern, targetPattern, name, phase); } - private GraphTransformation calculateTransformation() { + private GraphTransformation calculateTransformation() { List> newNodes = this.createNewNodes(sourcePattern, targetPattern); List ops = new ArrayList<>(); sourcePattern.compareTo(targetPattern, (srcPattern, tgtPattern) -> compare(srcPattern, tgtPattern, null, ops, null)); @@ -287,7 +286,6 @@ private void compar } - private void handleSimpleRelationships(NodePattern source, NodePattern target, List ops) { List> unprocessedTargetRelated = new ArrayList<>(target.getRelatedNodes()); for (NodePattern.RelatedNode sourceRelated : source.getRelatedNodes()) { @@ -295,8 +293,7 @@ private void handleSimpleRelationships(NodePattern source, N continue; Optional maybeNextTarget = unprocessedTargetRelated.stream() - .filter(rel -> sourceRelated.edge().isEquivalentTo(rel.edge())) - .map(rel -> sourceRelated.getClass().cast(rel)).findFirst(); + .filter(rel -> sourceRelated.edge().isEquivalentTo(rel.edge())).map(rel -> sourceRelated.getClass().cast(rel)).findFirst(); maybeNextTarget.ifPresent(unprocessedTargetRelated::remove); recurseSimple(source, sourceRelated, maybeNextTarget.orElse(null), ops); @@ -398,7 +395,7 @@ private void recurseSequence(NodeP } } - public GraphTransformation build() { + public GraphTransformation build() { return this.calculateTransformation(); } diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/TransformationRepository.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/TransformationRepository.java index 090963b2d..9448b046e 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/TransformationRepository.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/TransformationRepository.java @@ -23,34 +23,34 @@ */ public class TransformationRepository { /* - * These constants are supposed to avoid uselessly building the same graph transformations multiple times. - * Alternatively, all factory methods could be public and use private fields to create a kind-of singleton pattern. - */ - public static final GraphTransformation ifWithNegatedConditionResolution = ifWithNegatedConditionResolution(); - public static final GraphTransformation forStatementToWhileStatement = forStatementToWhileStatement(); - public static final GraphTransformation removeGetterMethod = removeGetterMethod(); - public static final GraphTransformation removeUnusedVariableDeclaration = removeUnusedVariableDeclaration(); - public static final GraphTransformation removeUnusedVariableDeclarationStatement = removeUnusedVariableDeclarationStatement(); - public static final GraphTransformation removeEmptyDeclarationStatement = removeEmptyDeclarationStatement(); - public static final GraphTransformation removeLibraryRecord = removeLibraryRecord(); - public static final GraphTransformation removeLibraryField = removeLibraryField(); - public static final GraphTransformation moveConstantToOnlyUsingClass = moveConstantToOnlyUsingClass(); - public static final GraphTransformation inlineSingleUseVariable = inlineSingleUseVariable(); - - public static final GraphTransformation inlineSingleUseConstant = inlineSingleUseConstant(); - public static final GraphTransformation removeEmptyConstructor = removeEmptyConstructor(); - - public static final GraphTransformation removeEmptyRecord = removeEmptyRecord(); - public static final GraphTransformation removeImplicitStandardConstructor = removeImplicitStandardConstructor(); - public static final GraphTransformation removeOptionalOfCall = removeOptionalOfCall(); - public static final GraphTransformation removeOptionalGetCall = removeOptionalGetCall(); - public static final GraphTransformation removeUnsupportedConstructor = removeUnsupportedConstructor(); - public static final GraphTransformation removeUnsupportedMethod = removeUnsupportedMethod(); - public static final GraphTransformation wrapElseStatement = wrapElseStatement(); - public static final GraphTransformation wrapForStatement = wrapForStatement(); - public static final GraphTransformation wrapThenStatement = wrapThenStatement(); - public static final GraphTransformation wrapWhileStatement = wrapWhileStatement(); - public static final GraphTransformation wrapDoStatement = wrapDoStatement(); + * These constants are supposed to avoid uselessly building the same graph transformations multiple times. + * Alternatively, all factory methods could be public and use private fields to create a kind-of singleton pattern. + */ + public static final GraphTransformation ifWithNegatedConditionResolution = ifWithNegatedConditionResolution(); + public static final GraphTransformation forStatementToWhileStatement = forStatementToWhileStatement(); + public static final GraphTransformation removeGetterMethod = removeGetterMethod(); + public static final GraphTransformation removeUnusedVariableDeclaration = removeUnusedVariableDeclaration(); + public static final GraphTransformation removeUnusedVariableDeclarationStatement = removeUnusedVariableDeclarationStatement(); + public static final GraphTransformation removeEmptyDeclarationStatement = removeEmptyDeclarationStatement(); + public static final GraphTransformation removeLibraryRecord = removeLibraryRecord(); + public static final GraphTransformation removeLibraryField = removeLibraryField(); + public static final GraphTransformation moveConstantToOnlyUsingClass = moveConstantToOnlyUsingClass(); + public static final GraphTransformation inlineSingleUseVariable = inlineSingleUseVariable(); + + public static final GraphTransformation inlineSingleUseConstant = inlineSingleUseConstant(); + public static final GraphTransformation removeEmptyConstructor = removeEmptyConstructor(); + + public static final GraphTransformation removeEmptyRecord = removeEmptyRecord(); + public static final GraphTransformation removeImplicitStandardConstructor = removeImplicitStandardConstructor(); + public static final GraphTransformation removeOptionalOfCall = removeOptionalOfCall(); + public static final GraphTransformation removeOptionalGetCall = removeOptionalGetCall(); + public static final GraphTransformation removeUnsupportedConstructor = removeUnsupportedConstructor(); + public static final GraphTransformation removeUnsupportedMethod = removeUnsupportedMethod(); + public static final GraphTransformation wrapElseStatement = wrapElseStatement(); + public static final GraphTransformation wrapForStatement = wrapForStatement(); + public static final GraphTransformation wrapThenStatement = wrapThenStatement(); + public static final GraphTransformation wrapWhileStatement = wrapWhileStatement(); + public static final GraphTransformation wrapDoStatement = wrapDoStatement(); private TransformationRepository() { } @@ -60,7 +60,7 @@ private TransformationRepository() { * blocks. * @return the graph transformation object */ - private static GraphTransformation ifWithNegatedConditionResolution() { + private static GraphTransformation ifWithNegatedConditionResolution() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -92,7 +92,7 @@ public SimpleGraphPattern build() { * {@link VariableDeclaration}. * @return the graph transformation object */ - private static GraphTransformation removeUnusedVariableDeclarationStatement() { + private static GraphTransformation removeUnusedVariableDeclarationStatement() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -124,7 +124,7 @@ public SimpleGraphPattern build() { * * @return the graph transformation object */ - private static GraphTransformation removeUnusedVariableDeclaration() { + private static GraphTransformation removeUnusedVariableDeclaration() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -146,7 +146,7 @@ public SimpleGraphPattern build() { * Creates a {@link GraphTransformation} that removes a {@link DeclarationStatement} with no {@link Declaration}s. * @return the graph transformation object */ - private static GraphTransformation removeEmptyDeclarationStatement() { + private static GraphTransformation removeEmptyDeclarationStatement() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -168,7 +168,7 @@ public SimpleGraphPattern build() { * Creates a {@link GraphTransformation} that replaces {@link ForStatement}s by equivalent {@link WhileStatement}s. * @return the graph transformation */ - private static GraphTransformation forStatementToWhileStatement() { + private static GraphTransformation forStatementToWhileStatement() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -184,13 +184,10 @@ public SimpleGraphPattern build() { @Override public SimpleGraphPattern build() { return wildcardParent(Block.class, "surroundingBlock", - related1ToNSequence(BLOCK__STATEMENTS, Statement.class, node(Statement.class, Statement.class, "initStatement"), - node(WhileStatement.class, Statement.class, "whileStatement", - related(WHILE_STATEMENT__CONDITION, Expression.class, "condition"), - related(WHILE_STATEMENT__STATEMENT, Block.class, "whileStatementBody", - related1ToNSequence(BLOCK__STATEMENTS, Statement.class, - node(Statement.class, Statement.class, "body"), - node(Statement.class, Statement.class, "iterationStatement")))))); + related1ToNSequence(BLOCK__STATEMENTS, Statement.class, node(Statement.class, "initStatement"), + node(WhileStatement.class, "whileStatement", related(WHILE_STATEMENT__CONDITION, Expression.class, "condition"), + related(WHILE_STATEMENT__STATEMENT, Block.class, "whileStatementBody", related1ToNSequence(BLOCK__STATEMENTS, + Statement.class, node(Statement.class, "body"), node(Statement.class, "iterationStatement")))))); } }.build(); @@ -204,7 +201,7 @@ public SimpleGraphPattern build() { * are fields of library classes that the CPG is unable to resolve correctly. * @return the graph transformation */ - private static GraphTransformation removeLibraryField() { + private static GraphTransformation removeLibraryField() { /* * The SymbolResolver pass may interpret references to the standard library (e.g. java.util.List) as fields of the * current record. This transformation removes these fields. @@ -232,7 +229,7 @@ public SimpleGraphPattern build() { * TranslationUnitDeclarations. These are classes from libraries that the CPG is unable to resolve correctly. * @return the graph transformation */ - private static GraphTransformation removeLibraryRecord() { + private static GraphTransformation removeLibraryRecord() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -255,7 +252,7 @@ public SimpleGraphPattern build() { * This serves to counter the formation of constant classes. * @return the graph transformation */ - private static GraphTransformation moveConstantToOnlyUsingClass() { + private static GraphTransformation moveConstantToOnlyUsingClass() { MultiGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public MultiGraphPattern build() { @@ -299,7 +296,7 @@ public MultiGraphPattern build() { * reference. * @return the graph transformation */ - private static GraphTransformation inlineSingleUseVariable() { + private static GraphTransformation inlineSingleUseVariable() { MultiGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public MultiGraphPattern build() { @@ -337,7 +334,7 @@ public MultiGraphPattern build() { * the value of the variable. * @return the graph transformation */ - private static GraphTransformation inlineSingleUseConstant() { + private static GraphTransformation inlineSingleUseConstant() { MultiGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public MultiGraphPattern build() { @@ -373,7 +370,7 @@ public MultiGraphPattern build() { * These are representations of the implicit constructor without parameters inherited from the Object class. * @return the graph transformation */ - private static GraphTransformation removeImplicitStandardConstructor() { + private static GraphTransformation removeImplicitStandardConstructor() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -399,7 +396,7 @@ public SimpleGraphPattern build() { * Creates a {@link GraphTransformation} that removes ConstructorDeclarations with an empty body. * @return the graph transformation */ - private static GraphTransformation removeEmptyConstructor() { + private static GraphTransformation removeEmptyConstructor() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -428,7 +425,7 @@ public SimpleGraphPattern build() { * ThrowStatement. * @return the graph transformation */ - private static GraphTransformation removeUnsupportedConstructor() { + private static GraphTransformation removeUnsupportedConstructor() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -459,7 +456,7 @@ public SimpleGraphPattern build() { * ThrowStatement. * @return the graph transformation */ - private static GraphTransformation removeUnsupportedMethod() { + private static GraphTransformation removeUnsupportedMethod() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -488,7 +485,7 @@ public SimpleGraphPattern build() { * Creates a {@link GraphTransformation} that removes MethodDeclarations with an empty body. * @return the graph transformation */ - private static GraphTransformation removeEmptyRecord() { + private static GraphTransformation removeEmptyRecord() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -509,12 +506,12 @@ public SimpleGraphPattern build() { } /** - * BEWARE: This transformation breaks the comparison algorithm, leading to a higher similarity value.
+ * BEWARE: This transformation breaks the comparison algorithm, leading to a higher similarity value.
*
* Creates a {@link GraphTransformation} that removes TranslationUnits with no declarations. * @return the graph transformation */ - private static GraphTransformation removeEmptyFile() { + private static GraphTransformation removeEmptyFile() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -538,7 +535,7 @@ public SimpleGraphPattern build() { * is not a {@link Block}. * @return the graph transformation */ - private static GraphTransformation wrapThenStatement() { + private static GraphTransformation wrapThenStatement() { return wrapInBlock(IfStatement.class, IF_STATEMENT__THEN_STATEMENT, "wrapThenStatement"); } @@ -547,7 +544,7 @@ private static GraphTransformation wrapThenStatement() { * is not a {@link Block}. * @return the graph transformation */ - private static GraphTransformation wrapElseStatement() { + private static GraphTransformation wrapElseStatement() { return wrapInBlock(IfStatement.class, IF_STATEMENT__ELSE_STATEMENT, "wrapElseStatement"); } @@ -556,7 +553,7 @@ private static GraphTransformation wrapElseStatement() { * is not a {@link Block}. * @return the graph transformation */ - private static GraphTransformation wrapForStatement() { + private static GraphTransformation wrapForStatement() { return wrapInBlock(ForStatement.class, FOR_STATEMENT__STATEMENT, "wrapForStatement"); } @@ -565,7 +562,7 @@ private static GraphTransformation wrapForStatement() { * is not a {@link Block}. * @return the graph transformation */ - private static GraphTransformation wrapWhileStatement() { + private static GraphTransformation wrapWhileStatement() { return wrapInBlock(null, WHILE_STATEMENT__STATEMENT, "wrapWhileStatement"); } @@ -574,11 +571,11 @@ private static GraphTransformation wrapWhileStatement() { * is not a {@link Block}. * @return the graph transformation */ - private static GraphTransformation wrapDoStatement() { + private static GraphTransformation wrapDoStatement() { return wrapInBlock(DoStatement.class, DO_STATEMENT__STATEMENT, "wrapDoWhileStatement"); } - private static GraphTransformation wrapInBlock(Class tClass, final CpgEdge blockEdge, String name) { + private static GraphTransformation wrapInBlock(Class tClass, final CpgEdge blockEdge, String name) { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -602,7 +599,7 @@ public SimpleGraphPattern build() { * contain only a {@link ReturnStatement} with a constant or field reference as the return value. * @return the graph transformation */ - private static GraphTransformation removeGetterMethod() { + private static GraphTransformation removeGetterMethod() { SimpleGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public SimpleGraphPattern build() { @@ -636,7 +633,7 @@ public SimpleGraphPattern build() { * Creates a {@link GraphTransformation} that replaces calls to Optional.of(x) with their argument x. * @return the graph transformation */ - private static GraphTransformation removeOptionalOfCall() { + private static GraphTransformation removeOptionalOfCall() { MultiGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public MultiGraphPattern build() { @@ -667,7 +664,7 @@ public MultiGraphPattern build() { * called. * @return the graph transformation */ - private static GraphTransformation removeOptionalGetCall() { + private static GraphTransformation removeOptionalGetCall() { MultiGraphPattern sourcePattern = new GraphPatternBuilder() { @Override public MultiGraphPattern build() { diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/CpgIsomorphismDetector.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/CpgIsomorphismDetector.java index 317cc609b..ce3085bfd 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/CpgIsomorphismDetector.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/CpgIsomorphismDetector.java @@ -48,7 +48,8 @@ public List getMatches(GraphPattern sourcePattern) { } /** - * Registers a concrete {@link Node} of a CPG so that it can be considered as a root candidate of a {@link GraphPattern}. + * Registers a concrete {@link Node} of a CPG so that it can be considered as a root candidate of a + * {@link GraphPattern}. * @param node the node */ private void registerNode(Node node) { @@ -83,9 +84,8 @@ public List getNodesOfType(Class superClass) { } /** - * Verifies that the given match of the source {@link GraphPattern} is still valid. After a transformation involving the match's {@link Node}s, - * a match may be invalidated. - * + * Verifies that the given match of the source {@link GraphPattern} is still valid. After a transformation involving the + * match's {@link Node}s, a match may be invalidated. * @param match the match * @return true iff the match is still valid */ @@ -96,8 +96,8 @@ public boolean validateMatch(Match match, GraphPattern sourcePattern) { /** * This comparator imposes a total order on classes by using their class hierarchy and name, where *
    - *
  • different subclasses of a common superclass are ordered alphanumerically
  • - *
  • the sublist of subclasses of a superclass comes directly after the superclass
  • + *
  • different subclasses of a common superclass are ordered alphanumerically
  • + *
  • the sublist of subclasses of a superclass comes directly after the superclass
  • *
*/ private static final class ClassComparator implements Comparator> { @@ -164,8 +164,8 @@ enum Mode { FIRST_COMPATIBLE(0, -1, 1), /** - * In this mode, a binary search of a class A in a list returns the position after the last class B in the list so that - * B is a subclass or equal to A. + * In this mode, a binary search of a class A in a list returns the position after the last class B in the list + * so that B is a subclass or equal to A. */ FIRST_INCOMPATIBLE(1, 1, 1); diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/PatternRepository.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/PatternRepository.java index 890f3e41f..a16a723fd 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/PatternRepository.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/PatternRepository.java @@ -3,8 +3,6 @@ import static de.jplag.java_cpg.transformation.matching.edges.Edges.*; import static de.jplag.java_cpg.transformation.matching.pattern.PatternUtil.*; -import java.util.List; - import de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration; import de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration; import de.fraunhofer.aisec.cpg.graph.declarations.ParameterDeclaration; @@ -18,11 +16,10 @@ import de.fraunhofer.aisec.cpg.graph.types.ObjectType; import de.jplag.java_cpg.transformation.matching.pattern.GraphPattern; import de.jplag.java_cpg.transformation.matching.pattern.GraphPatternBuilder; -import de.jplag.java_cpg.transformation.matching.pattern.NodePattern; import de.jplag.java_cpg.transformation.matching.pattern.PatternUtil; /** - * This class is used to collect sub-patterns that may appear repetitively, or used in tests. + * This class is used to collect sub-patterns that may appear repetitively, or used in tests. */ public final class PatternRepository { @@ -75,5 +72,4 @@ public GraphPattern build() { } - } diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/edges/EdgeUtil.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/edges/EdgeUtil.java index 5900cfc88..ddd572498 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/edges/EdgeUtil.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/edges/EdgeUtil.java @@ -1,12 +1,10 @@ package de.jplag.java_cpg.transformation.matching.edges; +import org.jetbrains.annotations.NotNull; + import de.fraunhofer.aisec.cpg.graph.Node; -import de.fraunhofer.aisec.cpg.graph.declarations.Declaration; import de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration; import de.fraunhofer.aisec.cpg.graph.scopes.Scope; -import org.jetbrains.annotations.NotNull; - -import java.util.function.Function; /** * This class provides auxiliary methods to build edges. diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/edges/Edges.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/edges/Edges.java index 7d34e5608..87d753dad 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/edges/Edges.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/edges/Edges.java @@ -1,6 +1,5 @@ package de.jplag.java_cpg.transformation.matching.edges; -import static de.jplag.java_cpg.transformation.matching.edges.EdgeUtil.getLocalName; import static de.jplag.java_cpg.transformation.matching.edges.IEdge.EdgeCategory.ANALYTIC; import static de.jplag.java_cpg.transformation.matching.edges.IEdge.EdgeCategory.REFERENCE; @@ -8,7 +7,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.function.Function; import de.fraunhofer.aisec.cpg.graph.Component; import de.fraunhofer.aisec.cpg.graph.Name; @@ -21,11 +19,8 @@ import de.fraunhofer.aisec.cpg.graph.types.ObjectType; import de.fraunhofer.aisec.cpg.graph.types.Type; import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation; - import de.jplag.java_cpg.transformation.GraphTransformation.Builder; import de.jplag.java_cpg.transformation.matching.pattern.WildcardGraphPattern; -import kotlin.Pair; -import org.jetbrains.annotations.NotNull; /** * A constant class containing relevant {@link IEdge} objects. @@ -173,12 +168,12 @@ private Edges() { } /** - * Registers AST edges to be found by {@link WildcardGraphPattern}s and by the general {@link Builder}. - * @param edge the e - * @param sClass - * @param tClass - * @param - * @param + * Registers an AST edge type to be found by {@link WildcardGraphPattern}s and by the general {@link Builder}. + * @param edge the edge + * @param sClass node class of the edge source + * @param tClass node class of the edge target + * @param type of the edge source + * @param type of the edge target */ private static void register(IEdge edge, Class sClass, Class tClass) { edge.setFromClass(sClass); @@ -187,6 +182,12 @@ private static void register(IEdge edge, toType.computeIfAbsent(tClass, c -> new ArrayList<>()).add(edge); } + /** + * Gets the list of edges with the given node class as target. + * @param tClass the target node class + * @return the list of edges + * @param the target node type + */ public static List> getEdgesToType(Class tClass) { List> result = new ArrayList<>(); Class type = tClass; diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/GraphPattern.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/GraphPattern.java index 97a4fb4f6..94e1a98eb 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/GraphPattern.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/GraphPattern.java @@ -20,15 +20,16 @@ public interface GraphPattern { Collection getAllIds(); /** - * Compares this {@link GraphPattern} to another {@link GraphPattern} in order to generate a {@link GraphTransformation} between the two. - * The type of {@link GraphPattern} determines how the comparison works. + * Compares this {@link GraphPattern} to another {@link GraphPattern} in order to generate a {@link GraphTransformation} + * between the two. The type of {@link GraphPattern} determines how the comparison works. * @param targetPattern the target pattern * @param compareFunction a comparison function for {@link NodePattern}s */ void compareTo(GraphPattern targetPattern, BiConsumer, NodePattern> compareFunction); /** - * Adds a newly created {@link NodePattern} to this pattern. This occurs when a {@link GraphTransformation} includes the generation of new {@link Node}s. + * Adds a newly created {@link NodePattern} to this pattern. This occurs when a {@link GraphTransformation} includes the + * generation of new {@link Node}s. */ NodePattern addNode(String roleName, NodePattern newNode); @@ -59,16 +60,16 @@ public interface GraphPattern { List> getRoots(); /** - * Matches the given candidate {@link Node}s against the respective root {@link NodePattern}s of this {@link GraphPattern}. + * Matches the given candidate {@link Node}s against the respective root {@link NodePattern}s of this + * {@link GraphPattern}. * @param rootCandidates the root candidate {@link Node}s for each root {@link NodePattern} * @return the matches */ List match(Map, List> rootCandidates); /** - * Verifies that the given match of this {@link GraphPattern} is still valid. After a transformation involving the match's {@link Node}s, - * a match may be invalidated. - * + * Verifies that the given match of this {@link GraphPattern} is still valid. After a transformation involving the + * match's {@link Node}s, a match may be invalidated. * @param match the match * @return true iff the match is still valid */ diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/GraphPatternBuilder.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/GraphPatternBuilder.java index bfb6dd74b..403d70a0e 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/GraphPatternBuilder.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/GraphPatternBuilder.java @@ -1,14 +1,15 @@ package de.jplag.java_cpg.transformation.matching.pattern; +import java.util.*; +import java.util.function.Predicate; + +import org.jetbrains.annotations.NotNull; + import de.fraunhofer.aisec.cpg.graph.Node; import de.jplag.java_cpg.transformation.matching.edges.CpgEdge; import de.jplag.java_cpg.transformation.matching.edges.CpgMultiEdge; import de.jplag.java_cpg.transformation.matching.edges.CpgPropertyEdge; import de.jplag.java_cpg.transformation.matching.pattern.NodePattern.ForAllRelatedNode; -import org.jetbrains.annotations.NotNull; - -import java.util.*; -import java.util.function.Predicate; /** * Abstract builder class for {@link GraphPattern}s, offering convenience methods. @@ -28,17 +29,16 @@ protected GraphPatternBuilder() { /** * Creates a {@link NodePattern} of the given {@link Node} {@link Class}. - * - * @param tClass the class of the represented {@link Node} - * @param id an identifier for this {@link NodePattern} - * @param patterns the pattern registry as in-out parameter + * @param tClass the class of the represented {@link Node} + * @param id an identifier for this {@link NodePattern} + * @param patterns the pattern registry as in-out parameter * @param modifications the modifications to be applied to the new {@link NodePattern} - * @param the {@link Node} type of the represented {@link Node} + * @param the {@link Node} type of the represented {@link Node} * @return the node pattern */ @NotNull private static NodePattern createNodePattern(Class tClass, String id, PatternRegistry patterns, - List> modifications) { + List> modifications) { NodePattern related = NodePattern.forNodeType(tClass); patterns.put(id, related); modifications.forEach(m -> m.apply(related, patterns)); @@ -46,12 +46,12 @@ private static NodePattern createNodePattern(Class tClass } /** - * Creates a {@link PatternModification} that adds the {@link MatchProperty} to a {@link NodePattern} that a specific attribute of a matching {@link Node} must be equal to the same attribute of another {@link Node}. - * + * Creates a {@link PatternModification} that adds the {@link MatchProperty} to a {@link NodePattern} that a specific + * attribute of a matching {@link Node} must be equal to the same attribute of another {@link Node}. * @param propertyEdge the property edge - * @param otherId the identifier of the other {@link NodePattern} - * @param the {@link Node} type - * @param

the attribute type + * @param otherId the identifier of the other {@link NodePattern} + * @param the {@link Node} type + * @param

the attribute type * @return the {@link PatternModification} */ public static PatternModification equalAttributes(CpgPropertyEdge propertyEdge, String otherId) { @@ -59,11 +59,11 @@ public static PatternModification equalAttributes(CpgProp } /** - * Creates a {@link PatternModification} that adds the {@link MatchProperty} to a {@link NodePattern} that the assigned value is unchanged between the evaluation of the two given {@link Node}s. - * + * Creates a {@link PatternModification} that adds the {@link MatchProperty} to a {@link NodePattern} that the assigned + * value is unchanged between the evaluation of the two given {@link Node}s. * @param startId the identifier of the starting node id - * @param endId the identifier of the end node id - * @param the type of + * @param endId the identifier of the end node id + * @param the type of * @return the {@link PatternModification} */ public static PatternModification assignedValueStableBetween(String startId, String endId) { @@ -77,18 +77,16 @@ public static PatternModification notEqualTo(String otherId) /** * Builds a {@link SimpleGraphPattern}. The specifics of the structure of the SimpleGraphPattern are defined in the * concrete subclasses of {@link GraphPatternBuilder}. - * * @return the graph pattern */ public abstract GraphPattern build(); /** * Convenience method to create a {@link NodePattern}. - * - * @param tClass the {@link Node} class of the pattern - * @param id the ID for the pattern + * @param tClass the {@link Node} class of the pattern + * @param id the ID for the pattern * @param modifications a list of modifications to the {@link NodePattern} - * @param the node type + * @param the node type * @return the node pattern */ @SafeVarargs @@ -105,8 +103,8 @@ public final SimpleGraphPattern create(Class tClass, Stri } /** - * Creates an empty {@link WildcardGraphPattern}. It can be used as a target pattern for a {@link de.jplag.java_cpg.transformation.GraphTransformation} where a {@link Node} shall be removed. - * + * Creates an empty {@link WildcardGraphPattern}. It can be used as a target pattern for a + * {@link de.jplag.java_cpg.transformation.GraphTransformation} where a {@link Node} shall be removed. * @return the wildcard graph pattern */ protected SimpleGraphPattern emptyWildcardParent() { @@ -126,15 +124,14 @@ protected SimpleGraphPattern emptyWildcardParent() { */ @SafeVarargs public final PatternModification forAllRelated(CpgMultiEdge multiEdge, Class cClass, - String id, PatternModification... modifications) { + String id, PatternModification... modifications) { return new AddForAllRelated<>(multiEdge, cClass, id, Arrays.asList(modifications)); } /** * Creates a {@link MultiGraphPattern} from the given {@link SimpleGraphPattern}s. - * - * @param subgraphs - * @return + * @param subgraphs the subgraph patterns + * @return the multi graph pattern */ public final MultiGraphPattern multiRoot(SimpleGraphPattern... subgraphs) { return new MultiGraphPattern(List.of(subgraphs), patterns); @@ -143,7 +140,6 @@ public final MultiGraphPattern multiRoot(SimpleGraphPattern... subgraphs) { /** * Creates a {@link PatternListModification} that adds a {@link NodePattern} to a {@link NodeListPattern}. * @param cClass the concrete class of the added NodePattern - * @param tClass the added node class as defined by the AST edge to the parent * @param id the identifier for the node pattern * @param modifications the modifications to be applied to the node * @return the pattern list modification @@ -151,16 +147,14 @@ public final MultiGraphPattern multiRoot(SimpleGraphPattern... subgraphs) { * @param the concrete type of the added node */ @SafeVarargs - public final PatternListModification node(Class cClass, Class tClass, String id, - PatternModification... modifications) { - return new AddNode<>(cClass, tClass, id, Arrays.asList(modifications)); + public final PatternListModification node(Class cClass, String id, PatternModification... modifications) { + return new AddNode<>(cClass, id, Arrays.asList(modifications)); } /** * Creates a {@link PatternModification} to add a property to a {@link NodePattern}. - * * @param property the predicate establishing the property - * @param the target {@link Node} type + * @param the target {@link Node} type * @return the pattern modification */ public final PatternModification property(Predicate property) { @@ -168,97 +162,90 @@ public final PatternModification property(Predicate prope } /** - - * Creates a {@link PatternModification} that adds a new node pattern that is related to the reference node pattern - * via a 1:n relation. - * - * @param edge the edge type connecting the node patterns - * @param rClass the class object indicating the specified target's class type - * @param id the name of the new related node pattern + * Creates a {@link PatternModification} that adds a new node pattern that is related to the reference node pattern via + * a 1:n relation. + * @param edge the edge type connecting the node patterns + * @param rClass the class object indicating the specified target's class type + * @param id the name of the new related node pattern * @param modifications a list of modifications targeting the new node pattern - * @param the type of the source node pattern - * @param the type of the relation target, defined by the edge - * @param the concrete type of the related node pattern + * @param the type of the source node pattern + * @param the type of the relation target, defined by the edge + * @param the concrete type of the related node pattern * @return the pattern modification object */ @SafeVarargs public final PatternModification related(CpgEdge edge, Class rClass, String id, - PatternModification... modifications) { + PatternModification... modifications) { return new AddRelatedNode<>(edge, rClass, id, Arrays.asList(modifications)); } /** - * Creates a {@link PatternModification} that adds a new node pattern that is related to the reference node pattern - * via a 1:n relation. - * - * @param edge the edge type connecting the node patterns - * @param rClass the class object indicating the specified target's class type - * @param id the name of the new related node pattern + * Creates a {@link PatternModification} that adds a new node pattern that is related to the reference node pattern via + * a 1:n relation. + * @param edge the edge type connecting the node patterns + * @param rClass the class object indicating the specified target's class type + * @param id the name of the new related node pattern * @param modifications a list of modifications targeting the new node pattern - * @param the type of the source node pattern - * @param the type of the relation target, defined by the edge - * @param the concrete type of the related node pattern + * @param the type of the source node pattern + * @param the type of the relation target, defined by the edge + * @param the concrete type of the related node pattern * @return the pattern modification object */ @SafeVarargs public final PatternModification related1ToN(CpgMultiEdge edge, Class rClass, String id, - PatternModification... modifications) { + PatternModification... modifications) { return new AddRelated1ToNNode<>(edge, rClass, id, Arrays.asList(modifications)); } /** * Creates a {@link PatternModification} to add a sequence of related node pattern to a {@link NodePattern}. - * - * @param edge the multi-edge establishing the relation + * @param edge the multi-edge establishing the relation * @param cClass the (super)type class of the related nodes - * @param the source {@link Node} type - * @param the target {@link Node} type + * @param the source {@link Node} type + * @param the target {@link Node} type * @return the pattern modification */ @SafeVarargs public final PatternModification related1ToNSequence(CpgMultiEdge edge, Class cClass, - PatternListModification... modifications) { + PatternListModification... modifications) { return new AddRelated1ToNSequence<>(edge, cClass, Arrays.asList(modifications)); } /** * Creates a {@link PatternModification} to add a 1:1 relation to an existing {@link NodePattern}. - * * @param edge the edge establishing the relation - * @param id the ID of the existing target {@link NodePattern} - * @param the target {@link Node} type + * @param id the ID of the existing target {@link NodePattern} + * @param the target {@link Node} type * @return the pattern modification */ @SafeVarargs public final PatternModification relatedExisting(CpgEdge edge, Class cClass, String id, - PatternModification... modifications) { + PatternModification... modifications) { return new AddRelatedExistingNode<>(edge, cClass, id, List.of(modifications)); } /** * Creates a {@link PatternModification} to add a 1:n relation to an existing {@link NodePattern}. - * * @param edge the multi-edge establishing the relation - * @param id the ID of the existing target {@link NodePattern} - * @param the target {@link Node} type - * @param the related {@link Node} type + * @param id the ID of the existing target {@link NodePattern} + * @param the target {@link Node} type + * @param the related {@link Node} type * @return the pattern modification */ @SafeVarargs public final PatternModification relatedExisting1ToN(CpgMultiEdge edge, Class cClass, - String id, PatternModification... modifications) { + String id, PatternModification... modifications) { return new AddRelatedExisting1ToNNode<>(edge, cClass, id, List.of(modifications)); } public final PatternModification setRepresentingNode() { - return new SetRepresentingNode(); + return new SetRepresentingNode<>(); } /** * Creates a {@link PatternModification} that sets a flag to indicate that the child patterns contained in this pattern * are not relevant for the transformation calculation, but only for the pattern matching. - * - * @param + * @param the target node pattern type * @return the pattern modification */ public final PatternModification stopRecursion() { @@ -267,16 +254,15 @@ public final PatternModification stopRecursion() { /** * Convenience method to create a {@link WildcardGraphPattern} with the specified child {@link NodePattern}. - * - * @param tClass the child {@link Node} class - * @param childId the ID for the child pattern + * @param tClass the child {@link Node} class + * @param childId the ID for the child pattern * @param modifications a list of modifications targeting the child node pattern - * @param the child {@link Node} type + * @param the child {@link Node} type * @return the {@link WildcardGraphPattern} */ @SafeVarargs public final WildcardGraphPattern wildcardParent(Class tClass, String childId, - PatternModification... modifications) { + PatternModification... modifications) { NodePattern child; if (!patterns.containsPattern(childId)) { child = createNodePattern(tClass, childId, patterns, Arrays.asList(modifications)); @@ -289,14 +275,12 @@ public final WildcardGraphPattern wildcardParent(Class tC /** * {@link PatternModification}s serve to modify a {@link NodePattern}, e.g. add relations and properties to it. - * * @param the target {@link Node} type */ public sealed interface PatternModification { /** * Applies this {@link PatternModification} to the given target {@link NodePattern}. - * - * @param target the target {@link NodePattern} + * @param target the target {@link NodePattern} * @param patterns the current {@link SimpleGraphPattern}'s patterns */ void apply(NodePattern target, PatternRegistry patterns); @@ -309,7 +293,6 @@ public sealed interface PatternListModification { /** * A PatternModification to add a {@link NodePattern} related via a {@link CpgEdge}. - * * @param The source {@link Node} type * @param The target {@link Node} type, specified by the edge * @param The concrete target {@link Node} type @@ -324,10 +307,9 @@ final static class AddRelatedNode i /** * Creates a new {@link AddRelatedNode} object. - * - * @param edge the edge connecting the source node with the target node - * @param rClass the concrete class of the target node - * @param id the id for the related node + * @param edge the edge connecting the source node with the target node + * @param rClass the concrete class of the target node + * @param id the id for the related node * @param modifications list of modifications to the target node */ public AddRelatedNode(CpgEdge edge, Class rClass, String id, List> modifications) { @@ -350,7 +332,7 @@ final static class AddNode implements PatternListMo private final String id; private final List> modifications; - public AddNode(Class cClass, Class tClass, String id, List> modifications) { + public AddNode(Class cClass, String id, List> modifications) { this.cClass = cClass; this.id = id; this.modifications = modifications; @@ -365,7 +347,6 @@ public void apply(NodeListPattern target, PatternRegistry patterns) { /** * A {@link PatternModification} to add a {@link NodePattern} related via a CpgEdge that has already been created. - * * @param The source {@link Node} type * @param The target {@link Node} type, specified by the edge */ @@ -378,9 +359,9 @@ final static class AddRelatedExistingNode edge, Class cClass, String id, List> modifications) { this.getter = edge; @@ -395,9 +376,9 @@ public void apply(NodePattern target, PatternRegistry patterns) { modifications.forEach(m -> m.apply(related, patterns)); } } + /** * A {@link PatternModification} to add a required {@link Predicate} property to a NodePattern. - * * @param The target {@link Node} type */ @@ -416,7 +397,6 @@ public void apply(NodePattern target, PatternRegistry patterns) { /** * A {@link PatternModification} to add a {@link NodePattern} related via a CpgMultiEdge that has already been created. - * * @param The source {@link Node} type * @param The target {@link Node} type, specified by the edge */ @@ -428,9 +408,8 @@ static final class AddRelatedExisting1ToNNode edge, Class cClass, String id, List> modifications) { this.edge = edge; @@ -448,7 +427,6 @@ public void apply(NodePattern target, PatternRegistry patterns) { /** * A {@link PatternModification} to add a {@link NodePattern} related via a CpgMultiEdge. - * * @param The source {@link Node} type * @param The target {@link Node} type, specified by the edge * @param The concrete target {@link Node} type @@ -461,10 +439,9 @@ static final class AddRelated1ToNNode edge, Class cClass, String id, List> modifications) { @@ -491,9 +468,8 @@ static final class AddForAllRelated /** * Creates a new {@link AddForAllRelated} object. - * - * @param edge the edge connecting the source node with the target node - * @param cClass the concrete class of the target node + * @param edge the edge connecting the source node with the target node + * @param cClass the concrete class of the target node * @param modifications list of modifications to the target node */ public AddForAllRelated(CpgMultiEdge edge, Class cClass, String id, List> modifications) { @@ -531,8 +507,7 @@ public void apply(NodePattern target, PatternRegistry patterns) { } } - private record AddEqualAttributes(CpgPropertyEdge propertyEdge, - String otherId) implements PatternModification { + private record AddEqualAttributes(CpgPropertyEdge propertyEdge, String otherId) implements PatternModification { @Override public void apply(NodePattern target, PatternRegistry patterns) { NodePattern otherPattern = (NodePattern) patterns.getPattern(otherId); @@ -569,8 +544,7 @@ public void apply(NodePattern target, PatternRegistry patterns) { } } - private record AddAssignedValueStableBetween(String startId, - String endId) implements PatternModification { + private record AddAssignedValueStableBetween(String startId, String endId) implements PatternModification { @Override public void apply(NodePattern target, PatternRegistry patterns) { diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/GraphPatternImpl.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/GraphPatternImpl.java index 52834b28f..c64d97bf8 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/GraphPatternImpl.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/GraphPatternImpl.java @@ -6,7 +6,7 @@ import de.fraunhofer.aisec.cpg.graph.Node; /** - * This abstract class contains the method implementations common to all types of concrete {@link GraphPattern}s. + * This abstract class contains the method implementations common to all types of concrete {@link GraphPattern}s. */ public abstract class GraphPatternImpl implements GraphPattern { protected final PatternRegistry patternRegistry; diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/Match.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/Match.java index 6f5438f0a..2a3b4d339 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/Match.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/Match.java @@ -11,8 +11,8 @@ import de.jplag.java_cpg.transformation.matching.edges.CpgNthEdge; /** - * A {@link Match} stores the mapping between a {@link GraphPattern} and {@link Node}s matching the pattern. - * Especially, a {@link WildcardGraphPattern.ParentNodePattern}'s match in the sourceGraph can be saved. + * A {@link Match} stores the mapping between a {@link GraphPattern} and {@link Node}s matching the pattern. Especially, + * a {@link WildcardGraphPattern.ParentNodePattern}'s match in the sourceGraph can be saved. */ public class Match implements Comparable { diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/MatchProperty.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/MatchProperty.java index a23debe38..0a38094ff 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/MatchProperty.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/MatchProperty.java @@ -3,8 +3,8 @@ import de.fraunhofer.aisec.cpg.graph.Node; /** - * A {@link MatchProperty} can be used to represent a property of a {@link Match} involving multiple {@link Node}s and their relations. - * + * A {@link MatchProperty} can be used to represent a property of a {@link Match} involving multiple {@link Node}s and + * their relations. * @param The node type of the node that the property is assigned to */ @FunctionalInterface diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/MultiGraphPattern.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/MultiGraphPattern.java index f5668dffc..3b0241e20 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/MultiGraphPattern.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/MultiGraphPattern.java @@ -10,8 +10,9 @@ import de.jplag.java_cpg.transformation.GraphTransformation; /** - * A {@link MultiGraphPattern} is a {@link GraphPattern} that involves multiple subtrees that may or may not be linked to each other directly in the AST. - * It can be used to facilitate the formulation of complex {@link GraphTransformation}s. + * A {@link MultiGraphPattern} is a {@link GraphPattern} that involves multiple subtrees that may or may not be linked + * to each other directly in the AST. It can be used to facilitate the formulation of complex + * {@link GraphTransformation}s. */ public class MultiGraphPattern extends GraphPatternImpl { private final List> subgraphs; diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/NodePattern.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/NodePattern.java index fd735aad1..66576e233 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/NodePattern.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/NodePattern.java @@ -365,8 +365,7 @@ private enum NodeAnnotation { } /** - * Pair of a node pattern of a related node and a function to get from a reference node to a candidate related - * node. + * Pair of a node pattern of a related node and a function to get from a reference node to a candidate related node. * @param type of the reference node * @param type of the related node * @param pattern the pattern describing the related node diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/PatternRegistry.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/PatternRegistry.java index 9ff0a7f11..933e2f448 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/PatternRegistry.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/matching/pattern/PatternRegistry.java @@ -5,21 +5,23 @@ import java.util.Map; import java.util.stream.Collectors; -import de.jplag.java_cpg.transformation.GraphTransformation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import de.fraunhofer.aisec.cpg.graph.Node; +import de.jplag.java_cpg.transformation.GraphTransformation; /** - * The {@link PatternRegistry} saves the {@link NodePattern}s involved in a {@link GraphTransformation} and their identifiers. + * The {@link PatternRegistry} saves the {@link NodePattern}s involved in a {@link GraphTransformation} and their + * identifiers. */ -class PatternRegistry { +public class PatternRegistry { public static final String WILDCARD_PARENT_ID = "wildcardParent#"; private final Map> patternById; private final Map, String> idByPattern; /** - * A NodePattern that represents the {@link GraphPattern}. If not set, it is the (first) root of the {@link GraphPattern}. + * A NodePattern that represents the {@link GraphPattern}. If not set, it is the (first) root of the + * {@link GraphPattern}. */ private NodePattern representingNode; diff --git a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/operations/TransformationUtil.java b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/operations/TransformationUtil.java index ea87b836f..4edadcfd1 100644 --- a/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/operations/TransformationUtil.java +++ b/languages/java-cpg/src/main/java/de/jplag/java_cpg/transformation/operations/TransformationUtil.java @@ -4,7 +4,6 @@ import java.util.List; import java.util.Objects; -import de.jplag.java_cpg.transformation.GraphTransformation; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,6 +13,7 @@ import de.fraunhofer.aisec.cpg.graph.statements.expressions.Block; import de.fraunhofer.aisec.cpg.graph.statements.expressions.UnaryOperator; import de.fraunhofer.aisec.cpg.helpers.SubgraphWalker; +import de.jplag.java_cpg.transformation.GraphTransformation; /** * This class is a collection of auxiliary methods related to {@link GraphTransformation}s. diff --git a/languages/java-cpg/src/test/java/de/jplag/java_cpg/CreateTransformTest.java b/languages/java-cpg/src/test/java/de/jplag/java_cpg/CreateTransformTest.java index 300035095..113310492 100644 --- a/languages/java-cpg/src/test/java/de/jplag/java_cpg/CreateTransformTest.java +++ b/languages/java-cpg/src/test/java/de/jplag/java_cpg/CreateTransformTest.java @@ -36,7 +36,7 @@ public static Stream provideTuples() { @ParameterizedTest @MethodSource("provideTuples") - public void createTransformTest(String fileName, GraphTransformation transformation) + public void createTransformTest(String fileName, GraphTransformation transformation) throws ParsingException, InterruptedException, ConnectException { Set files = Set.of(new File(baseDirectory, fileName)); @@ -51,7 +51,7 @@ public void createTransformTest(String fileName, GraphTransform } - private void instantiate(GraphTransformation transformation) { + private void instantiate(GraphTransformation transformation) { GraphPattern sourcePattern = transformation.getSourcePattern(); List maybeMatch = detector.getMatches(sourcePattern); diff --git a/languages/java-cpg/src/test/java/de/jplag/java_cpg/MatchingTest.java b/languages/java-cpg/src/test/java/de/jplag/java_cpg/MatchingTest.java index f94d8fe68..c7c8ea2ff 100644 --- a/languages/java-cpg/src/test/java/de/jplag/java_cpg/MatchingTest.java +++ b/languages/java-cpg/src/test/java/de/jplag/java_cpg/MatchingTest.java @@ -5,7 +5,6 @@ import java.util.Set; import java.util.stream.Stream; -import de.jplag.java_cpg.transformation.matching.pattern.SimpleGraphPattern; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -20,9 +19,9 @@ import de.jplag.ParsingException; import de.jplag.java_cpg.transformation.matching.CpgIsomorphismDetector; import de.jplag.java_cpg.transformation.matching.PatternRepository; -import de.jplag.java_cpg.transformation.matching.pattern.GraphPattern; import de.jplag.java_cpg.transformation.matching.pattern.GraphPatternBuilder; import de.jplag.java_cpg.transformation.matching.pattern.Match; +import de.jplag.java_cpg.transformation.matching.pattern.SimpleGraphPattern; public class MatchingTest extends AbstractJavaCpgLanguageTest { @@ -51,7 +50,7 @@ public void testMatch(String filename, Class rootType, Graph if (matches.isEmpty()) { return false; } - LOGGER.info("Mapping contained %d nodes.".formatted(matches.get(0).getSize())); + LOGGER.info("Mapping contained %d nodes.".formatted(matches.getFirst().getSize())); return true; })); } catch (ParsingException e) { diff --git a/languages/java-cpg/src/test/java/de/jplag/java_cpg/transform/PlagiarismDetectionTest.java b/languages/java-cpg/src/test/java/de/jplag/java_cpg/transform/PlagiarismDetectionTest.java index 4f155af83..6c1f2bcd9 100644 --- a/languages/java-cpg/src/test/java/de/jplag/java_cpg/transform/PlagiarismDetectionTest.java +++ b/languages/java-cpg/src/test/java/de/jplag/java_cpg/transform/PlagiarismDetectionTest.java @@ -53,7 +53,7 @@ private static Stream getArguments() { @ParameterizedTest @MethodSource("getArguments") - public void testPlagiarismPair(String submissionsPath, GraphTransformation[] transformation) { + public void testPlagiarismPair(String submissionsPath, GraphTransformation[] transformation) { language.addTransformations(transformation);