1 /*
   2  * Copyright 2004-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 5033583 6316717 6470106
  27  * @summary Check toGenericString() and toString() methods
  28  * @author Joseph D. Darcy
  29  * @compile -source 1.5 GenericStringTest.java
  30  * @run main GenericStringTest
  31  */
  32 
  33 import java.lang.reflect.*;
  34 import java.lang.annotation.*;
  35 import java.util.*;
  36 
  37 public class GenericStringTest {
  38     public static void main(String argv[]) throws Exception {
  39         int failures = 0;
  40         List<Class<?>> classList = new LinkedList<Class<?>>();
  41         classList.add(TestClass1.class);
  42         classList.add(TestClass2.class);
  43         classList.add(Roebling.class);
  44 
  45 
  46         for(Class<?> clazz: classList)
  47             for(Method method: clazz.getDeclaredMethods()) {
  48                 ExpectedGenericString egs = method.getAnnotation(ExpectedGenericString.class);
  49                 if (egs != null) {
  50                     String actual = method.toGenericString();
  51                     System.out.println(actual);
  52                     if (! egs.value().equals(actual)) {
  53                         failures++;
  54                         System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",
  55                                           egs.value(), actual);
  56                     }
  57                 }
  58 
  59                 if (method.isAnnotationPresent(ExpectedString.class)) {
  60                     ExpectedString es = method.getAnnotation(ExpectedString.class);
  61                     String actual = method.toString();
  62                     if (! es.value().equals(actual)) {
  63                         failures++;
  64                         System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",
  65                                           es.value(), actual);
  66                     }
  67                 }
  68 
  69             }
  70 
  71         // Bridge Test; no "volatile" methods
  72         for(Method method: Roebling.class.getDeclaredMethods()) {
  73             String s1 = method.toGenericString();
  74             String s2 = method.toString();
  75             System.out.println("Generic: " + s1);
  76             System.out.println("Regular: " + s2);
  77             if (s1.indexOf("volatile") != -1 ||
  78                 s2.indexOf("volatile") != -1) {
  79                 failures++;
  80                 System.err.println("ERROR: Bad string; unexpected  ``volatile''");
  81             }
  82         }
  83 
  84         if (failures > 0) {
  85             System.err.println("Test failed.");
  86             throw new RuntimeException();
  87         }
  88     }
  89 }
  90 
  91 class TestClass1 {
  92     @ExpectedGenericString(
  93    "void TestClass1.method1(int,double)")
  94     void method1(int x, double y) {}
  95 
  96     @ExpectedGenericString(
  97    "private static java.lang.String TestClass1.method2(int,int)")
  98     private static String method2(int x, int param2) {return null;}
  99 
 100     @ExpectedGenericString(
 101    "static void TestClass1.method3() throws java.lang.RuntimeException")
 102     static void method3() throws RuntimeException {return;}
 103 
 104     @ExpectedGenericString(
 105    "protected <S,T> S TestClass1.method4(S,T) throws java.lang.Exception")
 106     protected <S, T> S method4(S s, T t) throws Exception {return null;}
 107 }
 108 
 109 class TestClass2<E, F extends Exception> {
 110     @ExpectedGenericString(
 111    "public <T> T TestClass2.method1(E,T)")
 112     public <T> T method1(E e, T t) {return null;}
 113 
 114     @ExpectedGenericString(
 115    "public void TestClass2.method2() throws F")
 116     public void method2() throws F {return;}
 117 }
 118 
 119 class Roebling implements Comparable<Roebling> {
 120     @ExpectedGenericString(
 121    "public int Roebling.compareTo(Roebling)")
 122     public int compareTo(Roebling r) {
 123         throw new IllegalArgumentException();
 124     }
 125 
 126     // Not a transient method, (transient var-arg overlap)
 127     @ExpectedGenericString(
 128    "void Roebling.varArg(java.lang.Object...)")
 129     @ExpectedString(
 130    "void Roebling.varArg(java.lang.Object[])")
 131     void varArg(Object ... arg) {}
 132 }
 133 
 134 @Retention(RetentionPolicy.RUNTIME)
 135 @interface ExpectedGenericString {
 136     String value();
 137 }
 138 
 139 @Retention(RetentionPolicy.RUNTIME)
 140 @interface ExpectedString {
 141     String value();
 142 }