Search in sources :

Example 1 with JCTree

use of com.sun.tools.javac.tree.JCTree in project epoxy by airbnb.

the class ResourceProcessor method processorResources.

void processorResources(RoundEnvironment env) {
    resources.clear();
    if (trees == null) {
        return;
    }
    RClassScanner scanner = new RClassScanner();
    for (Class<? extends Annotation> annotation : EpoxyProcessor.getSupportedAnnotations()) {
        for (Element element : env.getElementsAnnotatedWith(annotation)) {
            JCTree tree = (JCTree) trees.getTree(element, getMirror(element, annotation));
            if (tree != null) {
                // tree can be null if the references are compiled types and not source
                tree.accept(scanner);
            }
        }
    }
    for (String rClass : scanner.getRClasses()) {
        parseRClass(rClass, resources);
    }
}
Also used : VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) JCTree(com.sun.tools.javac.tree.JCTree)

Example 2 with JCTree

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

the class HandleCleanup method handle.

@Override
public void handle(AnnotationValues<Cleanup> annotation, JCAnnotation ast, JavacNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.CLEANUP_FLAG_USAGE, "@Cleanup");
    if (inNetbeansEditor(annotationNode))
        return;
    deleteAnnotationIfNeccessary(annotationNode, Cleanup.class);
    String cleanupName = annotation.getInstance().value();
    if (cleanupName.length() == 0) {
        annotationNode.addError("cleanupName cannot be the empty string.");
        return;
    }
    if (annotationNode.up().getKind() != Kind.LOCAL) {
        annotationNode.addError("@Cleanup is legal only on local variable declarations.");
        return;
    }
    JCVariableDecl decl = (JCVariableDecl) annotationNode.up().get();
    if (decl.init == null) {
        annotationNode.addError("@Cleanup variable declarations need to be initialized.");
        return;
    }
    JavacNode ancestor = annotationNode.up().directUp();
    JCTree blockNode = ancestor.get();
    final List<JCStatement> statements;
    if (blockNode instanceof JCBlock) {
        statements = ((JCBlock) blockNode).stats;
    } else if (blockNode instanceof JCCase) {
        statements = ((JCCase) blockNode).stats;
    } else if (blockNode instanceof JCMethodDecl) {
        statements = ((JCMethodDecl) blockNode).body.stats;
    } else {
        annotationNode.addError("@Cleanup is legal only on a local variable declaration inside a block.");
        return;
    }
    boolean seenDeclaration = false;
    ListBuffer<JCStatement> newStatements = new ListBuffer<JCStatement>();
    ListBuffer<JCStatement> tryBlock = new ListBuffer<JCStatement>();
    for (JCStatement statement : statements) {
        if (!seenDeclaration) {
            if (statement == decl)
                seenDeclaration = true;
            newStatements.append(statement);
        } else {
            tryBlock.append(statement);
        }
    }
    if (!seenDeclaration) {
        annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent.");
        return;
    }
    doAssignmentCheck(annotationNode, tryBlock.toList(), decl.name);
    JavacTreeMaker maker = annotationNode.getTreeMaker();
    JCFieldAccess cleanupMethod = maker.Select(maker.Ident(decl.name), annotationNode.toName(cleanupName));
    List<JCStatement> cleanupCall = List.<JCStatement>of(maker.Exec(maker.Apply(List.<JCExpression>nil(), cleanupMethod, List.<JCExpression>nil())));
    JCExpression preventNullAnalysis = preventNullAnalysis(maker, annotationNode, maker.Ident(decl.name));
    JCBinary isNull = maker.Binary(CTC_NOT_EQUAL, preventNullAnalysis, maker.Literal(CTC_BOT, null));
    JCIf ifNotNullCleanup = maker.If(isNull, maker.Block(0, cleanupCall), null);
    Context context = annotationNode.getContext();
    JCBlock finalizer = recursiveSetGeneratedBy(maker.Block(0, List.<JCStatement>of(ifNotNullCleanup)), ast, context);
    newStatements.append(setGeneratedBy(maker.Try(setGeneratedBy(maker.Block(0, tryBlock.toList()), ast, context), List.<JCCatch>nil(), finalizer), ast, context));
    if (blockNode instanceof JCBlock) {
        ((JCBlock) blockNode).stats = newStatements.toList();
    } else if (blockNode instanceof JCCase) {
        ((JCCase) blockNode).stats = newStatements.toList();
    } else if (blockNode instanceof JCMethodDecl) {
        ((JCMethodDecl) blockNode).body.stats = newStatements.toList();
    } else
        throw new AssertionError("Should not get here");
    ancestor.rebuild();
}
Also used : Context(com.sun.tools.javac.util.Context) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCTree(com.sun.tools.javac.tree.JCTree) JCBinary(com.sun.tools.javac.tree.JCTree.JCBinary) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) JCIf(com.sun.tools.javac.tree.JCTree.JCIf) JCCase(com.sun.tools.javac.tree.JCTree.JCCase)

