|
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 import java.io.*; |
|
25 import java.util.*; |
|
26 import java.lang.reflect.Constructor; |
|
27 |
|
28 /** |
|
29 * Test driver for MethodParameters testing. |
|
30 * <p> |
|
31 * The intended use of this driver is to run it, giving the name of |
|
32 * a single class compiled with -parameters as argument. The driver |
|
33 * will test the specified class, and any nested classes it finds. |
|
34 * <p> |
|
35 * Each class is tested in two way. By refelction, and by directly |
|
36 * checking MethodParameters attributes in the classfile. The checking |
|
37 * is done using two visitor classes {@link ClassFileVisitor} and |
|
38 * {@link ReflectionVisitor}. |
|
39 * <p> |
|
40 * The {@code ReflectionVisitor} test logically belongs with library tests. |
|
41 * we wish to reuse the same test-cases, so both test are committed together, |
|
42 * under langtools. The tests, may be duplicated in the jdk repository. |
|
43 */ |
|
44 public class Tester { |
|
45 |
|
46 final static File classesdir = new File(System.getProperty("test.classes", ".")); |
|
47 |
|
48 /** |
|
49 * The visitor classes that does the actual checking are referenced |
|
50 * statically, to force compilations, without having to reference |
|
51 * them in individual test cases. |
|
52 * <p> |
|
53 * This makes it easy to change the set of visitors, without |
|
54 * complicating the design with dynamic discovery and compilation |
|
55 * of visitor classes. |
|
56 */ |
|
57 static final Class visitors[] = { |
|
58 ClassFileVisitor.class, |
|
59 ReflectionVisitor.class |
|
60 }; |
|
61 |
|
62 /** |
|
63 * Test-driver expect a single classname as argument. |
|
64 */ |
|
65 public static void main(String... args) throws Exception { |
|
66 if (args.length != 1) { |
|
67 throw new Error("A single class name is expected as argument"); |
|
68 } |
|
69 final String pattern = args[0] + ".*\\.class"; |
|
70 File files[] = classesdir.listFiles(new FileFilter() { |
|
71 public boolean accept(File f) { |
|
72 return f.getName().matches(pattern); |
|
73 } |
|
74 }); |
|
75 if (files.length == 0) { |
|
76 File file = new File(classesdir, args[0] + ".class"); |
|
77 throw new Error(file.getPath() + " not found"); |
|
78 } |
|
79 |
|
80 new Tester(args[0], files).run(); |
|
81 } |
|
82 |
|
83 public Tester(String name, File files[]) { |
|
84 this.classname = name; |
|
85 this.files = files; |
|
86 } |
|
87 |
|
88 void run() throws Exception { |
|
89 |
|
90 // Test with each visitor |
|
91 for (Class<Visitor> vclass : visitors) { |
|
92 try { |
|
93 String vname = vclass.getName(); |
|
94 Constructor c = vclass.getConstructor(Tester.class); |
|
95 |
|
96 info("\nRun " + vname + " for " + classname + "\n"); |
|
97 StringBuilder sb = new StringBuilder(); |
|
98 for (File f : files) { |
|
99 String fname = f.getName(); |
|
100 fname = fname.substring(0, fname.length() - 6); |
|
101 Visitor v = (Visitor) c.newInstance(this); |
|
102 try { |
|
103 v.visitClass(fname, f, sb); |
|
104 } catch(Exception e) { |
|
105 error("Uncaught exception in visitClass()"); |
|
106 e.printStackTrace(); |
|
107 } |
|
108 } |
|
109 info(sb.toString()); |
|
110 } catch(ReflectiveOperationException e) { |
|
111 warn("Class " + vclass.getName() + " ignored, not a Visitor"); |
|
112 continue; |
|
113 } |
|
114 } |
|
115 |
|
116 if(0 != warnings) |
|
117 System.err.println("Test generated " + warnings + " warnings"); |
|
118 |
|
119 if(0 != errors) |
|
120 throw new Exception("Tester test failed with " + |
|
121 errors + " errors"); |
|
122 } |
|
123 |
|
124 abstract static class Visitor { |
|
125 Tester tester; |
|
126 File classesdir; |
|
127 |
|
128 public Visitor(Tester tester) { |
|
129 this.tester = tester; |
|
130 } |
|
131 |
|
132 abstract void visitClass(final String classname, final File cfile, |
|
133 final StringBuilder sb) throws Exception; |
|
134 |
|
135 public void error(String msg) { |
|
136 tester.error(msg); |
|
137 } |
|
138 |
|
139 public void warn(String msg) { |
|
140 tester.warn(msg); |
|
141 } |
|
142 } |
|
143 |
|
144 void error(String msg) { |
|
145 System.err.println("Error: " + msg); |
|
146 errors++; |
|
147 } |
|
148 |
|
149 void warn(String msg) { |
|
150 System.err.println("Warning: " + msg); |
|
151 warnings++; |
|
152 } |
|
153 |
|
154 void info(String msg) { |
|
155 System.out.println(msg); |
|
156 } |
|
157 |
|
158 int errors; |
|
159 int warnings; |
|
160 String classname; |
|
161 File files[]; |
|
162 } |