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