jdk/test/java/lang/reflect/Constructor/GenericStringTest.java
author darcy
Tue, 26 Jul 2016 12:56:26 -0700
changeset 39825 6441823cea46
parent 39731 7a4bc90065bd
permissions -rw-r--r--
8162539: Test fails because it expects a blank between method signature and throws exception Reviewed-by: coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
39731
7a4bc90065bd 8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
39825
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    26
 * @bug 5033583 6316717 6470106 8161500 8162539
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check toGenericString() and toString() methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.annotation.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class GenericStringTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static void main(String argv[]) throws Exception{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
39731
7a4bc90065bd 8161500: Use getTypeName and StringJoiner in core reflection toString methods
darcy
parents: 5506
diff changeset
    39
        for(Class<?> clazz: List.of(TestClass1.class, TestClass2.class))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            for(Constructor<?> ctor: clazz.getDeclaredConstructors()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                ExpectedGenericString egs = ctor.getAnnotation(ExpectedGenericString.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                String actual = ctor.toGenericString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                System.out.println(actual);
39825
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    44
                failures += checkForFailure(egs.value(), actual);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                if (ctor.isAnnotationPresent(ExpectedString.class)) {
39825
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    47
                    failures += checkForFailure(ctor.getAnnotation(ExpectedString.class).value(),
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    48
                                                ctor.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            System.err.println("Test failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
39825
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    57
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    58
    private static int checkForFailure(String expected, String actual) {
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    59
        if (!expected.equals(actual)) {
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    60
            System.err.printf("ERROR: Expected ''%s'';%ngot             ''%s''.\n",
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    61
                              expected, actual);
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    62
            return 1;
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    63
        } else
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    64
            return 0;
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    65
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
class TestClass1 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    @ExpectedGenericString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
   "TestClass1(int,double)")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    TestClass1(int x, double y) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    @ExpectedGenericString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
   "private TestClass1(int,int)")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private TestClass1(int x, int param2) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    @ExpectedGenericString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
   "private TestClass1(java.lang.Object) throws java.lang.RuntimeException")
39825
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    79
    @ExpectedString(
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    80
   "private TestClass1(java.lang.Object) throws java.lang.RuntimeException")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private TestClass1(Object o) throws RuntimeException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    @ExpectedGenericString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
   "protected <S,T> TestClass1(S,T) throws java.lang.Exception")
39825
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    85
    @ExpectedString(
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    86
   "protected TestClass1(java.lang.Object,java.lang.Object) throws java.lang.Exception")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    protected <S, T> TestClass1(S s, T t) throws Exception{}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    @ExpectedGenericString(
39825
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    90
   "<E> TestClass1() throws E")
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    91
    @ExpectedString(
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    92
   "TestClass1() throws java.lang.Exception")
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    93
    <E extends Exception> TestClass1() throws E {}
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    94
6441823cea46 8162539: Test fails because it expects a blank between method signature and throws exception
darcy
parents: 39731
diff changeset
    95
    @ExpectedGenericString(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
   "TestClass1(java.lang.Object...)")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    @ExpectedString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
   "TestClass1(java.lang.Object[])")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    TestClass1(Object... o){}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
class TestClass2<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    @ExpectedGenericString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
   "public <T> TestClass2(E,T)")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public <T> TestClass2(E e, T t) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
@Retention(RetentionPolicy.RUNTIME)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
@interface ExpectedGenericString {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    String value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
@Retention(RetentionPolicy.RUNTIME)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
@interface ExpectedString {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    String value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}