0N/A/*
2362N/A * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A */
2362N/A
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4989091 5050782 5051962
0N/A * @summary Tests Declaration.getAnnotation method
0N/A * @library ../../lib
0N/A * @compile -source 1.5 GetAnno.java
0N/A * @run main/othervm GetAnno
0N/A */
0N/A
0N/A
0N/Aimport java.lang.annotation.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport com.sun.mirror.declaration.*;
0N/Aimport com.sun.mirror.type.*;
0N/A
0N/Aimport static java.lang.annotation.RetentionPolicy.*;
0N/A
0N/A
0N/Apublic class GetAnno extends Tester {
0N/A
0N/A public static void main(String[] args) {
0N/A (new GetAnno()).run();
0N/A }
0N/A
0N/A
0N/A // Annotations used by tests
0N/A
0N/A @Retention(RUNTIME)
0N/A @interface AT1 {
0N/A long l();
0N/A String s();
0N/A RetentionPolicy e();
0N/A String[] sa();
0N/A AT2 a();
0N/A }
0N/A
0N/A @Inherited
0N/A @interface AT2 {
0N/A }
0N/A
0N/A @interface AT3 {
0N/A Class value() default String.class;
0N/A }
0N/A
0N/A // Array-valued elements of various kinds.
0N/A @interface AT4 {
0N/A boolean[] bs();
0N/A long[] ls();
0N/A String[] ss();
0N/A RetentionPolicy[] es();
0N/A AT2[] as();
0N/A }
0N/A
0N/A
0N/A @Test(result="@GetAnno$AT1(l=7, s=sigh, e=CLASS, sa=[in, out], " +
0N/A "a=@GetAnno$AT2())")
0N/A @AT1(l=7, s="sigh", e=CLASS, sa={"in", "out"}, a=@AT2)
0N/A public Annotation getAnnotation() {
0N/A MethodDeclaration m = getMethod("getAnnotation");
0N/A AT1 a = m.getAnnotation(AT1.class);
0N/A if (a.l() != 7 || !a.s().equals("sigh") || a.e() != CLASS)
0N/A throw new AssertionError();
0N/A return a;
0N/A }
0N/A
0N/A @Test(result="null")
0N/A public Annotation getAnnotationNotThere() {
0N/A return thisClassDecl.getAnnotation(Deprecated.class);
0N/A }
0N/A
0N/A @Test(result="@GetAnno$AT4(bs=[true, false], " +
0N/A "ls=[9, 8], " +
0N/A "ss=[black, white], " +
0N/A "es=[CLASS, SOURCE], " +
0N/A "as=[@GetAnno$AT2(), @GetAnno$AT2()])")
0N/A @AT4(bs={true, false},
0N/A ls={9, 8},
0N/A ss={"black", "white"},
0N/A es={CLASS, SOURCE},
0N/A as={@AT2, @AT2})
0N/A public AT4 getAnnotationArrayValues() {
0N/A MethodDeclaration m = getMethod("getAnnotationArrayValues");
0N/A return m.getAnnotation(AT4.class);
0N/A }
0N/A
0N/A @Test(result="@GetAnno$AT3(value=java.lang.String)")
0N/A @AT3(String.class)
0N/A public AT3 getAnnotationWithClass1() {
0N/A MethodDeclaration m = getMethod("getAnnotationWithClass1");
0N/A return m.getAnnotation(AT3.class);
0N/A }
0N/A
0N/A @Test(result="java.lang.String")
0N/A public TypeMirror getAnnotationWithClass2() {
AT3 a = getAnnotationWithClass1();
try {
Class c = a.value();
throw new AssertionError();
} catch (MirroredTypeException e) {
return e.getTypeMirror();
}
}
@Test(result="boolean")
@AT3(boolean.class)
public TypeMirror getAnnotationWithPrim() {
MethodDeclaration m = getMethod("getAnnotationWithPrim");
AT3 a = m.getAnnotation(AT3.class);
try {
Class c = a.value();
throw new AssertionError();
} catch (MirroredTypeException e) {
return e.getTypeMirror();
}
}
// 5050782
@Test(result="null")
public AT2 getInheritedAnnotation() {
return thisClassDecl.getAnnotation(AT2.class);
}
/**
* Verify that an annotation created by Declaration.getAnnotation()
* has the same hash code as a like annotation created by core
* reflection.
*/
@Test(result="true")
@AT1(l=7, s="sigh", e=CLASS, sa={"in", "out"}, a=@AT2)
public boolean getAnnotationHashCode() {
MethodDeclaration m1 = getMethod("getAnnotationHashCode");
AT1 a1 = m1.getAnnotation(AT1.class);
java.lang.reflect.Method m2 = null;
try {
m2 = this.getClass().getMethod("getAnnotationHashCode");
} catch (NoSuchMethodException e) {
assert false;
}
AT1 a2 = m2.getAnnotation(AT1.class);
return a1.hashCode() == a2.hashCode();
}
}