1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lib/JavacTestingAbstractProcessor.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,259 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, 2011, Oracle and/or its affiliates. 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. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import java.util.*; 1.28 +import javax.annotation.processing.*; 1.29 +import javax.lang.model.SourceVersion; 1.30 +import javax.lang.model.util.*; 1.31 +import static javax.lang.model.SourceVersion.*; 1.32 + 1.33 +/** 1.34 + * An abstract annotation processor tailored to javac regression testing. 1.35 + */ 1.36 +public abstract class JavacTestingAbstractProcessor extends AbstractProcessor { 1.37 + private static final Set<String> allAnnotations; 1.38 + 1.39 + static { 1.40 + Set<String> tmp = new HashSet<>(); 1.41 + tmp.add("*"); 1.42 + allAnnotations = Collections.unmodifiableSet(tmp); 1.43 + } 1.44 + 1.45 + protected Elements eltUtils; 1.46 + protected Elements elements; 1.47 + protected Types typeUtils; 1.48 + protected Types types; 1.49 + protected Filer filer; 1.50 + protected Messager messager; 1.51 + protected Map<String, String> options; 1.52 + 1.53 + /** 1.54 + * Constructor for subclasses to call. 1.55 + */ 1.56 + protected JavacTestingAbstractProcessor() { 1.57 + super(); 1.58 + } 1.59 + 1.60 + /** 1.61 + * Return the latest source version. Unless this method is 1.62 + * overridden, an {@code IllegalStateException} will be thrown if a 1.63 + * subclass has a {@code SupportedSourceVersion} annotation. 1.64 + */ 1.65 + @Override 1.66 + public SourceVersion getSupportedSourceVersion() { 1.67 + SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class); 1.68 + if (ssv != null) 1.69 + throw new IllegalStateException("SupportedSourceVersion annotation not supported here."); 1.70 + 1.71 + return SourceVersion.latest(); 1.72 + } 1.73 + 1.74 + /** 1.75 + * If the processor class is annotated with {@link 1.76 + * SupportedAnnotationTypes}, return an unmodifiable set with the 1.77 + * same set of strings as the annotation. If the class is not so 1.78 + * annotated, a one-element set containing {@code "*"} is returned 1.79 + * to indicate all annotations are processed. 1.80 + * 1.81 + * @return the names of the annotation types supported by this 1.82 + * processor, or an empty set if none 1.83 + */ 1.84 + @Override 1.85 + public Set<String> getSupportedAnnotationTypes() { 1.86 + SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class); 1.87 + if (sat != null) 1.88 + return super.getSupportedAnnotationTypes(); 1.89 + else 1.90 + return allAnnotations; 1.91 + } 1.92 + 1.93 + @Override 1.94 + public void init(ProcessingEnvironment processingEnv) { 1.95 + super.init(processingEnv); 1.96 + elements = eltUtils = processingEnv.getElementUtils(); 1.97 + types = typeUtils = processingEnv.getTypeUtils(); 1.98 + filer = processingEnv.getFiler(); 1.99 + messager = processingEnv.getMessager(); 1.100 + options = processingEnv.getOptions(); 1.101 + } 1.102 + 1.103 + /* 1.104 + * The set of visitors below will directly extend the most recent 1.105 + * corresponding platform visitor type. 1.106 + */ 1.107 + 1.108 + @SupportedSourceVersion(RELEASE_8) 1.109 + public static abstract class AbstractAnnotationValueVisitor<R, P> extends AbstractAnnotationValueVisitor8<R, P> { 1.110 + 1.111 + /** 1.112 + * Constructor for concrete subclasses to call. 1.113 + */ 1.114 + protected AbstractAnnotationValueVisitor() { 1.115 + super(); 1.116 + } 1.117 + } 1.118 + 1.119 + @SupportedSourceVersion(RELEASE_8) 1.120 + public static abstract class AbstractElementVisitor<R, P> extends AbstractElementVisitor8<R, P> { 1.121 + /** 1.122 + * Constructor for concrete subclasses to call. 1.123 + */ 1.124 + protected AbstractElementVisitor(){ 1.125 + super(); 1.126 + } 1.127 + } 1.128 + 1.129 + @SupportedSourceVersion(RELEASE_8) 1.130 + public static abstract class AbstractTypeVisitor<R, P> extends AbstractTypeVisitor8<R, P> { 1.131 + /** 1.132 + * Constructor for concrete subclasses to call. 1.133 + */ 1.134 + protected AbstractTypeVisitor() { 1.135 + super(); 1.136 + } 1.137 + } 1.138 + 1.139 + @SupportedSourceVersion(RELEASE_8) 1.140 + public static class ElementKindVisitor<R, P> extends ElementKindVisitor8<R, P> { 1.141 + /** 1.142 + * Constructor for concrete subclasses; uses {@code null} for the 1.143 + * default value. 1.144 + */ 1.145 + protected ElementKindVisitor() { 1.146 + super(null); 1.147 + } 1.148 + 1.149 + /** 1.150 + * Constructor for concrete subclasses; uses the argument for the 1.151 + * default value. 1.152 + * 1.153 + * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 1.154 + */ 1.155 + protected ElementKindVisitor(R defaultValue) { 1.156 + super(defaultValue); 1.157 + } 1.158 + } 1.159 + 1.160 + @SupportedSourceVersion(RELEASE_8) 1.161 + public static class ElementScanner<R, P> extends ElementScanner8<R, P> { 1.162 + /** 1.163 + * Constructor for concrete subclasses; uses {@code null} for the 1.164 + * default value. 1.165 + */ 1.166 + protected ElementScanner(){ 1.167 + super(null); 1.168 + } 1.169 + 1.170 + /** 1.171 + * Constructor for concrete subclasses; uses the argument for the 1.172 + * default value. 1.173 + */ 1.174 + protected ElementScanner(R defaultValue){ 1.175 + super(defaultValue); 1.176 + } 1.177 + } 1.178 + 1.179 + @SupportedSourceVersion(RELEASE_8) 1.180 + public static class SimpleAnnotationValueVisitor<R, P> extends SimpleAnnotationValueVisitor8<R, P> { 1.181 + /** 1.182 + * Constructor for concrete subclasses; uses {@code null} for the 1.183 + * default value. 1.184 + */ 1.185 + protected SimpleAnnotationValueVisitor() { 1.186 + super(null); 1.187 + } 1.188 + 1.189 + /** 1.190 + * Constructor for concrete subclasses; uses the argument for the 1.191 + * default value. 1.192 + * 1.193 + * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 1.194 + */ 1.195 + protected SimpleAnnotationValueVisitor(R defaultValue) { 1.196 + super(defaultValue); 1.197 + } 1.198 + } 1.199 + 1.200 + @SupportedSourceVersion(RELEASE_8) 1.201 + public static class SimpleElementVisitor<R, P> extends SimpleElementVisitor8<R, P> { 1.202 + /** 1.203 + * Constructor for concrete subclasses; uses {@code null} for the 1.204 + * default value. 1.205 + */ 1.206 + protected SimpleElementVisitor(){ 1.207 + super(null); 1.208 + } 1.209 + 1.210 + /** 1.211 + * Constructor for concrete subclasses; uses the argument for the 1.212 + * default value. 1.213 + * 1.214 + * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 1.215 + */ 1.216 + protected SimpleElementVisitor(R defaultValue){ 1.217 + super(defaultValue); 1.218 + } 1.219 + } 1.220 + 1.221 + @SupportedSourceVersion(RELEASE_8) 1.222 + public static class SimpleTypeVisitor<R, P> extends SimpleTypeVisitor8<R, P> { 1.223 + /** 1.224 + * Constructor for concrete subclasses; uses {@code null} for the 1.225 + * default value. 1.226 + */ 1.227 + protected SimpleTypeVisitor(){ 1.228 + super(null); 1.229 + } 1.230 + 1.231 + /** 1.232 + * Constructor for concrete subclasses; uses the argument for the 1.233 + * default value. 1.234 + * 1.235 + * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 1.236 + */ 1.237 + protected SimpleTypeVisitor(R defaultValue){ 1.238 + super(defaultValue); 1.239 + } 1.240 + } 1.241 + 1.242 + @SupportedSourceVersion(RELEASE_8) 1.243 + public static class TypeKindVisitor<R, P> extends TypeKindVisitor8<R, P> { 1.244 + /** 1.245 + * Constructor for concrete subclasses to call; uses {@code null} 1.246 + * for the default value. 1.247 + */ 1.248 + protected TypeKindVisitor() { 1.249 + super(null); 1.250 + } 1.251 + 1.252 + /** 1.253 + * Constructor for concrete subclasses to call; uses the argument 1.254 + * for the default value. 1.255 + * 1.256 + * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 1.257 + */ 1.258 + protected TypeKindVisitor(R defaultValue) { 1.259 + super(defaultValue); 1.260 + } 1.261 + } 1.262 +}