Search in sources :

Example 16 with TypeDeclaration

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

Example 17 with TypeDeclaration

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;
            }
        }
    }
}
Also used : Node(com.github.javaparser.ast.Node) Navigator.demandParentNode(com.github.javaparser.symbolsolver.javaparser.Navigator.demandParentNode) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)

Example 18 with TypeDeclaration

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;
            }
        }
    }
}
Also used : Node(com.github.javaparser.ast.Node) Navigator.demandParentNode(com.github.javaparser.symbolsolver.javaparser.Navigator.demandParentNode) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)

Example 19 with TypeDeclaration

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Test(org.junit.jupiter.api.Test)

Example 20 with TypeDeclaration

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Test(org.junit.jupiter.api.Test)

Aggregations

TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)44 CompilationUnit (com.github.javaparser.ast.CompilationUnit)22 Node (com.github.javaparser.ast.Node)14 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)13 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)12 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)10 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)8 List (java.util.List)8 Optional (java.util.Optional)8 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)6 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)6 Collectors (java.util.stream.Collectors)6 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)5 NodeList (com.github.javaparser.ast.NodeList)5 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)5 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)5 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)5 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)5 ResolvedValueDeclaration (com.github.javaparser.resolution.declarations.ResolvedValueDeclaration)5 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)5