Search in sources :

Example 1 with FieldDeclaration

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());
}
Also used : TestUtils(de.fraunhofer.aisec.cpg.TestUtils) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) Assertions(org.junit.jupiter.api.Assertions) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) Path(java.nio.file.Path) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 2 with FieldDeclaration

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());
}
Also used : TestUtils(de.fraunhofer.aisec.cpg.TestUtils) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) Assertions(org.junit.jupiter.api.Assertions) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) Path(java.nio.file.Path) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 3 with FieldDeclaration

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());
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) Annotation(de.fraunhofer.aisec.cpg.graph.Annotation) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 4 with FieldDeclaration

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());
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) MethodDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) FieldDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

BaseTest (de.fraunhofer.aisec.cpg.BaseTest)4 FieldDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration)4 RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)4 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)4 Test (org.junit.jupiter.api.Test)4 MethodDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration)3 MemberExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression)3 TestUtils (de.fraunhofer.aisec.cpg.TestUtils)2 CallExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression)2 Path (java.nio.file.Path)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Assertions (org.junit.jupiter.api.Assertions)2 Annotation (de.fraunhofer.aisec.cpg.graph.Annotation)1 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)1 DeclaredReferenceExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression)1 File (java.io.File)1