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 5003916 6704655
  27  * @summary Testing parsing of signatures attributes of nested classes
  28  * @author Joseph D. Darcy
  29  * @compile -source 1.5 Probe.java
  30  * @run main Probe
  31  */
  32 
  33 import java.lang.reflect.*;
  34 import java.lang.annotation.*;
  35 import java.util.*;
  36 import static java.util.Arrays.*;
  37 
  38 @Classes(value={
  39         "java.util.concurrent.FutureTask",
  40         "java.util.concurrent.ConcurrentHashMap$EntryIterator",
  41         "java.util.concurrent.ConcurrentHashMap$KeyIterator",
  42         "java.util.concurrent.ConcurrentHashMap$ValueIterator",
  43         "java.util.AbstractList$ListItr",
  44         "java.util.EnumMap$EntryIterator",
  45         "java.util.EnumMap$KeyIterator",
  46         "java.util.EnumMap$ValueIterator",
  47         "java.util.IdentityHashMap$EntryIterator",
  48         "java.util.IdentityHashMap$KeyIterator",
  49         "java.util.IdentityHashMap$ValueIterator",
  50         "java.util.WeakHashMap$EntryIterator",
  51         "java.util.WeakHashMap$KeyIterator",
  52         "java.util.WeakHashMap$ValueIterator",
  53         "java.util.TreeMap$EntryIterator",
  54         "java.util.TreeMap$KeyIterator",
  55         "java.util.TreeMap$ValueIterator",
  56         "java.util.HashMap$EntryIterator",
  57         "java.util.HashMap$KeyIterator",
  58         "java.util.HashMap$ValueIterator",
  59         "java.util.LinkedHashMap$EntryIterator",
  60         "java.util.LinkedHashMap$KeyIterator",
  61         "java.util.LinkedHashMap$ValueIterator"
  62         },
  63         sunClasses={
  64         "javax.crypto.SunJCE_c",
  65         "javax.crypto.SunJCE_e",
  66         "javax.crypto.SunJCE_f",
  67         "javax.crypto.SunJCE_j",
  68         "javax.crypto.SunJCE_k",
  69         "javax.crypto.SunJCE_l"
  70         })
  71 public class Probe {
  72     public static void main (String[] args) throws Throwable {
  73         Classes classesAnnotation = (Probe.class).getAnnotation(Classes.class);
  74         List<String> names =
  75             new ArrayList<String>(asList(classesAnnotation.value()));
  76 
  77         if (System.getProperty("java.runtime.name").startsWith("Java(TM)")) {
  78             // Sun production JDK; test crypto classes too
  79             for(String name: classesAnnotation.sunClasses())
  80                 names.add(name);
  81         }
  82 
  83         int errs = 0;
  84         for(String name: names) {
  85             System.out.println("\nCLASS " + name);
  86             Class c = Class.forName(name, false, null);
  87             errs += probe(c);
  88             System.out.println(errs == 0 ? "  ok" : "  ERRORS:" + errs);
  89         }
  90 
  91         if (errs > 0 )
  92             throw new RuntimeException("Errors during probing.");
  93     }
  94 
  95     static int probe (Class c) {
  96         int errs = 0;
  97 
  98         try {
  99             c.getTypeParameters();
 100             c.getGenericSuperclass();
 101             c.getGenericInterfaces();
 102         } catch (Throwable t) {
 103             errs++;
 104             System.err.println(t);
 105         }
 106 
 107         Field[] fields = c.getDeclaredFields();
 108         if (fields != null)
 109             for(Field field: fields) {
 110                 try {
 111                     field.getGenericType();
 112                 } catch (Throwable t) {
 113                     errs++;
 114                     System.err.println("FIELD " + field);
 115                     System.err.println(t);
 116                 }
 117             }
 118 
 119         Method[] methods = c.getDeclaredMethods();
 120         if (methods != null)
 121             for(Method method: methods) {
 122                 try {
 123                     method.getTypeParameters();
 124                     method.getGenericReturnType();
 125                     method.getGenericParameterTypes();
 126                     method.getGenericExceptionTypes();
 127                 } catch (Throwable t) {
 128                     errs++;
 129                     System.err.println("METHOD " + method);
 130                     System.err.println(t);
 131                 }
 132             }
 133 
 134         Constructor[] ctors = c.getDeclaredConstructors();
 135         if (ctors != null)
 136             for(Constructor ctor: ctors) {
 137                 try {
 138                     ctor.getTypeParameters();
 139                     ctor.getGenericParameterTypes();
 140                     ctor.getGenericExceptionTypes();
 141                 } catch (Throwable t) {
 142                     errs++;
 143                     System.err.println("CONSTRUCTOR " + ctor);
 144                     System.err.println(t);
 145                 }
 146             }
 147 
 148         return errs;
 149     }
 150 }
 151 
 152 @Retention(RetentionPolicy.RUNTIME)
 153 @interface Classes {
 154     String [] value(); // list of classes to probe
 155     String [] sunClasses(); // list of Sun-production JDK specific classes to probe
 156 }