Tue, 16 Jun 2009 10:46:37 +0100
6638712: Inference with wildcard types causes selection of inapplicable method
Summary: Added global sanity check in order to make sure that return type inference does not violate bounds constraints
Reviewed-by: jjg
duke@1 | 1 | /* |
darcy@232 | 2 | * Copyright 2005-2009 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.processing; |
duke@1 | 27 | |
duke@1 | 28 | import java.lang.annotation.Annotation; |
duke@1 | 29 | import com.sun.tools.javac.tree.JCTree.*; |
duke@1 | 30 | import javax.annotation.processing.*; |
duke@1 | 31 | import javax.lang.model.element.*; |
duke@1 | 32 | import javax.lang.model.type.DeclaredType; |
duke@1 | 33 | import javax.lang.model.type.TypeMirror; |
duke@1 | 34 | import javax.lang.model.util.*; |
duke@1 | 35 | import java.util.*; |
duke@1 | 36 | |
duke@1 | 37 | /** |
duke@1 | 38 | * Object providing state about a prior round of annotation processing. |
duke@1 | 39 | * |
duke@1 | 40 | * <p><b>This is NOT part of any API supported by Sun Microsystems. |
duke@1 | 41 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 42 | * This code and its internal interfaces are subject to change or |
duke@1 | 43 | * deletion without notice.</b> |
duke@1 | 44 | */ |
duke@1 | 45 | public class JavacRoundEnvironment implements RoundEnvironment { |
duke@1 | 46 | // Default equals and hashCode methods are okay. |
duke@1 | 47 | |
duke@1 | 48 | private final boolean processingOver; |
duke@1 | 49 | private final boolean errorRaised; |
duke@1 | 50 | private final ProcessingEnvironment processingEnv; |
duke@1 | 51 | |
duke@1 | 52 | // Caller must pass in an immutable set |
duke@1 | 53 | private final Set<? extends Element> rootElements; |
duke@1 | 54 | |
duke@1 | 55 | JavacRoundEnvironment(boolean processingOver, |
duke@1 | 56 | boolean errorRaised, |
duke@1 | 57 | Set<? extends Element> rootElements, |
duke@1 | 58 | ProcessingEnvironment processingEnv) { |
duke@1 | 59 | this.processingOver = processingOver; |
duke@1 | 60 | this.errorRaised = errorRaised; |
duke@1 | 61 | this.rootElements = rootElements; |
duke@1 | 62 | this.processingEnv = processingEnv; |
duke@1 | 63 | } |
duke@1 | 64 | |
duke@1 | 65 | public String toString() { |
duke@1 | 66 | return String.format("[errorRaised=%b, rootElements=%s, processingOver=%b]", |
duke@1 | 67 | errorRaised, |
duke@1 | 68 | rootElements, |
duke@1 | 69 | processingOver); |
duke@1 | 70 | } |
duke@1 | 71 | |
duke@1 | 72 | public boolean processingOver() { |
duke@1 | 73 | return processingOver; |
duke@1 | 74 | } |
duke@1 | 75 | |
duke@1 | 76 | /** |
duke@1 | 77 | * Returns {@code true} if an error was raised in the prior round |
duke@1 | 78 | * of processing; returns {@code false} otherwise. |
duke@1 | 79 | * |
duke@1 | 80 | * @return {@code true} if an error was raised in the prior round |
duke@1 | 81 | * of processing; returns {@code false} otherwise. |
duke@1 | 82 | */ |
duke@1 | 83 | public boolean errorRaised() { |
duke@1 | 84 | return errorRaised; |
duke@1 | 85 | } |
duke@1 | 86 | |
duke@1 | 87 | /** |
duke@1 | 88 | * Returns the type elements specified by the prior round. |
duke@1 | 89 | * |
duke@1 | 90 | * @return the types elements specified by the prior round, or an |
duke@1 | 91 | * empty set if there were none |
duke@1 | 92 | */ |
duke@1 | 93 | public Set<? extends Element> getRootElements() { |
duke@1 | 94 | return rootElements; |
duke@1 | 95 | } |
duke@1 | 96 | |
duke@1 | 97 | private static final String NOT_AN_ANNOTATION_TYPE = |
duke@1 | 98 | "The argument does not represent an annotation type: "; |
duke@1 | 99 | |
duke@1 | 100 | /** |
duke@1 | 101 | * Returns the elements annotated with the given annotation type. |
duke@1 | 102 | * Only type elements <i>included</i> in this round of annotation |
duke@1 | 103 | * processing, or declarations of members, parameters, or type |
duke@1 | 104 | * parameters declared within those, are returned. Included type |
duke@1 | 105 | * elements are {@linkplain #getSpecifiedTypeElements specified |
duke@1 | 106 | * types} and any types nested within them. |
duke@1 | 107 | * |
duke@1 | 108 | * @param a annotation type being requested |
duke@1 | 109 | * @return the elements annotated with the given annotation type, |
duke@1 | 110 | * or an empty set if there are none |
duke@1 | 111 | */ |
duke@1 | 112 | public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) { |
duke@1 | 113 | Set<Element> result = Collections.emptySet(); |
darcy@232 | 114 | Types typeUtil = processingEnv.getTypeUtils(); |
duke@1 | 115 | if (a.getKind() != ElementKind.ANNOTATION_TYPE) |
duke@1 | 116 | throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); |
duke@1 | 117 | |
duke@1 | 118 | DeclaredType annotationTypeElement; |
duke@1 | 119 | TypeMirror tm = a.asType(); |
duke@1 | 120 | if ( tm instanceof DeclaredType ) |
duke@1 | 121 | annotationTypeElement = (DeclaredType) a.asType(); |
duke@1 | 122 | else |
duke@1 | 123 | throw new AssertionError("Bad implementation type for " + tm); |
duke@1 | 124 | |
duke@1 | 125 | ElementScanner6<Set<Element>, DeclaredType> scanner = |
darcy@232 | 126 | new AnnotationSetScanner(result, typeUtil); |
duke@1 | 127 | |
duke@1 | 128 | for (Element element : rootElements) |
duke@1 | 129 | result = scanner.scan(element, annotationTypeElement); |
duke@1 | 130 | |
duke@1 | 131 | return result; |
duke@1 | 132 | } |
duke@1 | 133 | |
duke@1 | 134 | // Could be written as a local class inside getElementsAnnotatedWith |
duke@1 | 135 | private class AnnotationSetScanner extends |
duke@1 | 136 | ElementScanner6<Set<Element>, DeclaredType> { |
duke@1 | 137 | // Insertion-order preserving set |
duke@1 | 138 | Set<Element> annotatedElements = new LinkedHashSet<Element>(); |
darcy@232 | 139 | Types typeUtil; |
duke@1 | 140 | |
darcy@232 | 141 | AnnotationSetScanner(Set<Element> defaultSet, Types typeUtil) { |
duke@1 | 142 | super(defaultSet); |
darcy@232 | 143 | this.typeUtil = typeUtil; |
duke@1 | 144 | } |
duke@1 | 145 | |
duke@1 | 146 | @Override |
duke@1 | 147 | public Set<Element> scan(Element e, DeclaredType p) { |
duke@1 | 148 | java.util.List<? extends AnnotationMirror> annotationMirrors = |
duke@1 | 149 | processingEnv.getElementUtils().getAllAnnotationMirrors(e); |
duke@1 | 150 | for (AnnotationMirror annotationMirror : annotationMirrors) { |
darcy@232 | 151 | if (typeUtil.isSameType(annotationMirror.getAnnotationType(), p)) |
duke@1 | 152 | annotatedElements.add(e); |
duke@1 | 153 | } |
duke@1 | 154 | e.accept(this, p); |
duke@1 | 155 | return annotatedElements; |
duke@1 | 156 | } |
duke@1 | 157 | |
duke@1 | 158 | } |
duke@1 | 159 | |
duke@1 | 160 | /** |
duke@1 | 161 | * {@inheritdoc} |
duke@1 | 162 | */ |
duke@1 | 163 | public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) { |
duke@1 | 164 | if (!a.isAnnotation()) |
duke@1 | 165 | throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); |
duke@1 | 166 | String name = a.getCanonicalName(); |
duke@1 | 167 | if (name == null) |
duke@1 | 168 | return Collections.emptySet(); |
duke@1 | 169 | else { |
duke@1 | 170 | TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name); |
duke@1 | 171 | if (annotationType == null) |
duke@1 | 172 | return Collections.emptySet(); |
duke@1 | 173 | else |
duke@1 | 174 | return getElementsAnnotatedWith(annotationType); |
duke@1 | 175 | } |
duke@1 | 176 | } |
duke@1 | 177 | } |