Example 3 with JCTree

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

the class JavacAST method printMessage.

/** Supply either a position or a node (in that case, position of the node is used) */
void printMessage(Diagnostic.Kind kind, String message, JavacNode node, DiagnosticPosition pos, boolean attemptToRemoveErrorsInRange) {
    JavaFileObject oldSource = null;
    JavaFileObject newSource = null;
    JCTree astObject = node == null ? null : node.get();
    JCCompilationUnit top = (JCCompilationUnit) top().get();
    newSource = top.sourcefile;
    if (newSource != null) {
        oldSource = log.useSource(newSource);
        if (pos == null)
            pos = astObject.pos();
    }
    if (pos != null && attemptToRemoveErrorsInRange) {
        removeFromDeferredDiagnostics(pos.getStartPosition(), node.getEndPosition(pos));
    }
    try {
        switch(kind) {
            case ERROR:
                increaseErrorCount(messager);
                boolean prev = log.multipleErrors;
                log.multipleErrors = true;
                try {
                    log.error(pos, "proc.messager", message);
                } finally {
                    log.multipleErrors = prev;
                }
                break;
            default:
            case WARNING:
                log.warning(pos, "proc.messager", message);
                break;
        }
    } finally {
        if (oldSource != null)
            log.useSource(oldSource);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JavaFileObject(javax.tools.JavaFileObject) JCTree(com.sun.tools.javac.tree.JCTree)

Example 4 with JCTree

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

the class JavacAST method buildTry.

private JavacNode buildTry(JCTry tryNode) {
    if (setAndGetAsHandled(tryNode))
        return null;
    List<JavacNode> childNodes = new ArrayList<JavacNode>();
    for (JCTree varDecl : getResourcesForTryNode(tryNode)) {
        if (varDecl instanceof JCVariableDecl) {
            addIfNotNull(childNodes, buildLocalVar((JCVariableDecl) varDecl, Kind.LOCAL));
        }
    }
    addIfNotNull(childNodes, buildStatement(tryNode.body));
    for (JCCatch jcc : tryNode.catchers) addIfNotNull(childNodes, buildTree(jcc, Kind.STATEMENT));
    addIfNotNull(childNodes, buildStatement(tryNode.finalizer));
    return putInMap(new JavacNode(this, tryNode, childNodes, Kind.STATEMENT));
}
Also used : ArrayList(java.util.ArrayList) JCTree(com.sun.tools.javac.tree.JCTree) JCCatch(com.sun.tools.javac.tree.JCTree.JCCatch) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 5 with JCTree

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

the class JavacImportList method applyNameToStarImports.

@Override
public Collection<String> applyNameToStarImports(String startsWith, String name) {
    ArrayList<String> out = new ArrayList<String>();
    if (pkgStr != null && topLevelName(pkgStr).equals(startsWith))
        out.add(pkgStr + "." + name);
    for (JCTree def : defs) {
        if (!(def instanceof JCImport))
            continue;
        if (((JCImport) def).staticImport)
            continue;
        JCTree qual = ((JCImport) def).qualid;
        if (!(qual instanceof JCFieldAccess))
            continue;
        String simpleName = ((JCFieldAccess) qual).name.toString();
        if (!"*".equals(simpleName))
            continue;
        String topLevelName = topLevelName(qual);
        if (topLevelName.equals(startsWith)) {
            out.add(((JCFieldAccess) qual).selected.toString() + "." + name);
        }
    }
    return out;
}
Also used : JCImport(com.sun.tools.javac.tree.JCTree.JCImport) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) ArrayList(java.util.ArrayList) 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