use of de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration in project cpg by Fraunhofer-AISEC.
the class VisitorTest method testAllEogNodeVisitor.
/**
* Visits all nodes along EOG.
*/
@Test
void testAllEogNodeVisitor() {
List<Node> nodeList = new ArrayList<>();
RecordDeclaration recordDeclaration = namespace.getDeclarationAs(0, RecordDeclaration.class);
MethodDeclaration method = recordDeclaration.getMethods().stream().filter(m -> m.getName().equals("method")).collect(Collectors.toList()).get(0);
/* TODO A better way to get the "first" statement in a method body is needed.
This is currently the only (fragile, ugly and unsafe) way to get to the first "real" statement in a method body.
getNextEOG() and getNextCFG() return empty lists.
*/
Statement firstStmt = ((CompoundStatement) method.getBody()).getStatements().get(0);
firstStmt.accept(Strategy::EOG_FORWARD, new IVisitor<Node>() {
public void visit(Node n) {
System.out.println(n);
nodeList.add(n);
}
});
assertEquals(22, nodeList.size());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration in project cpg by Fraunhofer-AISEC.
the class SuperCallTest method testInterfaceCall.
@Test
void testInterfaceCall() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze("java", topLevel, true);
List<RecordDeclaration> records = TestUtils.subnodesOfType(result, RecordDeclaration.class);
RecordDeclaration interface1 = TestUtils.findByUniqueName(records, "Interface1");
List<MethodDeclaration> interface1Methods = TestUtils.subnodesOfType(interface1, MethodDeclaration.class);
MethodDeclaration interface1Target = TestUtils.findByUniqueName(interface1Methods, "target");
RecordDeclaration interface2 = TestUtils.findByUniqueName(records, "Interface2");
List<MethodDeclaration> interface2Methods = TestUtils.subnodesOfType(interface2, MethodDeclaration.class);
MethodDeclaration interface2Target = TestUtils.findByUniqueName(interface2Methods, "target");
RecordDeclaration subClass = TestUtils.findByUniqueName(records, "SubClass");
List<MethodDeclaration> methods = TestUtils.subnodesOfType(subClass, MethodDeclaration.class);
MethodDeclaration target = TestUtils.findByUniqueName(methods, "target");
List<CallExpression> calls = TestUtils.subnodesOfType(target, CallExpression.class);
CallExpression interface1Call = TestUtils.findByUniquePredicate(calls, c -> "Interface1.super.target();".equals(c.getCode()));
CallExpression interface2Call = TestUtils.findByUniquePredicate(calls, c -> "Interface2.super.target();".equals(c.getCode()));
assertEquals(List.of(interface1Target), interface1Call.getInvokes());
assertEquals(List.of(interface2Target), interface2Call.getInvokes());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration in project cpg by Fraunhofer-AISEC.
the class StaticImportsTest method testAsteriskImport.
@Test
void testAsteriskImport() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze("java", topLevel.resolve("asterisk"), true);
List<MethodDeclaration> methods = TestUtils.subnodesOfType(result, MethodDeclaration.class);
MethodDeclaration main = TestUtils.findByUniqueName(methods, "main");
List<RecordDeclaration> records = TestUtils.subnodesOfType(result, RecordDeclaration.class);
RecordDeclaration A = TestUtils.findByUniqueName(records, "A");
RecordDeclaration B = TestUtils.findByUniqueName(records, "B");
for (CallExpression call : TestUtils.subnodesOfType(main, CallExpression.class)) {
switch(call.getName()) {
case "a":
assertEquals(List.of(TestUtils.findByUniqueName(methods, "a")), call.getInvokes());
assertTrue(((MethodDeclaration) call.getInvokes().get(0)).isStatic());
break;
case "b":
List<MethodDeclaration> bs = methods.stream().filter(m -> m.getName().equals("b") && m.isStatic()).collect(Collectors.toList());
assertEquals(call.getInvokes(), bs.stream().filter(b -> b.hasSignature(call.getSignature())).collect(Collectors.toList()));
break;
case "nonStatic":
MethodDeclaration nonStatic = TestUtils.findByUniqueName(B.getMethods(), "nonStatic");
assertTrue(nonStatic.isInferred());
assertEquals(List.of(nonStatic), call.getInvokes());
}
}
List<FieldDeclaration> testFields = TestUtils.subnodesOfType(A, FieldDeclaration.class);
FieldDeclaration staticField = TestUtils.findByUniqueName(testFields, "staticField");
FieldDeclaration nonStaticField = TestUtils.findByUniqueName(testFields, "nonStaticField");
assertTrue(staticField.getModifiers().contains("static"));
assertFalse(nonStaticField.getModifiers().contains("static"));
List<MemberExpression> declaredReferences = TestUtils.subnodesOfType(main, MemberExpression.class);
MemberExpression usage = TestUtils.findByUniqueName(declaredReferences, "staticField");
assertEquals(staticField, usage.getRefersTo());
MemberExpression nonStatic = TestUtils.findByUniqueName(declaredReferences, "nonStaticField");
assertNotEquals(nonStaticField, nonStatic.getRefersTo());
assertTrue(nonStatic.getRefersTo().isInferred());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration in project cpg by Fraunhofer-AISEC.
the class StaticImportsTest method testSingleStaticImport.
@Test
void testSingleStaticImport() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze("java", topLevel.resolve("single"), true);
List<MethodDeclaration> methods = TestUtils.subnodesOfType(result, MethodDeclaration.class);
MethodDeclaration test = TestUtils.findByUniqueName(methods, "test");
MethodDeclaration main = TestUtils.findByUniqueName(methods, "main");
CallExpression call = TestUtils.subnodesOfType(main, CallExpression.class).get(0);
assertEquals(List.of(test), call.getInvokes());
List<FieldDeclaration> testFields = TestUtils.subnodesOfType(result, FieldDeclaration.class).stream().filter(f -> f.getName().equals("test")).collect(Collectors.toList());
assertEquals(1, testFields.size());
FieldDeclaration staticField = testFields.get(0);
assertTrue(staticField.getModifiers().contains("static"));
List<MemberExpression> memberExpressions = TestUtils.subnodesOfType(main, MemberExpression.class);
MemberExpression usage = TestUtils.findByUniqueName(memberExpressions, "test");
assertEquals(staticField, usage.getRefersTo());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration in project cpg by Fraunhofer-AISEC.
the class DFGTest method testSensitivityWithLabels.
@Test
void testSensitivityWithLabels() throws Exception {
Path topLevel = Path.of("src", "test", "resources", "dfg");
TranslationUnitDeclaration result = TestUtils.analyze(List.of(topLevel.resolve("LoopDFGs.java").toFile()), topLevel, true).get(0);
MethodDeclaration looping = TestUtils.getSubnodeOfTypeWithName(result, MethodDeclaration.class, "labeledBreakContinue");
List<Node> methodNodes = SubgraphWalker.flattenAST(looping);
Literal l0 = getLiteral(methodNodes, 0);
Literal l1 = getLiteral(methodNodes, 1);
Literal l2 = getLiteral(methodNodes, 2);
Literal l3 = getLiteral(methodNodes, 3);
Literal l4 = getLiteral(methodNodes, 4);
List<Node> calls = TestUtils.findByPredicate(SubgraphWalker.flattenAST(looping), n -> n instanceof CallExpression && n.getName().equals("println"));
calls.sort(new NodeComparator());
Set<Node> dfgNodesA0 = flattenDFGGraph(TestUtils.getSubnodeOfTypeWithName(calls.get(0), DeclaredReferenceExpression.class, "a"), false);
Set<Node> dfgNodesA1 = flattenDFGGraph(TestUtils.getSubnodeOfTypeWithName(calls.get(1), DeclaredReferenceExpression.class, "a"), false);
Set<Node> dfgNodesA2 = flattenDFGGraph(TestUtils.getSubnodeOfTypeWithName(calls.get(2), DeclaredReferenceExpression.class, "a"), false);
assertTrue(dfgNodesA0.contains(l0));
assertTrue(dfgNodesA0.contains(l1));
assertTrue(dfgNodesA0.contains(l3));
assertFalse(dfgNodesA0.contains(l4));
assertTrue(dfgNodesA1.contains(l0));
assertTrue(dfgNodesA1.contains(l1));
assertTrue(dfgNodesA1.contains(l3));
assertFalse(dfgNodesA1.contains(l4));
assertTrue(dfgNodesA2.contains(l0));
assertTrue(dfgNodesA2.contains(l1));
assertTrue(dfgNodesA2.contains(l2));
assertTrue(dfgNodesA2.contains(l3));
assertFalse(dfgNodesA2.contains(l4));
}
Aggregations