langtools/src/share/classes/com/sun/tools/apt/comp/Apt.java
author mcimadamore
Tue, 13 Jan 2009 13:27:14 +0000
changeset 1789 7ac8c0815000
parent 10 06bc494ca11e
child 1870 57a1138dffc8
permissions -rw-r--r--
6765045: Remove rawtypes warnings from langtools Summary: Removed all occurrences of rawtypes warnings from langtools Reviewed-by: jjg, bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004-2005 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.apt.comp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.javac.comp.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.tree.TreeScanner;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.apt.util.Bark;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.util.Position;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.util.regex.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import java.lang.reflect.InvocationTargetException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.apt.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.tools.apt.comp.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.mirror.declaration.TypeDeclaration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.mirror.apt.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
// import com.sun.mirror.apt.AnnotationProcessorFactory;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.mirror.apt.AnnotationProcessors;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
import com.sun.tools.apt.mirror.AptEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import com.sun.tools.apt.mirror.apt.FilerImpl;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
import com.sun.tools.apt.mirror.apt.AnnotationProcessorEnvironmentImpl;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
import static com.sun.tools.apt.mirror.declaration.DeclarationMaker.isJavaIdentifier;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 * Apt compiler phase.
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 *  If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 *  risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 *  or deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
public class Apt extends ListBuffer<Env<AttrContext>> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    java.util.Set<String> genSourceFileNames = new java.util.LinkedHashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    public java.util.Set<String> getSourceFileNames() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        return genSourceFileNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    /** List of names of generated class files.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    java.util.Set<String> genClassFileNames  = new java.util.LinkedHashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    public java.util.Set<String> getClassFileNames() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        return genClassFileNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /* AptEnvironment */
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    AptEnv aptenv;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    private Context context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    /** The context key for the todo list. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    protected static final Context.Key<Apt> aptKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        new Context.Key<Apt>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /** Get the Apt instance for this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    public static Apt instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        Apt instance = context.get(aptKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            instance = new Apt(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    /** Create a new apt list. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    protected Apt(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        this.context = context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        context.put(aptKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        aptenv = AptEnv.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * Used to scan javac trees to build data structures needed for
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * bootstrapping the apt environment.  In particular:
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * <ul>
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     * <li> Generate list of canonical names of annotation types that
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     * appear in source files given on the command line
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     * <li> Collect list of javac symbols representing source files
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     * given on the command line
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     * </ul>
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    static class AptTreeScanner extends TreeScanner {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        // Set of fully qualified names of annotation types present in
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        // examined source
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        private Set<String> annotationSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        // Symbols to build bootstrapping declaration list
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        private Collection<ClassSymbol> specifiedDeclCollection;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        private Collection<ClassSymbol> declCollection;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        public Set<String> getAnnotationSet() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            return annotationSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        public AptTreeScanner() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            annotationSet = new  LinkedHashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            specifiedDeclCollection = new LinkedHashSet<ClassSymbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            declCollection = new LinkedHashSet<ClassSymbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        public void visitTopLevel(JCTree.JCCompilationUnit tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            super.visitTopLevel(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            // Print out contents -- what are we dealing with?
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            for(JCTree d: tree.defs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                if (d instanceof JCTree.JCClassDecl)
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                    specifiedDeclCollection.add(((JCTree.JCClassDecl) d).sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        public void visitBlock(JCTree.JCBlock tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            ; // Do nothing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        // should add nested classes to packages, etc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        public void visitClassDef(JCTree.JCClassDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            if (tree.sym == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                // could be an anon class w/in an initializer
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            super.visitClassDef(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            declCollection.add(tree.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        public void visitMethodDef(JCTree.JCMethodDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            super.visitMethodDef(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        public void visitVarDef(JCTree.JCVariableDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            super.visitVarDef(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        public void visitAnnotation(JCTree.JCAnnotation tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            super.visitAnnotation(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
            annotationSet.add(tree.type.tsym.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    Set<String> computeAnnotationSet(Collection<ClassSymbol> classSymbols) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        Set<String> annotationSet = new HashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        for(ClassSymbol classSymbol: classSymbols) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
            computeAnnotationSet(classSymbol, annotationSet);
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        return annotationSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    void computeAnnotationSet(Symbol symbol, Set<String> annotationSet) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        if (symbol != null ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            if (symbol.getAnnotationMirrors() != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                for(Attribute.Compound compound: symbol.getAnnotationMirrors())
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                    annotationSet.add(compound.type.tsym.toString()); // should fullName be used instead of toString?
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            if (symbol instanceof Symbol.MethodSymbol) // add parameter annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                for(Symbol param: ((MethodSymbol) symbol).params())
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                    computeAnnotationSet(param, annotationSet);
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
            if (symbol.members() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                for(Scope.Entry e: symbol.members().table)
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                    computeAnnotationSet(e.sym, annotationSet);
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    public void main(com.sun.tools.javac.util.List<JCTree.JCCompilationUnit> treeList,
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                     ListBuffer<ClassSymbol> classes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                     Map<String, String> origOptions,
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                     ClassLoader aptCL,
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                     AnnotationProcessorFactory providedFactory,
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                     java.util.Set<Class<? extends AnnotationProcessorFactory> > productiveFactories) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        Bark bark = Bark.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        java.io.PrintWriter out = bark.warnWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        Collection<TypeDeclaration> spectypedecls =     new LinkedHashSet<TypeDeclaration>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        Collection<TypeDeclaration> typedecls =         new LinkedHashSet<TypeDeclaration>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        Set<String> unmatchedAnnotations =              new LinkedHashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        Set<AnnotationTypeDeclaration> emptyATDS =      Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        Set<Class<? extends AnnotationProcessorFactory> > currentRoundFactories =
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            new LinkedHashSet<Class<? extends AnnotationProcessorFactory> >();
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        // Determine what annotations are present on the input source
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        // files, create collections of specified type declarations,
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        // and type declarations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        AptTreeScanner ats = new AptTreeScanner();
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        for(JCTree t: treeList) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            t.accept(ats);
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        // Turn collection of ClassSymbols into Collection of apt decls
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        for (ClassSymbol cs : ats.specifiedDeclCollection) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            TypeDeclaration decl = aptenv.declMaker.getTypeDeclaration(cs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            spectypedecls.add(decl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        for (ClassSymbol cs : ats.declCollection) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            TypeDeclaration decl = aptenv.declMaker.getTypeDeclaration(cs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            typedecls.add(decl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        unmatchedAnnotations.addAll(ats.getAnnotationSet());
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        // Process input class files
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        for(ClassSymbol cs : classes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
            TypeDeclaration decl = aptenv.declMaker.getTypeDeclaration(cs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            // System.out.println("Adding a class to spectypedecls");
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            spectypedecls.add(decl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            typedecls.add(decl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            computeAnnotationSet(cs, unmatchedAnnotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        if (options.get("-XListAnnotationTypes") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            out.println("Set of annotations found:" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                        (new TreeSet<String>(unmatchedAnnotations)).toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        AnnotationProcessorEnvironmentImpl trivAPE =
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            new AnnotationProcessorEnvironmentImpl(spectypedecls, typedecls, origOptions, context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        if (options.get("-XListDeclarations") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            out.println("Set of Specified Declarations:" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                        spectypedecls);
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
            out.println("Set of Included Declarations: " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                           typedecls);
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        if (options.get("-print") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            if (spectypedecls.size() == 0 )
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                throw new UsageMessageNeededException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            // Run the printing processor
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            AnnotationProcessor proc = (new BootstrapAPF()).getProcessorFor(new HashSet<AnnotationTypeDeclaration>(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                                                                            trivAPE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            proc.process();
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            // Discovery process
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            // List of annotation processory factory instances
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
   284
            java.util.Iterator<AnnotationProcessorFactory> providers = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            {
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
                /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                 * If a factory is provided by the user, the
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                 * "-factory" and "-factorypath" options are not used.
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                 *
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                 * Otherwise, if the "-factory" option is used, search
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                 * the appropriate path for the named class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                 * Otherwise, use sun.misc.Service to implement the
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                 * default discovery policy.
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                java.util.List<AnnotationProcessorFactory> list =
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                    new LinkedList<AnnotationProcessorFactory>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                String factoryName = options.get("-factory");
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                if (providedFactory != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                    list.add(providedFactory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                    providers = list.iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                } else if (factoryName != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                        AnnotationProcessorFactory factory =
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                            (AnnotationProcessorFactory) (aptCL.loadClass(factoryName).newInstance());
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                        list.add(factory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                    } catch (ClassNotFoundException cnfe) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                        bark.aptWarning("FactoryNotFound", factoryName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                    } catch (ClassCastException cce) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                        bark.aptWarning("FactoryWrongType", factoryName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                    } catch (Exception e ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                        bark.aptWarning("FactoryCantInstantiate", factoryName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                    } catch(Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                        throw new AnnotationProcessingError(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                    providers = list.iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                    providers = sun.misc.Service.providers(AnnotationProcessorFactory.class, aptCL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            java.util.Map<AnnotationProcessorFactory, Set<AnnotationTypeDeclaration>> factoryToAnnotation =
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                new LinkedHashMap<AnnotationProcessorFactory, Set<AnnotationTypeDeclaration>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            if (!providers.hasNext() && productiveFactories.size() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                if (unmatchedAnnotations.size() > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                    bark.aptWarning("NoAnnotationProcessors");
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                if (spectypedecls.size() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
                    throw new UsageMessageNeededException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
                return; // no processors; nothing else to do
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                // If there are no annotations, still give
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                // processors that match everything a chance to
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                // run.
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
                if(unmatchedAnnotations.size() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
                    unmatchedAnnotations.add("");
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                Set<String> emptyStringSet = new HashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                emptyStringSet.add("");
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                emptyStringSet = Collections.unmodifiableSet(emptyStringSet);
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                while (providers.hasNext() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
                    Object provider = providers.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                        Set<String> matchedStrings = new HashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
                        AnnotationProcessorFactory apf = (AnnotationProcessorFactory) provider;
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                        Collection<String> supportedTypes = apf.supportedAnnotationTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                        Collection<Pattern> supportedTypePatterns = new LinkedList<Pattern>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                        for(String s: supportedTypes)
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                            supportedTypePatterns.add(importStringToPattern(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                        for(String s: unmatchedAnnotations) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                            for(Pattern p: supportedTypePatterns) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                                if (p.matcher(s).matches()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                                    matchedStrings.add(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                        unmatchedAnnotations.removeAll(matchedStrings);
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
                        if (options.get("-XPrintFactoryInfo") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
                            out.println("Factory " + apf.getClass().getName() +
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                                        " matches " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
                                        ((matchedStrings.size() == 0)?
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
                                         "nothing.": matchedStrings));
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                        if (matchedStrings.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                            // convert annotation names to annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                            // type decls
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                            Set<AnnotationTypeDeclaration> atds = new HashSet<AnnotationTypeDeclaration>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                            // If a "*" processor is called on the
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                            // empty string, pass in an empty set of
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                            // annotation type declarations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                            if (!matchedStrings.equals(emptyStringSet)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                                for(String s: matchedStrings) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
                                    TypeDeclaration decl = aptenv.declMaker.getTypeDeclaration(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
                                    AnnotationTypeDeclaration annotdecl;
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
                                    if (decl == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
                                        bark.aptError("DeclarationCreation", s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
                                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                                        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
                                            annotdecl = (AnnotationTypeDeclaration)decl;
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                                            atds.add(annotdecl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                                        } catch (ClassCastException cce) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                                            bark.aptError("BadDeclaration", s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                            currentRoundFactories.add(apf.getClass());
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                            productiveFactories.add(apf.getClass());
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
                            factoryToAnnotation.put(apf, atds);
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                        } else if (productiveFactories.contains(apf.getClass())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
                            // If a factory provided a processor in a
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
                            // previous round but doesn't match any
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
                            // annotations this round, call it with an
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                            // empty set of declarations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                            currentRoundFactories.add(apf.getClass());
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                            factoryToAnnotation.put(apf, emptyATDS );
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
                        if (unmatchedAnnotations.size() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
                            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                    } catch (ClassCastException cce) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                        bark.aptWarning("BadFactory", cce);
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                unmatchedAnnotations.remove("");
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
            // If the set difference of productiveFactories and
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
            // currentRoundFactories is non-empty, call the remaining
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
            // productive factories with an empty set of declarations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
            {
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
                java.util.Set<Class<? extends AnnotationProcessorFactory> > neglectedFactories =
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
                    new LinkedHashSet<Class<? extends AnnotationProcessorFactory>>(productiveFactories);
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                neglectedFactories.removeAll(currentRoundFactories);
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
                for(Class<? extends AnnotationProcessorFactory> working : neglectedFactories) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                        AnnotationProcessorFactory factory = working.newInstance();
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
                        factoryToAnnotation.put(factory, emptyATDS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
                    } catch (Exception e ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
                        bark.aptWarning("FactoryCantInstantiate", working.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
                    } catch(Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                        throw new AnnotationProcessingError(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
            if (unmatchedAnnotations.size() > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                bark.aptWarning("AnnotationsWithoutProcessors", unmatchedAnnotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            Set<AnnotationProcessor> processors = new LinkedHashSet<AnnotationProcessor>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
            // If there were no source files AND no factory matching "*",
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            // make sure the usage message is printed
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
            if (spectypedecls.size() == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                factoryToAnnotation.keySet().size() == 0 )
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
                throw new UsageMessageNeededException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
                for(AnnotationProcessorFactory apFactory: factoryToAnnotation.keySet()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                    AnnotationProcessor processor = apFactory.getProcessorFor(factoryToAnnotation.get(apFactory),
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
                                                                              trivAPE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
                    if (processor != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
                        processors.add(processor);
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
                        bark.aptWarning("NullProcessor", apFactory.getClass().getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
            } catch(Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
                throw new AnnotationProcessingError(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
            LinkedList<AnnotationProcessor> temp = new LinkedList<AnnotationProcessor>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
            temp.addAll(processors);
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
            AnnotationProcessor proc = AnnotationProcessors.getCompositeAnnotationProcessor(temp);
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
                proc.process();
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
            } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
                throw new AnnotationProcessingError(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
            // Invoke listener callback mechanism
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
            trivAPE.roundComplete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
            FilerImpl filerimpl = (FilerImpl)trivAPE.getFiler();
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
            genSourceFileNames = filerimpl.getSourceFileNames();
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
            genClassFileNames = filerimpl.getClassFileNames();
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
            filerimpl.flush(); // Make sure new files are written out
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
     * Convert import-style string to regex matching that string.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
     * the string is a valid import-style string, return a regex that
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
     * won't match anything.
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
    Pattern importStringToPattern(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
        if (s.equals("*")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
            return allMatches;
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
            String t = s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
            boolean star = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
            /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
             * Validate string from factory is legal.  If the string
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
             * has more than one asterisks or the asterisks does not
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
             * appear as the last character (preceded by a period),
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
             * the string is not legal.
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
             */
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
            boolean valid = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
            int index = t.indexOf('*');
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
            if (index != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
                // '*' must be last character...
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
                if (index == t.length() -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
                     // ... and preceeding character must be '.'
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
                    if ( index-1 >= 0 ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
                        valid = t.charAt(index-1) == '.';
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
                        // Strip off ".*$" for identifier checks
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
                        t = t.substring(0, t.length()-2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
                } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                    valid = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
            // Verify string is off the form (javaId \.)+ or javaId
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
            if (valid) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                String[] javaIds = t.split("\\.", t.length()+2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
                for(String javaId: javaIds)
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
                    valid &= isJavaIdentifier(javaId);
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            if (!valid) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
                Bark bark = Bark.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
                bark.aptWarning("MalformedSupportedString", s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
                return noMatches; // won't match any valid identifier
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
            String s_prime = s.replaceAll("\\.", "\\\\.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            if (s_prime.endsWith("*")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
                s_prime =  s_prime.substring(0, s_prime.length() - 1) + ".+";
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
            return Pattern.compile(s_prime);
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
    private static final Pattern allMatches = Pattern.compile(".*");
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
    private static final Pattern noMatches  = Pattern.compile("(\\P{all})+");
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
}