1 /*
2  * Copyright (c) 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * @test
28  * @bug 8054213
29  * @summary Check that toString method works properly for generic return type
30  * obtained via reflection
31  * @run main TestGenericReturnTypeToString
32  */
33 
34 import java.lang.annotation.Retention;
35 import java.lang.annotation.RetentionPolicy;
36 import java.lang.reflect.Method;
37 import java.util.List;
38 
39 public class TestGenericReturnTypeToString {
40 
main(String[] args)41     public static void main(String[] args) {
42         boolean hasFailures = false;
43         for (Method method : TestGenericReturnTypeToString.class.getMethods()) {
44             if (method.isAnnotationPresent(ExpectedGenericString.class)) {
45                 ExpectedGenericString es = method.getAnnotation
46                         (ExpectedGenericString.class);
47                 String result = method.getGenericReturnType().toString();
48                 if (!es.value().equals(result)) {
49                     hasFailures = true;
50                     System.err.println("Unexpected result of " +
51                             "getGenericReturnType().toString() " +
52                             " for " + method.getName()
53                             + " expected: " + es.value() + " actual: " + result);
54                 }
55             }
56             if (hasFailures) {
57                 throw new RuntimeException("Test failed");
58             }
59         }
60     }
61 
62     @ExpectedGenericString("TestGenericReturnTypeToString$" +
63           "FirstInnerClassGeneric<Dummy>$SecondInnerClassGeneric<Dummy>")
foo1()64     public FirstInnerClassGeneric<Dummy>.SecondInnerClassGeneric<Dummy> foo1() {
65         return null;
66     }
67 
68     @ExpectedGenericString("TestGenericReturnTypeToString$" +
69           "FirstInnerClassGeneric<Dummy>$SecondInnerClass")
foo2()70     public FirstInnerClassGeneric<Dummy>.SecondInnerClass foo2() {
71         return null;
72     }
73 
74     @ExpectedGenericString("TestGenericReturnTypeToString$" +
75           "FirstInnerClass$SecondInnerClassGeneric<Dummy>")
foo3()76     public FirstInnerClass.SecondInnerClassGeneric<Dummy> foo3() {
77         return null;
78     }
79 
80     @ExpectedGenericString("class TestGenericReturnTypeToString$" +
81           "FirstInnerClass$SecondInnerClass")
foo4()82     public FirstInnerClass.SecondInnerClass foo4() {
83         return null;
84     }
85 
86     @ExpectedGenericString(
87           "java.util.List<java.lang.String>")
foo5()88     public java.util.List<java.lang.String> foo5() {
89         return null;
90     }
91 
92     @ExpectedGenericString("interface TestGenericReturnTypeToString$" +
93           "FirstInnerClass$Interface")
foo6()94     public FirstInnerClass.Interface foo6() {
95         return null;
96     }
97 
98     @ExpectedGenericString("TestGenericReturnTypeToString$" +
99           "FirstInnerClass$InterfaceGeneric<Dummy>")
foo7()100     public FirstInnerClass.InterfaceGeneric<Dummy> foo7() {
101         return null;
102     }
103 
104     public static class FirstInnerClass {
105 
106         public class SecondInnerClassGeneric<T> {
107         }
108 
109         public class SecondInnerClass {
110         }
111 
112         interface Interface {
113         }
114 
115         interface InterfaceGeneric<T> {
116         }
117     }
118 
119     public class FirstInnerClassGeneric<T> {
120 
121         public class SecondInnerClassGeneric<T> {
122         }
123 
124         public class SecondInnerClass {
125         }
126     }
127 }
128 
129 @Retention(RetentionPolicy.RUNTIME)
130 @interface ExpectedGenericString {
value()131     String value();
132 }
133 
134 class Dummy {
135 }
136