< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2004, 2013, 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
  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 (method.isBridge()) {
  52                         if (! egs.bridgeValue().equals(actual)) {
  53                             failures++;
  54                             System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",
  55                                               egs.value(), actual);
  56                         }
  57                     } else {
  58                         if (! egs.value().equals(actual)) {
  59                             failures++;
  60                             System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",
  61                                               egs.value(), actual);
  62                         }
  63                     }
  64                 }
  65 


 104    "private static java.lang.String TestClass1.method2(int,int)")
 105     private static String method2(int x, int param2) {return null;}
 106 
 107     @ExpectedGenericString(
 108    "static void TestClass1.method3() throws java.lang.RuntimeException")
 109     static void method3() throws RuntimeException {return;}
 110 
 111     @ExpectedGenericString(
 112    "protected <S,T> S TestClass1.method4(S,T) throws java.lang.Exception")
 113     protected <S, T> S method4(S s, T t) throws Exception {return null;}
 114 }
 115 
 116 class TestClass2<E, F extends Exception> {
 117     @ExpectedGenericString(
 118    "public <T> T TestClass2.method1(E,T)")
 119     public <T> T method1(E e, T t) {return null;}
 120 
 121     @ExpectedGenericString(
 122    "public void TestClass2.method2() throws F")
 123     public void method2() throws F {return;}
























 124 }
 125 
 126 class Roebling implements Comparable<Roebling> {
 127     @ExpectedGenericString(
 128     value="public int Roebling.compareTo(Roebling)",
 129     bridgeValue="public int Roebling.compareTo(java.lang.Object)")
 130     public int compareTo(Roebling r) {
 131         throw new IllegalArgumentException();
 132     }
 133 
 134     // Not a transient method, (transient var-arg overlap)
 135     @ExpectedGenericString(
 136    "void Roebling.varArg(java.lang.Object...)")
 137     @ExpectedString(
 138    "void Roebling.varArg(java.lang.Object[])")
 139     void varArg(Object ... arg) {}
 140 }
 141 
 142 interface TestInterface1 {
 143     @ExpectedGenericString(
 144    "public default void TestInterface1.foo()")
 145     @ExpectedString(
 146    "public default void TestInterface1.foo()")
 147     public default void foo(){;}
 148 
 149     @ExpectedString(
 150    "public default java.lang.Object TestInterface1.bar()")
 151     @ExpectedGenericString(
 152    "public default <A> A TestInterface1.bar()")
 153     default <A> A bar(){return null;}
 154 
 155     @ExpectedString(
 156    "public default strictfp double TestInterface1.quux()")
 157     @ExpectedGenericString(
 158     "public default strictfp double TestInterface1.quux()")
 159     strictfp default double quux(){return 1.0;}
 160 
 161 }
 162 
 163 @Retention(RetentionPolicy.RUNTIME)
 164 @interface ExpectedGenericString {
 165     String value();
 166     String bridgeValue() default "";
 167 }
 168 
 169 @Retention(RetentionPolicy.RUNTIME)
 170 @interface ExpectedString {
 171     String value();
 172 }
 173 
   1 /*
   2  * Copyright (c) 2004, 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.
   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 


  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()")
 138     public List<?>[] method7() {return null;}
 139 
 140     @ExpectedGenericString(
 141    "public <K,V> java.util.Map<K, V> TestClass2.method8()")
 142     public <K, V> Map<K, V> method8() {return null;}
 143 }
 144 
 145 class Roebling implements Comparable<Roebling> {
 146     @ExpectedGenericString(
 147     value="public int Roebling.compareTo(Roebling)",
 148     bridgeValue="public int Roebling.compareTo(java.lang.Object)")
 149     public int compareTo(Roebling r) {
 150         throw new IllegalArgumentException();
 151     }
 152 
 153     // Not a transient method, (transient var-arg overlap)
 154     @ExpectedGenericString(
 155    "void Roebling.varArg(java.lang.Object...)")
 156     @ExpectedString(
 157    "void Roebling.varArg(java.lang.Object[])")
 158     void varArg(Object ... arg) {}
 159 }
 160 
 161 interface TestInterface1 {
 162     @ExpectedGenericString(
 163    "public default void TestInterface1.foo()")
 164     @ExpectedString(
 165    "public default void TestInterface1.foo()")
 166     public default void foo(){;}
 167 
 168     @ExpectedString(
 169    "public default java.lang.Object TestInterface1.bar()")
 170     @ExpectedGenericString(
 171    "public default <A> A TestInterface1.bar()")
 172     default <A> A bar(){return null;}
 173 
 174     @ExpectedString(
 175    "public default strictfp double TestInterface1.quux()")
 176     @ExpectedGenericString(
 177     "public default strictfp double TestInterface1.quux()")
 178     strictfp default double quux(){return 1.0;}

 179 }
 180 
 181 @Retention(RetentionPolicy.RUNTIME)
 182 @interface ExpectedGenericString {
 183     String value();
 184     String bridgeValue() default "";
 185 }
 186 
 187 @Retention(RetentionPolicy.RUNTIME)
 188 @interface ExpectedString {
 189     String value();
 190 }
 191 
< prev index next >