annotate test/runtime/8007320/ConstMethodTest.java @ 10378:a589c78a8811

8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType Reviewed-by: stefank, rbackman Contributed-by: Joel Borggren-Franck <joel.franck@oracle.com>
author rbackman
date Fri, 31 May 2013 13:02:24 +0200
parents 927a311d00f9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8031
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
1 /*
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
4 *
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
7 * published by the Free Software Foundation.
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
8 *
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
13 * accompanied this code).
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
14 *
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
18 *
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
21 * questions.
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
22 */
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
23
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
24 /*
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
25 * @test
10378
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
26 * @bug 8007320 8014709
8031
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
27 * @summary Test all optional fields in ConstMethod
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
28 * @compile -g -parameters ConstMethodTest.java
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
29 * @run main ConstMethodTest
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
30 */
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
31
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
32 import java.util.*;
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
33 import java.lang.annotation.*;
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
34 import java.lang.reflect.*;
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
35 import java.io.Serializable;
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
36
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
37 @Retention(RetentionPolicy.RUNTIME)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
38 @interface MyAnnotation {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
39 public String name();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
40 public String value();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
41 public String date() default "today";
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
42 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
43
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
44 @Target(ElementType.TYPE_USE)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
45 @Retention(RetentionPolicy.RUNTIME)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
46 @interface TypeAnno {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
47 String value();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
48 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
49
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
50 @Target(ElementType.TYPE_USE)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
51 @Retention(RetentionPolicy.RUNTIME)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
52 @interface TypeAnno2 {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
53 String value();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
54 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
55
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
56 @Retention(RetentionPolicy.RUNTIME)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
57 @Target(ElementType.PARAMETER)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
58 @interface Named {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
59 String value();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
60 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
61
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
62 @Retention(RetentionPolicy.RUNTIME)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
63 @interface ScalarTypesWithDefault {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
64 byte b() default 11;
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
65 short s() default 12;
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
66 int i() default 13;
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
67 long l() default 14;
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
68 char c() default 'V';
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
69 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
70
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
71 // Some exception class
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
72 class OkException extends RuntimeException {};
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
73
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
74
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
75 @MyAnnotation(name="someName", value = "Hello World")
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
76 public class ConstMethodTest {
10378
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
77 public @TypeAnno("constructor") ConstMethodTest() { }
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
78
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
79 public ConstMethodTest(int i) {
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
80 // needs a second unannotated constructor
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
81 }
8031
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
82
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
83 private static void check(boolean b) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
84 if (!b)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
85 throw new RuntimeException();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
86 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
87 private static void fail(String msg) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
88 System.err.println(msg);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
89 throw new RuntimeException();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
90 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
91 private static void equal(Object x, Object y) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
92 if (x == null ? y == null : x.equals(y)) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
93 } else {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
94 fail(x + " not equal to " + y);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
95 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
96 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
97 private static final String[] parameter_names = {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
98 "parameter", "parameter2", "x"
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
99 };
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
100
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
101 // Declare a function with everything in it.
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
102 @MyAnnotation(name="someName", value="Hello World")
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
103 static <T> void kitchenSinkFunc(@Named(value="aName") String parameter,
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
104 @Named("bName") String parameter2,
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
105 @ScalarTypesWithDefault T x)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
106 throws @TypeAnno("RE") @TypeAnno2("RE2") RuntimeException,
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
107 NullPointerException,
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
108 @TypeAnno("AIOOBE") ArrayIndexOutOfBoundsException {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
109 int i, j, k;
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
110 try {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
111 System.out.println("calling kitchenSinkFunc " + parameter);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
112 throw new OkException(); // to see stack trace with line numbers
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
113 } catch (Exception e) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
114 e.printStackTrace();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
115 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
116 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
117
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
118 private static void test1() throws Throwable {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
119 for (Method m : ConstMethodTest.class.getDeclaredMethods()) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
120 if (m.getName().equals("kitchenSinkFunc")) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
121 Annotation[][] ann = m.getParameterAnnotations();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
122 equal(ann.length, 3);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
123 Annotation foo = ann[0][0];
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
124 Annotation bar = ann[1][0];
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
125 equal(foo.toString(), "@Named(value=aName)");
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
126 equal(bar.toString(), "@Named(value=bName)");
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
127 check(foo.equals(foo));
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
128 check(bar.equals(bar));
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
129 check(! foo.equals(bar));
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
130 // method annotations
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
131 Annotation[] ann2 = m.getAnnotations();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
132 equal(ann2.length, 1);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
133 Annotation mann = ann2[0];
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
134 equal(mann.toString(), "@MyAnnotation(date=today, name=someName, value=Hello World)");
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
135 // Test Method parameter names
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
136 Parameter[] parameters = m.getParameters();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
137 if(parameters == null)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
138 throw new Exception("getParameters should never be null");
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
139 for(int i = 0; i < parameters.length; i++) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
140 Parameter p = parameters[i];
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
141 equal(parameters[i].getName(), parameter_names[i]);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
142 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
143 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
144 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
145 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
146
10378
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
147 private static void testConstructor() throws Exception {
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
148 for (Constructor c : ConstMethodTest.class.getDeclaredConstructors()) {
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
149 Annotation[] aa = c.getAnnotatedReturnType().getAnnotations();
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
150 if (c.getParameterTypes().length == 1) { // should be un-annotated
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
151 check(aa.length == 0);
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
152 } else if (c.getParameterTypes().length == 0) { //should be annotated
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
153 check(aa.length == 1);
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
154 check(((TypeAnno)aa[0]).value().equals("constructor"));
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
155 } else {
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
156 //should not happen
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
157 check(false);
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
158 }
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
159 }
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
160 }
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
161
8031
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
162 public static void main(java.lang.String[] unused) throws Throwable {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
163 // pass 5 so kitchenSinkFunc is instantiated with an int
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
164 kitchenSinkFunc("parameter", "param2", 5);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
165 test1();
10378
a589c78a8811 8014709: Constructor.getAnnotatedReturnType() returns empty AnnotatedType
rbackman
parents: 8031
diff changeset
166 testConstructor();
8031
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
167 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
168 };
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
169