test/java/lang/reflect/Method/GenericStringTest.java

Print this page




   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
  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++;


 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 }


   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 
  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         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++;


 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 
 118 class 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 
 133 interface 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 
 147 @Retention(RetentionPolicy.RUNTIME)
 148 @interface ExpectedGenericString {
 149     String value();
 150 }
 151 
 152 @Retention(RetentionPolicy.RUNTIME)
 153 @interface ExpectedString {
 154     String value();
 155 }