Skip to content

Commit 3944766

Browse files
author
mgricken
committed
Fixes bug 2976836: getPackage()
git-svn-id: file:///tmp/test-svn/trunk@5201 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent ff4f8f1 commit 3944766

File tree

11 files changed

+168
-5
lines changed

11 files changed

+168
-5
lines changed

drjava/lib/dynamicjava-base.jar

484 Bytes
Binary file not shown.

drjava/lib/javalanglevels-base.jar

-191 Bytes
Binary file not shown.

drjava/lib/plt.jar

730 Bytes
Binary file not shown.

drjava/src/edu/rice/cs/drjava/model/repl/InteractionsModelErrorTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public final class InteractionsModelErrorTest extends GlobalModelTestCase {
9696
"public abstract class UnaryFun {\n"+
9797
" public abstract Object apply(final Object arg);\n"+
9898
"}";
99+
protected static final String CLASS_IN_PACKAGE_CLASS_TEXT =
100+
"package foo;\n"+
101+
"public class Bar {\n"+
102+
" public void run() { }\n"+
103+
"}";
99104

100105
private volatile InteractionsPaneOptions _interpreterOptions;
101106
private volatile Interpreter _interpreter;
@@ -260,4 +265,50 @@ public void testInterpretExtendPublicClass()
260265

261266
_interpreter.interpret("UnaryFun f = new UnaryFun() { public Object apply(Object arg) { return (Integer)arg * (Integer)arg; }}");
262267
}
268+
269+
/** Test that we get the right package using getPackage(). */
270+
@SuppressWarnings("unchecked")
271+
public void testInterpretGetPackageClass()
272+
throws BadLocationException, IOException, InterruptedException, InterpreterException {
273+
_log.log("testInterpretGetPackageClass started");
274+
275+
OpenDefinitionsDocument doc = setupDocument(CLASS_IN_PACKAGE_CLASS_TEXT);
276+
277+
final File dir = tempDirectory();
278+
final File packDir = new File(dir, "foo");
279+
packDir.mkdir();
280+
final File file = new File(packDir, "Bar.java");
281+
saveFile(doc, new FileSelector(file));
282+
CompileShouldSucceedListener listener = new CompileShouldSucceedListener();
283+
_model.addListener(listener);
284+
listener.compile(doc);
285+
if (_model.getCompilerModel().getNumErrors() > 0) {
286+
fail("compile failed: " + getCompilerErrorString());
287+
}
288+
listener.checkCompileOccurred();
289+
_model.removeListener(listener);
290+
assertCompileErrorsPresent(_name(), false);
291+
292+
// Make sure .class exists
293+
File compiled = classForJava(file, "Bar");
294+
assertTrue(_name() + "Class file should exist after compile", compiled.exists());
295+
296+
_classPathManager.addBuildDirectoryCP(compiled.getParentFile().getParentFile());
297+
298+
Object out = interpretDirectly("new foo.Bar().getClass().getPackage().getName()");
299+
assertEquals("Package of foo.Bar should be foo", "foo", out);
300+
}
301+
302+
/** Test that we get the right package using getPackage() with anonymous inner classes defined in the Interactions Pane. */
303+
@SuppressWarnings("unchecked")
304+
public void testInterpretGetPackageAnonymous()
305+
throws BadLocationException, IOException, InterruptedException, InterpreterException {
306+
_log.log("testInterpretGetPackageAnonymous started");
307+
308+
Object out = interpretDirectly("new Runnable() { public void run() { } }.getClass().getPackage()");
309+
assertEquals("Package of $1 should be null", null, out);
310+
311+
out = interpretDirectly("package foo; new Runnable() { public void run() { } }.getClass().getPackage().getName()");
312+
assertEquals("Package of foo.$1 should be foo", "foo", out);
313+
}
263314
}

dynamicjava/lib/plt.jar

730 Bytes
Binary file not shown.

