Search in sources :

Example 86 with ClassOrInterfaceDeclaration

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

the class JavaParserTypeParameterResolutionTest method declaredOnMethodPositiveCase.

@Test
public void declaredOnMethodPositiveCase() {
    CompilationUnit cu = parseSample("MethodTypeParameter");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ClassOrInterfaceDeclaration classDecl = Navigator.demandClass(cu, "Foo");
    MethodDeclaration methodDecl = Navigator.demandMethod(classDecl, "usage");
    MethodCallExpr callToFoo = (MethodCallExpr) Navigator.findReturnStmt(methodDecl).getExpression().get();
    ResolvedMethodDeclaration methodDeclaration = javaParserFacade.solve(callToFoo).getCorrespondingDeclaration();
    for (ResolvedTypeParameterDeclaration tp : methodDeclaration.getTypeParameters()) {
        assertTrue(tp instanceof JavaParserTypeParameter);
        assertEquals("C", tp.getName());
        assertEquals(true, tp.declaredOnMethod());
        assertEquals(false, tp.declaredOnType());
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 87 with ClassOrInterfaceDeclaration

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

the class SymbolResolutionResolutionTest method conditionalExpressionExample.

@Test
public void conditionalExpressionExample() {
    CompilationUnit cu = parseSample("JreConditionalExpression");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo1");
    ReturnStmt returnStmt = Navigator.findReturnStmt(method);
    Expression expression = returnStmt.getExpression().get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedType ref = JavaParserFacade.get(typeSolver).getType(expression);
    assertEquals("java.lang.String", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 88 with ClassOrInterfaceDeclaration

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

the class SymbolResolutionResolutionTest method getTypeOfFieldAccess.

@Test
public void getTypeOfFieldAccess() {
    CompilationUnit cu = parseSample("ReflectionFieldOfItself");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
    VariableDeclarator field = Navigator.demandField(clazz, "PUBLIC");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedType ref = JavaParserFacade.get(typeSolver).getType(field.getInitializer().get());
    assertEquals("int", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 89 with ClassOrInterfaceDeclaration

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

the class SymbolResolutionResolutionTest method getTypeOfField.

@Test
public void getTypeOfField() {
    CompilationUnit cu = parseSample("ReflectionFieldOfItself");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
    VariableDeclarator field = Navigator.demandField(clazz, "PUBLIC");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedType ref = JavaParserFacade.get(typeSolver).getType(field);
    assertEquals("int", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 90 with ClassOrInterfaceDeclaration

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

the class FinalRClassBuilder method brewJava.

/**
 * JavaPoet 生成 R2
 *
 * @param rFile R.java File
 * @param outputDir R2.java 输出文件夹
 * @param packageName 包名
 * @param className R2 name
 * @throws Exception
 */
public static void brewJava(File rFile, File outputDir, String packageName, String className) throws Exception {
    /*
     * JavaParser 解析 R.java File
     * 获取到 TypeDeclaration
     */
    CompilationUnit compilationUnit = JavaParser.parse(rFile);
    TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
    /*
     * 定义 R2.java class
     */
    TypeSpec.Builder result = TypeSpec.classBuilder(className).addModifiers(PUBLIC).addModifiers(FINAL);
    /*
     * 遍历 R.java File 的每一个节点( 内部类或者接口 --> ClassOrInterfaceDeclaration )
     * 添加到 R2.java 内
     * 这里是给 TypeSpec 添加生成 内部类 的 语句
     */
    for (Node node : resourceClass.getChildNodes()) {
        if (node instanceof ClassOrInterfaceDeclaration) {
            addResourceType(Arrays.asList(SUPPORTED_TYPES), result, (ClassOrInterfaceDeclaration) node);
        }
    }
    JavaFile finalR = JavaFile.builder(packageName, result.build()).addFileComment("Generated code from Butter Knife gradle plugin. Do not modify!").build();
    finalR.writeTo(outputDir);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) JavaFile(com.squareup.javapoet.JavaFile) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)261 Test (org.junit.Test)174 CompilationUnit (com.github.javaparser.ast.CompilationUnit)164 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)84 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)79 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)71 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)52 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 Expression (com.github.javaparser.ast.expr.Expression)33 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 HashMap (java.util.HashMap)21 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)20 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)20 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)20 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)19