1 /* 2 * Copyright (c) 2004, 2018, 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 6304578 27 * @summary Check toGenericString() and toString() methods 28 * @author Joseph D. Darcy 29 */ 30 31 import java.lang.reflect.*; 32 import java.lang.annotation.*; 33 import java.util.*; 34 35 public 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 90 class 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 @ExpectedGenericString( 108 "public static <T> T TestClass1.max(java.util.Collection<? extends T>,java.util.Comparator<? super T>)") 109 public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) {return null;} 110 } 111 112 class TestClass2<E, F extends Exception> { 113 @ExpectedGenericString( 114 "public <T> T TestClass2.method1(E,T)") 115 public <T> T method1(E e, T t) {return null;} 116 117 @ExpectedGenericString( 118 "public void TestClass2.method2() throws F") 119 @ExpectedString( 120 "public void TestClass2.method2() throws java.lang.Exception") 121 public void method2() throws F {return;} 122 123 @ExpectedGenericString( 124 "public E[] TestClass2.method3()") 125 public E[] method3() {return null;} 126 127 @ExpectedGenericString( 128 "public E[][] TestClass2.method4()") 129 public E[][] method4() {return null;} 130 131 @ExpectedGenericString( 132 "public java.util.List<E[]> TestClass2.method5()") 133 public List<E[]> method5() {return null;} 134 135 @ExpectedGenericString( 136 "public java.util.List<?> TestClass2.method6()") 137 public List<?> method6() {return null;} 138 139 @ExpectedGenericString( 140 "public java.util.List<?>[] TestClass2.method7()") 141 public List<?>[] method7() {return null;} 142 143 @ExpectedGenericString( 144 "public <K,V> java.util.Map<K, V> TestClass2.method8()") 145 public <K, V> Map<K, V> method8() {return null;} 146 147 @ExpectedGenericString( 148 "public <V extends java.lang.Number & java.lang.Runnable> java.util.Set<V> TestClass2.method9(V)") 149 public <V extends Number & Runnable> Set<V> method9(V v) {return null;} 150 } 151 152 class Roebling implements Comparable<Roebling> { 153 @ExpectedGenericString( 154 value="public int Roebling.compareTo(Roebling)", 155 bridgeValue="public int Roebling.compareTo(java.lang.Object)") 156 public int compareTo(Roebling r) { 157 throw new IllegalArgumentException(); 158 } 159 160 // Not a transient method, (transient var-arg overlap) 161 @ExpectedGenericString( 162 "void Roebling.varArg(java.lang.Object...)") 163 @ExpectedString( 164 "void Roebling.varArg(java.lang.Object[])") 165 void varArg(Object ... arg) {} 166 } 167 168 interface TestInterface1 { 169 @ExpectedGenericString( 170 "public default void TestInterface1.foo()") 171 @ExpectedString( 172 "public default void TestInterface1.foo()") 173 public default void foo(){;} 174 175 @ExpectedString( 176 "public default java.lang.Object TestInterface1.bar()") 177 @ExpectedGenericString( 178 "public default <A> A TestInterface1.bar()") 179 default <A> A bar(){return null;} 180 181 @ExpectedString( 182 "public default strictfp double TestInterface1.quux()") 183 @ExpectedGenericString( 184 "public default strictfp double TestInterface1.quux()") 185 strictfp default double quux(){return 1.0;} 186 } 187 188 @Retention(RetentionPolicy.RUNTIME) 189 @interface ExpectedGenericString { 190 String value(); 191 String bridgeValue() default ""; 192 } 193 194 @Retention(RetentionPolicy.RUNTIME) 195 @interface ExpectedString { 196 String value(); 197 } 198