annotate test/runtime/8007320/ConstMethodTest.java @ 8031:927a311d00f9

8007320: NPG: move method annotations Summary: allocate method annotations and attach to ConstMethod if present Reviewed-by: dcubed, jiangli, sspitsyn, iklam
author coleenp
date Mon, 11 Feb 2013 14:06:22 -0500
parents
children a589c78a8811
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
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
26 * @bug 8007320
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 {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
77
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
78 private static void check(boolean b) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
79 if (!b)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
80 throw new RuntimeException();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
81 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
82 private static void fail(String msg) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
83 System.err.println(msg);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
84 throw new RuntimeException();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
85 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
86 private static void equal(Object x, Object y) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
87 if (x == null ? y == null : x.equals(y)) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
88 } else {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
89 fail(x + " not equal to " + y);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
90 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
91 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
92 private static final String[] parameter_names = {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
93 "parameter", "parameter2", "x"
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
94 };
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
95
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
96 // Declare a function with everything in it.
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
97 @MyAnnotation(name="someName", value="Hello World")
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
98 static <T> void kitchenSinkFunc(@Named(value="aName") String parameter,
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
99 @Named("bName") String parameter2,
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
100 @ScalarTypesWithDefault T x)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
101 throws @TypeAnno("RE") @TypeAnno2("RE2") RuntimeException,
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
102 NullPointerException,
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
103 @TypeAnno("AIOOBE") ArrayIndexOutOfBoundsException {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
104 int i, j, k;
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
105 try {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
106 System.out.println("calling kitchenSinkFunc " + parameter);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
107 throw new OkException(); // to see stack trace with line numbers
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
108 } catch (Exception e) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
109 e.printStackTrace();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
110 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
111 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
112
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
113 private static void test1() throws Throwable {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
114 for (Method m : ConstMethodTest.class.getDeclaredMethods()) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
115 if (m.getName().equals("kitchenSinkFunc")) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
116 Annotation[][] ann = m.getParameterAnnotations();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
117 equal(ann.length, 3);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
118 Annotation foo = ann[0][0];
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
119 Annotation bar = ann[1][0];
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
120 equal(foo.toString(), "@Named(value=aName)");
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
121 equal(bar.toString(), "@Named(value=bName)");
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
122 check(foo.equals(foo));
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
123 check(bar.equals(bar));
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
124 check(! foo.equals(bar));
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
125 // method annotations
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
126 Annotation[] ann2 = m.getAnnotations();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
127 equal(ann2.length, 1);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
128 Annotation mann = ann2[0];
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
129 equal(mann.toString(), "@MyAnnotation(date=today, name=someName, value=Hello World)");
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
130 // Test Method parameter names
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
131 Parameter[] parameters = m.getParameters();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
132 if(parameters == null)
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
133 throw new Exception("getParameters should never be null");
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
134 for(int i = 0; i < parameters.length; i++) {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
135 Parameter p = parameters[i];
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
136 equal(parameters[i].getName(), parameter_names[i]);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
137 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
138 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
139 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
140 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
141
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
142 public static void main(java.lang.String[] unused) throws Throwable {
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
143 // pass 5 so kitchenSinkFunc is instantiated with an int
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
144 kitchenSinkFunc("parameter", "param2", 5);
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
145 test1();
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
146 }
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
147 };
927a311d00f9 8007320: NPG: move method annotations
coleenp
parents:
diff changeset
148