Search in sources :

Example 26 with TypeDeclaration

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

the class MethodChanger_2 method changeMethods.

private static void changeMethods(CompilationUnit cu) {
    // Go through all the types in the file
    NodeList<TypeDeclaration<?>> types = cu.getTypes();
    for (TypeDeclaration<?> type : types) {
        // Go through all fields, methods, etc. in this type
        NodeList<BodyDeclaration<?>> members = type.getMembers();
        for (BodyDeclaration<?> member : members) {
            if (member instanceof MethodDeclaration) {
                MethodDeclaration method = (MethodDeclaration) member;
                changeMethod(method);
            }
        }
    }
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 27 with TypeDeclaration

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

the class R2ClassBuilder method brewJava.

/**
 * JavaPoet 生成 R2
 *
 * @param rFile R.java File
 * @param outputDir R2.java 输出文件夹
 * @param packageName 包名
 * @param className R2 name
 * @throws Exception 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 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 28 with TypeDeclaration

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

the class CloneVisitor method visit.

@Override
public Node visit(TypeDeclarationStmt _n, Object _arg) {
    TypeDeclaration typeDecl = cloneNodes(_n.getTypeDeclaration(), _arg);
    Comment comment = cloneNodes(_n.getComment(), _arg);
    TypeDeclarationStmt r = new TypeDeclarationStmt(_n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(), typeDecl);
    r.setComment(comment);
    return r;
}
Also used : JavadocComment(com.github.javaparser.ast.comments.JavadocComment) BlockComment(com.github.javaparser.ast.comments.BlockComment) LineComment(com.github.javaparser.ast.comments.LineComment) Comment(com.github.javaparser.ast.comments.Comment) EmptyTypeDeclaration(com.github.javaparser.ast.body.EmptyTypeDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 29 with TypeDeclaration

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

the class CloneVisitor method visit.

@Override
public Node visit(CompilationUnit _n, Object _arg) {
    PackageDeclaration package_ = cloneNodes(_n.getPackage(), _arg);
    List<ImportDeclaration> imports = visit(_n.getImports(), _arg);
    List<TypeDeclaration> types = visit(_n.getTypes(), _arg);
    return new CompilationUnit(_n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(), package_, imports, types);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) EmptyTypeDeclaration(com.github.javaparser.ast.body.EmptyTypeDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Example 30 with TypeDeclaration

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

the class DumpVisitor method visit.

@Override
public void visit(final CompilationUnit n, final Object arg) {
    printJavaComment(n.getComment(), arg);
    if (n.getPackage() != null) {
        n.getPackage().accept(this, arg);
    }
    if (n.getImports() != null) {
        for (final ImportDeclaration i : n.getImports()) {
            i.accept(this, arg);
        }
        printer.printLn();
    }
    if (n.getTypes() != null) {
        for (final Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext(); ) {
            i.next().accept(this, arg);
            printer.printLn();
            if (i.hasNext()) {
                printer.printLn();
            }
        }
    }
    printOrphanCommentsEnding(n);
}
Also used : ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) EmptyTypeDeclaration(com.github.javaparser.ast.body.EmptyTypeDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Aggregations

TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)39 CompilationUnit (com.github.javaparser.ast.CompilationUnit)21 Node (com.github.javaparser.ast.Node)14 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)11 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)10 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)9 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 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 File (java.io.File)5