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 
  61                 if (method.isAnnotationPresent(ExpectedString.class)) {
  62                     ExpectedString es = method.getAnnotation(ExpectedString.class);
  63                     String actual = method.toString();
  64                     if (! es.value().equals(actual)) {
  65                         failures++;
  66                         System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",
  67                                           es.value(), actual);
  68                     }
  69                 }
  70 
  71             }
  72 
  73         // Bridge Test; no "volatile" methods
  74         for(Method method: Roebling.class.getDeclaredMethods()) {
  75             String s1 = method.toGenericString();
  76             String s2 = method.toString();
  77             System.out.println("Generic: " + s1);
  78             System.out.println("Regular: " + s2);
  79             if (s1.indexOf("volatile") != -1 ||
  80                 s2.indexOf("volatile") != -1) {
  81                 failures++;
  82                 System.err.println("ERROR: Bad string; unexpected  ``volatile''");
  83             }
  84         }
  85 
  86         if (failures > 0) {
  87             System.err.println("Test failed.");
  88             throw new RuntimeException();
  89         }
  90     }
  91 }
  92 
  93 class TestClass1 {
  94     @ExpectedGenericString(
  95    "void TestClass1.method1(int,double)")
  96     void method1(int x, double y) {}
  97 
  98     @ExpectedGenericString(
  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