1 /*
   2  * Copyright (c) 2004, 2018, 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 8161500 8162539 6304578
  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             for(Constructor<?> ctor: clazz.getDeclaredConstructors()) {
  41                 ExpectedGenericString egs = ctor.getAnnotation(ExpectedGenericString.class);
  42                 String actual = ctor.toGenericString();
  43                 System.out.println(actual);
  44                 failures += checkForFailure(egs.value(), actual);
  45 
  46                 if (ctor.isAnnotationPresent(ExpectedString.class)) {
  47                     failures += checkForFailure(ctor.getAnnotation(ExpectedString.class).value(),
  48                                                 ctor.toString());
  49                 }
  50             }
  51 
  52         if (failures > 0) {
  53             System.err.println("Test failed.");
  54             throw new RuntimeException();
  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     }
  66 }
  67 
  68 class TestClass1 {
  69     @ExpectedGenericString(
  70    "TestClass1(int,double)")
  71     TestClass1(int x, double y) {}
  72 
  73     @ExpectedGenericString(
  74    "private TestClass1(int,int)")
  75     private TestClass1(int x, int param2) {}
  76 
  77     @ExpectedGenericString(
  78    "private TestClass1(java.lang.Object) throws java.lang.RuntimeException")
  79     @ExpectedString(
  80    "private TestClass1(java.lang.Object) throws java.lang.RuntimeException")
  81     private TestClass1(Object o) throws RuntimeException {}
  82 
  83     @ExpectedGenericString(
  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")
  87     protected <S, T> TestClass1(S s, T t) throws Exception{}
  88 
  89     @ExpectedGenericString(
  90    "protected <V extends java.lang.Number & java.lang.Runnable> TestClass1(V)")
  91     @ExpectedString(
  92    "protected TestClass1(java.lang.Number)")
  93     protected <V extends Number & Runnable> TestClass1(V v){}
  94 
  95     @ExpectedGenericString(
  96    "<E extends java.lang.Exception> TestClass1() throws E")
  97     @ExpectedString(
  98    "TestClass1() throws java.lang.Exception")
  99     <E extends Exception> TestClass1() throws E {}
 100 
 101     @ExpectedGenericString(
 102    "TestClass1(java.lang.Object...)")
 103     @ExpectedString(
 104    "TestClass1(java.lang.Object[])")
 105     TestClass1(Object... o){}
 106 }
 107 
 108 class TestClass2<E> {
 109     @ExpectedGenericString(
 110    "public <T> TestClass2(E,T)")
 111     public <T> TestClass2(E e, T t) {}
 112 }
 113 
 114 @Retention(RetentionPolicy.RUNTIME)
 115 @interface ExpectedGenericString {
 116     String value();
 117 }
 118 
 119 @Retention(RetentionPolicy.RUNTIME)
 120 @interface ExpectedString {
 121     String value();
 122 }