Skip to content

Commit d460544

Browse files
committed
Integrated latest changes from my work. Fixed Reflector to use more
sophisticated class loader search to handle some situations, like those in BlueJ userlib jars. Fixed all classes to use Reflector for looking up classes and for setting accessibility on features accessed by reflection--the Reflector implementation is more robust and works in a wider variety of situations.
1 parent 6a9131c commit d460544

9 files changed

Lines changed: 916 additions & 630 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.project
33
bin/
44

5+
/target

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<groupId>net.java</groupId>
1919
<artifactId>quickcheck</artifactId>
2020
<version>0.6</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>javassist</groupId>
24+
<artifactId>javassist</artifactId>
25+
<version>3.12.1.GA</version>
2126
</dependency>
2227
</dependencies>
2328

src/tester/AnnotatedTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,26 @@
77
/**
88
* Ideally, this class will be used to create threaded tests that
99
* can execute sequentially. Unfortunately, the current limitations of
10-
* Cobertura prevent us from running non-synchronous tests.
10+
* Cobertura prevent us from running non-synchronous tests.
1111
* @author Weston Jossey
1212
* @since December 12 2008
1313
* @version 2.0
1414
*/
1515
public class AnnotatedTest implements Runnable{
1616
String name;
1717
Object o = null;
18-
18+
1919
public AnnotatedTest(String name){
2020
this.name = name;
2121
init();
2222
}
23-
23+
2424
private void init() {
25-
Class<?> examples;
26-
Constructor<?> constructor;
2725
try {
28-
examples = Class.forName(name);
29-
constructor = examples.getDeclaredConstructor();
30-
constructor.setAccessible(true);
26+
Class<?> examples = Reflector.classForName(name);
27+
Constructor<?> constructor =
28+
examples.getDeclaredConstructor();
29+
Reflector.ensureIsAccessible(constructor);
3130
o = constructor.newInstance();
3231

3332
System.out.println("Tester Results");
@@ -57,5 +56,5 @@ public void run() {
5756
t.runAnyTests(o);
5857
}
5958
}
60-
59+
6160
}

src/tester/Main.java

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
/**
99
* Copyright 2008, 2009, 2010, 2011 Viera K. Proulx, Matthias Felleisen
10-
* This program is distributed under the terms of the
10+
* This program is distributed under the terms of the
1111
* GNU Lesser General Public License (LGPL)
1212
*/
1313

@@ -22,49 +22,48 @@
2222
* The main method instantiates this class and invokes the
2323
* <CODE>{@link Tester Tester}</CODE> on this new instance.
2424
* </P>
25-
*
25+
*
2626
* @author Viera K. Proulx, Matthias Felleisen, Weston Jossey
27-
* @since 3 March 2008, 16 October 2008, 20 December 2008, 13 May 2009,
27+
* @since 3 March 2008, 16 October 2008, 20 December 2008, 13 May 2009,
2828
* 9 February 2010, 18 February 2011
29-
*
29+
*
3030
*/
3131
public class Main {
3232
/**
3333
* <p>Method that creates an instance of the <code>Tester</code> and an
3434
* instance of the class that defines the tests, invokes the test evaluation
3535
* by the <code>Tester</code>, and reports the results.</p>
36-
* <p>The method scans all classes for <code>@Example</code> annotation
37-
* and looks in those classes for methods annotated with the
36+
* <p>The method scans all classes for <code>@Example</code> annotation
37+
* and looks in those classes for methods annotated with the
3838
* <code>@TestMethod</code> annotation. Tests for each class are evaluated
3939
* and reported separately/consecutively.</p>
40-
* <p>If there are no annotated classes the class <code>Examples</code> or
40+
* <p>If there are no annotated classes the class <code>Examples</code> or
4141
* the class whose name is given as the argument is the only test class.</p>
42-
*
43-
* @param argv
44-
* [optional] the name of the class that defines the tests
45-
* @throws Exception
42+
*
43+
* @param argv [optional] the name of the class that defines the tests
44+
* @throws Exception
4645
*/
4746
public static void main(String argv[]) throws Exception {
4847
//pool = Executors.newCachedThreadPool(); //For concurrency
4948
//Instrumentor inst = new Instrumentor();
50-
//Set<String> instrumentedClasses =
49+
//Set<String> instrumentedClasses =
5150
// new AnnotationScanner(tester.cobertura.Instrument.class).scan();
52-
51+
5352
//if(instrumentedClasses != null){
5453
// inst.instrumentClasses();
5554
//}
5655

5756
boolean noTests = true; // set to false if Annotations found test to run
58-
57+
5958
// set up the scanner to look for '@Example' class annotation
6059
AnnotationScanner scanner = new AnnotationScanner(
6160
tester.Example.class);
6261
Set<String> classes = null;
63-
62+
6463
// Find all classes annotated with '@Example' and in each run all tests
6564
try {
6665
classes = scanner.scan();
67-
66+
6867
// get the name of the class that defines the tests
6968
if(classes != null){
7069
for (String clazz : classes){
@@ -81,7 +80,7 @@ public static void main(String argv[]) throws Exception {
8180
}finally{
8281
//if(instrumentedClasses != null){
8382
// inst.generateReports();
84-
//}
83+
//}
8584
//System.out.println("Done with the annotation scan");
8685
}
8786

@@ -91,7 +90,7 @@ public static void main(String argv[]) throws Exception {
9190
classname = "Examples";
9291
else
9392
classname = argv[0];
94-
93+
9594
// System.out.println("Class name for tester.Main to use is: " + classname);
9695

9796
// to represent an instance of the class that defines the tests
@@ -103,25 +102,25 @@ public static void main(String argv[]) throws Exception {
103102
// Note: if we already ran other tests, do not report errors here
104103
try {
105104
// get the specified 'Examples' class
106-
examples = Class.forName(classname);
105+
examples = Reflector.classForName(classname);
107106

108107
//System.out.println("Tests are defined in the class " + examples.getName());
109-
108+
110109
if (examples == null && noTests)
111110
throw new ClassNotFoundException("could not find the class " + classname);
112111
// else
113112
// System.out.println("found class " + examples.getName());
114113

115114
// create an instance of this class
116-
try {
117-
Constructor<?> construct = examples
118-
.getDeclaredConstructor();
119-
construct.setAccessible(true);
120-
o = construct.newInstance();
115+
try {
116+
Constructor<?> constructor = examples
117+
.getDeclaredConstructor();
118+
Reflector.ensureIsAccessible(constructor);
119+
o = constructor.newInstance();
121120

122121
System.out.println("\n\n---------------------------------\n" + "" +
123122
"Tests for the class: " + classname);
124-
123+
125124
// handle all exceptions when new instance cannot be created
126125
} catch (Throwable exception) {
127126

@@ -134,13 +133,13 @@ public static void main(String argv[]) throws Exception {
134133
excName = exception.getClass().getName();
135134
excMessage = exception.getMessage();
136135
}
137-
136+
138137
if (excName.equals("java.lang.NoSuchMethodException")){
139138
System.out.println("no default constructor: "
140139
+ excMessage);
141140
} else if (excName.equals("java.lang.InvocationTargetException")){
142141
System.out.println(
143-
"Invocation -- Exception thrown by the constructor: \n" +
142+
"Invocation -- Exception thrown by the constructor: \n" +
144143
"Exception: " + excName + "\nMessage: " +
145144
excMessage);
146145
} else if (excName.equals("java.lang.InstantiationException")){
@@ -150,13 +149,13 @@ public static void main(String argv[]) throws Exception {
150149
System.out.println(
151150
"Exception: " + excName + "\nMessage: " +
152151
excMessage);
153-
} else
152+
} else
154153
System.out.println(
155-
"Invocation -- Exception thrown by the constructor: \n" +
154+
"Invocation -- Exception thrown by the constructor: \n" +
156155
"Exception: " + excName + "\nMessage: " +
157-
excMessage);
158-
}
159-
156+
excMessage);
157+
}
158+
160159
// run tests if the instance was successfully constructed
161160
if (o != null) {
162161
Tester t = new Tester();
@@ -173,19 +172,19 @@ public static void main(String argv[]) throws Exception {
173172
"constructor that does not fail during instantiation.");
174173
}
175174
}
176-
175+
177176
// report error if the class with the given classname does not exist
178177
catch (ClassNotFoundException c) {
179178
if (noTests){
180179
if (classname != null)
181-
System.err.println(classname + " class doesn't exist.\n" +
180+
System.err.println(classname + " class doesn't exist.\n" +
182181
"Please check to make sure that the argment that gives the name" +
183182
" of your examples class has the correct spelling.");
184183
else
185184
System.err.println(c.getMessage());
186185
}
187-
}
188-
186+
}
187+
189188
// finish up
190189
finally {
191190
// System.out.println("done");

0 commit comments

Comments
 (0)