Search in sources :

Example 16 with JCTree

use of com.sun.tools.javac.tree.JCTree in project lombok by rzwitserloot.

the class JavacHandlerUtil method injectMethod.

/**
	 * Adds the given new method declaration to the provided type AST Node.
	 * Can also inject constructors.
	 * 
	 * Also takes care of updating the JavacAST.
	 */
public static void injectMethod(JavacNode typeNode, JCMethodDecl method, List<Type> paramTypes, Type returnType) {
    JCClassDecl type = (JCClassDecl) typeNode.get();
    if (method.getName().contentEquals("<init>")) {
        //Scan for default constructor, and remove it.
        int idx = 0;
        for (JCTree def : type.defs) {
            if (def instanceof JCMethodDecl) {
                if ((((JCMethodDecl) def).mods.flags & Flags.GENERATEDCONSTR) != 0) {
                    JavacNode tossMe = typeNode.getNodeFor(def);
                    if (tossMe != null)
                        tossMe.up().removeChild(tossMe);
                    type.defs = addAllButOne(type.defs, idx);
                    ClassSymbolMembersField.remove(type.sym, ((JCMethodDecl) def).sym);
                    break;
                }
            }
            idx++;
        }
    }
    addSuppressWarningsAll(method.mods, typeNode, method.pos, getGeneratedBy(method), typeNode.getContext());
    addGenerated(method.mods, typeNode, method.pos, getGeneratedBy(method), typeNode.getContext());
    type.defs = type.defs.append(method);
    fixMethodMirror(typeNode.getContext(), typeNode.getElement(), method.getModifiers().flags, method.getName(), paramTypes, returnType);
    typeNode.add(method, Kind.METHOD);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacNode(lombok.javac.JavacNode) JCTree(com.sun.tools.javac.tree.JCTree)

Example 17 with JCTree

use of com.sun.tools.javac.tree.JCTree in project lombok by rzwitserloot.

the class JavacHandlerUtil method injectField.

private static JavacNode injectField(JavacNode typeNode, JCVariableDecl field, boolean addGenerated) {
    JCClassDecl type = (JCClassDecl) typeNode.get();
    if (addGenerated) {
        addSuppressWarningsAll(field.mods, typeNode, field.pos, getGeneratedBy(field), typeNode.getContext());
        addGenerated(field.mods, typeNode, field.pos, getGeneratedBy(field), typeNode.getContext());
    }
    List<JCTree> insertAfter = null;
    List<JCTree> insertBefore = type.defs;
    while (true) {
        boolean skip = false;
        if (insertBefore.head instanceof JCVariableDecl) {
            JCVariableDecl f = (JCVariableDecl) insertBefore.head;
            if (isEnumConstant(f) || isGenerated(f))
                skip = true;
        } else if (insertBefore.head instanceof JCMethodDecl) {
            if ((((JCMethodDecl) insertBefore.head).mods.flags & GENERATEDCONSTR) != 0)
                skip = true;
        }
        if (skip) {
            insertAfter = insertBefore;
            insertBefore = insertBefore.tail;
            continue;
        }
        break;
    }
    List<JCTree> fieldEntry = List.<JCTree>of(field);
    fieldEntry.tail = insertBefore;
    if (insertAfter == null) {
        type.defs = fieldEntry;
    } else {
        insertAfter.tail = fieldEntry;
    }
    return typeNode.add(field, Kind.FIELD);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCTree(com.sun.tools.javac.tree.JCTree) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 18 with JCTree

use of com.sun.tools.javac.tree.JCTree in project antlr4 by antlr.

the class CommentHasStringValueProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    //		Messager messager = processingEnv.getMessager();
    //		messager.printMessage(Diagnostic.Kind.NOTE, "PROCESS--------------------");
    Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(CommentHasStringValue.class);
    for (Element annotatedElement : annotatedElements) {
        String docComment = utilities.getDocComment(annotatedElement);
        JCTree.JCLiteral literal = treeMaker.Literal(docComment != null ? docComment : "");
        JCTree elementTree = utilities.getTree(annotatedElement);
        if (elementTree instanceof JCTree.JCVariableDecl) {
            ((JCTree.JCVariableDecl) elementTree).init = literal;
        } else if (elementTree instanceof JCTree.JCMethodDecl) {
            JCTree.JCStatement[] statements = new JCTree.JCStatement[1];
            statements[0] = treeMaker.Return(literal);
            JCTree.JCBlock body = treeMaker.Block(0, List.from(statements));
            ((JCTree.JCMethodDecl) elementTree).body = body;
        }
    }
    return true;
}
Also used : Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) JCTree(com.sun.tools.javac.tree.JCTree)

