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