use of com.github.javaparser.ast.body.TypeDeclaration 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);
}
use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.
the class JavaParserFacade method findContainingTypeDeclOrObjectCreationExpr.
/**
* Where a node has an interface/class/enum declaration -- or an object creation expression in an inner class
* references an outer class -- as its ancestor, return the declaration corresponding to the class name specified.
*/
protected Node findContainingTypeDeclOrObjectCreationExpr(Node node, String className) {
Node parent = node;
boolean detachFlag = false;
while (true) {
parent = demandParentNode(parent);
if (parent instanceof BodyDeclaration) {
if (parent instanceof TypeDeclaration && ((TypeDeclaration<?>) parent).getFullyQualifiedName().get().endsWith(className)) {
return parent;
} else {
detachFlag = true;
}
} else if (parent instanceof ObjectCreationExpr && ((ObjectCreationExpr) parent).getType().getName().asString().equals(className)) {
if (detachFlag) {
return parent;
}
}
}
}
use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.
the class JavaParserFacade method findContainingTypeDeclOrObjectCreationExpr.
/**
* Where a node has an interface/class/enum declaration -- or an object creation expression (anonymous inner class)
* -- as its ancestor, return the nearest one.
* <p>
* NOTE: See {@link #findContainingTypeDecl} if wanting to not include anonymous inner classes.
* <p>
* For example, these all return X:
* <ul>
* <li>{@code public interface X { ... node here ... }}</li>
* <li>{@code public class X { ... node here ... }}</li>
* <li>{@code public enum X { ... node here ... }}</li>
* <li><pre>{@code
* new ActionListener() {
* ... node here ...
* public void actionPerformed(ActionEvent e) {
* ... or node here ...
* }
* }
* }</pre></li>
* </ul>
* <p>
*
* @param node The Node whose ancestors will be traversed,
* @return The first class/interface/enum declaration -- or object creation expression (anonymous inner class) -- in
* the Node's ancestry.
*/
protected Node findContainingTypeDeclOrObjectCreationExpr(Node node) {
Node parent = node;
boolean detachFlag = false;
while (true) {
parent = demandParentNode(parent);
if (parent instanceof BodyDeclaration) {
if (parent instanceof TypeDeclaration) {
return parent;
} else {
detachFlag = true;
}
} else if (parent instanceof ObjectCreationExpr) {
if (detachFlag) {
return parent;
}
}
}
}
use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.
the class DefaultPrettyPrinterTest method testIssue2578.
@Test
public void testIssue2578() {
String code = "class C{\n" + " //orphan\n" + " /*orphan*/\n" + "}";
CompilationUnit cu = StaticJavaParser.parse(code);
TypeDeclaration td = cu.findFirst(TypeDeclaration.class).get();
assertEquals(2, td.getAllContainedComments().size());
// --- simple AST change -----
td.setPublic(true);
// orphan and /*orphan*/ must be printed
System.out.println(cu.toString());
// the orphaned comments exist
assertEquals(2, td.getAllContainedComments().size());
}
use of com.github.javaparser.ast.body.TypeDeclaration in project javaparser by javaparser.
the class PrettyPrinterTest method testIssue2578.
@Test
public void testIssue2578() {
String code = "class C{\n" + " //orphan\n" + " /*orphan*/\n" + "}";
CompilationUnit cu = StaticJavaParser.parse(code);
TypeDeclaration td = cu.findFirst(TypeDeclaration.class).get();
assertEquals(2, td.getAllContainedComments().size());
// --- simple AST change -----
td.setPublic(true);
// orphan and /*orphan*/ must be printed
System.out.println(cu.toString());
// the orphaned comments exist
assertEquals(2, td.getAllContainedComments().size());
}
Aggregations