test/tools/javac/processing/model/type/MirroredTypeEx/Plurality.java

Thu, 03 Jun 2010 19:56:12 -0700

author
darcy
date
Thu, 03 Jun 2010 19:56:12 -0700
changeset 577
852d8bb356bc
child 699
d2aaaec153e8
permissions
-rw-r--r--

6519115: MirroredTypeException thrown but should be MirroredTypesException
Reviewed-by: jjg

darcy@577 1 /*
darcy@577 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
darcy@577 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
darcy@577 4 *
darcy@577 5 * This code is free software; you can redistribute it and/or modify it
darcy@577 6 * under the terms of the GNU General Public License version 2 only, as
darcy@577 7 * published by the Free Software Foundation.
darcy@577 8 *
darcy@577 9 * This code is distributed in the hope that it will be useful, but WITHOUT
darcy@577 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
darcy@577 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
darcy@577 12 * version 2 for more details (a copy is included in the LICENSE file that
darcy@577 13 * accompanied this code).
darcy@577 14 *
darcy@577 15 * You should have received a copy of the GNU General Public License version
darcy@577 16 * 2 along with this work; if not, write to the Free Software Foundation,
darcy@577 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
darcy@577 18 *
darcy@577 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
darcy@577 20 * or visit www.oracle.com if you need additional information or have any
darcy@577 21 * questions.
darcy@577 22 */
darcy@577 23
darcy@577 24 /*
darcy@577 25 * @test
darcy@577 26 * @bug 6519115
darcy@577 27 * @summary Verify MirroredTypeException vs MirroredTypesException is thrown
darcy@577 28 * @compile Plurality.java
darcy@577 29 * @compile -processor Plurality -proc:only Plurality.java
darcy@577 30 * @author Joseph D. Darcy
darcy@577 31 */
darcy@577 32 import java.lang.annotation.*;
darcy@577 33 import java.math.BigDecimal;
darcy@577 34 import java.util.*;
darcy@577 35 import javax.annotation.processing.*;
darcy@577 36 import javax.lang.model.*;
darcy@577 37 import javax.lang.model.element.*;
darcy@577 38 import javax.lang.model.type.*;
darcy@577 39 import javax.lang.model.util.*;
darcy@577 40
darcy@577 41 @SupportedAnnotationTypes("*")
darcy@577 42 @P0
darcy@577 43 @P1
darcy@577 44 @P2
darcy@577 45 @S1
darcy@577 46 public class Plurality extends AbstractProcessor {
darcy@577 47 private boolean executed = false;
darcy@577 48
darcy@577 49 Elements elements;
darcy@577 50 Types types;
darcy@577 51
darcy@577 52 @Override
darcy@577 53 public void init(ProcessingEnvironment penv) {
darcy@577 54 super.init(penv);
darcy@577 55 elements = penv.getElementUtils();
darcy@577 56 types = penv.getTypeUtils();
darcy@577 57 }
darcy@577 58
darcy@577 59
darcy@577 60 public boolean process(Set<? extends TypeElement> annotations,
darcy@577 61 RoundEnvironment roundEnv) {
darcy@577 62 if (!roundEnv.processingOver()) {
darcy@577 63 executed = true;
darcy@577 64 // Processing just this type
darcy@577 65 Element e = elements.getTypeElement("Plurality");
darcy@577 66 Class[] classes = null;
darcy@577 67
darcy@577 68 P0 p0 = e.getAnnotation(P0.class);
darcy@577 69 try {
darcy@577 70 classes = p0.value();
darcy@577 71 } catch (MirroredTypesException mtse) {
darcy@577 72 if (mtse instanceof MirroredTypeException) {
darcy@577 73 throw new RuntimeException("Wrong exception type!");
darcy@577 74 }
darcy@577 75
darcy@577 76 List<? extends TypeMirror> types = mtse.getTypeMirrors();
darcy@577 77 if (types.size() != 0)
darcy@577 78 throw new RuntimeException("List size != 0: " +
darcy@577 79 types);
darcy@577 80 }
darcy@577 81
darcy@577 82 P1 p1 = e.getAnnotation(P1.class);
darcy@577 83 try {
darcy@577 84 classes = p1.value();
darcy@577 85 } catch (MirroredTypesException mtse) {
darcy@577 86 if (mtse instanceof MirroredTypeException) {
darcy@577 87 throw new RuntimeException("Wrong exception type!");
darcy@577 88 }
darcy@577 89
darcy@577 90 List<? extends TypeMirror> types = mtse.getTypeMirrors();
darcy@577 91 if (types.size() != 1)
darcy@577 92 throw new RuntimeException("List size != 1: " +
darcy@577 93 types);
darcy@577 94 checkTypeListMatchesClasses(types,
darcy@577 95 this.getClass().getAnnotation(P1.class).value());
darcy@577 96 }
darcy@577 97
darcy@577 98
darcy@577 99 P2 p2 = e.getAnnotation(P2.class);
darcy@577 100 try {
darcy@577 101 classes = p2.value();
darcy@577 102 } catch(MirroredTypesException mtse) {
darcy@577 103 if (mtse instanceof MirroredTypeException) {
darcy@577 104 throw new RuntimeException("Wrong exception type!");
darcy@577 105 }
darcy@577 106
darcy@577 107 List<? extends TypeMirror> types = mtse.getTypeMirrors();
darcy@577 108 if (types.size() != 2)
darcy@577 109 throw new RuntimeException("List size != 2: " +
darcy@577 110 types);
darcy@577 111 checkTypeListMatchesClasses(types,
darcy@577 112 this.getClass().getAnnotation(P2.class).value());
darcy@577 113 }
darcy@577 114
darcy@577 115 Class<?> clazz = null;
darcy@577 116 S1 s1 = e.getAnnotation(S1.class);
darcy@577 117 try {
darcy@577 118 clazz = s1.value();
darcy@577 119 } catch(MirroredTypesException mtse) {
darcy@577 120 List<? extends TypeMirror> types = mtse.getTypeMirrors();
darcy@577 121 if (types.size() != 1)
darcy@577 122 throw new RuntimeException("List size != 1: " +
darcy@577 123 types);
darcy@577 124 Class<?>[] clazzes = new Class<?>[1];
darcy@577 125 clazzes[0] = this.getClass().getAnnotation(S1.class).value();
darcy@577 126 checkTypeListMatchesClasses(types,
darcy@577 127 clazzes);
darcy@577 128 }
darcy@577 129
darcy@577 130 try {
darcy@577 131 clazz = s1.value();
darcy@577 132 } catch(MirroredTypeException mte) {
darcy@577 133 TypeMirror type = mte.getTypeMirror();
darcy@577 134 if (type == null) {
darcy@577 135 throw new RuntimeException("Expected null");
darcy@577 136 }
darcy@577 137 List<TypeMirror> types = new ArrayList<>();
darcy@577 138 types.add(type);
darcy@577 139 Class<?>[] clazzes = new Class<?>[1];
darcy@577 140 clazzes[0] = this.getClass().getAnnotation(S1.class).value();
darcy@577 141 checkTypeListMatchesClasses(types, clazzes);
darcy@577 142 }
darcy@577 143 } else {
darcy@577 144 if (!executed) {
darcy@577 145 throw new RuntimeException("Didn't seem to do anything!");
darcy@577 146 }
darcy@577 147 }
darcy@577 148 return true;
darcy@577 149 }
darcy@577 150
darcy@577 151 private static void checkTypeListMatchesClasses(List<? extends TypeMirror> types,
darcy@577 152 Class<?>[] classes) {
darcy@577 153 if (types.size() != classes.length)
darcy@577 154 throw new RuntimeException("Size mismatch:\n\t" + types +
darcy@577 155 "\n\t" + Arrays.toString(classes));
darcy@577 156 int i = -1;
darcy@577 157 for(Class<?> clazz : classes) {
darcy@577 158 i++;
darcy@577 159 String canonicalName = clazz.getCanonicalName();
darcy@577 160 String toStringName = types.get(i).toString();
darcy@577 161 if (!canonicalName.equals(toStringName))
darcy@577 162 throw new RuntimeException("Mismatched names: " +
darcy@577 163 canonicalName + "\t" +
darcy@577 164 toStringName);
darcy@577 165 }
darcy@577 166 }
darcy@577 167
darcy@577 168 @Override
darcy@577 169 public SourceVersion getSupportedSourceVersion() {
darcy@577 170 return SourceVersion.latest();
darcy@577 171 }
darcy@577 172 }
darcy@577 173
darcy@577 174 @Retention(RetentionPolicy.RUNTIME)
darcy@577 175 @interface P0 {
darcy@577 176 Class[] value() default {};
darcy@577 177 }
darcy@577 178
darcy@577 179 @Retention(RetentionPolicy.RUNTIME)
darcy@577 180 @interface P1 {
darcy@577 181 Class[] value() default {Integer.class};
darcy@577 182 }
darcy@577 183
darcy@577 184 @Retention(RetentionPolicy.RUNTIME)
darcy@577 185 @interface P2 {
darcy@577 186 Class[] value() default {String.class, Number.class};
darcy@577 187 }
darcy@577 188
darcy@577 189 @Retention(RetentionPolicy.RUNTIME)
darcy@577 190 @interface S1 {
darcy@577 191 Class value() default BigDecimal.class;
darcy@577 192 }

mercurial