Search in sources :

Example 6 with JCCompilationUnit

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

the class LombokProcessor method process.

/** {@inheritDoc} */
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (lombokDisabled)
        return false;
    if (roundEnv.processingOver())
        return false;
    for (Element element : roundEnv.getRootElements()) {
        JCCompilationUnit unit = toUnit(element);
        if (unit == null)
            continue;
        if (roots.containsKey(unit))
            continue;
        roots.put(unit, priorityLevels[0]);
    }
    while (true) {
        for (long prio : priorityLevels) {
            List<JCCompilationUnit> cusForThisRound = new ArrayList<JCCompilationUnit>();
            for (Map.Entry<JCCompilationUnit, Long> entry : roots.entrySet()) {
                Long prioOfCu = entry.getValue();
                if (prioOfCu == null || prioOfCu != prio)
                    continue;
                cusForThisRound.add(entry.getKey());
            }
            transformer.transform(prio, processingEnv.getContext(), cusForThisRound);
        }
        // Step 3: Push up all CUs to the next level. Set level to null if there is no next level.
        Set<Long> newLevels = new HashSet<Long>();
        for (int i = priorityLevels.length - 1; i >= 0; i--) {
            Long curLevel = priorityLevels[i];
            Long nextLevel = (i == priorityLevels.length - 1) ? null : priorityLevels[i + 1];
            List<JCCompilationUnit> cusToAdvance = new ArrayList<JCCompilationUnit>();
            for (Map.Entry<JCCompilationUnit, Long> entry : roots.entrySet()) {
                if (curLevel.equals(entry.getValue())) {
                    cusToAdvance.add(entry.getKey());
                    newLevels.add(nextLevel);
                }
            }
            for (JCCompilationUnit unit : cusToAdvance) {
                roots.put(unit, nextLevel);
            }
        }
        newLevels.remove(null);
        if (newLevels.isEmpty())
            return false;
        newLevels.retainAll(priorityLevelsRequiringResolutionReset);
        if (!newLevels.isEmpty()) {
            // Force a new round to reset resolution. The next round will cause this method (process) to be called again.
            forceNewRound((JavacFiler) processingEnv.getFiler());
            return false;
        }
    // None of the new levels need resolution, so just keep going.
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashSet(java.util.HashSet)

Example 7 with JCCompilationUnit

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

the class JavacTransformer method transform.

public void transform(long priority, Context context, java.util.List<JCCompilationUnit> compilationUnitsRaw) {
    List<JCCompilationUnit> compilationUnits;
    if (compilationUnitsRaw instanceof List<?>) {
        compilationUnits = (List<JCCompilationUnit>) compilationUnitsRaw;
    } else {
        compilationUnits = List.nil();
        for (int i = compilationUnitsRaw.size() - 1; i >= 0; i--) {
            compilationUnits = compilationUnits.prepend(compilationUnitsRaw.get(i));
        }
    }
    java.util.List<JavacAST> asts = new ArrayList<JavacAST>();
    for (JCCompilationUnit unit : compilationUnits) asts.add(new JavacAST(messager, context, unit));
    for (JavacAST ast : asts) {
        ast.traverse(new AnnotationVisitor(priority));
        handlers.callASTVisitors(ast, priority);
    }
    for (JavacAST ast : asts) if (ast.isChanged())
        LombokOptions.markChanged(context, (JCCompilationUnit) ast.top().get());
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) ArrayList(java.util.ArrayList) List(com.sun.tools.javac.util.List) ArrayList(java.util.ArrayList)

Example 8 with JCCompilationUnit

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

the class HandleSneakyThrows method buildTryCatchBlock.

public JCStatement buildTryCatchBlock(JavacNode node, List<JCStatement> contents, String exception, JCTree source) {
    JavacTreeMaker maker = node.getTreeMaker();
    Context context = node.getContext();
    JCBlock tryBlock = setGeneratedBy(maker.Block(0, contents), source, context);
    JCExpression varType = chainDots(node, exception.split("\\."));
    JCVariableDecl catchParam = maker.VarDef(maker.Modifiers(Flags.FINAL | Flags.PARAMETER), node.toName("$ex"), varType, null);
    JCExpression lombokLombokSneakyThrowNameRef = chainDots(node, "lombok", "Lombok", "sneakyThrow");
    JCBlock catchBody = maker.Block(0, List.<JCStatement>of(maker.Throw(maker.Apply(List.<JCExpression>nil(), lombokLombokSneakyThrowNameRef, List.<JCExpression>of(maker.Ident(node.toName("$ex")))))));
    JCTry tryStatement = maker.Try(tryBlock, List.of(recursiveSetGeneratedBy(maker.Catch(catchParam, catchBody), source, context)), null);
    if (JavacHandlerUtil.inNetbeansEditor(node)) {
        //set span (start and end position) of the try statement and the main block
        //this allows NetBeans to dive into the statement correctly:
        JCCompilationUnit top = (JCCompilationUnit) node.top().get();
        int startPos = contents.head.pos;
        int endPos = Javac.getEndPosition(contents.last().pos(), top);
        tryBlock.pos = startPos;
        tryStatement.pos = startPos;
        Javac.storeEnd(tryBlock, endPos, top);
        Javac.storeEnd(tryStatement, endPos, top);
    }
    return setGeneratedBy(tryStatement, source, context);
}
Also used : Context(com.sun.tools.javac.util.Context) JCTry(com.sun.tools.javac.tree.JCTree.JCTry) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 9 with JCCompilationUnit

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

the class TreePrunerTest method qualifiedSuperConstructorChaining.

@Test
public void qualifiedSuperConstructorChaining() {
    String[] lines = { "class Test {", "  class Inner {", "    Inner(OuterInstance outer) {", "      outer.super();", "    }", "  }", "}" };
    JCCompilationUnit tree = parseLines(lines);
    TreePruner.prune(context, tree);
    String[] expected = { "class Test {", "    ", "    class Inner {", "        ", "        Inner(OuterInstance outer) {", "            outer.super();", "        }", "    }", "}" };
    assertThat(prettyPrint(tree)).isEqualTo(Joiner.on('\n').join(expected));
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) Test(org.junit.Test)

Example 10 with JCCompilationUnit

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

the class TreePrunerTest method interfaceDeclaration.

@Test
public void interfaceDeclaration() {
    String[] lines = { "interface Intf {", "  int CONST = 42;", "  int NONCONST = new Integer(42);", "}" };
    JCCompilationUnit tree = parseLines(lines);
    TreePruner.prune(context, tree);
    String[] expected = { "interface Intf {", "    int CONST = 42;", "    int NONCONST;", "}" };
    assertThat(prettyPrint(tree)).isEqualTo(Joiner.on('\n').join(expected));
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) Test(org.junit.Test)

Aggregations

JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)50 JCTree (com.sun.tools.javac.tree.JCTree)11 JavaFileObject (javax.tools.JavaFileObject)7 Test (org.junit.Test)7 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)6 IOException (java.io.IOException)6 CeylonCompilationUnit (com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit)5 TaskEvent (com.sun.source.util.TaskEvent)5 File (java.io.File)5 TaskListener (com.sun.source.util.TaskListener)4 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)4 ArrayList (java.util.ArrayList)4 ImmutableList (com.google.common.collect.ImmutableList)3 CeyloncTaskImpl (com.redhat.ceylon.compiler.java.tools.CeyloncTaskImpl)3 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)3 TreePath (com.sun.source.util.TreePath)3 JavacTool (com.sun.tools.javac.api.JavacTool)3 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)3 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)3 Context (com.sun.tools.javac.util.Context)3