1/*
2 * Copyright (c) 2004, 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 5033583 6316717 6470106 8004979 8161500 8162539
27 * @summary Check toGenericString() and toString() methods
28 * @author Joseph D. Darcy
29 */
30
31import java.lang.reflect.*;
32import java.lang.annotation.*;
33import java.util.*;
34
35public class GenericStringTest {
36    public static void main(String argv[]) throws Exception {
37        int failures = 0;
38
39        for(Class<?> clazz: List.of(TestClass1.class, TestClass2.class,
40                                    Roebling.class, TestInterface1.class))
41            for(Method method: clazz.getDeclaredMethods()) {
42                ExpectedGenericString egs = method.getAnnotation(ExpectedGenericString.class);
43                if (egs != null) {
44                    String actual = method.toGenericString();
45                    System.out.println(actual);
46                    if (method.isBridge()) {
47                        failures += checkForFailure(egs.bridgeValue(), actual);
48                    } else {
49                        failures += checkForFailure(egs.value(), actual);
50                    }
51                }
52
53                if (method.isAnnotationPresent(ExpectedString.class)) {
54                    ExpectedString es = method.getAnnotation(ExpectedString.class);
55                    String actual = method.toString();
56                    failures += checkForFailure(es.value(), actual);
57                }
58
59            }
60
61        // Bridge Test; no "volatile" methods
62        for(Method method: Roebling.class.getDeclaredMethods()) {
63            String s1 = method.toGenericString();
64            String s2 = method.toString();
65            System.out.println("Generic: " + s1);
66            System.out.println("Regular: " + s2);
67            if (s1.indexOf("volatile") != -1 ||
68                s2.indexOf("volatile") != -1) {
69                failures++;
70                System.err.println("ERROR: Bad string; unexpected  ``volatile''");
71            }
72        }
73
74        if (failures > 0) {
75            System.err.println("Test failed.");
76            throw new RuntimeException();
77        }
78    }
79
80    private static int checkForFailure(String expected, String actual) {
81        if (!expected.equals(actual)) {
82            System.err.printf("ERROR: Expected ''%s'';%ngot             ''%s''.\n",
83                              expected, actual);
84            return 1;
85        } else
86            return 0;
87    }
88}
89
90class TestClass1 {
91    @ExpectedGenericString(
92   "void TestClass1.method1(int,double)")
93    void method1(int x, double y) {}
94
95    @ExpectedGenericString(
96   "private static java.lang.String TestClass1.method2(int,int)")
97    private static String method2(int x, int param2) {return null;}
98
99    @ExpectedGenericString(
100   "static void TestClass1.method3() throws java.lang.RuntimeException")
101    static void method3() throws RuntimeException {return;}
102
103    @ExpectedGenericString(
104   "protected <S,T> S TestClass1.method4(S,T) throws java.lang.Exception")
105    protected <S, T> S method4(S s, T t) throws Exception {return null;}
106}
107
108class TestClass2<E, F extends Exception> {
109    @ExpectedGenericString(
110   "public <T> T TestClass2.method1(E,T)")
111    public <T> T method1(E e, T t) {return null;}
112
113    @ExpectedGenericString(
114   "public void TestClass2.method2() throws F")
115    @ExpectedString(
116   "public void TestClass2.method2() throws java.lang.Exception")
117    public void method2() throws F {return;}
118
119    @ExpectedGenericString(
120   "public E[] TestClass2.method3()")
121    public E[] method3() {return null;}
122
123    @ExpectedGenericString(
124   "public E[][] TestClass2.method4()")
125    public E[][] method4() {return null;}
126
127    @ExpectedGenericString(
128   "public java.util.List<E[]> TestClass2.method5()")
129    public List<E[]> method5() {return null;}
130
131    @ExpectedGenericString(
132   "public java.util.List<?> TestClass2.method6()")
133    public List<?> method6() {return null;}
134
135    @ExpectedGenericString(
136   "public java.util.List<?>[] TestClass2.method7()")
137    public List<?>[] method7() {return null;}
138
139    @ExpectedGenericString(
140   "public <K,V> java.util.Map<K, V> TestClass2.method8()")
141    public <K, V> Map<K, V> method8() {return null;}
142}
143
144class Roebling implements Comparable<Roebling> {
145    @ExpectedGenericString(
146    value="public int Roebling.compareTo(Roebling)",
147    bridgeValue="public int Roebling.compareTo(java.lang.Object)")
148    public int compareTo(Roebling r) {
149        throw new IllegalArgumentException();
150    }
151
152    // Not a transient method, (transient var-arg overlap)
153    @ExpectedGenericString(
154   "void Roebling.varArg(java.lang.Object...)")
155    @ExpectedString(
156   "void Roebling.varArg(java.lang.Object[])")
157    void varArg(Object ... arg) {}
158}
159
160interface TestInterface1 {
161    @ExpectedGenericString(
162   "public default void TestInterface1.foo()")
163    @ExpectedString(
164   "public default void TestInterface1.foo()")
165    public default void foo(){;}
166
167    @ExpectedString(
168   "public default java.lang.Object TestInterface1.bar()")
169    @ExpectedGenericString(
170   "public default <A> A TestInterface1.bar()")
171    default <A> A bar(){return null;}
172
173    @ExpectedString(
174   "public default strictfp double TestInterface1.quux()")
175    @ExpectedGenericString(
176    "public default strictfp double TestInterface1.quux()")
177    strictfp default double quux(){return 1.0;}
178}
179
180@Retention(RetentionPolicy.RUNTIME)
181@interface ExpectedGenericString {
182    String value();
183    String bridgeValue() default "";
184}
185
186@Retention(RetentionPolicy.RUNTIME)
187@interface ExpectedString {
188    String value();
189}
190
191