Skip to content

Commit 7f9f9a0

Browse files
author
dlsmith
committed
PLT Utilities: Fixed bug in PathClassLoader (was unable to locate classes with dots in their names); improved tests to catch the bug.
git-svn-id: file:///tmp/test-svn/trunk@4280 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent c600519 commit 7f9f9a0

File tree

18 files changed

+62
-47
lines changed

18 files changed

+62
-47
lines changed

plt/src/edu/rice/cs/plt/reflect/PathClassLoader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISI
4848
import edu.rice.cs.plt.io.IOUtil;
4949

5050
import static edu.rice.cs.plt.debug.DebugUtil.error;
51+
import static edu.rice.cs.plt.debug.DebugUtil.debug;
5152

5253
/**
5354
* A class loader that mimics the standard application system loader by loading classes from
@@ -90,7 +91,7 @@ public PathClassLoader(ClassLoader parent, Iterable<? extends File> path) {
9091
}
9192

9293
protected Class<?> findClass(String name) throws ClassNotFoundException {
93-
URL resource = findResource(name + ".class");
94+
URL resource = findResource(name.replace('.', '/') + ".class");
9495
if (resource == null) { throw new ClassNotFoundException(); }
9596
else {
9697
try {
@@ -103,6 +104,7 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
103104

104105
protected URL findResource(String name) {
105106
for (File f : _path) {
107+
//debug.logValues(new String[]{"searching for resource","in file"}, name, _path);
106108
try {
107109
// We use URLClassLoader to find the resource, not because we care about
108110
// most of that class's functionality, but because it implements the core

plt/src/edu/rice/cs/plt/reflect/PathClassLoaderTest.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,25 @@ public class PathClassLoaderTest extends ClassLoaderTestCase {
4848

4949
public void testLoadsPath() throws Exception {
5050
PathClassLoader l = new PathClassLoader(BASE_LOADER, INTBOX_DIR, A_DIR, B_DIR, C_DIR, D_DIR);
51-
assertLoadsClassAsLoader(l, "IntBox");
52-
assertLoadsClassAsLoader(l, "A");
53-
assertLoadsClassAsLoader(l, "B");
54-
assertLoadsClassAsLoader(l, "C");
51+
assertLoadsClassAsLoader(l, "pkg.IntBox");
52+
assertLoadsClassAsLoader(l, "pkg.A");
53+
assertLoadsClassAsLoader(l, "bpkg.B");
54+
assertLoadsClassAsLoader(l, "pkg.C");
5555
assertLoadsClassAsLoader(l, "D");
5656
assertLoadsSameClass(BASE_LOADER, l, "edu.rice.cs.plt.reflect.PathClassLoaderTest");
57-
assertCanGet(l, "A", 1);
57+
assertCanGet(l, "pkg.A", 1);
5858
assertCanGet(l, "D", 4);
5959
}
6060

6161
public void testLoadsJumbledPath() throws Exception {
6262
PathClassLoader l = new PathClassLoader(BASE_LOADER, D_DIR, INTBOX_DIR, B_DIR, C_DIR, A_DIR);
63-
assertLoadsClassAsLoader(l, "IntBox");
64-
assertLoadsClassAsLoader(l, "A");
65-
assertLoadsClassAsLoader(l, "B");
66-
assertLoadsClassAsLoader(l, "C");
63+
assertLoadsClassAsLoader(l, "pkg.IntBox");
64+
assertLoadsClassAsLoader(l, "pkg.A");
65+
assertLoadsClassAsLoader(l, "bpkg.B");
66+
assertLoadsClassAsLoader(l, "pkg.C");
6767
assertLoadsClassAsLoader(l, "D");
6868
assertLoadsSameClass(BASE_LOADER, l, "edu.rice.cs.plt.reflect.PathClassLoaderTest");
69-
assertCanGet(l, "A", 1);
69+
assertCanGet(l, "pkg.A", 1);
7070
assertCanGet(l, "D", 4);
7171
}
7272

@@ -77,37 +77,37 @@ public void testNestedLoaders() throws Exception {
7777
PathClassLoader lC = new PathClassLoader(lB, C_DIR);
7878
PathClassLoader lD = new PathClassLoader(lC, D_DIR);
7979

80-
assertLoadsClassAsLoader(l, "IntBox");
81-
assertDoesNotLoadClass(l, "A");
82-
assertDoesNotLoadClass(l, "B");
83-
assertDoesNotLoadClass(l, "C");
80+
assertLoadsClassAsLoader(l, "pkg.IntBox");
81+
assertDoesNotLoadClass(l, "pkg.A");
82+
assertDoesNotLoadClass(l, "bpkg.B");
83+
assertDoesNotLoadClass(l, "pkg.C");
8484
assertDoesNotLoadClass(l, "D");
8585

86-
assertLoadsSameClass(l, lA, "IntBox");
87-
assertLoadsClassAsLoader(lA, "A");
88-
assertDoesNotLoadClass(lA, "B");
89-
assertDoesNotLoadClass(lA, "C");
86+
assertLoadsSameClass(l, lA, "pkg.IntBox");
87+
assertLoadsClassAsLoader(lA, "pkg.A");
88+
assertDoesNotLoadClass(lA, "bpkg.B");
89+
assertDoesNotLoadClass(lA, "pkg.C");
9090
assertDoesNotLoadClass(lA, "D");
9191

92-
assertLoadsSameClass(l, lB, "IntBox");
93-
assertLoadsSameClass(lA, lB, "A");
94-
assertLoadsClassAsLoader(lB, "B");
95-
assertDoesNotLoadClass(lB, "C");
92+
assertLoadsSameClass(l, lB, "pkg.IntBox");
93+
assertLoadsSameClass(lA, lB, "pkg.A");
94+
assertLoadsClassAsLoader(lB, "bpkg.B");
95+
assertDoesNotLoadClass(lB, "pkg.C");
9696
assertDoesNotLoadClass(lB, "D");
9797

98-
assertLoadsSameClass(l, lC, "IntBox");
99-
assertLoadsSameClass(lA, lC, "A");
100-
assertLoadsSameClass(lB, lC, "B");
101-
assertLoadsClassAsLoader(lC, "C");
98+
assertLoadsSameClass(l, lC, "pkg.IntBox");
99+
assertLoadsSameClass(lA, lC, "pkg.A");
100+
assertLoadsSameClass(lB, lC, "bpkg.B");
101+
assertLoadsClassAsLoader(lC, "pkg.C");
102102
assertDoesNotLoadClass(lC, "D");
103103

104-
assertLoadsSameClass(l, lD, "IntBox");
105-
assertLoadsSameClass(lA, lD, "A");
106-
assertLoadsSameClass(lB, lD, "B");
107-
assertLoadsSameClass(lC, lD, "C");
104+
assertLoadsSameClass(l, lD, "pkg.IntBox");
105+
assertLoadsSameClass(lA, lD, "pkg.A");
106+
assertLoadsSameClass(lB, lD, "bpkg.B");
107+
assertLoadsSameClass(lC, lD, "pkg.C");
108108
assertLoadsClassAsLoader(lD, "D");
109109

110-
assertCanGet(lD, "A", 1);
110+
assertCanGet(lD, "pkg.A", 1);
111111
assertCanGet(lD, "D", 4);
112112
}
113113

@@ -117,7 +117,7 @@ public void testPoorlyNestedLoaders() throws Exception {
117117
PathClassLoader lD = new PathClassLoader(lAB, D_DIR);
118118
PathClassLoader lC = new PathClassLoader(lD, C_DIR);
119119

120-
assertCanGet(lC, "C", 3);
120+
assertCanGet(lC, "pkg.C", 3);
121121
assertCannotGet(lC, "D");
122122
}
123123

plt/src/edu/rice/cs/plt/reflect/ReflectUtilTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,12 @@ public void testGetStaticField() throws Exception {
189189
assertEquals(STATIC_FIELD, getStaticField("edu.rice.cs.plt.reflect.ReflectUtilTest", "STATIC_FIELD"));
190190

191191
ClassLoader l = new PathClassLoader(INTBOX_DIR, A_DIR, B_DIR, C_DIR, D_DIR);
192-
assertEquals("A", getStaticField(l, "A", "NAME"));
193-
assertEquals("B", getStaticField(l, "B", "NAME"));
194-
assertEquals("C", getStaticField(l, "C", "NAME"));
192+
assertEquals("A", getStaticField(l, "pkg.A", "NAME"));
193+
assertEquals("B", getStaticField(l, "bpkg.B", "NAME"));
194+
assertEquals("C", getStaticField(l, "pkg.C", "NAME"));
195195
assertEquals("D", getStaticField(l, "D", "NAME"));
196196

197-
try { getStaticField("A", "NAME"); fail("expected exception"); }
197+
try { getStaticField("pkg.A", "NAME"); fail("expected exception"); }
198198
catch (ReflectException e) { assertCorrectException(e, "ClassNotFound"); }
199199

200200
try { getStaticField("fishing.boats", "foo"); fail("expected exception"); }
@@ -223,12 +223,12 @@ public void testInvokeStaticMethod() throws Exception {
223223
assertEquals("123", invokeStaticMethod("java.lang.String", "valueOf", new Class<?>[]{ int.class }, 123));
224224

225225
ClassLoader l = new PathClassLoader(INTBOX_DIR, A_DIR, B_DIR, C_DIR, D_DIR);
226-
assertEquals("A", invokeStaticMethod(l, "A", "getName"));
227-
assertEquals("B", invokeStaticMethod(l, "B", "getName"));
228-
assertEquals("C", invokeStaticMethod(l, "C", "getName"));
226+
assertEquals("A", invokeStaticMethod(l, "pkg.A", "getName"));
227+
assertEquals("B", invokeStaticMethod(l, "bpkg.B", "getName"));
228+
assertEquals("C", invokeStaticMethod(l, "pkg.C", "getName"));
229229
assertEquals("D", invokeStaticMethod(l, "D", "getName"));
230230

231-
try { invokeStaticMethod("A", "getName"); fail("expected exception"); }
231+
try { invokeStaticMethod("pkg.A", "getName"); fail("expected exception"); }
232232
catch (ReflectException e) { assertCorrectException(e, "ClassNotFound"); }
233233

234234
try { invokeStaticMethod("fishing.boats", "foo"); fail("expected exception"); }
-421 Bytes
Binary file not shown.
433 Bytes
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package pkg;
2+
13
public class A implements IntBox {
24
public static String NAME = "A";
35
public static String getName() { return NAME; }
-421 Bytes
Binary file not shown.
434 Bytes
Binary file not shown.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
public class B implements IntBox {
1+
package bpkg;
2+
3+
public class B implements pkg.IntBox {
24
public static String NAME = "B";
35
public static String getName() { return NAME; }
46
public int get() { return 2; }
-454 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)