Skip to content

Commit

Permalink
Fix errors from merging JPlag v5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmaisch committed Feb 29, 2024
1 parent 1b900a8 commit 722b256
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 49 deletions.
2 changes: 1 addition & 1 deletion cli/src/test/java/de/jplag/cli/LanguageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void testInvalidLanguage() {
@Test
void testLoading() {
var languages = LanguageLoader.getAllAvailableLanguages();
assertEquals(19, languages.size(), "Loaded Languages: " + languages.keySet());
assertEquals(20, languages.size(), "Loaded Languages: " + languages.keySet());
}

@Test
Expand Down
21 changes: 17 additions & 4 deletions languages/java-cpg/src/main/java/de/jplag/java_cpg/CpgAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
Expand All @@ -28,13 +29,19 @@
public class CpgAdapter {

private List<Token> tokenList;
private boolean reorderingEnabled = true;

public CpgAdapter(GraphTransformation<?>... transformations) {
addTransformations(transformations);
}

/* package-private */ List<Token> adapt(Set<File> files) throws ParsingException {
/* package-private */ List<Token> adapt(Set<File> files, boolean normalize) throws ParsingException {
assert !files.isEmpty();

if (!normalize) {
clearTransformations();
setReorderingEnabled(false);
}
TranslationResult translationResult = translate(files);

boolean doPushToNeo4j = false;
Expand All @@ -57,7 +64,7 @@ public CpgAdapter(GraphTransformation<?>... transformations) {
.registerLanguage(new CPPLanguage());


List<Class<? extends Pass<?>>> passClasses = List.of(
List<Class<? extends Pass<?>>> passClasses = new ArrayList<>(List.of(
TypeResolver.class,
TypeHierarchyResolver.class,
ImportResolver.class,
Expand All @@ -69,7 +76,9 @@ public CpgAdapter(GraphTransformation<?>... transformations) {
DFGSortPass.class,
CleanupTransformationPass.class,
TokenizationPass.class
);
));

if (!reorderingEnabled) passClasses.remove(DFGSortPass.class);

for (Class<? extends Pass<?>> passClass : passClasses) {
configBuilder.registerPass(getKClass(passClass));
Expand All @@ -78,7 +87,7 @@ public CpgAdapter(GraphTransformation<?>... transformations) {
translationResult = TranslationManager.builder().config(configBuilder.build()).build().analyze().get();

} catch (InterruptedException | ExecutionException | ConfigurationException e) {
throw new ParsingException(List.copyOf(files).get(0), e);
throw new ParsingException(List.copyOf(files).getFirst(), e);
}
return translationResult;
}
Expand Down Expand Up @@ -123,5 +132,9 @@ public void clearTransformations() {
TransformationPass.clearTransformations();
CleanupTransformationPass.clearTransformations();
}

public void setReorderingEnabled(boolean enabled) {
this.reorderingEnabled = enabled;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public int minimumTokenMatch() {
}

@Override
public List<Token> parse(Set<File> files) throws ParsingException {
return cpgAdapter.adapt(files);
public List<Token> parse(Set<File> files, boolean normalize) throws ParsingException {
return cpgAdapter.adapt(files, normalize);
}

private GraphTransformation<?>[] standardTransformations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CleanupTransformationPass(ctx: TranslationContext) : TranslationResultPass
}

@JvmStatic
val LOGGER: Logger = LoggerFactory.getLogger(CleanupTransformationPass::class.java)
val logger: Logger = LoggerFactory.getLogger(CleanupTransformationPass::class.java)
}

override fun accept(t: TranslationResult) {
Expand Down Expand Up @@ -75,7 +75,7 @@ class CleanupTransformationPass(ctx: TranslationContext) : TranslationResultPass
}
} while (invalidated)

LOGGER.info("%s: Found %d matches".format(transformation.name, count))
logger.info("%s: Found %d matches".format(transformation.name, count))
}

override fun cleanup() {
Expand All @@ -89,7 +89,7 @@ class CleanupTransformationPass(ctx: TranslationContext) : TranslationResultPass
if (successors.size == 1 && successors[0] == dummy
&& predecessors.size == 1 && predecessors[0] == dummy
) {
LOGGER.debug("The node %s got isolated and will likely be removed.".format(it))
logger.debug("The node %s got isolated and will likely be removed.".format(it))
dummy.nextEOGEdges.removeIf { e -> e.end == it }
dummy.prevEOGEdges.removeIf { e -> e.start == it }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TransformationPass(ctx: TranslationContext) : TranslationResultPass(ctx) {
}

@JvmStatic
val LOGGER: Logger = LoggerFactory.getLogger(TransformationPass::class.java)
val logger: Logger = LoggerFactory.getLogger(TransformationPass::class.java)
}

override fun accept(t: TranslationResult) {
Expand All @@ -63,13 +63,13 @@ class TransformationPass(ctx: TranslationContext) : TranslationResultPass(ctx) {
val sourcePattern: GraphPattern = transformation.sourcePattern
val matches: Iterator<Match> = detector.getMatches(sourcePattern)

var count = 0;
var count = 0
while (matches.hasNext()) {
val match = matches.next()
count++;
count++
transformation.apply(match, ctx)
}
LOGGER.info("%s: Found %d matches".format(transformation.name, count))
logger.info("%s: Found %d matches".format(transformation.name, count))

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ enum ExecutionPhase {
}

class GraphTransformationImpl<T extends Node> implements GraphTransformation<T> {
private final static Logger LOGGER = LoggerFactory.getLogger(GraphTransformationImpl.class);
private final static Logger logger = LoggerFactory.getLogger(GraphTransformationImpl.class);
protected final GraphPattern sourcePattern;
protected final GraphPattern targetPattern;
private final List<CreateNodeOperation<?>> newNodes;
Expand All @@ -90,7 +90,7 @@ public void apply(Match match, TranslationContext ctx) {
// create nodes of the target sourceGraph missing parentPattern the source sourceGraph
newNodes.forEach(op -> op.resolve(match, ctx));

LOGGER.debug("Apply %s to node %s".formatted(name, match.get(sourcePattern.getRepresentingNode())));
logger.debug("Apply %s to node %s".formatted(name, match.get(sourcePattern.getRepresentingNode())));
// apply other operations
apply(match, concreteOperations, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PatternRegistry {
private final Map<NodePattern<?>, String> idByPattern;
private NodePattern<?> representingNode;

private static final Logger LOGGER = LoggerFactory.getLogger(PatternRegistry.class);
private static final Logger logger = LoggerFactory.getLogger(PatternRegistry.class);
private int wildcardCounter;


Expand Down Expand Up @@ -44,7 +44,7 @@ public Collection<String> allIds() {

public <T extends Node> void put(String id, NodePattern<T> pattern) {
if (patternById.containsKey(id)) {
LOGGER.warn("A NodePattern with the id '%s' is already present in the PatternRegistry");
logger.warn("A NodePattern with the id '%s' is already present in the PatternRegistry");
}
this.patternById.put(id, pattern);
this.idByPattern.put(pattern, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
*/
public final class InsertOperation<S extends Node, T extends Node> extends GraphOperationImpl<S, T> {

private static final Logger LOGGER;
private static final Logger logger;

static {
LOGGER = LoggerFactory.getLogger(RemoveOperation.class);
logger = LoggerFactory.getLogger(RemoveOperation.class);
}

private final CpgNthEdge<S, T> edge;
Expand Down Expand Up @@ -56,7 +56,7 @@ public void resolve(Match match, TranslationContext ctx) {
// match should contain newChildPattern node because of Builder.createNewNodes()
T newTarget = match.get(newChildPattern);
int index = edge.getIndex();
LOGGER.debug("Insert %s into %s at position #%d".formatted(desc(newTarget), desc(parent), index));
logger.debug("Insert %s into %s at position #%d".formatted(desc(newTarget), desc(parent), index));

apply(ctx, parent, newTarget, index);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public RemoveOperation(NodePattern<? extends S> parentPattern, CpgEdge<S, T> edg
}
}

private static final Logger LOGGER;
private static final Logger logger;

static {
LOGGER = LoggerFactory.getLogger(RemoveOperation.class);
logger = LoggerFactory.getLogger(RemoveOperation.class);

}

Expand All @@ -49,7 +49,7 @@ public void resolve(Match match, TranslationContext ctx) throws TransformationEx
}

public static <S extends Node, T extends Node> void apply(T element, S parent, CpgEdge<S, T> edge, boolean disconnectEog) {
LOGGER.debug("Remove " + element.toString());
logger.debug("Remove " + element.toString());

if (!(edge instanceof CpgNthEdge<S, T> nthEdge)) {
edge.setter().accept(parent, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
*/
public final class ReplaceOperation<S extends Node, T extends Node> extends GraphOperationImpl<S, T> {

private static final Logger LOGGER;
private static final Logger logger;

static {
LOGGER = LoggerFactory.getLogger(ReplaceOperation.class);
logger = LoggerFactory.getLogger(ReplaceOperation.class);
}

private final NodePattern<? extends T> newChildPattern;
Expand Down Expand Up @@ -52,7 +52,7 @@ public void resolve(Match match, TranslationContext ctx) {

// Replace AST edge
T oldTarget = edge.getter().apply(parent);
LOGGER.debug("Replace %s by %s".formatted(desc(oldTarget), desc(newTarget)));
logger.debug("Replace %s by %s".formatted(desc(oldTarget), desc(newTarget)));
if (Objects.isNull(newTarget.getLocation())) {
newTarget.setLocation(oldTarget.getLocation());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.Objects;

public final class SetOperation<S extends Node, T extends Node> extends GraphOperationImpl<S, T> {
private static final Logger LOGGER;
private static final Logger logger;
private final NodePattern<? extends T> newChildPattern;
private final boolean disconnectEog;

Expand All @@ -26,15 +26,15 @@ public SetOperation(NodePattern<? extends S> parentPattern,
}

static {
LOGGER = LoggerFactory.getLogger(SetOperation.class);
logger = LoggerFactory.getLogger(SetOperation.class);
}

@Override
public void resolve(Match match, TranslationContext ctx) {
S parent = match.get(parentPattern);
// match should contain newChildPattern node because of Builder.createNewNodes()
T newChild = match.get(newChildPattern);
LOGGER.debug("Set %s as AST child of %s".formatted(desc(newChild), desc(parent)));
logger.debug("Set %s as AST child of %s".formatted(desc(newChild), desc(parent)));

assert Objects.isNull(edge.getter().apply(parent));
edge.setter().accept(parent, newChild);
Expand All @@ -43,7 +43,7 @@ public void resolve(Match match, TranslationContext ctx) {
newChild.setScope(parentScope);

if (disconnectEog) {
LOGGER.warn("disconnectEog in SetOperation – not yet implemented");
logger.warn("disconnectEog in SetOperation – not yet implemented");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public final class TransformationHelper {

static final Logger LOGGER = LoggerFactory.getLogger(TransformationHelper.class);
static final Logger logger = LoggerFactory.getLogger(TransformationHelper.class);
public static final DummyNeighbor DUMMY = DummyNeighbor.getInstance();

private TransformationHelper() {
Expand All @@ -39,11 +39,11 @@ public static SubgraphWalker.Border getEogBorders(Node astRoot) {
result = SubgraphWalker.INSTANCE.getEOGPathEdges(astRoot);
if (result.getEntries().isEmpty()) {
Node entry = astRoot;
while (!entry.getPrevEOG().isEmpty()) entry = entry.getPrevEOG().get(0);
while (!entry.getPrevEOG().isEmpty()) entry = entry.getPrevEOG().getFirst();
result.setEntries(List.of(entry));
} if (result.getExits().isEmpty()) {
Node exit = astRoot;
while (!exit.getNextEOG().isEmpty()) exit = exit.getNextEOG().get(0);
while (!exit.getNextEOG().isEmpty()) exit = exit.getNextEOG().getFirst();
result.setExits(List.of(exit));
}

Expand All @@ -56,9 +56,9 @@ public static SubgraphWalker.Border getEogBorders(Node astRoot) {

private static void checkBorder(Node astRoot, SubgraphWalker.Border result) {
if (result.getEntries().isEmpty()) {
LOGGER.debug("AST subtree of %s has no EOG entry".formatted(astRoot));
logger.debug("AST subtree of %s has no EOG entry".formatted(astRoot));
} else if (result.getEntries().size() > 1) {
LOGGER.debug("AST subtree of %s has multiple EOG entries".formatted(astRoot));
logger.debug("AST subtree of %s has multiple EOG entries".formatted(astRoot));
}
}

Expand Down Expand Up @@ -91,7 +91,7 @@ public static Node disconnectFromSuccessor(Node astRoot) {
List<PropertyEdge<Node>> exitEdges = getExitEdges(astRoot, List.of(exit), true);
if (exitEdges.isEmpty()) return null;

Node entry = exitEdges.get(0).getEnd();
Node entry = exitEdges.getFirst().getEnd();

exitEdges.stream()
.filter(e -> !Objects.equals(e.getEnd(), DUMMY))
Expand Down Expand Up @@ -181,11 +181,11 @@ public static List<Node> disconnectFromPredecessor(Node astRoot) {
}

private static Node getEntry(Node astRoot) {
return getEogBorders(astRoot).getEntries().get(0);
return getEogBorders(astRoot).getEntries().getFirst();
}

private static Node getExit(Node astRoot) {
return getEogBorders(astRoot).getExits().get(0);
return getEogBorders(astRoot).getExits().getFirst();
}

static Node connectNewPredecessor(Node target, Node newPredecessor, boolean asAstRoot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class AbstractJavaCpgLanguageTest {
protected static final Path BASE_PATH = Path.of("src", "test", "resources", "java");
private static final String LOG_MESSAGE = "Tokens of {}: {}";
private final Logger logger = LoggerFactory.getLogger(AbstractJavaCpgLanguageTest.class);
private de.jplag.Language language;
private de.jplag.java_cpg.Language language;
protected File baseDirectory;

/**
Expand All @@ -43,8 +43,8 @@ void setUp() {
* @return the token types.
* @throws ParsingException if parsing fails.
*/
protected List<TokenType> parseJavaFile(String fileName) throws ParsingException {
List<Token> parsedTokens = language.parse(Set.of(new File(baseDirectory.getAbsolutePath(), fileName)));
protected List<TokenType> parseJavaFile(String fileName, boolean transform) throws ParsingException {
List<Token> parsedTokens = language.parse(Set.of(new File(baseDirectory.getAbsolutePath(), fileName)), transform);
List<TokenType> tokenTypes = parsedTokens.stream().map(Token::getType).toList();
logger.info(LOG_MESSAGE, fileName, tokenTypes);
logger.info(TokenPrinter.printTokens(parsedTokens, BASE_PATH.toAbsolutePath().toFile()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class JavaBlockTest extends AbstractJavaCpgLanguageTest {
@MethodSource("provideSrcDirectories")
@DisplayName("Test pairs of classes with explicit vs. implicit blocks.")
void testJavaClassPair(String dir) throws ParsingException {
parseJavaFile(dir);
parseJavaFile(dir, false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class JavaTryTest extends AbstractJavaCpgLanguageTest {
@Test
@DisplayName("Test difference between try block and try-with-resource block.")
void testJavaClassPair() throws ParsingException {
assertIterableEquals(parseJavaFile("try/Try.java"), parseJavaFile("try/TryWithResource.java"));
assertIterableEquals(parseJavaFile("try/Try.java", true), parseJavaFile("try/TryWithResource.java", true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class TransformTest extends AbstractJavaCpgLanguageTest {
@Test
@DisplayName("Test the transformation of source code files to graphs.")
void testJavaTransformation() throws ParsingException {
parseJavaFile("GetterSetter.java");
parseJavaFile("GetterSetter.java", true);
}
}
Loading

0 comments on commit 722b256

Please sign in to comment.