jdk/test/java/lang/reflect/Constructor/GenericStringTest.java
changeset 39825 6441823cea46
parent 39731 7a4bc90065bd
equal deleted inserted replaced
39824:629cd556bb2e 39825:6441823cea46
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 5033583 6316717 6470106 8161500
    26  * @bug 5033583 6316717 6470106 8161500 8162539
    27  * @summary Check toGenericString() and toString() methods
    27  * @summary Check toGenericString() and toString() methods
    28  * @author Joseph D. Darcy
    28  * @author Joseph D. Darcy
    29  */
    29  */
    30 
    30 
    31 import java.lang.reflect.*;
    31 import java.lang.reflect.*;
    39         for(Class<?> clazz: List.of(TestClass1.class, TestClass2.class))
    39         for(Class<?> clazz: List.of(TestClass1.class, TestClass2.class))
    40             for(Constructor<?> ctor: clazz.getDeclaredConstructors()) {
    40             for(Constructor<?> ctor: clazz.getDeclaredConstructors()) {
    41                 ExpectedGenericString egs = ctor.getAnnotation(ExpectedGenericString.class);
    41                 ExpectedGenericString egs = ctor.getAnnotation(ExpectedGenericString.class);
    42                 String actual = ctor.toGenericString();
    42                 String actual = ctor.toGenericString();
    43                 System.out.println(actual);
    43                 System.out.println(actual);
    44                 if (! egs.value().equals(actual)) {
    44                 failures += checkForFailure(egs.value(), actual);
    45                     failures++;
       
    46                     System.err.printf("ERROR: Expected generic string ''%s''; got ''%s''.\n",
       
    47                                       egs.value(), actual);
       
    48                 }
       
    49 
    45 
    50                 if (ctor.isAnnotationPresent(ExpectedString.class)) {
    46                 if (ctor.isAnnotationPresent(ExpectedString.class)) {
    51                     ExpectedString es = ctor.getAnnotation(ExpectedString.class);
    47                     failures += checkForFailure(ctor.getAnnotation(ExpectedString.class).value(),
    52                     String result = ctor.toString();
    48                                                 ctor.toString());
    53                     if (! es.value().equals(result)) {
       
    54                         failures++;
       
    55                         System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",
       
    56                                           es.value(), result);
       
    57                     }
       
    58                 }
    49                 }
    59             }
    50             }
    60 
    51 
    61         if (failures > 0) {
    52         if (failures > 0) {
    62             System.err.println("Test failed.");
    53             System.err.println("Test failed.");
    63             throw new RuntimeException();
    54             throw new RuntimeException();
    64         }
    55         }
       
    56     }
       
    57 
       
    58     private static int checkForFailure(String expected, String actual) {
       
    59         if (!expected.equals(actual)) {
       
    60             System.err.printf("ERROR: Expected ''%s'';%ngot             ''%s''.\n",
       
    61                               expected, actual);
       
    62             return 1;
       
    63         } else
       
    64             return 0;
    65     }
    65     }
    66 }
    66 }
    67 
    67 
    68 class TestClass1 {
    68 class TestClass1 {
    69     @ExpectedGenericString(
    69     @ExpectedGenericString(
    74    "private TestClass1(int,int)")
    74    "private TestClass1(int,int)")
    75     private TestClass1(int x, int param2) {}
    75     private TestClass1(int x, int param2) {}
    76 
    76 
    77     @ExpectedGenericString(
    77     @ExpectedGenericString(
    78    "private TestClass1(java.lang.Object) throws java.lang.RuntimeException")
    78    "private TestClass1(java.lang.Object) throws java.lang.RuntimeException")
       
    79     @ExpectedString(
       
    80    "private TestClass1(java.lang.Object) throws java.lang.RuntimeException")
    79     private TestClass1(Object o) throws RuntimeException {}
    81     private TestClass1(Object o) throws RuntimeException {}
    80 
    82 
    81     @ExpectedGenericString(
    83     @ExpectedGenericString(
    82    "protected <S,T> TestClass1(S,T) throws java.lang.Exception")
    84    "protected <S,T> TestClass1(S,T) throws java.lang.Exception")
       
    85     @ExpectedString(
       
    86    "protected TestClass1(java.lang.Object,java.lang.Object) throws java.lang.Exception")
    83     protected <S, T> TestClass1(S s, T t) throws Exception{}
    87     protected <S, T> TestClass1(S s, T t) throws Exception{}
       
    88 
       
    89     @ExpectedGenericString(
       
    90    "<E> TestClass1() throws E")
       
    91     @ExpectedString(
       
    92    "TestClass1() throws java.lang.Exception")
       
    93     <E extends Exception> TestClass1() throws E {}
    84 
    94 
    85     @ExpectedGenericString(
    95     @ExpectedGenericString(
    86    "TestClass1(java.lang.Object...)")
    96    "TestClass1(java.lang.Object...)")
    87     @ExpectedString(
    97     @ExpectedString(
    88    "TestClass1(java.lang.Object[])")
    98    "TestClass1(java.lang.Object[])")