Example 19 with JCTree

use of com.sun.tools.javac.tree.JCTree in project bazel by bazelbuild.

the class NodeUtils method isBooleanTypeNode.

/**
     * @return true iff <code>node</code> corresponds to a boolean typed
     *         expression (either the primitive type <code>boolean</code>, or
     *         class type {@link java.lang.Boolean})
     */
public static boolean isBooleanTypeNode(Node node) {
    if (node instanceof ConditionalOrNode) {
        return true;
    }
    // not all nodes have an associated tree, but those are all not of a
    // boolean type.
    Tree tree = node.getTree();
    if (tree == null) {
        return false;
    }
    Type type = ((JCTree) tree).type;
    if (TypesUtils.isBooleanType(type)) {
        return true;
    }
    return false;
}
Also used : Type(com.sun.tools.javac.code.Type) Tree(com.sun.source.tree.Tree) JCTree(com.sun.tools.javac.tree.JCTree) JCTree(com.sun.tools.javac.tree.JCTree) ConditionalOrNode(org.checkerframework.dataflow.cfg.node.ConditionalOrNode)

Example 20 with JCTree

use of com.sun.tools.javac.tree.JCTree in project error-prone by google.

the class FilesLinesLeak method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    if (!MATCHER.matches(tree, state)) {
        return NO_MATCH;
    }
    if (inTWR(state)) {
        return NO_MATCH;
    }
    Description.Builder description = buildDescription(tree);
    Tree parent = state.getPath().getParentPath().getLeaf();
    if (parent instanceof MemberSelectTree) {
        MemberSelectTree select = (MemberSelectTree) parent;
        StatementTree statement = state.findEnclosing(StatementTree.class);
        SuggestedFix.Builder fix = SuggestedFix.builder();
        if (statement instanceof VariableTree) {
            VariableTree var = (VariableTree) statement;
            int pos = ((JCTree) var).getStartPosition();
            int initPos = ((JCTree) var.getInitializer()).getStartPosition();
            int eqPos = pos + state.getSourceForNode(var).substring(0, initPos - pos).lastIndexOf('=');
            fix.replace(eqPos, initPos, String.format(";\ntry (Stream<String> stream = %s) {\n%s =", state.getSourceForNode(tree), var.getName().toString()));
        } else {
            fix.prefixWith(statement, String.format("try (Stream<String> stream = %s) {\n", state.getSourceForNode(tree)));
            fix.replace(select.getExpression(), "stream");
        }
        fix.replace(tree, "stream");
        fix.postfixWith(statement, "}");
        fix.addImport("java.util.stream.Stream");
        description.addFix(fix.build());
    }
    return description.build();
}
Also used : StatementTree(com.sun.source.tree.StatementTree) Description(com.google.errorprone.matchers.Description) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MemberSelectTree(com.sun.source.tree.MemberSelectTree) VariableTree(com.sun.source.tree.VariableTree) ExpressionTree(com.sun.source.tree.ExpressionTree) VariableTree(com.sun.source.tree.VariableTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) StatementTree(com.sun.source.tree.StatementTree) Tree(com.sun.source.tree.Tree) JCTree(com.sun.tools.javac.tree.JCTree)

Aggregations

JCTree (com.sun.tools.javac.tree.JCTree)183 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)28 Symbol (com.sun.tools.javac.code.Symbol)22 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)22 Type (com.sun.tools.javac.code.Type)19 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)17 Tree (com.sun.source.tree.Tree)15 ExpressionTree (com.sun.source.tree.ExpressionTree)14 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)14 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)11 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)11 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)10 ArrayList (java.util.ArrayList)10 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)9 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)9 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)8 ListBuffer (com.sun.tools.javac.util.ListBuffer)8 Type (com.redhat.ceylon.model.typechecker.model.Type)7 ClassTree (com.sun.source.tree.ClassTree)7 MemberSelectTree (com.sun.source.tree.MemberSelectTree)7