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());
}
}
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());
}
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());
}
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());
}
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);
}
Aggregations