Search in sources :

Example 76 with Node

use of com.github.javaparser.ast.Node in project javaparser by javaparser.

the class ConcreteSyntaxModelTest method printParameters.

@Test
public void printParameters() {
    Node node = JavaParser.parseBodyDeclaration("int x(int y, int z) {}");
    assertEquals("int x(int y, int z) {" + EOL + "}", print(node));
}
Also used : Node(com.github.javaparser.ast.Node) Test(org.junit.Test)

Example 77 with Node

use of com.github.javaparser.ast.Node in project javaparser by javaparser.

the class CommentsInserter method insertComments.

/**
 * Comments are attributed to the thing they comment and are removed from
 * the comments.
 */
private void insertComments(CompilationUnit cu, TreeSet<Comment> comments) {
    if (comments.isEmpty())
        return;
    /* I should sort all the direct children and the comments, if a comment
         is the first thing then it
         a comment to the CompilationUnit */
    // FIXME if there is no package it could be also a comment to the following class...
    // so I could use some heuristics in these cases to distinguish the two
    // cases
    List<Node> children = cu.getChildrenNodes();
    PositionUtils.sortByBeginPosition(children);
    Comment firstComment = comments.iterator().next();
    if (cu.getPackage() != null && (children.isEmpty() || PositionUtils.areInOrder(firstComment, children.get(0)))) {
        cu.setComment(firstComment);
        comments.remove(firstComment);
    }
}
Also used : LineComment(com.github.javaparser.ast.comments.LineComment) Comment(com.github.javaparser.ast.comments.Comment) Node(com.github.javaparser.ast.Node)

Example 78 with Node

use of com.github.javaparser.ast.Node in project javaparser by javaparser.

the class CommentsInserter method insertComments.

/**
 * This method try to attributes the nodes received to child of the node. It
 * returns the node that were not attributed.
 */
void insertComments(Node node, TreeSet<Comment> commentsToAttribute) {
    if (commentsToAttribute.isEmpty())
        return;
    if (node instanceof CompilationUnit) {
        insertComments((CompilationUnit) node, commentsToAttribute);
    }
    // the comments can:
    // 1) Inside one of the child, then it is the child that have to
    // associate them
    // 2) If they are not inside a child they could be preceeding nothing, a
    // comment or a child
    // if they preceed a child they are assigned to it, otherweise they
    // remain "orphans"
    List<Node> children = node.getChildrenNodes();
    PositionUtils.sortByBeginPosition(children);
    for (Node child : children) {
        TreeSet<Comment> commentsInsideChild = new TreeSet<>(NODE_BY_BEGIN_POSITION);
        for (Comment c : commentsToAttribute) {
            if (PositionUtils.nodeContains(child, c, configuration.doNotConsiderAnnotationsAsNodeStartForCodeAttribution)) {
                commentsInsideChild.add(c);
            }
        }
        commentsToAttribute.removeAll(commentsInsideChild);
        insertComments(child, commentsInsideChild);
    }
    /* I can attribute in line comments to elements preceeding them, if
         there is something contained in their line */
    List<Comment> attributedComments = new LinkedList<>();
    for (Comment comment : commentsToAttribute) {
        if (comment.isLineComment()) {
            for (Node child : children) {
                if (child.getEnd().line == comment.getBegin().line && attributeLineCommentToNodeOrChild(child, comment.asLineComment())) {
                    attributedComments.add(comment);
                }
            }
        }
    }
    /* at this point I create an ordered list of all remaining comments and
         children */
    Comment previousComment = null;
    attributedComments = new LinkedList<>();
    List<Node> childrenAndComments = new LinkedList<>();
    childrenAndComments.addAll(children);
    childrenAndComments.addAll(commentsToAttribute);
    PositionUtils.sortByBeginPosition(childrenAndComments, configuration.doNotConsiderAnnotationsAsNodeStartForCodeAttribution);
    for (Node thing : childrenAndComments) {
        if (thing instanceof Comment) {
            previousComment = (Comment) thing;
            if (!previousComment.isOrphan()) {
                previousComment = null;
            }
        } else {
            if (previousComment != null && !thing.hasComment()) {
                if (!configuration.doNotAssignCommentsPrecedingEmptyLines || !thereAreLinesBetween(previousComment, thing)) {
                    thing.setComment(previousComment);
                    attributedComments.add(previousComment);
                    previousComment = null;
                }
            }
        }
    }
    commentsToAttribute.removeAll(attributedComments);
    // all the remaining are orphan nodes
    for (Comment c : commentsToAttribute) {
        if (c.isOrphan()) {
            node.addOrphanComment(c);
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) LineComment(com.github.javaparser.ast.comments.LineComment) Comment(com.github.javaparser.ast.comments.Comment) Node(com.github.javaparser.ast.Node)

Example 79 with Node

use of com.github.javaparser.ast.Node 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 80 with Node

use of com.github.javaparser.ast.Node in project butterknife by JakeWharton.

the class FinalRClassBuilder method brewJava.

public static void brewJava(File rFile, File outputDir, String packageName, String className) throws Exception {
    CompilationUnit compilationUnit = JavaParser.parse(rFile);
    TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
    TypeSpec.Builder result = TypeSpec.classBuilder(className).addModifiers(PUBLIC).addModifiers(FINAL);
    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)

Aggregations

Node (com.github.javaparser.ast.Node)95 Test (org.junit.Test)24 Expression (com.github.javaparser.ast.expr.Expression)22 NodeList (com.github.javaparser.ast.NodeList)18 Comment (com.github.javaparser.ast.comments.Comment)13 CompilationUnit (com.github.javaparser.ast.CompilationUnit)12 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)12 NameExpr (com.github.javaparser.ast.expr.NameExpr)12 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 ArrayList (java.util.ArrayList)11 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)8 Collectors (java.util.stream.Collectors)8 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)7 LineComment (com.github.javaparser.ast.comments.LineComment)7 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)7 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)7 SimpleName (com.github.javaparser.ast.expr.SimpleName)7 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)7 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)6 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)6