1 /*
   2  * Copyright (c) 2004, 2013, 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
  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         List<Class<?>> classList = new LinkedList<Class<?>>();
  39         classList.add(TestClass1.class);
  40         classList.add(TestClass2.class);
  41         classList.add(Roebling.class);
  42         classList.add(TestInterface1.class);
  43 
  44 
  45         for(Class<?> clazz: classList)
  46             for(Method method: clazz.getDeclaredMethods()) {
  47                 ExpectedGenericString egs = method.getAnnotation(ExpectedGenericString.class);
  48                 if (egs != null) {
  49                     String actual = method.toGenericString();
  50                     System.out.println(actual);
  51                     if (method.isBridge()) {
  52                         if (! egs.bridgeValue().equals(actual)) {
  53                             failures++;
  54                             System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",
  55                                               egs.value(), actual);
  56                         }
  57                     } else {
  58                         if (! egs.value().equals(actual)) {
  59                             failures++;
  60                             System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",
  61                                               egs.value(), actual);
  62                         }
  63                     }
  64                 }
  65 
  66                 if (method.isAnnotationPresent(ExpectedString.class)) {
  67                     ExpectedString es = method.getAnnotation(ExpectedString.class);
  68                     String actual = method.toString();
  69                     if (! es.value().equals(actual)) {
  70                         failures++;
  71                         System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",
  72                                           es.value(), actual);
  73                     }
  74                 }
  75 
  76             }
  77 
  78         // Bridge Test; no "volatile" methods
  79         for(Method method: Roebling.class.getDeclaredMethods()) {
  80             String s1 = method.toGenericString();
  81             String s2 = method.toString();
  82             System.out.println("Generic: " + s1);
  83             System.out.println("Regular: " + s2);
  84             if (s1.indexOf("volatile") != -1 ||
  85                 s2.indexOf("volatile") != -1) {
  86                 failures++;
  87                 System.err.println("ERROR: Bad string; unexpected  ``volatile''");
  88             }
  89         }
  90 
  91         if (failures > 0) {
  92             System.err.println("Test failed.");
  93             throw new RuntimeException();
  94         }
  95     }
  96 }
  97 
  98 class TestClass1 {
  99     @ExpectedGenericString(
 100    "void TestClass1.method1(int,double)")
 101     void method1(int x, double y) {}
 102 
 103     @ExpectedGenericString(
 104    "private static java.lang.String TestClass1.method2(int,int)")
 105     private static String method2(int x, int param2) {return null;}
 106 
 107     @ExpectedGenericString(
 108    "static void TestClass1.method3() throws java.lang.RuntimeException")
 109     static void method3() throws RuntimeException {return;}
 110 
 111     @ExpectedGenericString(
 112    "protected <S,T> S TestClass1.method4(S,T) throws java.lang.Exception")
 113     protected <S, T> S method4(S s, T t) throws Exception {return null;}
 114 }
 115 
 116 class TestClass2<E, F extends Exception> {
 117     @ExpectedGenericString(
 118    "public <T> T TestClass2.method1(E,T)")
 119     public <T> T method1(E e, T t) {return null;}
 120 
 121     @ExpectedGenericString(
 122    "public void TestClass2.method2() throws F")
 123     public void method2() throws F {return;}
 124 }
 125 
 126 class Roebling implements Comparable<Roebling> {
 127     @ExpectedGenericString(
 128     value="public int Roebling.compareTo(Roebling)",
 129     bridgeValue="public int Roebling.compareTo(java.lang.Object)")
 130     public int compareTo(Roebling r) {
 131         throw new IllegalArgumentException();
 132     }
 133 
 134     // Not a transient method, (transient var-arg overlap)
 135     @ExpectedGenericString(
 136    "void Roebling.varArg(java.lang.Object...)")
 137     @ExpectedString(
 138    "void Roebling.varArg(java.lang.Object[])")
 139     void varArg(Object ... arg) {}
 140 }
 141 
 142 interface TestInterface1 {
 143     @ExpectedGenericString(
 144    "public default void TestInterface1.foo()")
 145     @ExpectedString(
 146    "public default void TestInterface1.foo()")
 147     public default void foo(){;}
 148 
 149     @ExpectedString(
 150    "public default java.lang.Object TestInterface1.bar()")
 151     @ExpectedGenericString(
 152    "public default <A> A TestInterface1.bar()")
 153     default <A> A bar(){return null;}
 154 
 155     @ExpectedString(
 156    "public default strictfp double TestInterface1.quux()")
 157     @ExpectedGenericString(
 158     "public default strictfp double TestInterface1.quux()")
 159     strictfp default double quux(){return 1.0;}
 160 
 161 }
 162 
 163 @Retention(RetentionPolicy.RUNTIME)
 164 @interface ExpectedGenericString {
 165     String value();
 166     String bridgeValue() default "";
 167 }
 168 
 169 @Retention(RetentionPolicy.RUNTIME)
 170 @interface ExpectedString {
 171     String value();
 172 }
 173