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 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 if (! egs.bridgeValue().equals(actual)) { 48 failures++; 49 System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n", 50 egs.value(), actual); 51 } 52 } else { 53 if (! egs.value().equals(actual)) { 54 failures++; 55 System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n", 56 egs.value(), actual); 57 } 58 } 59 } 60 61 if (method.isAnnotationPresent(ExpectedString.class)) { 62 ExpectedString es = method.getAnnotation(ExpectedString.class); 63 String actual = method.toString(); 64 if (! es.value().equals(actual)) { 65 failures++; 66 System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n", 67 es.value(), actual); 68 } 69 } 70 71 } 72 73 // Bridge Test; no "volatile" methods 74 for(Method method: Roebling.class.getDeclaredMethods()) { 75 String s1 = method.toGenericString(); 76 String s2 = method.toString(); 77 System.out.println("Generic: " + s1); 78 System.out.println("Regular: " + s2); 79 if (s1.indexOf("volatile") != -1 || 80 s2.indexOf("volatile") != -1) { 81 failures++; 82 System.err.println("ERROR: Bad string; unexpected ``volatile''"); 83 } 84 } 85 86 if (failures > 0) { 87 System.err.println("Test failed."); 88 throw new RuntimeException(); 89 } 90 } 91 } 92 93 class TestClass1 { 94 @ExpectedGenericString( 95 "void TestClass1.method1(int,double)") 96 void method1(int x, double y) {} 97 98 @ExpectedGenericString( 99 "private static java.lang.String TestClass1.method2(int,int)") 100 private static String method2(int x, int param2) {return null;} 101 102 @ExpectedGenericString( 103 "static void TestClass1.method3() throws java.lang.RuntimeException") 104 static void method3() throws RuntimeException {return;} 105 106 @ExpectedGenericString( 107 "protected <S,T> S TestClass1.method4(S,T) throws java.lang.Exception") 108 protected <S, T> S method4(S s, T t) throws Exception {return null;} 109 } 110 111 class TestClass2<E, F extends Exception> { 112 @ExpectedGenericString( 113 "public <T> T TestClass2.method1(E,T)") 114 public <T> T method1(E e, T t) {return null;} 115 116 @ExpectedGenericString( 117 "public void TestClass2.method2() throws F") 118 public void method2() throws F {return;} 119 120 @ExpectedGenericString( 121 "public E[] TestClass2.method3()") 122 public E[] method3() {return null;} 123 124 @ExpectedGenericString( 125 "public E[][] TestClass2.method4()") 126 public E[][] method4() {return null;} 127 128 @ExpectedGenericString( 129 "public java.util.List<E[]> TestClass2.method5()") 130 public List<E[]> method5() {return null;} 131 132 @ExpectedGenericString( 133 "public java.util.List<?> TestClass2.method6()") 134 public List<?> method6() {return null;} 135 136 @ExpectedGenericString( 137 "public java.util.List<?>[] TestClass2.method7()") | 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 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 108 class 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()") |