Search in sources :

Example 31 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 32 with JCTree

use of com.sun.tools.javac.tree.JCTree in project butterknife by JakeWharton.

the class ButterKnifeProcessor method scanForRClasses.

private void scanForRClasses(RoundEnvironment env) {
    if (trees == null)
        return;
    RClassScanner scanner = new RClassScanner();
    for (Class<? extends Annotation> annotation : 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
                String respectivePackageName = elementUtils.getPackageOf(element).getQualifiedName().toString();
                scanner.setCurrentPackageName(respectivePackageName);
                tree.accept(scanner);
            }
        }
    }
    for (Map.Entry<String, Set<String>> packageNameToRClassSet : scanner.getRClasses().entrySet()) {
        String respectivePackageName = packageNameToRClassSet.getKey();
        for (String rClass : packageNameToRClassSet.getValue()) {
            parseRClass(respectivePackageName, rClass);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) BitSet(java.util.BitSet) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) JCTree(com.sun.tools.javac.tree.JCTree) BindString(butterknife.BindString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 33 with JCTree

use of com.sun.tools.javac.tree.JCTree in project butterknife by JakeWharton.

the class ButterKnifeProcessor method parseRClass.

private void parseRClass(String respectivePackageName, String rClass) {
    Element element;
    try {
        element = elementUtils.getTypeElement(rClass);
    } catch (MirroredTypeException mte) {
        element = typeUtils.asElement(mte.getTypeMirror());
    }
    JCTree tree = (JCTree) trees.getTree(element);
    if (tree != null) {
        // tree can be null if the references are compiled types and not source
        IdScanner idScanner = new IdScanner(symbols, elementUtils.getPackageOf(element).getQualifiedName().toString(), respectivePackageName);
        tree.accept(idScanner);
    } else {
        parseCompiledR(respectivePackageName, (TypeElement) element);
    }
}
Also used : MirroredTypeException(javax.lang.model.type.MirroredTypeException) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) JCTree(com.sun.tools.javac.tree.JCTree)

Example 34 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 35 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)

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