1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/source/util/AbstractTypeProcessor.java Fri Jun 26 19:12:41 2009 -0700 1.3 @@ -0,0 +1,245 @@ 1.4 +/* 1.5 + * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.source.util; 1.30 + 1.31 +import java.util.ArrayList; 1.32 +import java.util.HashSet; 1.33 +import java.util.List; 1.34 +import java.util.Set; 1.35 + 1.36 +import javax.annotation.processing.*; 1.37 +import javax.lang.model.element.Name; 1.38 +import javax.lang.model.element.TypeElement; 1.39 +import javax.lang.model.util.ElementFilter; 1.40 + 1.41 +import com.sun.tools.javac.processing.JavacProcessingEnvironment; 1.42 +import com.sun.tools.javac.util.Context; 1.43 +import com.sun.tools.javac.util.Log; 1.44 + 1.45 +import com.sun.source.tree.ClassTree; 1.46 + 1.47 +/** 1.48 + * This class is an abstract annotation processor designed to be a 1.49 + * convenient superclass for concrete "type processors", processors that 1.50 + * require the type information in the processed source. 1.51 + * 1.52 + * <p>Type processing occurs in one round after the tool (e.g. java compiler) 1.53 + * analyzes the source (all sources taken as input to the tool and sources 1.54 + * generated by other annotation processors). 1.55 + * 1.56 + * <p>The tool infrastructure will interact with classes extending this abstract 1.57 + * class as follows: 1.58 + * 1.59 + * <ol> 1.60 + * [1-3: Identical to {@link Processor} life cycle] 1.61 + * 1.62 + * <li>If an existing {@code Processor} object is not being used, to 1.63 + * create an instance of a processor the tool calls the no-arg 1.64 + * constructor of the processor class. 1.65 + * 1.66 + * <li>Next, the tool calls the {@link #init init} method with 1.67 + * an appropriate {@code ProcessingEnvironment}. 1.68 + * 1.69 + * <li>Afterwards, the tool calls {@link #getSupportedAnnotationTypes 1.70 + * getSupportedAnnotationTypes}, {@link #getSupportedOptions 1.71 + * getSupportedOptions}, and {@link #getSupportedSourceVersion 1.72 + * getSupportedSourceVersion}. These methods are only called once per 1.73 + * run, not on each round. 1.74 + * 1.75 + * [4-5Unique to {@code AbstractTypeProcessor} subclasses] 1.76 + * 1.77 + * <li>For each class containing a supported annotation, the tool calls 1.78 + * {@link #typeProcess(TypeElement, TreePath) typeProcess} method on the 1.79 + * {@code Processor}. The class is guaranteed to be type-checked Java code 1.80 + * and all the tree type and symbol information is resolved. 1.81 + * 1.82 + * <li>Finally, the tools calls the 1.83 + * {@link #typeProcessingOver() typeProcessingOver} method 1.84 + * on the {@code Processor}. 1.85 + * 1.86 + * </ol> 1.87 + * 1.88 + * <p>The tool is permitted to ask type processors to process a class once 1.89 + * it is analyzed before the rest of classes are analyzed. The tool is also 1.90 + * permitted to stop type processing immediately if any errors are raised, 1.91 + * without invoking {@code typeProcessingOver} 1.92 + * 1.93 + * <p>A subclass may override any of the methods in this class, as long as the 1.94 + * general {@link javax.annotation.processing.Processor Processor} 1.95 + * contract is obeyed, with one notable exception. 1.96 + * {@link #process(Set, RoundEnvironment)} may not be overridden, as it 1.97 + * is called during the regular annotation phase before classes are analyzed. 1.98 + * 1.99 + * @author Mahmood Ali 1.100 + * @since 1.7 1.101 + */ 1.102 +public abstract class AbstractTypeProcessor extends AbstractProcessor { 1.103 + private final Set<Name> elements = new HashSet<Name>(); 1.104 + private boolean hasInvokedTypeProcessingOver = false; 1.105 + private JavacProcessingEnvironment env; 1.106 + private final AttributionTaskListener listener = new AttributionTaskListener(); 1.107 + 1.108 + /** 1.109 + * Constructor for subclasses to call. 1.110 + */ 1.111 + protected AbstractTypeProcessor() { } 1.112 + 1.113 + /** 1.114 + * {@inheritDoc} 1.115 + */ 1.116 + @Override 1.117 + public void init(ProcessingEnvironment env) { 1.118 + super.init(env); 1.119 + this.env = (JavacProcessingEnvironment)env; 1.120 + prepareContext(this.env.getContext()); 1.121 + } 1.122 + 1.123 + /** 1.124 + * The use of this method is obsolete in type processors. The method is 1.125 + * called during regular annotation processing phase only. 1.126 + */ 1.127 + @Override 1.128 + public final boolean process(Set<? extends TypeElement> annotations, 1.129 + RoundEnvironment roundEnv) { 1.130 + for (TypeElement elem : ElementFilter.typesIn(roundEnv.getRootElements())) { 1.131 + elements.add(elem.getQualifiedName()); 1.132 + } 1.133 + return false; 1.134 + } 1.135 + 1.136 + /** 1.137 + * Processes a fully analyzed class that contains a supported annotation 1.138 + * (look {@link #getSupportedAnnotationTypes()}). 1.139 + * 1.140 + * <p>The passed class is always a valid type-checked Java code. 1.141 + * 1.142 + * @param element element of the analyzed class 1.143 + * @param tree the tree path to the element, with the leaf being a 1.144 + * {@link ClassTree} 1.145 + */ 1.146 + public abstract void typeProcess(TypeElement element, TreePath tree); 1.147 + 1.148 + /** 1.149 + * A method to be called once all the classes are processed and no error 1.150 + * is reported. 1.151 + * 1.152 + * <p>Subclasses may override this method to do any aggregate analysis 1.153 + * (e.g. generate report, persistence) or resource deallocation. 1.154 + * 1.155 + * <p>If an error (a Java error or a processor error) is reported, this 1.156 + * method is not guaranteed to be invoked. 1.157 + */ 1.158 + public void typeProcessingOver() { } 1.159 + 1.160 + /** 1.161 + * adds a listener for attribution. 1.162 + */ 1.163 + private void prepareContext(Context context) { 1.164 + TaskListener otherListener = context.get(TaskListener.class); 1.165 + if (otherListener == null) { 1.166 + context.put(TaskListener.class, listener); 1.167 + } else { 1.168 + // handle cases of multiple listeners 1.169 + context.put(TaskListener.class, (TaskListener)null); 1.170 + TaskListeners listeners = new TaskListeners(); 1.171 + listeners.add(otherListener); 1.172 + listeners.add(listener); 1.173 + context.put(TaskListener.class, listeners); 1.174 + } 1.175 + } 1.176 + 1.177 + /** 1.178 + * A task listener that invokes the processor whenever a class is fully 1.179 + * analyzed. 1.180 + */ 1.181 + private final class AttributionTaskListener implements TaskListener { 1.182 + 1.183 + @Override 1.184 + public void finished(TaskEvent e) { 1.185 + Log log = Log.instance(env.getContext()); 1.186 + 1.187 + if (!hasInvokedTypeProcessingOver && elements.isEmpty() && log.nerrors == 0) { 1.188 + typeProcessingOver(); 1.189 + hasInvokedTypeProcessingOver = true; 1.190 + } 1.191 + 1.192 + if (e.getKind() != TaskEvent.Kind.ANALYZE) 1.193 + return; 1.194 + 1.195 + if (e.getTypeElement() == null) 1.196 + throw new AssertionError("event task without a type element"); 1.197 + if (e.getCompilationUnit() == null) 1.198 + throw new AssertionError("even task without compilation unit"); 1.199 + 1.200 + if (!elements.remove(e.getTypeElement().getQualifiedName())) 1.201 + return; 1.202 + 1.203 + if (log.nerrors != 0) 1.204 + return; 1.205 + 1.206 + TypeElement elem = e.getTypeElement(); 1.207 + TreePath p = Trees.instance(env).getPath(elem); 1.208 + 1.209 + typeProcess(elem, p); 1.210 + 1.211 + if (!hasInvokedTypeProcessingOver && elements.isEmpty() && log.nerrors == 0) { 1.212 + typeProcessingOver(); 1.213 + hasInvokedTypeProcessingOver = true; 1.214 + } 1.215 + } 1.216 + 1.217 + @Override 1.218 + public void started(TaskEvent e) { } 1.219 + 1.220 + } 1.221 + 1.222 + /** 1.223 + * A task listener multiplexer. 1.224 + */ 1.225 + private static class TaskListeners implements TaskListener { 1.226 + private final List<TaskListener> listeners = new ArrayList<TaskListener>(); 1.227 + 1.228 + public void add(TaskListener listener) { 1.229 + listeners.add(listener); 1.230 + } 1.231 + 1.232 + public void remove(TaskListener listener) { 1.233 + listeners.remove(listener); 1.234 + } 1.235 + 1.236 + @Override 1.237 + public void finished(TaskEvent e) { 1.238 + for (TaskListener listener : listeners) 1.239 + listener.finished(e); 1.240 + } 1.241 + 1.242 + @Override 1.243 + public void started(TaskEvent e) { 1.244 + for (TaskListener listener : listeners) 1.245 + listener.started(e); 1.246 + } 1.247 + } 1.248 +}