Skip to content

Commit 689d9f2

Browse files
author
pakruse
committed
Fixed static import to prevent importation of non-static members.
git-svn-id: file:///tmp/test-svn/trunk@2048 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent dbdd53c commit 689d9f2

File tree

3 files changed

+163
-12
lines changed

3 files changed

+163
-12
lines changed

dynamicjava/src/koala/Version.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* This file is copied to Version.java by the build process, which also
5454
* fills in the right values of the date and time.
5555
*
56-
* This javadoc corresponds to build drjava-20040601-1531;
56+
* This javadoc corresponds to build drjava-20040601-1635;
5757
*
5858
* @version $Id$
5959
*/
@@ -62,7 +62,7 @@ public abstract class Version {
6262
* This string will be automatically expanded upon "ant commit".
6363
* Do not edit it by hand!
6464
*/
65-
private static final String BUILD_TIME_STRING = "20040601-1531";
65+
private static final String BUILD_TIME_STRING = "20040601-1635";
6666

6767
/** A {@link Date} version of the build time. */
6868
private static final Date BUILD_TIME = _getBuildDate();

dynamicjava/src/koala/dynamicjava/tree/tiger/TigerTest.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@ public void testStaticImportOfStaticInnerClass(){
9090
"D.m();\n";
9191
assertEquals(0,interpret(testString));
9292

93+
//Tests that a non-static inner class cannot be imported
94+
testString =
95+
"package T;\n"+
96+
"public class U {\n"+
97+
" public class V { }\n"+
98+
"}\n"+
99+
"package W;\n"+
100+
"import static T.U.*;\n"+
101+
"V v = new V();";
102+
try {
103+
interpret(testString);
104+
fail("Non static member should not be imported");
105+
}
106+
catch(InterpreterException e) {
107+
//Expected to fail
108+
}
109+
110+
//Tests that a non-static inner class cannot be imported
111+
testString =
112+
"package X;\n"+
113+
"public class Y {\n"+
114+
" public class Z { \n"+
115+
" public static int m() { return 5; }\n"+
116+
" }\n"+
117+
"}\n"+
118+
"package AA;\n"+
119+
"import static X.Y.Z;\n"+
120+
"Z.m()";
121+
try {
122+
assertEquals(5,interpret(testString));
123+
fail("Non static member should not be imported");
124+
}
125+
catch(RuntimeException e) {
126+
//Expected to fail
127+
}
128+
93129
}
94130
/**
95131
* Testing various forms of static importation of methods
@@ -232,6 +268,78 @@ public void testStaticImportOfMethods(){
232268
assertEquals("Member of class should take precedence over staticly imported member",0.0,interpret(testString));
233269

234270

271+
272+
testString =
273+
"package NN;\n"+
274+
"import static java.lang.Math.abs;\n"+
275+
"public abstract class OO {\n"+
276+
" public int abs(int i) { return i; }\n"+
277+
"}\n"+
278+
"public class PP extends OO {\n"+
279+
" public static int m() {\n"+
280+
" return abs(-2);\n"+
281+
" }\n"+
282+
"}\n"+
283+
"PP.m();";
284+
try {
285+
interpret(testString);
286+
fail("Static method cannot reference non-static members of super class");
287+
} catch(Error e) {
288+
//Expected to fail
289+
}
290+
291+
292+
testString =
293+
"package QQ;\n"+
294+
"import static java.lang.Math.abs;\n"+
295+
"public abstract class RR {\n"+
296+
" public static int abs(int i) { return i; }\n"+
297+
"}\n"+
298+
"public class SS extends RR {\n"+
299+
" public static int m() {\n"+
300+
" return abs(-2);\n"+
301+
" }\n"+
302+
"}\n"+
303+
"SS.m();";
304+
assertEquals("Super class method should take precedence over staticly imported member",-2,interpret(testString));
305+
306+
307+
//Tests that a non-static method cannot be imported
308+
testString =
309+
"package TT;\n"+
310+
"public class UU {\n"+
311+
" public int m1() { return 5;}\n"+
312+
"}\n"+
313+
"package VV;\n"+
314+
"import static TT.UU.*;\n"+
315+
"public class WW {\n"+
316+
" public int m2() { return m1(); } \n"+
317+
"}\n"+
318+
"WW ww = new WW(); ww.m2();";
319+
try {
320+
assertEquals(5,interpret(testString));
321+
fail("Non static member should not be imported");
322+
}
323+
catch(InterpreterException e) {
324+
//Expected to fail
325+
}
326+
327+
//Tests that a non-static method cannot be imported
328+
testString =
329+
"package XX;\n"+
330+
"public class YY {\n"+
331+
" public int m() { return 5;}\n"+
332+
"}\n"+
333+
"package ZZ;\n"+
334+
"import static XX.YY.m;\n";
335+
try {
336+
interpret(testString);
337+
fail("Non static member should not be imported");
338+
}
339+
catch(RuntimeException e) {
340+
//Expected to fail
341+
}
342+
235343
}
236344

237345
/**
@@ -336,6 +444,42 @@ public void testStaticImportOfFields(){
336444
assertEquals(3,interpret(testString));
337445

338446

447+
448+
//Tests that a non-static field cannot be imported
449+
testString =
450+
"package N;\n"+
451+
"public class O {\n"+
452+
" public int field = 5;\n"+
453+
"}\n"+
454+
"package P;\n"+
455+
"import static N.O.*;\n"+
456+
"public class Q {\n"+
457+
" public int m() { return field; } \n"+
458+
"}\n"+
459+
"Q q = new Q(); q.m();";
460+
try {
461+
assertEquals(5,interpret(testString));
462+
fail("Non static member should not be imported");
463+
}
464+
catch(InterpreterException e) {
465+
//Expected to fail
466+
}
467+
468+
//Tests that a non-static field cannot be imported
469+
testString =
470+
"package R;\n"+
471+
"public class S {\n"+
472+
" public int field = 5;\n"+
473+
"}\n"+
474+
"package T;\n"+
475+
"import static R.S.field;\n";
476+
try {
477+
interpret(testString);
478+
fail("Non static member should not be imported");
479+
}
480+
catch(RuntimeException e) {
481+
//Expected to fail
482+
}
339483
}
340484

341485

dynamicjava/src/koala/dynamicjava/util/ImportationManager.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,16 @@ public void declareMemberStaticImport(String member) {
303303
//Different methods require different formats, and having both can't hurt anything. Any of the methods that use the list of classes try and catch through the
304304
//list until they come across a class that fits
305305
try {
306+
Class c;
306307
try {
307-
Class.forName(member, true, classLoader);
308+
c = Class.forName(member, true, classLoader);
308309
} catch (ClassNotFoundException cnfe) {
309-
findInnerClass(member);
310+
c = findInnerClass(member);
311+
}
312+
if(isPublicAndStatic(c.getModifiers())) {
313+
declareClassImport(member);
314+
foundSomethingToImport = true;
310315
}
311-
declareClassImport(member);
312-
foundSomethingToImport = true;
313316
}
314317
catch(ClassNotFoundException e) {
315318
}
@@ -323,9 +326,11 @@ public void declareMemberStaticImport(String member) {
323326
//Next, check for all static fields
324327
try {
325328
Field f = surroundingClass.getField(name);
326-
singleTypeImportStaticFieldClauses.remove(f);
327-
singleTypeImportStaticFieldClauses.add(0,f);
328-
foundSomethingToImport = true;
329+
if(isPublicAndStatic(f.getModifiers())) {
330+
singleTypeImportStaticFieldClauses.remove(f);
331+
singleTypeImportStaticFieldClauses.add(0,f);
332+
foundSomethingToImport = true;
333+
}
329334
}
330335
catch(NoSuchFieldException e) {
331336
}
@@ -339,9 +344,11 @@ public void declareMemberStaticImport(String member) {
339344
for(int j = 0; j<methodArray.length; j++) {
340345
if(isPublicAndStatic(methodArray[j].getModifiers()) && methodArray[j].getName().equals(name)) {
341346
Method m = methodArray[j];
342-
singleTypeImportStaticMethodClauses.remove(m);
343-
singleTypeImportStaticMethodClauses.add(0,m);
344-
foundSomethingToImport = true;
347+
if(isPublicAndStatic(m.getModifiers())) {
348+
singleTypeImportStaticMethodClauses.remove(m);
349+
singleTypeImportStaticMethodClauses.add(0,m);
350+
foundSomethingToImport = true;
351+
}
345352
}
346353
}
347354
}

0 commit comments

Comments
 (0)