use of de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration 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.FieldDeclaration 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.FieldDeclaration in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testAttributes.
@Test
void testAttributes() throws Exception {
File file = new File("src/test/resources/attributes.cpp");
List<TranslationUnitDeclaration> declarations = TestUtils.analyzeWithBuilder(TranslationConfiguration.builder().sourceLocations(List.of(file)).topLevel(file.getParentFile()).defaultPasses().defaultLanguages().processAnnotations(true).symbols(Map.of("PROPERTY_ATTRIBUTE(...)", "[[property_attribute(#__VA_ARGS__)]]")));
assertFalse(declarations.isEmpty());
TranslationUnitDeclaration tu = declarations.get(0);
assertNotNull(tu);
FunctionDeclaration main = tu.getDeclarationsByName("main", FunctionDeclaration.class).iterator().next();
assertNotNull(main);
assertEquals("function_attribute", main.getAnnotations().get(0).getName());
RecordDeclaration someClass = tu.getDeclarationsByName("SomeClass", RecordDeclaration.class).iterator().next();
assertNotNull(someClass);
assertEquals("record_attribute", someClass.getAnnotations().get(0).getName());
FieldDeclaration a = someClass.getFields().stream().filter(f -> f.getName().equals("a")).findAny().orElse(null);
assertNotNull(a);
Annotation annotation = a.getAnnotations().get(0);
assertNotNull(annotation);
assertEquals("property_attribute", annotation.getName());
assertEquals(3, annotation.getMembers().size());
assertEquals("a", ((Literal<String>) annotation.getMembers().get(0).getValue()).getValue());
FieldDeclaration b = someClass.getFields().stream().filter(f -> f.getName().equals("b")).findAny().orElse(null);
assertNotNull(a);
annotation = b.getAnnotations().get(0);
assertNotNull(annotation);
assertEquals("property_attribute", annotation.getName());
assertEquals(1, annotation.getMembers().size());
assertEquals("SomeCategory, SomeOtherThing", ((Literal<String>) annotation.getMembers().get(0).getValue()).getValue());
}
use of de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration in project cpg by Fraunhofer-AISEC.
the class SuperCallTest method testSuperField.
@Test
void testSuperField() throws Exception {
List<TranslationUnitDeclaration> result = TestUtils.analyze("java", topLevel, true);
List<RecordDeclaration> records = TestUtils.subnodesOfType(result, RecordDeclaration.class);
RecordDeclaration superClass = TestUtils.findByUniqueName(records, "SuperClass");
FieldDeclaration superField = TestUtils.findByUniqueName(superClass.getFields(), "field");
RecordDeclaration subClass = TestUtils.findByUniqueName(records, "SubClass");
List<MethodDeclaration> methods = TestUtils.subnodesOfType(subClass, MethodDeclaration.class);
FieldDeclaration field = TestUtils.findByUniqueName(subClass.getFields(), "field");
MethodDeclaration getField = TestUtils.findByUniqueName(methods, "getField");
List<MemberExpression> refs = TestUtils.subnodesOfType(getField, MemberExpression.class);
MemberExpression fieldRef = TestUtils.findByUniquePredicate(refs, r -> "field".equals(r.getCode()));
MethodDeclaration getSuperField = TestUtils.findByUniqueName(methods, "getSuperField");
refs = TestUtils.subnodesOfType(getSuperField, MemberExpression.class);
MemberExpression superFieldRef = TestUtils.findByUniquePredicate(refs, r -> "super.field".equals(r.getCode()));
assertTrue(fieldRef.getBase() instanceof DeclaredReferenceExpression);
assertEquals(subClass.getThis(), ((DeclaredReferenceExpression) fieldRef.getBase()).getRefersTo());
assertEquals(field, fieldRef.getRefersTo());
assertTrue(superFieldRef.getBase() instanceof DeclaredReferenceExpression);
assertEquals(superClass.getThis(), ((DeclaredReferenceExpression) superFieldRef.getBase()).getRefersTo());
assertEquals(superField, superFieldRef.getRefersTo());
}
Aggregations