1 /*
   2  * Copyright (c) 2013, 2015, 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 Map<String, Integer>[] mixed = null;
  38 
  39     public static void main(String... args) throws ReflectiveOperationException {
  40         int failures = 0;
  41 
  42         String[][] nested = {{""}};
  43         int[][]    intArray = {{1}};
  44 
  45         failures += checkToGenericString(int.class, "int");
  46         failures += checkToGenericString(void.class, "void");
  47         failures += checkToGenericString(args.getClass(), "java.lang.String[]");
  48         failures += checkToGenericString(nested.getClass(), "java.lang.String[][]");
  49         failures += checkToGenericString(intArray.getClass(), "int[][]");
  50         failures += checkToGenericString(java.util.Map.class, "public abstract interface java.util.Map<K,V>");
  51 
  52         Field f = GenericStringTest.class.getDeclaredField("mixed");
  53         // The expected value includes "<K,V>" rather than
  54         // "<...String,...Integer>" since the Class object rather than
  55         // Type objects is being queried.
  56         failures += checkToGenericString(f.getType(), "java.util.Map<K,V>[]");
  57 
  58         Class<?>[] types = {
  59             GenericStringTest.class,
  60             AnInterface.class,
  61             LocalMap.class,
  62             AnEnum.class,
  63             AnotherEnum.class,
  64         };
  65 
  66         for(Class<?> clazz : types) {
  67             failures += checkToGenericString(clazz, clazz.getAnnotation(ExpectedGenericString.class).value());
  68         }
  69 
  70         if (failures > 0) {
  71             throw new RuntimeException();
  72         }
  73     }
  74 
  75     private static int checkToGenericString(Class<?> clazz, String expected) {
  76         String genericString = clazz.toGenericString();
  77         if (!genericString.equals(expected)) {
  78             System.err.printf("Unexpected Class.toGenericString output; expected '%s', got '%s'.%n",
  79                               expected,
  80                               genericString);
  81             return 1;
  82         } else
  83             return 0;
  84     }
  85 }
  86 
  87 @Retention(RetentionPolicy.RUNTIME)
  88 @interface ExpectedGenericString {
  89     String value();
  90 }
  91 
  92 @ExpectedGenericString("abstract interface AnInterface")
  93 strictfp interface AnInterface {}
  94 
  95 @ExpectedGenericString("abstract interface LocalMap<K,V>")
  96 interface LocalMap<K,V> {}
  97 
  98 @ExpectedGenericString("final enum AnEnum")
  99 enum AnEnum {
 100     FOO;
 101 }
 102 
 103 @ExpectedGenericString("enum AnotherEnum")
 104 enum AnotherEnum {
 105     BAR{};
 106 }