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