Search in sources :

Example 1 with AnnotationProcessorEnvironmentImpl

use of com.sun.tools.apt.mirror.apt.AnnotationProcessorEnvironmentImpl in project ceylon-compiler by ceylon.

the class Apt method main.

public void main(com.sun.tools.javac.util.List<JCTree.JCCompilationUnit> treeList, ListBuffer<ClassSymbol> classes, Map<String, String> origOptions, ClassLoader aptCL, AnnotationProcessorFactory providedFactory, java.util.Set<Class<? extends AnnotationProcessorFactory>> productiveFactories) {
    Bark bark = Bark.instance(context);
    java.io.PrintWriter out = bark.warnWriter;
    Options options = Options.instance(context);
    Collection<TypeDeclaration> spectypedecls = new LinkedHashSet<TypeDeclaration>();
    Collection<TypeDeclaration> typedecls = new LinkedHashSet<TypeDeclaration>();
    Set<String> unmatchedAnnotations = new LinkedHashSet<String>();
    Set<AnnotationTypeDeclaration> emptyATDS = Collections.emptySet();
    Set<Class<? extends AnnotationProcessorFactory>> currentRoundFactories = new LinkedHashSet<Class<? extends AnnotationProcessorFactory>>();
    // Determine what annotations are present on the input source
    // files, create collections of specified type declarations,
    // and type declarations.
    AptTreeScanner ats = new AptTreeScanner();
    for (JCTree t : treeList) {
        t.accept(ats);
    }
    // Turn collection of ClassSymbols into Collection of apt decls
    for (ClassSymbol cs : ats.specifiedDeclCollection) {
        TypeDeclaration decl = aptenv.declMaker.getTypeDeclaration(cs);
        spectypedecls.add(decl);
    }
    for (ClassSymbol cs : ats.declCollection) {
        TypeDeclaration decl = aptenv.declMaker.getTypeDeclaration(cs);
        typedecls.add(decl);
    }
    unmatchedAnnotations.addAll(ats.getAnnotationSet());
    // Process input class files
    for (ClassSymbol cs : classes) {
        TypeDeclaration decl = aptenv.declMaker.getTypeDeclaration(cs);
        // System.out.println("Adding a class to spectypedecls");
        spectypedecls.add(decl);
        typedecls.add(decl);
        computeAnnotationSet(cs, unmatchedAnnotations);
    }
    if (options.get("-XListAnnotationTypes") != null) {
        out.println("Set of annotations found:" + (new TreeSet<String>(unmatchedAnnotations)).toString());
    }
    AnnotationProcessorEnvironmentImpl trivAPE = new AnnotationProcessorEnvironmentImpl(spectypedecls, typedecls, origOptions, context);
    if (options.get("-XListDeclarations") != null) {
        out.println("Set of Specified Declarations:" + spectypedecls);
        out.println("Set of Included Declarations: " + typedecls);
    }
    if (options.get("-print") != null) {
        if (spectypedecls.size() == 0)
            throw new UsageMessageNeededException();
        // Run the printing processor
        AnnotationProcessor proc = (new BootstrapAPF()).getProcessorFor(new HashSet<AnnotationTypeDeclaration>(), trivAPE);
        proc.process();
    } else {
        // Discovery process
        // List of annotation processory factory instances
        java.util.Iterator<AnnotationProcessorFactory> providers = null;
        {
            /*
                 * If a factory is provided by the user, the
                 * "-factory" and "-factorypath" options are not used.
                 *
                 * Otherwise, if the "-factory" option is used, search
                 * the appropriate path for the named class.
                 * Otherwise, use sun.misc.Service to implement the
                 * default discovery policy.
                 */
            java.util.List<AnnotationProcessorFactory> list = new LinkedList<AnnotationProcessorFactory>();
            String factoryName = options.get("-factory");
            if (providedFactory != null) {
                list.add(providedFactory);
                providers = list.iterator();
            } else if (factoryName != null) {
                try {
                    AnnotationProcessorFactory factory = (AnnotationProcessorFactory) (aptCL.loadClass(factoryName).newInstance());
                    list.add(factory);
                } catch (ClassNotFoundException cnfe) {
                    bark.aptWarning("FactoryNotFound", factoryName);
                } catch (ClassCastException cce) {
                    bark.aptWarning("FactoryWrongType", factoryName);
                } catch (Exception e) {
                    bark.aptWarning("FactoryCantInstantiate", factoryName);
                } catch (Throwable t) {
                    throw new AnnotationProcessingError(t);
                }
                providers = list.iterator();
            } else {
                @SuppressWarnings("unchecked") Iterator<AnnotationProcessorFactory> iter = sun.misc.Service.providers(AnnotationProcessorFactory.class, aptCL);
                providers = iter;
            }
        }
        java.util.Map<AnnotationProcessorFactory, Set<AnnotationTypeDeclaration>> factoryToAnnotation = new LinkedHashMap<AnnotationProcessorFactory, Set<AnnotationTypeDeclaration>>();
        if (!providers.hasNext() && productiveFactories.size() == 0) {
            if (unmatchedAnnotations.size() > 0)
                bark.aptWarning("NoAnnotationProcessors");
            if (spectypedecls.size() == 0)
                throw new UsageMessageNeededException();
            // no processors; nothing else to do
            return;
        } else {
            if (unmatchedAnnotations.size() == 0)
                unmatchedAnnotations.add("");
            Set<String> emptyStringSet = new HashSet<String>();
            emptyStringSet.add("");
            emptyStringSet = Collections.unmodifiableSet(emptyStringSet);
            while (providers.hasNext()) {
                Object provider = providers.next();
                try {
                    Set<String> matchedStrings = new HashSet<String>();
                    AnnotationProcessorFactory apf = (AnnotationProcessorFactory) provider;
                    Collection<String> supportedTypes = apf.supportedAnnotationTypes();
                    Collection<Pattern> supportedTypePatterns = new LinkedList<Pattern>();
                    for (String s : supportedTypes) supportedTypePatterns.add(importStringToPattern(s));
                    for (String s : unmatchedAnnotations) {
                        for (Pattern p : supportedTypePatterns) {
                            if (p.matcher(s).matches()) {
                                matchedStrings.add(s);
                                break;
                            }
                        }
                    }
                    unmatchedAnnotations.removeAll(matchedStrings);
                    if (options.get("-XPrintFactoryInfo") != null) {
                        out.println("Factory " + apf.getClass().getName() + " matches " + ((matchedStrings.size() == 0) ? "nothing." : matchedStrings));
                    }
                    if (matchedStrings.size() > 0) {
                        // convert annotation names to annotation
                        // type decls
                        Set<AnnotationTypeDeclaration> atds = new HashSet<AnnotationTypeDeclaration>();
                        // annotation type declarations.
                        if (!matchedStrings.equals(emptyStringSet)) {
                            for (String s : matchedStrings) {
                                TypeDeclaration decl = aptenv.declMaker.getTypeDeclaration(s);
                                AnnotationTypeDeclaration annotdecl;
                                if (decl == null) {
                                    bark.aptError("DeclarationCreation", s);
                                } else {
                                    try {
                                        annotdecl = (AnnotationTypeDeclaration) decl;
                                        atds.add(annotdecl);
                                    } catch (ClassCastException cce) {
                                        bark.aptError("BadDeclaration", s);
                                    }
                                }
                            }
                        }
                        currentRoundFactories.add(apf.getClass());
                        productiveFactories.add(apf.getClass());
                        factoryToAnnotation.put(apf, atds);
                    } else if (productiveFactories.contains(apf.getClass())) {
                        // If a factory provided a processor in a
                        // previous round but doesn't match any
                        // annotations this round, call it with an
                        // empty set of declarations.
                        currentRoundFactories.add(apf.getClass());
                        factoryToAnnotation.put(apf, emptyATDS);
                    }
                    if (unmatchedAnnotations.size() == 0)
                        break;
                } catch (ClassCastException cce) {
                    bark.aptWarning("BadFactory", cce);
                }
            }
            unmatchedAnnotations.remove("");
        }
        // If the set difference of productiveFactories and
        // currentRoundFactories is non-empty, call the remaining
        // productive factories with an empty set of declarations.
        {
            java.util.Set<Class<? extends AnnotationProcessorFactory>> neglectedFactories = new LinkedHashSet<Class<? extends AnnotationProcessorFactory>>(productiveFactories);
            neglectedFactories.removeAll(currentRoundFactories);
            for (Class<? extends AnnotationProcessorFactory> working : neglectedFactories) {
                try {
                    AnnotationProcessorFactory factory = working.newInstance();
                    factoryToAnnotation.put(factory, emptyATDS);
                } catch (Exception e) {
                    bark.aptWarning("FactoryCantInstantiate", working.getName());
                } catch (Throwable t) {
                    throw new AnnotationProcessingError(t);
                }
            }
        }
        if (unmatchedAnnotations.size() > 0)
            bark.aptWarning("AnnotationsWithoutProcessors", unmatchedAnnotations);
        Set<AnnotationProcessor> processors = new LinkedHashSet<AnnotationProcessor>();
        // make sure the usage message is printed
        if (spectypedecls.size() == 0 && factoryToAnnotation.keySet().size() == 0)
            throw new UsageMessageNeededException();
        try {
            for (Map.Entry<AnnotationProcessorFactory, Set<AnnotationTypeDeclaration>> entry : factoryToAnnotation.entrySet()) {
                AnnotationProcessorFactory apFactory = entry.getKey();
                AnnotationProcessor processor = apFactory.getProcessorFor(entry.getValue(), trivAPE);
                if (processor != null)
                    processors.add(processor);
                else
                    bark.aptWarning("NullProcessor", apFactory.getClass().getName());
            }
        } catch (Throwable t) {
            throw new AnnotationProcessingError(t);
        }
        LinkedList<AnnotationProcessor> temp = new LinkedList<AnnotationProcessor>();
        temp.addAll(processors);
        AnnotationProcessor proc = AnnotationProcessors.getCompositeAnnotationProcessor(temp);
        try {
            proc.process();
        } catch (Throwable t) {
            throw new AnnotationProcessingError(t);
        }
        // Invoke listener callback mechanism
        trivAPE.roundComplete();
        FilerImpl filerimpl = (FilerImpl) trivAPE.getFiler();
        genSourceFileNames = filerimpl.getSourceFileNames();
        genClassFileNames = filerimpl.getClassFileNames();
        // Make sure new files are written out
        filerimpl.flush();
    }
}
Also used : AnnotationProcessorEnvironmentImpl(com.sun.tools.apt.mirror.apt.AnnotationProcessorEnvironmentImpl) java.util(java.util) com.sun.tools.javac.util(com.sun.tools.javac.util) Bark(com.sun.tools.apt.util.Bark) FilerImpl(com.sun.tools.apt.mirror.apt.FilerImpl) AnnotationTypeDeclaration(com.sun.mirror.declaration.AnnotationTypeDeclaration) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TypeDeclaration(com.sun.mirror.declaration.TypeDeclaration) AnnotationTypeDeclaration(com.sun.mirror.declaration.AnnotationTypeDeclaration)

Aggregations

AnnotationTypeDeclaration (com.sun.mirror.declaration.AnnotationTypeDeclaration)1 TypeDeclaration (com.sun.mirror.declaration.TypeDeclaration)1 AnnotationProcessorEnvironmentImpl (com.sun.tools.apt.mirror.apt.AnnotationProcessorEnvironmentImpl)1 FilerImpl (com.sun.tools.apt.mirror.apt.FilerImpl)1 Bark (com.sun.tools.apt.util.Bark)1 com.sun.tools.javac.util (com.sun.tools.javac.util)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 java.util (java.util)1