Search in sources :

Example 1 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project cas by apereo.

the class ConfigurationMetadataGenerator method parseCompilationUnit.

@SneakyThrows
private void parseCompilationUnit(final Set<ConfigurationMetadataProperty> collectedProps, final Set<ConfigurationMetadataProperty> collectedGroups, final ConfigurationMetadataProperty p, final String typePath, final String typeName, final boolean indexNameWithBrackets) {
    try (InputStream is = new FileInputStream(typePath)) {
        final CompilationUnit cu = JavaParser.parse(is);
        new FieldVisitor(collectedProps, collectedGroups, indexNameWithBrackets, typeName).visit(cu, p);
        if (cu.getTypes().size() > 0) {
            final ClassOrInterfaceDeclaration decl = ClassOrInterfaceDeclaration.class.cast(cu.getType(0));
            for (int i = 0; i < decl.getExtendedTypes().size(); i++) {
                final ClassOrInterfaceType parentType = decl.getExtendedTypes().get(i);
                final Class parentClazz = locatePropertiesClassForType(parentType);
                final String parentTypePath = buildTypeSourcePath(parentClazz.getName());
                parseCompilationUnit(collectedProps, collectedGroups, p, parentTypePath, parentClazz.getName(), indexNameWithBrackets);
            }
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) FileInputStream(java.io.FileInputStream) ValueHint(org.springframework.boot.configurationmetadata.ValueHint) SneakyThrows(lombok.SneakyThrows)

Example 2 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project controller by opendaylight.

the class SieASTVisitor method visit.

@Override
public void visit(final NormalAnnotationExpr expr, final Void arg) {
    final String fqcn = expr.getName().toString();
    if (fqcn.equals(Description.class.getCanonicalName())) {
        final Node parent = expr.getParentNode();
        final String value = expr.getPairs().get(0).toString();
        if (parent instanceof ClassOrInterfaceDeclaration) {
            descriptionAnotValue = value;
        } else if (parent instanceof MethodDeclaration) {
            methodDescriptions.put(((MethodDeclaration) parent).getName(), value);
        }
    } else if (fqcn.equals(ServiceInterfaceAnnotation.class.getCanonicalName())) {
        String text1 = expr.getPairs().get(0).toString();
        String text2 = expr.getPairs().get(1).toString();
        if (text1.contains("value")) {
            sieAnnotValue = text1;
            sieAnnotOsgiRegistrationType = text2;
        } else {
            sieAnnotValue = text2;
            sieAnnotOsgiRegistrationType = text1;
        }
    }
    super.visit(expr, arg);
}
Also used : Description(org.opendaylight.controller.config.api.annotations.Description) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Node(com.github.javaparser.ast.Node)

Example 3 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project javaparser by javaparser.

the class DifferentiateDotExpressionTest method staticMethodCallsFromInnerClasses.

@Test
public void staticMethodCallsFromInnerClasses() {
    ClassOrInterfaceDeclaration clazz = ((JavaParserClassDeclaration) typeSolver.solveType("InnerClassDotExpressions")).getWrappedNode();
    MethodDeclaration mainMethod = Navigator.demandMethod(clazz, "main");
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodCallExpr methodCall = Navigator.findMethodCall(mainMethod, "methodCall").get();
    MethodCallExpr innerMethodCall = Navigator.findMethodCall(mainMethod, "innerMethodCall").get();
    MethodCallExpr innerInnerMethodCall = Navigator.findMethodCall(mainMethod, "innerInnerMethodCall").get();
    assertEquals(true, javaParserFacade.solve(methodCall).isSolved());
    assertEquals(true, javaParserFacade.solve(innerMethodCall).isSolved());
    assertEquals(true, javaParserFacade.solve(innerInnerMethodCall).isSolved());
}
Also used : JavaParserClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 4 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project javaparser by javaparser.

the class DifferentiateDotExpressionTest method staticFieldCallsFromInnerClasses.

@Test
public void staticFieldCallsFromInnerClasses() {
    ClassOrInterfaceDeclaration clazz = ((JavaParserClassDeclaration) typeSolver.solveType("InnerStaticClassFieldDotExpressions")).getWrappedNode();
    MethodDeclaration mainMethod = Navigator.demandMethod(clazz, "main");
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodCallExpr methodCallWithNestedStaticFieldParam = Navigator.findMethodCall(mainMethod, "parseInt").get();
    assertEquals(true, javaParserFacade.solve(methodCallWithNestedStaticFieldParam).isSolved());
}
Also used : JavaParserClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 5 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project javaparser by javaparser.

the class JavaParserAnonymousClassDeclarationTest method callingSuperClassInnerClassMethod.

@Test
public void callingSuperClassInnerClassMethod() {
    CompilationUnit cu = parseSample("AnonymousClassDeclarations");
    ClassOrInterfaceDeclaration aClass = Navigator.demandClass(cu, "AnonymousClassDeclarations");
    MethodDeclaration method = Navigator.demandMethod(aClass, "fooBar2");
    MethodCallExpr methodCall = Navigator.findMethodCall(method, "innerClassMethod").get();
    CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
    combinedTypeSolver.add(new ReflectionTypeSolver());
    MethodUsage methodUsage = JavaParserFacade.get(combinedTypeSolver).solveMethodAsUsage(methodCall);
    assertThat(methodUsage.getQualifiedSignature(), is("AnonymousClassDeclarations.DoFn.ProcessContext.innerClassMethod()"));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Aggregations

ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)187 Test (org.junit.Test)160 CompilationUnit (com.github.javaparser.ast.CompilationUnit)118 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)79 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)71 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)61 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)39 Context (com.github.javaparser.symbolsolver.core.resolution.Context)39 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)38 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)37 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)36 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)28 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)27 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)24 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)20 AbstractLexicalPreservingTest (com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)18 MethodUsage (com.github.javaparser.resolution.MethodUsage)17 Expression (com.github.javaparser.ast.expr.Expression)15 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)15 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)14