6519115: MirroredTypeException thrown but should be MirroredTypesException

Reviewed-by: jjg
This commit is contained in:
Joe Darcy 2010-06-03 19:56:12 -07:00
parent 3bc126336f
commit da823fbf44
4 changed files with 211 additions and 8 deletions
langtools
src/share/classes
test/tools/javac/processing/model/type/MirroredTypeEx

@ -181,16 +181,16 @@ public class AnnotationProxyMaker {
}
public void visitArray(Attribute.Array a) {
Name elemName = ((ArrayType) a.type).elemtype.tsym.name;
Name elemName = ((ArrayType) a.type).elemtype.tsym.getQualifiedName();
if (elemName == elemName.table.names.java_lang_Class) { // Class[]
if (elemName.equals(elemName.table.names.java_lang_Class)) { // Class[]
// Construct a proxy for a MirroredTypesException
List<TypeMirror> elems = List.nil();
ListBuffer<TypeMirror> elems = new ListBuffer<TypeMirror>();
for (Attribute value : a.values) {
Type elem = ((Attribute.Class) value).type;
elems.add(elem);
elems.append(elem);
}
value = new MirroredTypesExceptionProxy(elems);
value = new MirroredTypesExceptionProxy(elems.toList());
} else {
int len = a.values.length;

@ -42,7 +42,7 @@ import javax.lang.model.element.Element;
* @see Element#getAnnotation(Class)
* @since 1.6
*/
public class MirroredTypeException extends RuntimeException {
public class MirroredTypeException extends MirroredTypesException {
private static final long serialVersionUID = 269;
@ -54,7 +54,7 @@ public class MirroredTypeException extends RuntimeException {
* @param type the type being accessed
*/
public MirroredTypeException(TypeMirror type) {
super("Attempt to access Class object for TypeMirror " + type.toString());
super("Attempt to access Class object for TypeMirror " + type.toString(), type);
this.type = type;
}
@ -76,5 +76,6 @@ public class MirroredTypeException extends RuntimeException {
throws IOException, ClassNotFoundException {
s.defaultReadObject();
type = null;
types = null;
}
}

@ -49,7 +49,17 @@ public class MirroredTypesException extends RuntimeException {
private static final long serialVersionUID = 269;
private transient List<? extends TypeMirror> types; // cannot be serialized
transient List<? extends TypeMirror> types; // cannot be serialized
/*
* Trusted constructor to be called by MirroredTypeException.
*/
MirroredTypesException(String message, TypeMirror type) {
super(message);
List<TypeMirror> tmp = (new ArrayList<TypeMirror>());
tmp.add(type);
types = Collections.unmodifiableList(tmp);
}
/**
* Constructs a new MirroredTypesException for the specified types.

@ -0,0 +1,192 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6519115
* @summary Verify MirroredTypeException vs MirroredTypesException is thrown
* @compile Plurality.java
* @compile -processor Plurality -proc:only Plurality.java
* @author Joseph D. Darcy
*/
import java.lang.annotation.*;
import java.math.BigDecimal;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.*;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
import javax.lang.model.util.*;
@SupportedAnnotationTypes("*")
@P0
@P1
@P2
@S1
public class Plurality extends AbstractProcessor {
private boolean executed = false;
Elements elements;
Types types;
@Override
public void init(ProcessingEnvironment penv) {
super.init(penv);
elements = penv.getElementUtils();
types = penv.getTypeUtils();
}
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
if (!roundEnv.processingOver()) {
executed = true;
// Processing just this type
Element e = elements.getTypeElement("Plurality");
Class[] classes = null;
P0 p0 = e.getAnnotation(P0.class);
try {
classes = p0.value();
} catch (MirroredTypesException mtse) {
if (mtse instanceof MirroredTypeException) {
throw new RuntimeException("Wrong exception type!");
}
List<? extends TypeMirror> types = mtse.getTypeMirrors();
if (types.size() != 0)
throw new RuntimeException("List size != 0: " +
types);
}
P1 p1 = e.getAnnotation(P1.class);
try {
classes = p1.value();
} catch (MirroredTypesException mtse) {
if (mtse instanceof MirroredTypeException) {
throw new RuntimeException("Wrong exception type!");
}
List<? extends TypeMirror> types = mtse.getTypeMirrors();
if (types.size() != 1)
throw new RuntimeException("List size != 1: " +
types);
checkTypeListMatchesClasses(types,
this.getClass().getAnnotation(P1.class).value());
}
P2 p2 = e.getAnnotation(P2.class);
try {
classes = p2.value();
} catch(MirroredTypesException mtse) {
if (mtse instanceof MirroredTypeException) {
throw new RuntimeException("Wrong exception type!");
}
List<? extends TypeMirror> types = mtse.getTypeMirrors();
if (types.size() != 2)
throw new RuntimeException("List size != 2: " +
types);
checkTypeListMatchesClasses(types,
this.getClass().getAnnotation(P2.class).value());
}
Class<?> clazz = null;
S1 s1 = e.getAnnotation(S1.class);
try {
clazz = s1.value();
} catch(MirroredTypesException mtse) {
List<? extends TypeMirror> types = mtse.getTypeMirrors();
if (types.size() != 1)
throw new RuntimeException("List size != 1: " +
types);
Class<?>[] clazzes = new Class<?>[1];
clazzes[0] = this.getClass().getAnnotation(S1.class).value();
checkTypeListMatchesClasses(types,
clazzes);
}
try {
clazz = s1.value();
} catch(MirroredTypeException mte) {
TypeMirror type = mte.getTypeMirror();
if (type == null) {
throw new RuntimeException("Expected null");
}
List<TypeMirror> types = new ArrayList<>();
types.add(type);
Class<?>[] clazzes = new Class<?>[1];
clazzes[0] = this.getClass().getAnnotation(S1.class).value();
checkTypeListMatchesClasses(types, clazzes);
}
} else {
if (!executed) {
throw new RuntimeException("Didn't seem to do anything!");
}
}
return true;
}
private static void checkTypeListMatchesClasses(List<? extends TypeMirror> types,
Class<?>[] classes) {
if (types.size() != classes.length)
throw new RuntimeException("Size mismatch:\n\t" + types +
"\n\t" + Arrays.toString(classes));
int i = -1;
for(Class<?> clazz : classes) {
i++;
String canonicalName = clazz.getCanonicalName();
String toStringName = types.get(i).toString();
if (!canonicalName.equals(toStringName))
throw new RuntimeException("Mismatched names: " +
canonicalName + "\t" +
toStringName);
}
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface P0 {
Class[] value() default {};
}
@Retention(RetentionPolicy.RUNTIME)
@interface P1 {
Class[] value() default {Integer.class};
}
@Retention(RetentionPolicy.RUNTIME)
@interface P2 {
Class[] value() default {String.class, Number.class};
}
@Retention(RetentionPolicy.RUNTIME)
@interface S1 {
Class value() default BigDecimal.class;
}