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 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             for(Constructor<?> ctor: clazz.getDeclaredConstructors()) {
  41                 ExpectedGenericString egs = ctor.getAnnotation(ExpectedGenericString.class);
  42                 String actual = ctor.toGenericString();
  43                 System.out.println(actual);
  44                 if (! egs.value().equals(actual)) {
  45                     failures++;
  46                     System.err.printf("ERROR: Expected generic string ''%s''; got ''%s''.\n",
  47                                       egs.value(), actual);
  48                 }
  49 
  50                 if (ctor.isAnnotationPresent(ExpectedString.class)) {
  51                     ExpectedString es = ctor.getAnnotation(ExpectedString.class);
  52                     String result = 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                 }
  59             }
  60 
  61         if (failures > 0) {
  62             System.err.println("Test failed.");
  63             throw new RuntimeException();
  64         }
  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     private TestClass1(Object o) throws RuntimeException {}
  80 
  81     @ExpectedGenericString(
  82    "protected <S,T> TestClass1(S,T) throws java.lang.Exception")
  83     protected <S, T> TestClass1(S s, T t) throws Exception{}
  84 
  85     @ExpectedGenericString(
  86    "TestClass1(java.lang.Object...)")
  87     @ExpectedString(
  88    "TestClass1(java.lang.Object[])")
  89     TestClass1(Object... o){}
  90 }
  91 
  92 class TestClass2<E> {
  93     @ExpectedGenericString(
  94    "public <T> TestClass2(E,T)")
  95     public <T> TestClass2(E e, T t) {}
  96 }
  97 
  98 @Retention(RetentionPolicy.RUNTIME)
  99 @interface ExpectedGenericString {
 100     String value();
 101 }
 102 
 103 @Retention(RetentionPolicy.RUNTIME)
 104 @interface ExpectedString {
 105     String value();
 106 }
--- EOF ---