Path: blob/master/test/jdk/java/lang/annotation/repeatingAnnotations/RepeatedUnitTest.java
41153 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 7154390 8005712 8007278 800491226* @summary Unit test for repeated annotation reflection27*28* @compile RepeatedUnitTest.java subpackage/package-info.java subpackage/Container.java subpackage/Containee.java subpackage/NonRepeated.java subpackage/InheritedContainee.java subpackage/InheritedContainer.java subpackage/InheritedNonRepeated.java29* @run main RepeatedUnitTest30*/3132import subpackage.*;3334import java.lang.annotation.*;35import java.lang.reflect.*;36import java.util.*;3738public class RepeatedUnitTest {39public static void main(String[] args) throws Exception {40// PACKAGE ANNOTATIONS41Class c = Class.forName("subpackage.NonRepeated"); // force package "subpackage" load42Package p = Package.getPackage("subpackage");43packageNonRepeated(p);44packageRepeated(p);45packageContainer(p);4647// INHERITED/NON-INHERITED ON CLASS48inheritedMe1();49inheritedMe2();50inheritedMe3();51inheritedMe4();5253inheritedMe5(); // ContainerOnSuperSingleOnSub54inheritedMe6(); // RepeatableOnSuperSingleOnSub55inheritedMe7(); // SingleAnnoOnSuperContainerOnSub56inheritedMe8(); // SingleOnSuperRepeatableOnSub575859// CONSTRUCTOR60checkMultiplier(Me1.class.getConstructor(new Class[0]), 10);6162// FIELD63checkMultiplier(Me1.class.getField("foo"), 1);6465// METHOD66checkMultiplier(Me1.class.getDeclaredMethod("mee", (Class<?>[])null), 100);6768// INNER CLASS69checkMultiplier(Me1.MiniMee.class, 1000);7071// ENUM ELEMENT72checkMultiplier(Me1.E.class.getField("EE"), 10000);7374// ENUM75checkMultiplier(Me1.E.class, 100000);76}7778static void packageNonRepeated(AnnotatedElement e) {79NonRepeated nr = e.getAnnotation(NonRepeated.class);80check(nr.value() == 10);8182check(1 == countAnnotation(e, NonRepeated.class));8384nr = e.getAnnotationsByType(NonRepeated.class)[0];85check(nr.value() == 10);8687check(1 == containsAnnotationOfType(e.getAnnotations(), NonRepeated.class));88}8990static void packageRepeated(AnnotatedElement e) {91Containee c = e.getAnnotation(Containee.class);92check(c == null);93check(2 == countAnnotation(e, Containee.class));9495c = e.getAnnotationsByType(Containee.class)[0];96check(c.value() == 1);97c = e.getAnnotationsByType(Containee.class)[1];98check(c.value() == 2);99100check(0 == containsAnnotationOfType(e.getAnnotations(), Containee.class));101}102103static void packageContainer(AnnotatedElement e) {104Container cr = e.getAnnotation(Container.class);105check(null != cr);106check(1 == containsAnnotationOfType(e.getAnnotationsByType(Container.class), Container.class));107check(1 == countAnnotation(e, Container.class));108}109110static void inheritedMe1() {111AnnotatedElement e = Me1.class;112check(null == e.getAnnotation(NonRepeated.class));113check(e.getAnnotation(InheritedNonRepeated.class).value() == 20);114check(0 == countAnnotation(e, Containee.class));115check(4 == countAnnotation(e, InheritedContainee.class));116check(0 == countAnnotation(e, Container.class));117check(1 == countAnnotation(e, InheritedContainer.class));118}119120static void inheritedMe2() {121AnnotatedElement e = Me2.class;122check(e.getAnnotation(NonRepeated.class).value() == 100);123check(e.getAnnotation(InheritedNonRepeated.class).value() == 200);124check(4 == countAnnotation(e, Containee.class));125check(4 == countAnnotation(e, InheritedContainee.class));126check(1 == countAnnotation(e, Container.class));127check(1 == countAnnotation(e, InheritedContainer.class));128check(1 == countAnnotation(e, NonRepeated.class));129check(1 == countAnnotation(e, InheritedNonRepeated.class));130131check(e.getAnnotationsByType(Containee.class)[2].value() == 300);132check(e.getAnnotationsByType(InheritedContainee.class)[2].value() == 300);133check(e.getAnnotationsByType(InheritedNonRepeated.class)[0].value() == 200);134check(e.getAnnotationsByType(NonRepeated.class)[0].value() == 100);135}136137static void inheritedMe3() {138AnnotatedElement e = Me3.class;139check(null == e.getAnnotation(NonRepeated.class));140141check(0 == countAnnotation(e, Containee.class));142check(4 == countAnnotation(e, InheritedContainee.class));143check(0 == countAnnotation(e, Container.class));144check(1 == countAnnotation(e, InheritedContainer.class));145146check(e.getAnnotationsByType(InheritedContainee.class)[2].value() == 350);147check(e.getAnnotationsByType(InheritedNonRepeated.class)[0].value() == 15);148}149150static void inheritedMe4() {151AnnotatedElement e = Me4.class;152check(e.getAnnotation(NonRepeated.class).value() == 1000);153check(e.getAnnotation(InheritedNonRepeated.class).value() == 2000);154check(4 == countAnnotation(e, Containee.class));155check(4 == countAnnotation(e, InheritedContainee.class));156check(1 == countAnnotation(e, Container.class));157check(1 == countAnnotation(e, InheritedContainer.class));158check(1 == countAnnotation(e, NonRepeated.class));159check(1 == countAnnotation(e, InheritedNonRepeated.class));160161check(e.getAnnotationsByType(Containee.class)[2].value() == 3000);162check(e.getAnnotationsByType(InheritedContainee.class)[2].value() == 3000);163check(e.getAnnotationsByType(InheritedNonRepeated.class)[0].value() == 2000);164check(e.getAnnotationsByType(NonRepeated.class)[0].value() == 1000);165}166167static void inheritedMe5() {168AnnotatedElement e = Me5.class;169check(2 == e.getAnnotations().length);170check(1 == countAnnotation(e, InheritedContainee.class));171}172173static void inheritedMe6() {174AnnotatedElement e = Me6.class;175check(2 == e.getAnnotations().length);176check(1 == countAnnotation(e, InheritedContainee.class));177}178179static void inheritedMe7() {180AnnotatedElement e = Me7.class;181check(2 == e.getAnnotations().length);182check(2 == countAnnotation(e, InheritedContainee.class));183}184185static void inheritedMe8() {186AnnotatedElement e = Me8.class;187check(2 == e.getAnnotations().length);188check(2 == countAnnotation(e, InheritedContainee.class));189}190191static void checkMultiplier(AnnotatedElement e, int m) {192// Basic sanity of non-repeating getAnnotation(Class)193check(e.getAnnotation(NonRepeated.class).value() == 5 * m);194195// Check count of annotations returned from getAnnotationsByType(Class)196check(4 == countAnnotation(e, Containee.class));197check(1 == countAnnotation(e, Container.class));198check(1 == countAnnotation(e, NonRepeated.class));199200// Check contents of array returned from getAnnotationsByType(Class)201check(e.getAnnotationsByType(Containee.class)[2].value() == 3 * m);202check(e.getAnnotationsByType(NonRepeated.class)[0].value() == 5 * m);203204// Check getAnnotation(Class)205check(e.getAnnotation(Containee.class) == null);206check(e.getAnnotation(Container.class) != null);207208// Check count of annotations returned from getAnnotations()209check(0 == containsAnnotationOfType(e.getAnnotations(), Containee.class));210check(1 == containsAnnotationOfType(e.getAnnotations(), Container.class));211check(1 == containsAnnotationOfType(e.getAnnotations(), NonRepeated.class));212}213214static void check(Boolean b) {215if (!b) throw new RuntimeException();216}217218static int countAnnotation(AnnotatedElement e, Class<? extends Annotation> c) {219return containsAnnotationOfType(e.getAnnotationsByType(c), c);220}221222static <A extends Annotation> int containsAnnotationOfType(A[] l, Class<? extends Annotation> a) {223int count = 0;224for (Annotation an : l) {225if (an.annotationType().equals(a))226count++;227}228return count;229}230}231232@NonRepeated @InheritedNonRepeated233@InheritedContainee(1) @InheritedContainee(2) @InheritedContainee(3) @InheritedContainee(4)234@Containee(1) @Containee(2) @Containee(3) @Containee(4)235class Grandma {}236237class Mother extends Grandma {}238239@NonRepeated(5) @InheritedNonRepeated(15)240@InheritedContainee(150) @InheritedContainee(250) @InheritedContainee(350) @InheritedContainee(450)241@Containee(150) @Containee(250) @Containee(350) @Containee(450)242class Father extends Grandma {}243244class Me1 extends Mother {245246@NonRepeated(5)247@Containee(1) @Containee(2) @Containee(3) @Containee(4)248public String foo = "";249250@NonRepeated(50)251@Containee(10) @Containee(20) @Containee(30) @Containee(40)252public Me1() {253}254255@NonRepeated(500)256@Containee(100) @Containee(200) @Containee(300) @Containee(400)257public void mee() {258}259260@NonRepeated(5000)261@Containee(1000) @Containee(2000) @Containee(3000) @Containee(4000)262public class MiniMee {}263264@NonRepeated(500000)265@Containee(100000) @Containee(200000) @Containee(300000) @Containee(400000)266public enum E {267@NonRepeated(50000)268@Containee(10000) @Containee(20000) @Containee(30000) @Containee(40000)269EE(),270}271}272273@NonRepeated(100) @InheritedNonRepeated(200)274@InheritedContainee(100) @InheritedContainee(200) @InheritedContainee(300) @InheritedContainee(400)275@Containee(100) @Containee(200) @Containee(300) @Containee(400)276class Me2 extends Mother {}277278class Me3 extends Father {}279280@NonRepeated(1000) @InheritedNonRepeated(2000)281@InheritedContainee(1000) @InheritedContainee(2000) @InheritedContainee(3000) @InheritedContainee(4000)282@Containee(1000) @Containee(2000) @Containee(3000) @Containee(4000)283class Me4 extends Father {}284285286@InheritedContainer({@InheritedContainee(1), @InheritedContainee(2)})287class SuperOf5 {}288289@InheritedContainee(3)290class Me5 extends SuperOf5{}291292293@InheritedContainee(1) @InheritedContainee(2)294class SuperOf6 {}295296@InheritedContainee(3)297class Me6 extends SuperOf6 {}298299300@InheritedContainee(1)301class SuperOf7 {}302303@InheritedContainer({@InheritedContainee(2), @InheritedContainee(3)})304class Me7 extends SuperOf7 {}305306307@InheritedContainee(1)308class SuperOf8 {}309310@InheritedContainee(2) @InheritedContainee(3)311class Me8 extends SuperOf8 {}312313314