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