1 /*
   2  * Copyright (c) 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 6298888 6992705
  27  * @summary Check Class.toGenericString()
  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import java.lang.reflect.*;
  32 import java.lang.annotation.*;
  33 import java.util.*;
  34 
  35 @ExpectedGenericString("public class GenericStringTest")
  36 public class GenericStringTest {
  37     public static void main(String... args){
  38         int failures = 0;
  39 
  40         failures += checkToGenericString(int.class, "int");
  41 
  42         Class<?>[] types = {
  43             GenericStringTest.class,
  44             AnInterface.class,
  45             LocalMap.class,
  46             AnEnum.class,
  47             AnotherEnum.class,
  48         };
  49 
  50         for(Class<?> clazz : types) {
  51             failures += checkToGenericString(clazz, clazz.getAnnotation(ExpectedGenericString.class).value());
  52         }
  53 
  54         if (failures > 0) {
  55             throw new RuntimeException();
  56         }
  57     }
  58 
  59     private static int checkToGenericString(Class<?> clazz, String expected) {
  60         String genericString = clazz.toGenericString();
  61         if (!genericString.equals(expected)) {
  62             System.err.printf("Unexpected Class.toGenericString output; expected '%s', got '%s'.%n",
  63                               expected,
  64                               genericString);   
  65             return 1;
  66         } else
  67             return 0;
  68     }
  69 }
  70 
  71 @Retention(RetentionPolicy.RUNTIME)
  72 @interface ExpectedGenericString {
  73     String value();
  74 }
  75 
  76 @ExpectedGenericString("abstract interface AnInterface")
  77 strictfp interface AnInterface {}
  78 
  79 @ExpectedGenericString("abstract interface LocalMap<K,V>")
  80 interface LocalMap<K,V> {}
  81 
  82 @ExpectedGenericString("final enum AnEnum")
  83 enum AnEnum {
  84     FOO;
  85 }
  86 
  87 @ExpectedGenericString("enum AnotherEnum")
  88 enum AnotherEnum {
  89     BAR{};
  90 }