Search in sources :

Example 21 with BodyDeclaration

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

the class CloneVisitor method visit.

@Override
public Node visit(EnumDeclaration _n, Object _arg) {
    JavadocComment javaDoc = cloneNodes(_n.getJavaDoc(), _arg);
    List<AnnotationExpr> annotations = visit(_n.getAnnotations(), _arg);
    List<ClassOrInterfaceType> implementsList = visit(_n.getImplements(), _arg);
    List<EnumConstantDeclaration> entries = visit(_n.getEntries(), _arg);
    List<BodyDeclaration> members = visit(_n.getMembers(), _arg);
    Comment comment = cloneNodes(_n.getComment(), _arg);
    EnumDeclaration r = new EnumDeclaration(_n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(), _n.getModifiers(), annotations, _n.getName(), implementsList, entries, members);
    r.setComment(comment);
    return r;
}
Also used : EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration) 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) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration)

Example 22 with BodyDeclaration

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

the class CloneVisitor method visit.

@Override
public Node visit(ClassOrInterfaceDeclaration _n, Object _arg) {
    JavadocComment javaDoc = cloneNodes(_n.getJavaDoc(), _arg);
    List<AnnotationExpr> annotations = visit(_n.getAnnotations(), _arg);
    List<TypeParameter> typeParameters = visit(_n.getTypeParameters(), _arg);
    List<ClassOrInterfaceType> extendsList = visit(_n.getExtends(), _arg);
    List<ClassOrInterfaceType> implementsList = visit(_n.getImplements(), _arg);
    List<BodyDeclaration> members = visit(_n.getMembers(), _arg);
    Comment comment = cloneNodes(_n.getComment(), _arg);
    ClassOrInterfaceDeclaration r = new ClassOrInterfaceDeclaration(_n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(), _n.getModifiers(), annotations, _n.isInterface(), _n.getName(), typeParameters, extendsList, implementsList, members);
    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) TypeParameter(com.github.javaparser.ast.TypeParameter) MultiTypeParameter(com.github.javaparser.ast.body.MultiTypeParameter) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration)

Example 23 with BodyDeclaration

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

the class JavaParser method parseBodyDeclaration.

/**
 * Parses the Java body declaration(e.g fields or methods) contained in a
 * {@link String} and returns a {@link BodyDeclaration} that represents it.
 *
 * @param body
 *            {@link String} containing Java body declaration
 * @return BodyDeclaration representing the Java annotation
 * @throws ParseException
 *             if the source code has parser errors
 */
public static BodyDeclaration parseBodyDeclaration(final String body) {
    StringReader sr = new StringReader(body);
    BodyDeclaration bd = new ASTParser(sr).AnnotationBodyDeclaration();
    sr.close();
    return bd;
}
Also used : StringReader(java.io.StringReader) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) ASTParser(com.github.javaparser.ASTParser)

Example 24 with BodyDeclaration

use of com.github.javaparser.ast.body.BodyDeclaration 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 25 with BodyDeclaration

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

the class R2ClassBuilder method addResourceType.

/**
 * 复制资源 内部 class 到 R2 class 中
 *
 * 这里是给 R2 的 TypeSpec 添加生成局域
 *
 * @param supportedTypes 支持的类型( "array", "attr", "bool", "color", "dimen", "drawable", "id",
 * "integer", "string" )
 * @param result R2 的 TypeSpec
 * @param node R 的 接口或者类 ClassOrInterfaceDeclaration
 */
private static void addResourceType(List<String> supportedTypes, TypeSpec.Builder result, ClassOrInterfaceDeclaration node) {
    // 判断是否是资源内部类( "array", "attr", "bool", "color", "dimen", "drawable", "id", "integer", "string" )
    if (!supportedTypes.contains(node.getNameAsString())) {
        return;
    }
    // 创建 R2 的内部类 TypeSpec,为了进行资源复制
    String type = node.getNameAsString();
    TypeSpec.Builder resourceType = TypeSpec.classBuilder(type).addModifiers(PUBLIC, STATIC, FINAL);
    /*
         * 遍历 R 内部类的每个资源 field
         * 然后给 R2 的内部类 TypeSpec 添加 field 生成语句
         */
    for (BodyDeclaration field : node.getMembers()) {
        if (field instanceof FieldDeclaration) {
            addResourceField(resourceType, ((FieldDeclaration) field).getVariables().get(0), getSupportAnnotationClass(type));
        }
    }
    // R2 的内部类 TypeSpec 添加到 R2 TypeSpec 内
    result.addType(resourceType.build());
}
Also used : BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)29 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)10 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)9 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)9 CompilationUnit (com.github.javaparser.ast.CompilationUnit)7 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)7 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)7 NodeList (com.github.javaparser.ast.NodeList)6 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)6 ArrayList (java.util.ArrayList)6 EnumConstantDeclaration (com.github.javaparser.ast.body.EnumConstantDeclaration)5 BlockComment (com.github.javaparser.ast.comments.BlockComment)5 Comment (com.github.javaparser.ast.comments.Comment)5 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)5 LineComment (com.github.javaparser.ast.comments.LineComment)5 List (java.util.List)5 Parameter (com.github.javaparser.ast.body.Parameter)4 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)3 Expression (com.github.javaparser.ast.expr.Expression)3 Type (com.github.javaparser.ast.type.Type)3