1 /* 2 * Copyright 2004-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 */ 23 24 /* 25 * @test 26 * @bug 5033583 6316717 6470106 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 List<Class<?>> classList = new LinkedList<Class<?>>(); 39 classList.add(TestClass1.class); 40 classList.add(TestClass2.class); 41 classList.add(Roebling.class); 42 43 44 for(Class<?> clazz: classList) 45 for(Method method: clazz.getDeclaredMethods()) { 46 ExpectedGenericString egs = method.getAnnotation(ExpectedGenericString.class); 47 if (egs != null) { 48 String actual = method.toGenericString(); 49 System.out.println(actual); 50 if (! egs.value().equals(actual)) { 51 failures++; 52 System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n", 53 egs.value(), actual); 54 } 55 } 56 57 if (method.isAnnotationPresent(ExpectedString.class)) { 58 ExpectedString es = method.getAnnotation(ExpectedString.class); 59 String actual = method.toString(); 60 if (! es.value().equals(actual)) { 61 failures++; 62 System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n", 63 es.value(), actual); 64 } 65 } 66 67 } 68 69 // Bridge Test; no "volatile" methods 70 for(Method method: Roebling.class.getDeclaredMethods()) { 71 String s1 = method.toGenericString(); 72 String s2 = method.toString(); 73 System.out.println("Generic: " + s1); 74 System.out.println("Regular: " + s2); 75 if (s1.indexOf("volatile") != -1 || 76 s2.indexOf("volatile") != -1) { 77 failures++; 78 System.err.println("ERROR: Bad string; unexpected ``volatile''"); 79 } 80 } 81 82 if (failures > 0) { 83 System.err.println("Test failed."); 84 throw new RuntimeException(); 85 } 86 } 87 } 88 89 class TestClass1 { 90 @ExpectedGenericString( 91 "void TestClass1.method1(int,double)") 92 void method1(int x, double y) {} 93 94 @ExpectedGenericString( 95 "private static java.lang.String TestClass1.method2(int,int)") 96 private static String method2(int x, int param2) {return null;} 97 98 @ExpectedGenericString( 99 "static void TestClass1.method3() throws java.lang.RuntimeException") 100 static void method3() throws RuntimeException {return;} 101 102 @ExpectedGenericString( 103 "protected <S,T> S TestClass1.method4(S,T) throws java.lang.Exception") 104 protected <S, T> S method4(S s, T t) throws Exception {return null;} 105 } 106 107 class TestClass2<E, F extends Exception> { 108 @ExpectedGenericString( 109 "public <T> T TestClass2.method1(E,T)") 110 public <T> T method1(E e, T t) {return null;} 111 112 @ExpectedGenericString( 113 "public void TestClass2.method2() throws F") 114 public void method2() throws F {return;} 115 } 116 117 class Roebling implements Comparable<Roebling> { 118 @ExpectedGenericString( 119 "public int Roebling.compareTo(Roebling)") 120 public int compareTo(Roebling r) { 121 throw new IllegalArgumentException(); 122 } 123 124 // Not a transient method, (transient var-arg overlap) 125 @ExpectedGenericString( 126 "void Roebling.varArg(java.lang.Object...)") 127 @ExpectedString( 128 "void Roebling.varArg(java.lang.Object[])") 129 void varArg(Object ... arg) {} 130 } 131 132 @Retention(RetentionPolicy.RUNTIME) 133 @interface ExpectedGenericString { 134 String value(); 135 } 136 137 @Retention(RetentionPolicy.RUNTIME) 138 @interface ExpectedString { 139 String value(); 140 }