GenericStringTest.java revision 6855:b4f68aca1000
1/*
2 * Copyright (c) 2004, 2013, 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
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        List<Class<?>> classList = new LinkedList<Class<?>>();
39        classList.add(TestClass1.class);
40        classList.add(TestClass2.class);
41        classList.add(Roebling.class);
42        classList.add(TestInterface1.class);
43
44
45        for(Class<?> clazz: classList)
46            for(Method method: clazz.getDeclaredMethods()) {
47                ExpectedGenericString egs = method.getAnnotation(ExpectedGenericString.class);
48                if (egs != null) {
49                    String actual = method.toGenericString();
50                    System.out.println(actual);
51                    if (! egs.value().equals(actual)) {
52                        failures++;
53                        System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",
54                                          egs.value(), actual);
55                    }
56                }
57
58                if (method.isAnnotationPresent(ExpectedString.class)) {
59                    ExpectedString es = method.getAnnotation(ExpectedString.class);
60                    String actual = method.toString();
61                    if (! es.value().equals(actual)) {
62                        failures++;
63                        System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",
64                                          es.value(), actual);
65                    }
66                }
67
68            }
69
70        // Bridge Test; no "volatile" methods
71        for(Method method: Roebling.class.getDeclaredMethods()) {
72            String s1 = method.toGenericString();
73            String s2 = method.toString();
74            System.out.println("Generic: " + s1);
75            System.out.println("Regular: " + s2);
76            if (s1.indexOf("volatile") != -1 ||
77                s2.indexOf("volatile") != -1) {
78                failures++;
79                System.err.println("ERROR: Bad string; unexpected  ``volatile''");
80            }
81        }
82
83        if (failures > 0) {
84            System.err.println("Test failed.");
85            throw new RuntimeException();
86        }
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    public void method2() throws F {return;}
116}
117
118class Roebling implements Comparable<Roebling> {
119    @ExpectedGenericString(
120   "public int Roebling.compareTo(Roebling)")
121    public int compareTo(Roebling r) {
122        throw new IllegalArgumentException();
123    }
124
125    // Not a transient method, (transient var-arg overlap)
126    @ExpectedGenericString(
127   "void Roebling.varArg(java.lang.Object...)")
128    @ExpectedString(
129   "void Roebling.varArg(java.lang.Object[])")
130    void varArg(Object ... arg) {}
131}
132
133interface TestInterface1 {
134    @ExpectedGenericString(
135   "public default void TestInterface1.foo()")
136    @ExpectedString(
137   "public default void TestInterface1.foo()")
138    public default void foo(){;}
139
140    @ExpectedString(
141   "public default java.lang.Object TestInterface1.bar()")
142    @ExpectedGenericString(
143   "public default <A> A TestInterface1.bar()")
144    default <A> A bar(){return null;}
145
146    @ExpectedString(
147   "public default strictfp double TestInterface1.quux()")
148    @ExpectedGenericString(
149    "public default strictfp double TestInterface1.quux()")
150    strictfp default double quux(){return 1.0;}
151
152}
153
154@Retention(RetentionPolicy.RUNTIME)
155@interface ExpectedGenericString {
156    String value();
157}
158
159@Retention(RetentionPolicy.RUNTIME)
160@interface ExpectedString {
161    String value();
162}
163