blob: 0d9cb59d1c4a2311858c5048255220808e0080a4 [file] [log] [blame]
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 7154390
* @summary Unit test for repeated annotation reflection
*
* @compile RepeatedUnitTest.java subpackage/package-info.java subpackage/Container.java subpackage/Containee.java subpackage/NonRepeated.java subpackage/InheritedContainee.java subpackage/InheritedContainer.java subpackage/InheritedNonRepeated.java
* @run main RepeatedUnitTest
*/
import subpackage.*;
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;
public class RepeatedUnitTest {
public static void main(String[] args) throws Exception {
// PACKAGE ANNOTATIONS
Class c = Class.forName("subpackage.NonRepeated"); // force package "subpackage" load
Package p = Package.getPackage("subpackage");
packageNonRepeated(p);
packageRepeated(p);
packageContainer(p);
// INHERITED/NON-INHERITED ON CLASS
inheritedMe1();
inheritedMe2();
inheritedMe3();
inheritedMe4();
// CONSTRUCTOR
checkMultiplier(Me1.class.getConstructor(new Class[0]), 10);
// FIELD
checkMultiplier(Me1.class.getField("foo"), 1);
// METHOD
checkMultiplier(Me1.class.getDeclaredMethod("mee", null), 100);
// INNER CLASS
checkMultiplier(Me1.MiniMee.class, 1000);
// ENUM ELEMENT
checkMultiplier(Me1.E.class.getField("EE"), 10000);
// ENUM
checkMultiplier(Me1.E.class, 100000);
}
static void packageNonRepeated(AnnotatedElement e) {
NonRepeated nr = e.getAnnotation(NonRepeated.class);
check(nr.value() == 10);
check(1 == countAnnotation(e, NonRepeated.class));
nr = e.getAnnotations(NonRepeated.class)[0];
check(nr.value() == 10);
check(1 == containsAnnotationOfType(e.getAnnotations(), NonRepeated.class));
}
static void packageRepeated(AnnotatedElement e) {
Containee c = e.getAnnotation(Containee.class);
check(c.value() == 1);
check(2 == countAnnotation(e, Containee.class));
c = e.getAnnotations(Containee.class)[0];
check(c.value() == 1);
c = e.getAnnotations(Containee.class)[1];
check(c.value() == 2);
check(2 == containsAnnotationOfType(e.getAnnotations(), Containee.class));
}
static void packageContainer(AnnotatedElement e) {
Container cr = e.getAnnotation(Container.class);
check(null != cr);
check(1 == containsAnnotationOfType(e.getAnnotations(Container.class), Container.class));
check(1 == countAnnotation(e, Container.class));
}
static void inheritedMe1() {
AnnotatedElement e = Me1.class;
check(null == e.getAnnotation(NonRepeated.class));
check(e.getAnnotation(InheritedNonRepeated.class).value() == 20);
check(0 == countAnnotation(e, Containee.class));
check(4 == countAnnotation(e, InheritedContainee.class));
check(0 == countAnnotation(e, Container.class));
check(1 == countAnnotation(e, InheritedContainer.class));
}
static void inheritedMe2() {
AnnotatedElement e = Me2.class;
check(e.getAnnotation(NonRepeated.class).value() == 100);
check(e.getAnnotation(InheritedNonRepeated.class).value() == 200);
check(4 == countAnnotation(e, Containee.class));
check(4 == countAnnotation(e, InheritedContainee.class));
check(1 == countAnnotation(e, Container.class));
check(1 == countAnnotation(e, InheritedContainer.class));
check(1 == countAnnotation(e, NonRepeated.class));
check(1 == countAnnotation(e, InheritedNonRepeated.class));
check(e.getAnnotations(Containee.class)[2].value() == 300);
check(e.getAnnotations(InheritedContainee.class)[2].value() == 300);
check(e.getAnnotations(InheritedNonRepeated.class)[0].value() == 200);
check(e.getAnnotations(NonRepeated.class)[0].value() == 100);
}
static void inheritedMe3() {
AnnotatedElement e = Me3.class;
check(null == e.getAnnotation(NonRepeated.class));
check(0 == countAnnotation(e, Containee.class));
check(4 == countAnnotation(e, InheritedContainee.class));
check(0 == countAnnotation(e, Container.class));
check(1 == countAnnotation(e, InheritedContainer.class));
check(e.getAnnotations(InheritedContainee.class)[2].value() == 350);
check(e.getAnnotations(InheritedNonRepeated.class)[0].value() == 15);
}
static void inheritedMe4() {
AnnotatedElement e = Me4.class;
check(e.getAnnotation(NonRepeated.class).value() == 1000);
check(e.getAnnotation(InheritedNonRepeated.class).value() == 2000);
check(4 == countAnnotation(e, Containee.class));
check(4 == countAnnotation(e, InheritedContainee.class));
check(1 == countAnnotation(e, Container.class));
check(1 == countAnnotation(e, InheritedContainer.class));
check(1 == countAnnotation(e, NonRepeated.class));
check(1 == countAnnotation(e, InheritedNonRepeated.class));
check(e.getAnnotations(Containee.class)[2].value() == 3000);
check(e.getAnnotations(InheritedContainee.class)[2].value() == 3000);
check(e.getAnnotations(InheritedNonRepeated.class)[0].value() == 2000);
check(e.getAnnotations(NonRepeated.class)[0].value() == 1000);
}
static void checkMultiplier(AnnotatedElement e, int m) {
check(e.getAnnotation(NonRepeated.class).value() == 5 * m);
check(4 == countAnnotation(e, Containee.class));
check(1 == countAnnotation(e, Container.class));
check(1 == countAnnotation(e, NonRepeated.class));
check(e.getAnnotations(Containee.class)[2].value() == 3 * m);
check(e.getAnnotations(NonRepeated.class)[0].value() == 5 * m);
}
static void check(Boolean b) {
if (!b) throw new RuntimeException();
}
static int countAnnotation(AnnotatedElement e, Class<? extends Annotation> c) {
return containsAnnotationOfType(e.getAnnotations(c), c);
}
static <A extends Annotation> int containsAnnotationOfType(A[] l, Class<? extends Annotation> a) {
int count = 0;
for (Annotation an : l) {
if (an.annotationType().equals(a))
count++;
}
return count;
}
}
@NonRepeated @InheritedNonRepeated
@InheritedContainee(1) @InheritedContainee(2) @InheritedContainee(3) @InheritedContainee(4)
@Containee(1) @Containee(2) @Containee(3) @Containee(4)
class Grandma {}
class Mother extends Grandma {}
@NonRepeated(5) @InheritedNonRepeated(15)
@InheritedContainee(150) @InheritedContainee(250) @InheritedContainee(350) @InheritedContainee(450)
@Containee(150) @Containee(250) @Containee(350) @Containee(450)
class Father extends Grandma {}
class Me1 extends Mother {
@NonRepeated(5)
@Containee(1) @Containee(2) @Containee(3) @Containee(4)
public String foo = "";
@NonRepeated(50)
@Containee(10) @Containee(20) @Containee(30) @Containee(40)
public Me1() {
}
@NonRepeated(500)
@Containee(100) @Containee(200) @Containee(300) @Containee(400)
public void mee() {
}
@NonRepeated(5000)
@Containee(1000) @Containee(2000) @Containee(3000) @Containee(4000)
public class MiniMee {}
@NonRepeated(500000)
@Containee(100000) @Containee(200000) @Containee(300000) @Containee(400000)
public enum E {
@NonRepeated(50000)
@Containee(10000) @Containee(20000) @Containee(30000) @Containee(40000)
EE(),
}
}
@NonRepeated(100) @InheritedNonRepeated(200)
@InheritedContainee(100) @InheritedContainee(200) @InheritedContainee(300) @InheritedContainee(400)
@Containee(100) @Containee(200) @Containee(300) @Containee(400)
class Me2 extends Mother {}
class Me3 extends Father {}
@NonRepeated(1000) @InheritedNonRepeated(2000)
@InheritedContainee(1000) @InheritedContainee(2000) @InheritedContainee(3000) @InheritedContainee(4000)
@Containee(1000) @Containee(2000) @Containee(3000) @Containee(4000)
class Me4 extends Father {}