Skip to content

Commit 2cdeaf0

Browse files
hansonrhansonr
authored andcommitted
allows checking first in class package for Class.forName(name, init,
ClassLoader)
1 parent ec0ba05 commit 2cdeaf0

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

sources/net.sf.j2s.java.core/src/java/lang/Class.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,12 @@ private String getName0() {
720720
* @see java.lang.RuntimePermission
721721
*/
722722
public ClassLoader getClassLoader() {
723-
return getClassLoader0();
723+
ClassLoader cl = getClassLoader0();
724+
/**
725+
* @j2sNative
726+
* cl.baseClass = this;
727+
*/
728+
return cl;
724729
// SecurityManager sm = System.getSecurityManager();
725730
// if (sm != null) {
726731
// ClassLoader ccl = ClassLoader.getCallerClassLoader();

sources/net.sf.j2s.java.core/src/test/Test_Reflect.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package test;
22

3+
import java.awt.Button;
34
import java.lang.reflect.Field;
45

56
class Test_Reflect extends Test_ {
@@ -68,6 +69,22 @@ public void test(String s, float[][] aaf) {
6869
*/
6970

7071
public static void main(String[] args) {
72+
73+
String name = "";
74+
try {
75+
Test_Path tp = (Test_Path) Class.forName(name = "Test_Path", true, Test_Reflect.class.getClassLoader()).newInstance();
76+
assert(tp != null);
77+
System.out.println("class loaded: " + tp.getClass().getName());
78+
79+
Button b = (Button) Class.forName(name = "java.awt.Button", true, Test_Reflect.class.getClassLoader()).newInstance();
80+
assert(b != null);
81+
System.out.println("class loaded: " + b.getClass().getName());
82+
83+
} catch (Throwable t) {
84+
System.out.println("problems loading " + name);
85+
assert(false);
86+
}
87+
7188
Test_Reflect tr = new Test_Reflect();
7289
// Field is not implemented
7390
// Field f = Test_Reflect.class.getDeclaredField("s");

sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,19 @@ Clazz.exceptionOf = function(e, clazz) {
303303
|| clazz == NullPointerException && _isNPEExceptionPredicate(e));
304304
};
305305

306-
Clazz.forName = function(name, initialize, loader) {
307-
return Clazz._4Name(name, null, null, false, initialize);
306+
Clazz.forName = function(name, initialize, loader, isQuiet) {
307+
// we need to consider loading a class from the path of the calling class.
308+
var cl = null;
309+
if (loader) {
310+
try {
311+
isQuiet = true;
312+
var className = loader.baseClass.getName$(); // set in java.lang.Class.getClassLoader$()
313+
var i = className.lastIndexOf(".");
314+
var name1 = className.substring(0, i + 1) + name;
315+
cl = Clazz._4Name(name1, null, null, false, initialize, true);
316+
} catch (e) {}
317+
}
318+
return cl || Clazz._4Name(name, null, null, false, initialize, isQuiet);
308319
}
309320

310321
Clazz.getClass = function(cl, methodList) {
@@ -2487,7 +2498,7 @@ var evaluate = function(file, js) {
24872498
}
24882499
}
24892500

2490-
Clazz._4Name = function(clazzName, applet, state, asClazz, initialize) {
2501+
Clazz._4Name = function(clazzName, applet, state, asClazz, initialize, isQuiet) {
24912502
if (clazzName.indexOf("[") == 0)
24922503
return getArrayClass(clazzName);
24932504
if (clazzName.indexOf(".") < 0)
@@ -2524,6 +2535,8 @@ Clazz._4Name = function(clazzName, applet, state, asClazz, initialize) {
25242535
}
25252536
var cl = evalType(clazzName);
25262537
if (!cl){
2538+
if (isQuiet)
2539+
return null;
25272540
alert(clazzName + " could not be loaded");
25282541
doDebugger();
25292542
}

0 commit comments

Comments
 (0)