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

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

mercurial