Skip to content

Commit ca09d4e

Browse files
committed
fixes 'L' for long should be 'J'
also fixes Method.getName() and Class.getMethods(), class.getDeclaredMethods() to give proper method name
1 parent c6c0d10 commit ca09d4e

File tree

8 files changed

+132
-61
lines changed

8 files changed

+132
-61
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,15 @@ private String getName0() {
723723
case "B":
724724
code = "Byte";
725725
break;
726-
case "L":
726+
case "J":
727727
code = "Long";
728728
break;
729729
case "C":
730730
code = "Character";
731731
break;
732+
case "O":
733+
code = "Object";
734+
break;
732735
default:
733736
return null;
734737
}
@@ -1725,7 +1728,7 @@ public Map<String,Object[]> getFieldAnnMap(Object cl) {
17251728
* @since JDK1.1
17261729
*/
17271730
public Method[] getMethods() throws SecurityException {
1728-
return /*copyMethods*/(privateGetPublicMethods());
1731+
return /*copyMethods*/(privateGetPublicMethods(true));
17291732
}
17301733

17311734
/**
@@ -2169,7 +2172,7 @@ public Field[] getDeclaredFields() throws SecurityException {
21692172
* @since JDK1.1
21702173
*/
21712174
public Method[] getDeclaredMethods() throws SecurityException {
2172-
return getMethods();
2175+
return /*copyMethods*/(privateGetPublicMethods(false));
21732176
// // be very careful not to change the stack depth of this
21742177
// // checkMemberAccess call for security reasons
21752178
// // see java.lang.SecurityManager.checkMemberAccess
@@ -2630,7 +2633,7 @@ public static Class<?> getPrimitiveOrStringClass(String name) {
26302633
case "I":
26312634
case "int":
26322635
return Integer.TYPE;
2633-
case "L":
2636+
case "J":
26342637
case "long":
26352638
return Long.TYPE;
26362639
case "F":
@@ -2639,6 +2642,8 @@ public static Class<?> getPrimitiveOrStringClass(String name) {
26392642
case "D":
26402643
case "double":
26412644
return Double.TYPE;
2645+
case "O":
2646+
return Object.class;
26422647
default:
26432648
return null;
26442649
}
@@ -3034,7 +3039,7 @@ Method[] getArray() {
30343039
// Returns an array of "root" methods. These Method objects must NOT
30353040
// be propagated to the outside world, but must instead be copied
30363041
// via ReflectionFactory.copyMethod.
3037-
private Method[] privateGetPublicMethods() {
3042+
private Method[] privateGetPublicMethods(boolean isAll) {
30383043
if (isAnnotation()) {
30393044
if ($members$ == null) {
30403045
$members$ = AnnotationParser.JSAnnotationObject.createMethods((Class<? extends Annotation>) this);
@@ -3062,9 +3067,14 @@ private Method[] privateGetPublicMethods() {
30623067
*
30633068
* var p = this.$clazz$.prototype;
30643069
*
3065-
* for (attr in p) { o = p[attr]; if (typeof o == "function" &&
3066-
* o.exName && !o.__CLASS_NAME__ && o != this.$clazz$[attr] &&
3067-
* o.exClazz == this.$clazz$) { // there are polynormical methods.
3070+
* for (attr in p) { o = p[attr]; if (
3071+
* typeof o == "function"
3072+
* && o.exName
3073+
* && !o.__CLASS_NAME__
3074+
* && o != this.$clazz$[attr]
3075+
* && (isAll || o.exClazz == this.$clazz$)
3076+
* && !o.exName.startsWith("c$")
3077+
* ) { // there are polynormical methods.
30683078
*/
30693079

30703080
Method m = new Method(this, attr, UNKNOWN_PARAMETERS, Void.class, NO_PARAMETERS, Modifier.PUBLIC);
@@ -3076,9 +3086,13 @@ private Method[] privateGetPublicMethods() {
30763086
* ms.push(m);
30773087
* }}
30783088
* p = this.$clazz$;
3079-
* for (attr in p) { o = p[attr];if (typeof o ==
3080-
* "function" && o.exName && !o.__CLASS_NAME__ &&
3081-
* o.exClazz == this.$clazz$) {
3089+
* for (attr in p) { o = p[attr];if (
3090+
* typeof o == "function"
3091+
* && o.exName && !o.__CLASS_NAME__
3092+
* && (isAll || o.exClazz == this.$clazz$)
3093+
* && o.exName.indexOf("$") != 0
3094+
* && !o.exName.startsWith("c$")
3095+
* ) {
30823096
*/
30833097
m = new Method(this, attr, UNKNOWN_PARAMETERS, Void.class, NO_PARAMETERS, Modifier.PUBLIC);
30843098
m._setJSMethod(o, Modifier.STATIC);

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final class Method extends AccessibleObject implements GenericDeclaration
4242
private Class<?> returnType;
4343
private Class<?>[] parameterTypes;
4444
private Class<?>[] exceptionTypes;
45-
private int modifiers = Member.PUBLIC;
45+
private int modifiers = PUBLIC;
4646
boolean isProxy;
4747

4848
// private Annotation[] annotations;
@@ -57,7 +57,8 @@ public final class Method extends AccessibleObject implements GenericDeclaration
5757
public Method(Class<?> declaringClass, String name, Class<?>[] paramTypes, Class<?> returnType,
5858
Class<?>[] checkedExceptions, int modifiers) {
5959
this.Class_ = declaringClass;
60-
this.name = name;
60+
int pt = name.indexOf("$");
61+
this.name = (pt >= 0 ? name.substring(0, pt) : name);
6162
this.parameterTypes = (paramTypes == null ? Class.NO_PARAMETERS : paramTypes);
6263
this.returnType = returnType;
6364
this.exceptionTypes = checkedExceptions;
@@ -396,7 +397,11 @@ public Class<?> getReturnType() {
396397
}
397398

398399

399-
// @Override
400+
/**
401+
* SwingJS method to retrieve actual JavaScript name.
402+
*
403+
* @return
404+
*/
400405
public String getSignature() {
401406
return (String) signature;
402407
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -660,14 +660,13 @@ private static Class<?> defineClass0(ClassLoader loader, String name, Class<?>[]
660660
*
661661
* cl$.$clinit$ = 1;
662662
* Clazz.newMeth(cl$, '$init$', function () {}, 1);
663-
* Clazz.newMeth(cl$, "c$$reflect_InvocationHandler", function(h) {
664-
* cl$.superclazz.c$$reflect_InvocationHandler.apply(this, [h]);
663+
* Clazz.newMeth(cl$, "c$$java_lang_reflect_InvocationHandler", function(h) {
664+
* cl$.superclazz.c$$java_lang_reflect_InvocationHandler.apply(this, [h]);
665665
* cl$.$init$.apply(this);
666666
* }, 1);
667667
*
668668
*
669669
*/
670-
{}
671670
for (int i = 0; i < interfaces.length; i++) {
672671
Method[] methods = interfaces[i].getDeclaredMethods();
673672
for (int j = 0; j < methods.length; j++) {
@@ -681,7 +680,7 @@ private static Class<?> defineClass0(ClassLoader loader, String name, Class<?>[]
681680

682681
@SuppressWarnings("unused")
683682
private static void setJSPrototype(Class<?> cl, Method m, boolean add$) {
684-
String mname = m.getName();
683+
String mname = m.getSignature();
685684

686685
// SwingJS transfers the invocation to a temporary method, then invokes it
687686
/**
@@ -692,7 +691,7 @@ private static void setJSPrototype(Class<?> cl, Method m, boolean add$) {
692691
* cl.$clazz$.prototype[mname] = cl.$clazz$.prototype[mname1] =
693692
* function() { var args = new Array(arguments.length); for (var k =
694693
* arguments.length; --k >= 0;)args[k] = arguments[k];
695-
* return(this.h.invoke$O$reflect_Method$OA(this, m, args)); }
694+
* return(this.h.invoke$O$java_lang_reflect_Method$OA(this, m, args)); }
696695
*/
697696
}
698697
}

sources/net.sf.j2s.java.core/src/sun/reflect/annotation/AnnotationParser.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -461,26 +461,27 @@ public static Class[] getDeclaredClasses(Object clazz) {
461461

462462
@SuppressWarnings("null")
463463
public static Class<?>[] guessMethodParameterTypes(String signature) {
464-
String[] args = /** @j2sNative signature.split("$")|| */
464+
if (signature.startsWith("c$$"))
465+
signature = signature.substring(2);
466+
String[] args = /** @j2sNative signature.split("$") || */
465467
null;
466-
Class<?>[] classes = new Class<?>[args.length - 1];
467-
if (args.length > 1) {
468-
for (int i = 0; i < classes.length; i++) {
469-
String param = args[i + 1];
470-
String arrays = "";
471-
int len = param.length();
472-
while (len > 1 && param.endsWith("A")) {
473-
arrays += "[]";
474-
param = param.substring(0, --len);
475-
}
476-
param += arrays;
477-
// in case we have __ initially due to non-packaged classes being put in package _.
478-
param = param.substring(0, 1) + param.substring(1).replace('_', '.');
479-
classes[i] = typeForString(param, true);
480-
if (classes[i] == null) {
481-
classes[i] = Object.class;
482-
System.err.println("JSAnnotationObject - method parameter typing failed for " + signature);
483-
}
468+
Class<?>[] classes = new Class<?>[args.length == 2 && args[1] == "" ? 0 : args.length - 1];
469+
for (int i = 0; i < classes.length; i++) {
470+
String param = args[i + 1];
471+
String arrays = "";
472+
int len = param.length();
473+
while (len > 1 && param.endsWith("A")) {
474+
arrays += "[]";
475+
param = param.substring(0, --len);
476+
}
477+
param += arrays;
478+
// in case we have __ initially due to non-packaged classes being put in package
479+
// _.
480+
param = param.substring(0, 1) + param.substring(1).replace('_', '.');
481+
classes[i] = typeForString(param, true);
482+
if (classes[i] == null) {
483+
classes[i] = Object.class;
484+
System.err.println("JSAnnotationObject - method parameter typing failed for " + signature);
484485
}
485486
}
486487
return classes;

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

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.lang.reflect.Method;
1010
import java.util.Hashtable;
1111

12-
@SuppressWarnings("rawtypes")
1312
class Test_Class extends Test_Class2<Integer> {
1413

1514
Test_Class(byte[]...d) {
@@ -267,37 +266,43 @@ Class C() {
267266
public static void main(String[] args) {
268267

269268

269+
System.out.println("===========");
270+
showMethods(Test_Class.class.getDeclaredMethods());
271+
System.out.println("-----------");
272+
showMethods(Test_Class.class.getMethods());
273+
System.out.println("===========");
274+
showMethods(Test_Class_int.class.getDeclaredMethods());
275+
System.out.println("-----------");
276+
270277
new C(new byte[0], new byte[100], new byte[1000]);
271-
278+
272279
new C(new byte[3][5]);
273280

274281
try {
275-
new C((byte[][])null);
282+
new C((byte[][]) null);
276283
} catch (Throwable t) {
277284
System.out.println("Right!");
278285
}
279-
280-
286+
281287
Class<?> type = Object.class;
282-
assert(type instanceof Class<?>);
288+
assert (type instanceof Class<?>);
283289

284290
System.out.println(Test_Class.Singleton.instance);
285291
String ss = "testing \10\13a \7777 \u0052";
286-
System.out.println(ss + " "+ ss.length());
292+
System.out.println(ss + " " + ss.length());
287293
try {
288294
System.out.println(new String(ss.getBytes(), "UTF-8"));
289295
} catch (UnsupportedEncodingException e2) {
290296
// TODO Auto-generated catch block
291297
e2.printStackTrace();
292298
}
293-
//System.out.println("Test_Class.main() " + cl1);
294-
try {
295-
Constructor<Test_Class> constr = Test_Class.class.getConstructor(new Class[] {Test_.class});
299+
// System.out.println("Test_Class.main() " + cl1);
300+
try {
301+
Constructor<Test_Class> constr = Test_Class.class.getConstructor(new Class[] { Test_.class });
296302
} catch (NoSuchMethodException | SecurityException e1) {
297303
// TODO Auto-generated catch block
298304
e1.printStackTrace();
299305
}
300-
301306

302307
class LocalClass {
303308

@@ -307,16 +312,14 @@ String hello() {
307312
}
308313

309314
try {
310-
315+
311316
System.out.println("main istatic=" + istatic);
312317

313-
Class<?> cls = Class.forName("test.Test_Call",false, test.Test_Class.class.getClassLoader());
318+
Class<?> cls = Class.forName("test.Test_Call", false, test.Test_Class.class.getClassLoader());
314319
Method m = cls.getMethod("main", String[].class);
315-
String[] params = null;
316-
m.invoke(null, (Object) params);
317-
318-
319-
320+
String[] params = null;
321+
m.invoke(null, (Object) params);
322+
320323
String s = new LocalClass().hello();
321324
System.out.println(s);
322325
assert (s.equals("LocalClass says hello"));
@@ -373,6 +376,17 @@ String hello() {
373376

374377
}
375378

379+
private static void showMethods(Method[] methods) {
380+
for (int i = 0; i < methods.length; i++) {
381+
System.out.print("name=" + methods[i].getName());
382+
Class<?>[] parameters = methods[i].getParameterTypes();
383+
for (int j = 0; j < parameters.length; j++)
384+
System.out.print(" " + parameters[j].getName());
385+
System.out.println();
386+
}
387+
388+
}
389+
376390
public static void testStatic() {
377391
// TODO Auto-generated method stub
378392

@@ -390,6 +404,19 @@ public void testAbstract(int i, Double n, long j) {
390404

391405
}
392406

407+
@Override
408+
public void testClassInt(int i) {
409+
// TODO Auto-generated method stub
410+
411+
}
412+
413+
@Override
414+
public void testClassLong(long i) {
415+
// TODO Auto-generated method stub
416+
417+
}
418+
419+
393420

394421
}
395422

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,16 @@ public Test_Class_1(String s) {
2626
public static void main(String[] args) {
2727
new Test_Class_1("main");
2828
}
29+
30+
@Override
31+
public void testClassInt(int i) {
32+
// TODO Auto-generated method stub
33+
34+
}
35+
36+
@Override
37+
public void testClassLong(long i) {
38+
// TODO Auto-generated method stub
39+
40+
}
2941
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ public interface Test_Class_int {
55
static int i5 = 5;
66

77
int i8 = 8;
8+
9+
void testClassInt(int i);
10+
11+
void testClassLong(long i);
812

913
//static Test_ cl1 = new Test_Class_1("test-notstatic1 " + i5 + " " + i8);
1014

0 commit comments

Comments
 (0)