Wed, 29 Sep 2010 23:27:57 -0700
6983738: Use a JavacTestingAbstractProcessor
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@699 | 28 | * @library ../../../../lib |
darcy@699 | 29 | * @build JavacTestingAbstractProcessor |
darcy@577 | 30 | * @compile Plurality.java |
darcy@577 | 31 | * @compile -processor Plurality -proc:only Plurality.java |
darcy@577 | 32 | * @author Joseph D. Darcy |
darcy@577 | 33 | */ |
darcy@577 | 34 | import java.lang.annotation.*; |
darcy@577 | 35 | import java.math.BigDecimal; |
darcy@577 | 36 | import java.util.*; |
darcy@577 | 37 | import javax.annotation.processing.*; |
darcy@577 | 38 | import javax.lang.model.*; |
darcy@577 | 39 | import javax.lang.model.element.*; |
darcy@577 | 40 | import javax.lang.model.type.*; |
darcy@577 | 41 | import javax.lang.model.util.*; |
darcy@577 | 42 | |
darcy@577 | 43 | @P0 |
darcy@577 | 44 | @P1 |
darcy@577 | 45 | @P2 |
darcy@577 | 46 | @S1 |
darcy@699 | 47 | public class Plurality extends JavacTestingAbstractProcessor { |
darcy@577 | 48 | private boolean executed = false; |
darcy@577 | 49 | |
darcy@577 | 50 | public boolean process(Set<? extends TypeElement> annotations, |
darcy@577 | 51 | RoundEnvironment roundEnv) { |
darcy@577 | 52 | if (!roundEnv.processingOver()) { |
darcy@577 | 53 | executed = true; |
darcy@577 | 54 | // Processing just this type |
darcy@577 | 55 | Element e = elements.getTypeElement("Plurality"); |
darcy@577 | 56 | Class[] classes = null; |
darcy@577 | 57 | |
darcy@577 | 58 | P0 p0 = e.getAnnotation(P0.class); |
darcy@577 | 59 | try { |
darcy@577 | 60 | classes = p0.value(); |
darcy@577 | 61 | } catch (MirroredTypesException mtse) { |
darcy@577 | 62 | if (mtse instanceof MirroredTypeException) { |
darcy@577 | 63 | throw new RuntimeException("Wrong exception type!"); |
darcy@577 | 64 | } |
darcy@577 | 65 | |
darcy@577 | 66 | List<? extends TypeMirror> types = mtse.getTypeMirrors(); |
darcy@577 | 67 | if (types.size() != 0) |
darcy@577 | 68 | throw new RuntimeException("List size != 0: " + |
darcy@577 | 69 | types); |
darcy@577 | 70 | } |
darcy@577 | 71 | |
darcy@577 | 72 | P1 p1 = e.getAnnotation(P1.class); |
darcy@577 | 73 | try { |
darcy@577 | 74 | classes = p1.value(); |
darcy@577 | 75 | } catch (MirroredTypesException mtse) { |
darcy@577 | 76 | if (mtse instanceof MirroredTypeException) { |
darcy@577 | 77 | throw new RuntimeException("Wrong exception type!"); |
darcy@577 | 78 | } |
darcy@577 | 79 | |
darcy@577 | 80 | List<? extends TypeMirror> types = mtse.getTypeMirrors(); |
darcy@577 | 81 | if (types.size() != 1) |
darcy@577 | 82 | throw new RuntimeException("List size != 1: " + |
darcy@577 | 83 | types); |
darcy@577 | 84 | checkTypeListMatchesClasses(types, |
darcy@577 | 85 | this.getClass().getAnnotation(P1.class).value()); |
darcy@577 | 86 | } |
darcy@577 | 87 | |
darcy@577 | 88 | |
darcy@577 | 89 | P2 p2 = e.getAnnotation(P2.class); |
darcy@577 | 90 | try { |
darcy@577 | 91 | classes = p2.value(); |
darcy@577 | 92 | } catch(MirroredTypesException mtse) { |
darcy@577 | 93 | if (mtse instanceof MirroredTypeException) { |
darcy@577 | 94 | throw new RuntimeException("Wrong exception type!"); |
darcy@577 | 95 | } |
darcy@577 | 96 | |
darcy@577 | 97 | List<? extends TypeMirror> types = mtse.getTypeMirrors(); |
darcy@577 | 98 | if (types.size() != 2) |
darcy@577 | 99 | throw new RuntimeException("List size != 2: " + |
darcy@577 | 100 | types); |
darcy@577 | 101 | checkTypeListMatchesClasses(types, |
darcy@577 | 102 | this.getClass().getAnnotation(P2.class).value()); |
darcy@577 | 103 | } |
darcy@577 | 104 | |
darcy@577 | 105 | Class<?> clazz = null; |
darcy@577 | 106 | S1 s1 = e.getAnnotation(S1.class); |
darcy@577 | 107 | try { |
darcy@577 | 108 | clazz = s1.value(); |
darcy@577 | 109 | } catch(MirroredTypesException mtse) { |
darcy@577 | 110 | List<? extends TypeMirror> types = mtse.getTypeMirrors(); |
darcy@577 | 111 | if (types.size() != 1) |
darcy@577 | 112 | throw new RuntimeException("List size != 1: " + |
darcy@577 | 113 | types); |
darcy@577 | 114 | Class<?>[] clazzes = new Class<?>[1]; |
darcy@577 | 115 | clazzes[0] = this.getClass().getAnnotation(S1.class).value(); |
darcy@577 | 116 | checkTypeListMatchesClasses(types, |
darcy@577 | 117 | clazzes); |
darcy@577 | 118 | } |
darcy@577 | 119 | |
darcy@577 | 120 | try { |
darcy@577 | 121 | clazz = s1.value(); |
darcy@577 | 122 | } catch(MirroredTypeException mte) { |
darcy@577 | 123 | TypeMirror type = mte.getTypeMirror(); |
darcy@577 | 124 | if (type == null) { |
darcy@577 | 125 | throw new RuntimeException("Expected null"); |
darcy@577 | 126 | } |
darcy@577 | 127 | List<TypeMirror> types = new ArrayList<>(); |
darcy@577 | 128 | types.add(type); |
darcy@577 | 129 | Class<?>[] clazzes = new Class<?>[1]; |
darcy@577 | 130 | clazzes[0] = this.getClass().getAnnotation(S1.class).value(); |
darcy@577 | 131 | checkTypeListMatchesClasses(types, clazzes); |
darcy@577 | 132 | } |
darcy@577 | 133 | } else { |
darcy@577 | 134 | if (!executed) { |
darcy@577 | 135 | throw new RuntimeException("Didn't seem to do anything!"); |
darcy@577 | 136 | } |
darcy@577 | 137 | } |
darcy@577 | 138 | return true; |
darcy@577 | 139 | } |
darcy@577 | 140 | |
darcy@577 | 141 | private static void checkTypeListMatchesClasses(List<? extends TypeMirror> types, |
darcy@577 | 142 | Class<?>[] classes) { |
darcy@577 | 143 | if (types.size() != classes.length) |
darcy@577 | 144 | throw new RuntimeException("Size mismatch:\n\t" + types + |
darcy@577 | 145 | "\n\t" + Arrays.toString(classes)); |
darcy@577 | 146 | int i = -1; |
darcy@577 | 147 | for(Class<?> clazz : classes) { |
darcy@577 | 148 | i++; |
darcy@577 | 149 | String canonicalName = clazz.getCanonicalName(); |
darcy@577 | 150 | String toStringName = types.get(i).toString(); |
darcy@577 | 151 | if (!canonicalName.equals(toStringName)) |
darcy@577 | 152 | throw new RuntimeException("Mismatched names: " + |
darcy@577 | 153 | canonicalName + "\t" + |
darcy@577 | 154 | toStringName); |
darcy@577 | 155 | } |
darcy@577 | 156 | } |
darcy@577 | 157 | } |
darcy@577 | 158 | |
darcy@577 | 159 | @Retention(RetentionPolicy.RUNTIME) |
darcy@577 | 160 | @interface P0 { |
darcy@577 | 161 | Class[] value() default {}; |
darcy@577 | 162 | } |
darcy@577 | 163 | |
darcy@577 | 164 | @Retention(RetentionPolicy.RUNTIME) |
darcy@577 | 165 | @interface P1 { |
darcy@577 | 166 | Class[] value() default {Integer.class}; |
darcy@577 | 167 | } |
darcy@577 | 168 | |
darcy@577 | 169 | @Retention(RetentionPolicy.RUNTIME) |
darcy@577 | 170 | @interface P2 { |
darcy@577 | 171 | Class[] value() default {String.class, Number.class}; |
darcy@577 | 172 | } |
darcy@577 | 173 | |
darcy@577 | 174 | @Retention(RetentionPolicy.RUNTIME) |
darcy@577 | 175 | @interface S1 { |
darcy@577 | 176 | Class value() default BigDecimal.class; |
darcy@577 | 177 | } |