Skip to content

Commit cc7de71

Browse files
hansonrhansonr
authored andcommitted
fix for Class.getMethod()
1 parent f4127c7 commit cc7de71

File tree

5 files changed

+60
-23
lines changed

5 files changed

+60
-23
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private static class JSClass {
115115
public String __CLASS_NAME__;
116116
}
117117
private JSClass $clazz$; // BH SwingJS
118-
private String[] $methodList$; // BH SwingJS for proxy interfaces
118+
public String[] $methodList$; // BH see j2sClazz.js Clazz.getClass, from interface parameters
119119

120120
// private static native void registerNatives();
121121
//
@@ -139,6 +139,7 @@ private Class() {
139139
*
140140
* @return a string representation of this class object.
141141
*/
142+
@Override
142143
public String toString() {
143144
return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) + getName();
144145
}
@@ -769,7 +770,7 @@ ClassLoader getClassLoader0() {
769770
* Specification, 3rd edition
770771
* @since 1.5
771772
*/
772-
@SuppressWarnings("unchecked")
773+
@Override
773774
public TypeVariable<Class<T>>[] getTypeParameters() {
774775
// if (getGenericSignature() != null)
775776
// return (TypeVariable<Class<T>>[]) getGenericInfo().getTypeParameters();
@@ -1811,7 +1812,7 @@ private void addField(Field[] fields, String m, int modifiers) {
18111812
*
18121813
* @param name
18131814
* the name of the method
1814-
* @param parameterTypes
1815+
* @param paramTypes
18151816
* the list of parameters
18161817
* @return the {@code Method} object that matches the specified {@code name}
18171818
* and {@code parameterTypes}
@@ -1852,7 +1853,7 @@ public Method getMethod(String name, Class<?>... paramTypes) throws NoSuchMethod
18521853
Method m = new Method(this, name, paramTypes, null, null, 0);
18531854
if (!isInterface()) {
18541855
Object o = null;
1855-
String qname = name + argumentTypesToString(paramTypes);
1856+
String qname = m.getSignature();
18561857
/**
18571858
* @j2sNative
18581859
*
@@ -1925,15 +1926,11 @@ public Method getMethod(String name, Class<?>... paramTypes) throws NoSuchMethod
19251926
*
19261927
* @since JDK1.1
19271928
*/
1928-
@SuppressWarnings("unchecked")
19291929
public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException {
19301930
// be very careful not to change the stack depth of this
19311931
// checkMemberAccess call for security reasons
19321932
// see java.lang.SecurityManager.checkMemberAccess
19331933
// checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
1934-
Class<?>[] x = parameterTypes;
1935-
if (parameterTypes == null)
1936-
parameterTypes = new Class<?>[0];
19371934
return new Constructor(this, parameterTypes, new Class<?>[0], Member.PUBLIC);
19381935
// return getConstructor0(parameterTypes, Member.PUBLIC);
19391936
}
@@ -3218,6 +3215,8 @@ public static String argumentTypesToString(Class<?>[] parameterTypes) {
32183215
// */
32193216
// private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0];
32203217

3218+
public static final Class<?>[] NO_PARAMETERS = new Class<?>[0];
3219+
32213220
// /**
32223221
// * Returns the assertion status that would be assigned to this class if it
32233222
// * were to be initialized at the time this method is invoked. If this class
@@ -3295,7 +3294,7 @@ public boolean isEnum() {
32953294
// private static ReflectionFactory reflectionFactory;
32963295
//
32973296
// To be able to query system properties as soon as they're available
3298-
private static boolean initted = false;
3297+
// private static boolean initted = false;
32993298

33003299
// private static void checkInitted() {
33013300
// if (initted)
@@ -3558,8 +3557,9 @@ public boolean equals(Object o) {
35583557
/**
35593558
* A SwingJS method for Constructor and Method
35603559
*
3561-
* @param parameterTypes
3560+
* @param types
35623561
* @param args
3562+
* @param isProxy
35633563
* @return
35643564
*/
35653565
public static Object[] getArgumentArray(Class<?>[] types, Object[] args, boolean isProxy) {

sources/net.sf.j2s.java.core/src/java/lang/reflect/Constructor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public Constructor(Class<T> declaringClass, Class<?>[] parameterTypes, Class<?>[
4646
// NO!! wrong: all of the SwingJS primitive classes run without parameterization
4747
//if (";Integer;Long;Short;Byte;Float;Double;".indexOf(";" + declaringClass.getName() + ";") >= 0)
4848
//parameterTypes = null;
49-
this.parameterTypes = parameterTypes;
49+
// Special case - constructors with parameters have c$$, not just c$. For whatever reason!
50+
// This signals NOT to add "$" if there are no parameters.
51+
if (parameterTypes == null)
52+
parameterTypes = Class.NO_PARAMETERS;
5053
this.signature = "c$" + Class.argumentTypesToString(parameterTypes);
5154
constr = /** @j2sNative this.Class_.$clazz$[this.signature] || */ null;
5255
}

sources/net.sf.j2s.java.core/src/java/lang/reflect/Method.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,14 @@ public Method(Class<?> declaringClass, String name, Class<?>[] parameterTypes, C
4646
Class<?>[] checkedExceptions, int modifiers) {
4747
this.Class_ = declaringClass;
4848
this.name = name;
49-
this.parameterTypes = parameterTypes;
49+
this.parameterTypes = (parameterTypes == null ? Class.NO_PARAMETERS : parameterTypes);
5050
this.returnType = returnType;
5151
this.exceptionTypes = checkedExceptions;
5252
this.modifiers = modifiers;
53-
this.signature = name + Class.argumentTypesToString(parameterTypes);
53+
// modifier PUBLIC means this is from Class.java getMethods
54+
if (parameterTypes != null && parameterTypes.length == 0)
55+
parameterTypes = null;
56+
this.signature = (declaringClass.$methodList$ == null ? name + Class.argumentTypesToString(parameterTypes) : name);
5457
}
5558

5659
/**

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
@@ -5,6 +5,7 @@
55

66
class Test_Reflect extends Test_ {
77

8+
static long ltime = System.currentTimeMillis();
89
public String s = "field s";
910

1011
public void test(char i, String s) {
@@ -71,6 +72,8 @@ public void test(String s, float[][] aaf) {
7172

7273
public static void main(String[] args) {
7374

75+
76+
new Test_Reflect().test("int[]", new int[] {1,2,3});
7477
String name = "";
7578
try {
7679
Test_Path tp = (Test_Path) Class.forName(name = "Test_Path", true, Test_Reflect.class.getClassLoader()).newInstance();
@@ -87,6 +90,20 @@ public static void main(String[] args) {
8790
}
8891

8992
Test_Reflect tr = new Test_Reflect();
93+
94+
new Test_Char();
95+
assert(tr instanceof Test_);
96+
assert(tr instanceof Test_);
97+
assert(!(((Test_)tr) instanceof Test_Char));
98+
99+
long l = System.currentTimeMillis();
100+
System.out.println(l - ltime);
101+
for (int i = 0; i < 10000; i++) {
102+
assert(!(((Test_)tr) instanceof Test_Char));
103+
}
104+
System.out.println(l - System.currentTimeMillis());
105+
106+
90107
// Field is not implemented
91108
// Field f = Test_Reflect.class.getDeclaredField("s");
92109
// System.out.println(f.getDeclaringClass());

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
// TODO: still a lot of references to window[...]
1111

12+
// BH 2019.08.16 adds cache for instanceof
1213
// BH 2019.07.27 fixes array(intArray).clone
1314
// BH 2019.07.09 adds Java String.trim()
1415
// BH 2019.05.21 changes Clazz.isClassDefined to Clazz._isClassDefined for compression
@@ -351,11 +352,14 @@ Clazz.instanceOf = function (obj, clazz) {
351352
if (typeof clazz == "string") {
352353
clazz = window[clazz];
353354
}
355+
354356
if (obj == null || !clazz)
355357
return false;
356358
// check for object being a java.lang.Class and the other not
357359
if (obj.$clazz$ && !clazz.$clazz$) return false;
358360
obj.$clazz$ && (obj = obj.$clazz$);
361+
if (clazz == String)
362+
return typeof obj == "string";
359363
clazz.$clazz$ && (clazz = clazz.$clazz$);
360364
if (obj == clazz)
361365
return true;
@@ -1396,11 +1400,7 @@ var extendPrototype = function(clazz, isPrimitive, addAll) {
13961400
}
13971401

13981402

1399-
Clazz.saemCount0 = 0 // methods defined 5400 (Ripple.js)
1400-
Clazz.saemCount1 = 0 // delegates created 937
1401-
Clazz.saemCount2 = 0 // delegates bound 397
1402-
Clazz.saemCount3 = 0 // isInstanceOfs started
1403-
Clazz.saemCount4 = 0 // isInstanceOfs checked
1403+
Clazz.saemCount0 = 0 // methods defined
14041404

14051405
var NullObject = function () {};
14061406

@@ -1457,14 +1457,16 @@ var equalsOrExtendsLevel = function (clazzThis, clazzAncestor) {
14571457
return false;
14581458
};
14591459

1460+
var knownInst = {};
1461+
14601462
var isInstanceOf = function (clazzTarget, clazzBase, isTgtStr, isBaseStr) {
14611463
if (clazzTarget === clazzBase)
14621464
return true;
1463-
clazzBase.$clinit$ && clazzBase.$clinit$();
14641465
if (isTgtStr && ("void" == clazzTarget || "unknown" == clazzTarget))
14651466
return false;
14661467
if (isBaseStr && ("void" == clazzBase || "unknown" == clazzBase))
14671468
return false;
1469+
clazzBase.$clinit$ && clazzBase.$clinit$();
14681470
if (clazzTarget === (isTgtStr ? "NullObject" : NullObject)) {
14691471
switch (clazzBase) {
14701472
case "n":
@@ -1477,15 +1479,27 @@ var isInstanceOf = function (clazzTarget, clazzBase, isTgtStr, isBaseStr) {
14771479
default:
14781480
return true;
14791481
}
1480-
}
1481-
isTgtStr && (clazzTarget = evalType(clazzTarget));
1482-
isBaseStr && (clazzBase = evalType(clazzBase));
1483-
return (clazzBase && clazzTarget && (
1482+
}
1483+
var t = (isTgtStr ? clazzTarget : clazzTarget.__CLASS_NAME__ || clazzTarget.type);
1484+
var b = (isBaseStr ? clazzBase : clazzBase.__CLASS_NAME__ || clazzBase.type);
1485+
if (t && t == b)
1486+
return true;
1487+
var key = t + "|" + b;
1488+
var val = knownInst[key];
1489+
if (val)
1490+
return (val == 1 ? true : false);
1491+
1492+
isTgtStr && (clazzTarget = window[clazzTarget]);
1493+
isBaseStr && (clazzBase = window[clazzBase]);
1494+
var ret = (clazzBase && clazzTarget && (
14841495
clazzTarget == clazzBase
14851496
|| clazzBase === Object
14861497
|| clazzBase === Clazz._O
14871498
|| equalsOrExtendsLevel(clazzTarget, clazzBase)
14881499
));
1500+
if (t && b)
1501+
knownInst[key] = (ret ? 1 : -1);
1502+
return ret;
14891503
};
14901504

14911505

0 commit comments

Comments
 (0)