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