use of de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression 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.statements.expressions.CallExpression 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.statements.expressions.CallExpression 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.statements.expressions.CallExpression in project cpg by Fraunhofer-AISEC.
the class VariableResolverJavaTest method initTests.
@BeforeAll
void initTests() throws ExecutionException, InterruptedException {
final String topLevelPath = "src/test/resources/variables_extended/java/";
List<String> fileNames = Arrays.asList("ScopeVariables.java", "ExternalClass.java");
List<File> fileLocations = fileNames.stream().map(fileName -> new File(topLevelPath + fileName)).collect(Collectors.toList());
TranslationConfiguration config = TranslationConfiguration.builder().sourceLocations(fileLocations.toArray(new File[fileNames.size()])).topLevel(new File(topLevelPath)).defaultPasses().defaultLanguages().debugParser(true).failOnError(true).build();
TranslationManager analyzer = TranslationManager.builder().config(config).build();
List<TranslationUnitDeclaration> tu = analyzer.analyze().get().getTranslationUnits();
List<Node> nodes = tu.stream().flatMap(tUnit -> SubgraphWalker.flattenAST(tUnit).stream()).collect(Collectors.toList());
List<CallExpression> calls = TestUtils.findByName(Util.filterCast(nodes, CallExpression.class), "printLog");
calls.sort(new NodeComparator());
List<RecordDeclaration> records = Util.filterCast(nodes, RecordDeclaration.class);
// Extract all Variable declarations and field declarations for matching
externalClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "variables_extended.ExternalClass");
externVarName = TestUtils.getSubnodeOfTypeWithName(externalClass, FieldDeclaration.class, "varName");
externStaticVarName = TestUtils.getSubnodeOfTypeWithName(externalClass, FieldDeclaration.class, "staticVarName");
outerClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "variables_extended.ScopeVariables");
outerVarName = outerClass.getFields().stream().filter(n -> n.getName().equals("varName")).findFirst().get();
outerStaticVarName = outerClass.getFields().stream().filter(n -> n.getName().equals("staticVarName")).findFirst().get();
outerImpThis = outerClass.getFields().stream().filter(n -> n.getName().equals("this")).findFirst().get();
// Inner class and its fields
innerClass = TestUtils.getOfTypeWithName(nodes, RecordDeclaration.class, "variables_extended.ScopeVariables.InnerClass");
innerVarName = innerClass.getFields().stream().filter(n -> n.getName().equals("varName")).findFirst().get();
innerStaticVarName = TestUtils.getSubnodeOfTypeWithName(innerClass, FieldDeclaration.class, "staticVarName");
innerImpThis = TestUtils.getSubnodeOfTypeWithName(innerClass, FieldDeclaration.class, "this");
innerImpOuter = TestUtils.getSubnodeOfTypeWithName(innerClass, FieldDeclaration.class, "ScopeVariables.this");
main = TestUtils.getSubnodeOfTypeWithName(outerClass, MethodDeclaration.class, "main");
outerFunction1 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function1")).collect(Collectors.toList()).get(0);
forStatements = Util.filterCast(SubgraphWalker.flattenAST(outerFunction1), ForStatement.class);
// Functions i nthe outer and inner object
outerFunction2 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function2")).collect(Collectors.toList()).get(0);
outerFunction3 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function3")).collect(Collectors.toList()).get(0);
outerFunction4 = outerClass.getMethods().stream().filter(method -> method.getName().equals("function4")).collect(Collectors.toList()).get(0);
innerFunction1 = innerClass.getMethods().stream().filter(method -> method.getName().equals("function1")).collect(Collectors.toList()).get(0);
innerFunction2 = innerClass.getMethods().stream().filter(method -> method.getName().equals("function2")).collect(Collectors.toList()).get(0);
innerFunction3 = innerClass.getMethods().stream().filter(method -> method.getName().equals("function3")).collect(Collectors.toList()).get(0);
for (CallExpression call : calls) {
Expression first = call.getArguments().get(0);
String logId = ((Literal) first).getValue().toString();
Expression second = call.getArguments().get(1);
callParamMap.put(logId, second);
}
}
use of de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression in project cpg by Fraunhofer-AISEC.
the class CallResolverTest method testImplicitCastMethodCallResolution.
@Test
void testImplicitCastMethodCallResolution() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze(List.of(Path.of(topLevel.toString(), "implicitcast", "implicitCastInMethod.cpp").toFile()), topLevel, true);
List<FunctionDeclaration> functionDeclarations = TestUtils.subnodesOfType(result, FunctionDeclaration.class);
List<CallExpression> callExpressions = TestUtils.subnodesOfType(result, CallExpression.class);
// Check resolution of calc
CallExpression calc = TestUtils.findByUniqueName(callExpressions, "calc");
FunctionDeclaration calcFunctionDeclaration = TestUtils.findByUniquePredicate(functionDeclarations, f -> f.getName().equals("calc") && !f.isInferred());
assertEquals(1, calc.getInvokes().size());
assertEquals(calcFunctionDeclaration, calc.getInvokes().get(0));
assertTrue(calc.getArguments().get(0) instanceof CastExpression);
assertEquals(2.0, ((Literal) ((CastExpression) calc.getArguments().get(0)).getExpression()).getValue());
assertEquals("int", ((CastExpression) calc.getArguments().get(0)).getCastType().getName());
// Check resolution of doSmth
CallExpression doSmth = TestUtils.findByUniqueName(callExpressions, "doSmth");
FunctionDeclaration doSmthFunctionDeclaration = TestUtils.findByUniquePredicate(functionDeclarations, f -> f.getName().equals("doSmth") && !f.isInferred());
assertEquals(1, doSmth.getInvokes().size());
assertEquals(doSmthFunctionDeclaration, doSmth.getInvokes().get(0));
assertTrue(doSmth.getArguments().get(0) instanceof CastExpression);
assertEquals(10.0, ((Literal) ((CastExpression) doSmth.getArguments().get(0)).getExpression()).getValue());
assertEquals("int", ((CastExpression) doSmth.getArguments().get(0)).getCastType().getName());
}
Aggregations