< prev index next >

test/java/lang/Class/GenericStringTest.java

Print this page




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


   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 8161500
  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     public Map<String, Integer>[][] mixed2 = null;
  39 
  40     public static void main(String... args) throws ReflectiveOperationException {
  41         int failures = 0;
  42 
  43         String[][] nested = {{""}};
  44         int[][]    intArray = {{1}};
  45 
  46         failures += checkToGenericString(int.class, "int");
  47         failures += checkToGenericString(void.class, "void");
  48         failures += checkToGenericString(args.getClass(), "java.lang.String[]");
  49         failures += checkToGenericString(nested.getClass(), "java.lang.String[][]");
  50         failures += checkToGenericString(intArray.getClass(), "int[][]");
  51         failures += checkToGenericString(java.util.Map.class, "public abstract interface java.util.Map<K,V>");
  52 
  53         Field f = GenericStringTest.class.getDeclaredField("mixed");
  54         // The expected value includes "<K,V>" rather than
  55         // "<...String,...Integer>" since the Class object rather than
  56         // Type objects is being queried.
  57         failures += checkToGenericString(f.getType(), "java.util.Map<K,V>[]");
  58         f = GenericStringTest.class.getDeclaredField("mixed2");
  59         failures += checkToGenericString(f.getType(), "java.util.Map<K,V>[][]");
  60 
  61         for(Class<?> clazz : List.of(GenericStringTest.class,

  62                                      AnInterface.class,
  63                                      LocalMap.class,
  64                                      AnEnum.class,
  65                                      AnotherEnum.class)) {



  66             failures += checkToGenericString(clazz, clazz.getAnnotation(ExpectedGenericString.class).value());
  67         }
  68 
  69         if (failures > 0) {
  70             throw new RuntimeException();
  71         }
  72     }
  73 
  74     private static int checkToGenericString(Class<?> clazz, String expected) {
  75         String genericString = clazz.toGenericString();
  76         if (!genericString.equals(expected)) {
  77             System.err.printf("Unexpected Class.toGenericString output; expected '%s', got '%s'.%n",
  78                               expected,
  79                               genericString);
  80             return 1;
  81         } else
  82             return 0;
  83     }
  84 }
  85 
< prev index next >