dynamicjava/src/edu/rice/cs/dynamicjava/interpreter/TreeClassLoader.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.HashMap;
55
import edu.rice.cs.dynamicjava.Options;
66
import edu.rice.cs.dynamicjava.symbol.TreeClass;
7+
import edu.rice.cs.plt.reflect.AbstractClassLoader;
78
import edu.rice.cs.plt.reflect.ShadowingClassLoader;
89
import edu.rice.cs.plt.reflect.ComposedClassLoader;
910
import edu.rice.cs.plt.iter.IterUtil;
@@ -12,7 +13,7 @@
1213
* A class loader with the additional ability of loading classes from their (type-checked)
1314
* AST representations.
1415
*/
15-
public class TreeClassLoader extends ClassLoader {
16+
public class TreeClassLoader extends AbstractClassLoader {
1617

1718
private final Options _opt;
1819
// trees that have been declared but not yet loaded
@@ -62,6 +63,10 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
6263
TreeCompiler compiler = new TreeCompiler(treeClass, _opt);
6364
byte[] bytes = compiler.bytecode();
6465
_adapters.put(name, compiler.evaluationAdapter());
66+
67+
// define package
68+
definePackageForClass(name);
69+
6570
Class<?> result = defineClass(name, bytes, 0, bytes.length);
6671
return result;
6772
}

javalanglevels/lib/plt.jar

1.5 KB
Binary file not shown.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*BEGIN_COPYRIGHT_BLOCK*
2+
*
3+
PLT Utilities BSD License
4+
5+
Copyright (c) 2007-2010 JavaPLT group at Rice University
6+
All rights reserved.
7+
8+
Developed by: Java Programming Languages Team
9+
Rice University
10+
http://www.cs.rice.edu/~javaplt/
11+
12+
Redistribution and use in source and binary forms, with or without modification, are permitted
13+
provided that the following conditions are met:
14+
15+
- Redistributions of source code must retain the above copyright notice, this list of conditions
16+
and the following disclaimer.
17+
- Redistributions in binary form must reproduce the above copyright notice, this list of
18+
conditions and the following disclaimer in the documentation and/or other materials provided
19+
with the distribution.
20+
- Neither the name of the JavaPLT group, Rice University, nor the names of the library's
21+
contributors may be used to endorse or promote products derived from this software without
22+
specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
25+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
26+
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND
27+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
30+
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31+
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
33+
*END_COPYRIGHT_BLOCK*/
34+
35+
package edu.rice.cs.plt.reflect;
36+
37+
import java.io.InputStream;
38+
import java.io.IOException;
39+
40+
import edu.rice.cs.plt.iter.IterUtil;
41+
import edu.rice.cs.plt.io.IOUtil;
42+
43+
/**
44+
* <p>A class loader that provides a helper method definePackageForClass.</p>
45+
*/
46+
public abstract class AbstractClassLoader extends ClassLoader {
47+
/**
48+
* Creates a new class loader using the <tt>ClassLoader</tt> returned by
49+
* the method {@link #getSystemClassLoader()
50+
* <tt>getSystemClassLoader()</tt>} as the parent class loader.
51+
*
52+
* <p> If there is a security manager, its {@link
53+
* SecurityManager#checkCreateClassLoader()
54+
* <tt>checkCreateClassLoader</tt>} method is invoked. This may result in
55+
* a security exception. </p>
56+
*
57+
* @throws SecurityException
58+
* If a security manager exists and its
59+
* <tt>checkCreateClassLoader</tt> method doesn't allow creation
60+
* of a new class loader.
61+
*/
62+
protected AbstractClassLoader() { super(); }
63+
64+
/**
65+
* Creates a new class loader using the specified parent class loader for
66+
* delegation.
67+
*
68+
* <p> If there is a security manager, its {@link
69+
* SecurityManager#checkCreateClassLoader()
70+
* <tt>checkCreateClassLoader</tt>} method is invoked. This may result in
71+
* a security exception. </p>
72+
*
73+
* @param parent
74+
* The parent class loader
75+
*
76+
* @throws SecurityException
77+
* If a security manager exists and its
78+
* <tt>checkCreateClassLoader</tt> method doesn't allow creation
79+
* of a new class loader.
80+
*
81+
* @since 1.2
82+
*/
83+
protected AbstractClassLoader(ClassLoader parent) { super(parent); }
84+
85+
/** Defines a package for a class, unless it has been defined already. This must be done before
86+
* the class is defined.
87+
* @param className the name of the class that is being loaded
88+
*/
89+
protected Package definePackageForClass(String className) {
90+
int lastDotPos = className.lastIndexOf('.');
91+
if (lastDotPos<0) return null; // no package in name, default package == null
92+
93+
String packageName = className.substring(0,lastDotPos);
94+
Package pack = getPackage(packageName);
95+
if (pack==null) {
96+
// not yet defined
97+
pack = definePackage(packageName, null, null, null, null, null, null, null);
98+
}
99+
return pack;
100+
}
101+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISI
6262
* shadowed. This is not a unique problem, however -- the standard system class loader is
6363
* based on an underlying file system that may also change in arbitrary ways at any time.
6464
*/
65-
public class PathClassLoader extends ClassLoader {
65+
public class PathClassLoader extends AbstractClassLoader {
6666

6767
/**
6868
* Locate a resource in the given path. Returns {@code null} if the resource is not found.
@@ -149,6 +149,10 @@ private void updateURLLoader() {
149149
else {
150150
try {
151151
byte[] bytes = IOUtil.toByteArray(resource.openStream());
152+
153+
// define package
154+
definePackageForClass(name);
155+
152156
return defineClass(name, bytes, 0, bytes.length);
153157
}
154158
catch (IOException e) { throw new ClassNotFoundException(); }

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISI
5757
* only if a resource with the appropriate class file name exists. But there is no guarantee that all
5858
* class loaders (such as those that generate class definitions on the fly) will follow this convention.</p>
5959
*/
60-
public class PreemptingClassLoader extends ClassLoader {
60+
public class PreemptingClassLoader extends AbstractClassLoader {
6161

6262
private Iterable<String> _prefixes;
6363

@@ -98,6 +98,10 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
9898
if (in == null) { throw new ClassNotFoundException("Resource not found: " + filename); }
9999
try {
100100
byte[] data = IOUtil.toByteArray(in);
101+
102+
// define package
103+
definePackageForClass(name);
104+
101105
result = defineClass(name, data, 0, data.length);
102106
}
103107
catch (IOException e) {
@@ -125,5 +129,4 @@ private boolean shouldPreempt(String name) {
125129
}
126130
return false;
127131
}
128-
129132
}

0 commit comments

Comments
 (0)