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.
}
}
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());
}
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);
}
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));
}
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));
}
Aggregations