Search in sources :

Example 16 with JCCompilationUnit

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

the class JavacAST method removeFromDeferredDiagnostics.

public void removeFromDeferredDiagnostics(int startPos, int endPos) {
    JCCompilationUnit self = (JCCompilationUnit) top().get();
    new CompilerMessageSuppressor(getContext()).removeAllBetween(self.sourcefile, startPos, endPos);
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit)

Example 17 with JCCompilationUnit

use of com.sun.tools.javac.tree.JCTree.JCCompilationUnit 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 18 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 19 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 20 with JCCompilationUnit

use of com.sun.tools.javac.tree.JCTree.JCCompilationUnit in project ceylon-compiler by ceylon.

the class JavadocTool method getRootDocImpl.

public RootDocImpl getRootDocImpl(String doclocale, String encoding, ModifierFilter filter, List<String> javaNames, List<String[]> options, boolean breakiterator, List<String> subPackages, List<String> excludedPackages, boolean docClasses, boolean legacyDoclet, boolean quiet) throws IOException {
    docenv = DocEnv.instance(context);
    docenv.showAccess = filter;
    docenv.quiet = quiet;
    docenv.breakiterator = breakiterator;
    docenv.setLocale(doclocale);
    docenv.setEncoding(encoding);
    docenv.docClasses = docClasses;
    docenv.legacyDoclet = legacyDoclet;
    reader.sourceCompleter = docClasses ? null : this;
    ListBuffer<String> names = new ListBuffer<String>();
    ListBuffer<JCCompilationUnit> classTrees = new ListBuffer<JCCompilationUnit>();
    ListBuffer<JCCompilationUnit> packTrees = new ListBuffer<JCCompilationUnit>();
    try {
        StandardJavaFileManager fm = (StandardJavaFileManager) docenv.fileManager;
        for (List<String> it = javaNames; it.nonEmpty(); it = it.tail) {
            String name = it.head;
            if (!docClasses && name.endsWith(".java") && new File(name).exists()) {
                JavaFileObject fo = fm.getJavaFileObjects(name).iterator().next();
                docenv.notice("main.Loading_source_file", name);
                JCCompilationUnit tree = parse(fo);
                classTrees.append(tree);
            } else if (isValidPackageName(name)) {
                names = names.append(name);
            } else if (name.endsWith(".java")) {
                docenv.error(null, "main.file_not_found", name);
            } else {
                docenv.error(null, "main.illegal_package_name", name);
            }
        }
        if (!docClasses) {
            // Recursively search given subpackages.  If any packages
            //are found, add them to the list.
            Map<String, List<JavaFileObject>> packageFiles = searchSubPackages(subPackages, names, excludedPackages);
            // Parse the packages
            for (List<String> packs = names.toList(); packs.nonEmpty(); packs = packs.tail) {
                // Parse sources ostensibly belonging to package.
                String packageName = packs.head;
                parsePackageClasses(packageName, packageFiles.get(packageName), packTrees, excludedPackages);
            }
            if (messager.nerrors() != 0)
                return null;
            // Enter symbols for all files
            docenv.notice("main.Building_tree");
            enter.main(classTrees.toList().appendList(packTrees.toList()));
        }
    } catch (Abort ex) {
    }
    if (messager.nerrors() != 0)
        return null;
    if (docClasses)
        return new RootDocImpl(docenv, javaNames, options);
    else
        return new RootDocImpl(docenv, listClasses(classTrees.toList()), names.toList(), options);
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) ListBuffer(com.sun.tools.javac.util.ListBuffer) JavaFileObject(javax.tools.JavaFileObject) Abort(com.sun.tools.javac.util.Abort) StandardJavaFileManager(javax.tools.StandardJavaFileManager) List(com.sun.tools.javac.util.List) File(java.io.File)

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