Skip to content

Commit 06149de

Browse files
authored
Merge pull request #240 from BobHanson/hanson1
Hanson1 issue #238
2 parents ce8307b + 764b732 commit 06149de

File tree

6 files changed

+72
-20
lines changed

6 files changed

+72
-20
lines changed
-102 KB
Binary file not shown.

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ public static void main(String[] args) {
1616
boolean b = (e instanceof Error);
1717
System.out.println(b);
1818
}
19+
20+
Throwable error = null;
21+
/** @j2sNative error = new Error(); */
22+
assert error instanceof Throwable;
23+
24+
Error x = new java.lang.Error();
25+
System.out.println(x.getMessage() + "??");
26+
27+
28+
Throwable typeError = null;
29+
/** @j2sNative
30+
*
31+
* typeError = new TypeError(); */
32+
assert error instanceof Throwable; // pass
33+
assert typeError instanceof Throwable; // fail
34+
System.out.println(typeError.getStackTrace());
35+
1936
System.out.println("Test_Error OK");
2037
}
2138

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ public void testDollar(Test_Abstract_a abs) {
7878
}
7979

8080
public static void main(String[] args) {
81+
try {
82+
Class<?> t = Class.forName("test.Test_InnerAo$AiStat");
83+
System.out.println(t.getName() + " loaded");
84+
System.out.println(t.newInstance());
85+
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
86+
// TODO Auto-generated catch block
87+
e.printStackTrace();
88+
assert(false);
89+
}
90+
8191
Test_Inner inner = new Test_Inner(6) {};
8292
try {
8393
System.out.println(inner.getClass().getMethod("testDollar", new Class<?>[] {Test_Abstract_a.class}).getName());

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ public void run() {
2828

2929
x.run();
3030
}
31-
class Ai {
31+
32+
public static class AiStat {
33+
public AiStat() {
34+
System.out.println("loaded " + getClass().getName());
35+
}
36+
}
37+
38+
class Ai {
3239

3340
public String atesti = "Ai";
3441

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// j2sClazz.js
2-
// NOTE: updates to this file should be copies to j2sjmol.js
2+
// NOTE: updates to this file should be copied to j2sjmol.js
33

44
// latest author: Bob Hanson, St. Olaf College, hansonr@stolaf.edu
55

66
// NOTES by Bob Hanson
77

88
// Google closure compiler cannot handle Clazz.new or Clazz.super
99

10+
// BH 2022.09.20 fix for Class.forName not loading static inner classes directly
11+
// BH 2022.09.20 fix for default toString for classes using "." name not "$" name for inner classes
1012
// BH 2022.09.15 fix for new Error() failing; just letting java.lang.Error subclass Throwable
1113
// BH 2022.09.08 Fix new Test_Inner().getClass().getMethod("testDollar", new Class<?>[] {Test_Abstract_a.class}).getName()
1214
// BH 2022.04.19 TypeError and ResourceError gain printStackTrace$() methods
@@ -1493,7 +1495,7 @@ var _jsid = 0;
14931495
addProto(proto, "notifyAll$", function () {});
14941496
addProto(proto, "wait$", function () {alert("Object.wait was called!" + arguments.callee.caller.toString())});
14951497
addProto(proto, "toString$", Object.prototype.toString);
1496-
addProto(proto, "toString", function () { return (this.__CLASS_NAME__ ? "[" + this.__CLASS_NAME__ + " object]" : this.toString$.apply(this, arguments)); });
1498+
addProto(proto, "toString", function () { return (this.__CLASS_NAME__ ? "[" + (this.__CLASS_NAME$__ || this.__CLASS_NAME__) + " object]" : this.toString$.apply(this, arguments)); });
14971499

14981500
})(Clazz._O.prototype);
14991501

@@ -2632,14 +2634,17 @@ Clazz._4Name = function(clazzName, applet, state, asClazz, initialize, isQuiet)
26322634
}
26332635
if (!isok) {
26342636
var name2 = null;
2635-
if (clazzName.indexOf("$") >= 0) {
2637+
var pt = clazzName.lastIndexOf("$");
2638+
if (pt >= 0) {
26362639
// BH we allow Java's java.swing.JTable.$BooleanRenderer as a stand-in
26372640
// for java.swing.JTable.BooleanRenderer
26382641
// when the static nested class is created using declareType
26392642
name2 = clazzName.replace(/\$/g,".");
26402643
if (Clazz._isClassDefined(name2)) {
26412644
clazzName = name2;
26422645
} else {
2646+
cl = Clazz._4Name(clazzName.substring(0, pt), applet, state, true, initialize, isQuiet);
2647+
cl && (clazzName = name2);
26432648
name2 = null;
26442649
}
26452650
}
@@ -6705,8 +6710,6 @@ var printStackTrace = function(e, ps) {
67056710
}
67066711
}
67076712

6708-
TypeError.prototype.printStackTrace$ = ReferenceError.prototype.printStackTrace$ = function() { console.log(this);printStackTrace(this,System.err) }
6709-
67106713
var C$ = Clazz.newClass(java.lang, "Throwable", function () {
67116714
Clazz.newInstance(this, arguments);
67126715
}, null, java.io.Serializable);
@@ -6735,7 +6738,7 @@ this.detailMessage = (cause == null ? this.stack : cause.toString ());
67356738
this.cause = cause;
67366739
}, 1);
67376740

6738-
m$(C$, 'getMessage$', function () {return this.message || this.detailMessage});
6741+
m$(C$, 'getMessage$', function () {return this.message || this.detailMessage || null});
67396742

67406743
m$(C$, 'getLocalizedMessage$', function () {
67416744
return this.getMessage$();
@@ -6917,11 +6920,17 @@ if(lineNum>=0){
69176920
});
69186921

69196922

6920-
TypeError.prototype.getMessage$ || (TypeError.prototype.getMessage$ = TypeError.prototype.getLocalizedMessage$
6923+
TypeError.prototype.getMessage$ || (
6924+
6925+
ReferenceError.prototype.getMessage$ = TypeError.prototype.getMessage$
6926+
= ReferenceError.prototype.getMessage$ = TypeError.prototype.getLocalizedMessage$
69216927
= function(){ return (this.stack ? this.stack : this.message || this.toString()) + (this.getStackTrace ? this.getStackTrace$() : Clazz._getStackTrace())});
6922-
TypeError.prototype.printStackTrace$ = function(){System.out.println(this + "\n" + this.stack)};
6923-
TypeError.prototype.printStackTrace$java_io_PrintStream = function(stream){stream.println$S(this + "\n" + this.stack);};
6924-
TypeError.prototype.printStackTrace$java_io_PrintWriter = function(printer){printer.println$S(this + "\n" + this.stack);};
6928+
6929+
TypeError.prototype.getStackTrace$ = ReferenceError.prototype.getStackTrace$ = function() { return Clazz._getStackTrace() }
6930+
TypeError.prototype.printStackTrace$ = ReferenceError.prototype.printStackTrace$ = function() { printStackTrace(this,System.err) }
6931+
ReferenceError.prototype.printStackTrace$java_io_PrintStream = TypeError.prototype.printStackTrace$java_io_PrintStream = function(stream){stream.println$S(this + "\n" + this.stack);};
6932+
ReferenceError.prototype.printStackTrace$java_io_PrintWriter = TypeError.prototype.printStackTrace$java_io_PrintWriter = function(printer){printer.println$S(this + "\n" + this.stack);};
6933+
69256934
Clazz.Error = Error;
69266935

69276936
var declareType = function(prefix, name, clazzSuper, interfacez) {

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14045,6 +14045,8 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
1404514045

1404614046
// Google closure compiler cannot handle Clazz.new or Clazz.super
1404714047

14048+
// BH 2022.09.20 fix for Class.forName not loading static inner classes directly
14049+
// BH 2022.09.20 fix for default toString for classes using "." name not "$" name for inner classes
1404814050
// BH 2022.09.15 fix for new Error() failing; just letting java.lang.Error subclass Throwable
1404914051
// BH 2022.09.08 Fix new Test_Inner().getClass().getMethod("testDollar", new Class<?>[] {Test_Abstract_a.class}).getName()
1405014052
// BH 2022.04.19 TypeError and ResourceError gain printStackTrace$() methods
@@ -15531,7 +15533,7 @@ var _jsid = 0;
1553115533
addProto(proto, "notifyAll$", function () {});
1553215534
addProto(proto, "wait$", function () {alert("Object.wait was called!" + arguments.callee.caller.toString())});
1553315535
addProto(proto, "toString$", Object.prototype.toString);
15534-
addProto(proto, "toString", function () { return (this.__CLASS_NAME__ ? "[" + this.__CLASS_NAME__ + " object]" : this.toString$.apply(this, arguments)); });
15536+
addProto(proto, "toString", function () { return (this.__CLASS_NAME__ ? "[" + (this.__CLASS_NAME$__ || this.__CLASS_NAME__) + " object]" : this.toString$.apply(this, arguments)); });
1553515537

1553615538
})(Clazz._O.prototype);
1553715539

@@ -16670,14 +16672,17 @@ Clazz._4Name = function(clazzName, applet, state, asClazz, initialize, isQuiet)
1667016672
}
1667116673
if (!isok) {
1667216674
var name2 = null;
16673-
if (clazzName.indexOf("$") >= 0) {
16675+
var pt = clazzName.lastIndexOf("$");
16676+
if (pt >= 0) {
1667416677
// BH we allow Java's java.swing.JTable.$BooleanRenderer as a stand-in
1667516678
// for java.swing.JTable.BooleanRenderer
1667616679
// when the static nested class is created using declareType
1667716680
name2 = clazzName.replace(/\$/g,".");
1667816681
if (Clazz._isClassDefined(name2)) {
1667916682
clazzName = name2;
1668016683
} else {
16684+
cl = Clazz._4Name(clazzName.substring(0, pt), applet, state, true, initialize, isQuiet);
16685+
cl && (clazzName = name2);
1668116686
name2 = null;
1668216687
}
1668316688
}
@@ -20743,8 +20748,6 @@ var printStackTrace = function(e, ps) {
2074320748
}
2074420749
}
2074520750

20746-
TypeError.prototype.printStackTrace$ = ReferenceError.prototype.printStackTrace$ = function() { console.log(this);printStackTrace(this,System.err) }
20747-
2074820751
var C$ = Clazz.newClass(java.lang, "Throwable", function () {
2074920752
Clazz.newInstance(this, arguments);
2075020753
}, null, java.io.Serializable);
@@ -20773,7 +20776,7 @@ this.detailMessage = (cause == null ? this.stack : cause.toString ());
2077320776
this.cause = cause;
2077420777
}, 1);
2077520778

20776-
m$(C$, 'getMessage$', function () {return this.message || this.detailMessage});
20779+
m$(C$, 'getMessage$', function () {return this.message || this.detailMessage || null});
2077720780

2077820781
m$(C$, 'getLocalizedMessage$', function () {
2077920782
return this.getMessage$();
@@ -20955,11 +20958,17 @@ if(lineNum>=0){
2095520958
});
2095620959

2095720960

20958-
TypeError.prototype.getMessage$ || (TypeError.prototype.getMessage$ = TypeError.prototype.getLocalizedMessage$
20961+
TypeError.prototype.getMessage$ || (
20962+
20963+
ReferenceError.prototype.getMessage$ = TypeError.prototype.getMessage$
20964+
= ReferenceError.prototype.getMessage$ = TypeError.prototype.getLocalizedMessage$
2095920965
= function(){ return (this.stack ? this.stack : this.message || this.toString()) + (this.getStackTrace ? this.getStackTrace$() : Clazz._getStackTrace())});
20960-
TypeError.prototype.printStackTrace$ = function(){System.out.println(this + "\n" + this.stack)};
20961-
TypeError.prototype.printStackTrace$java_io_PrintStream = function(stream){stream.println$S(this + "\n" + this.stack);};
20962-
TypeError.prototype.printStackTrace$java_io_PrintWriter = function(printer){printer.println$S(this + "\n" + this.stack);};
20966+
20967+
TypeError.prototype.getStackTrace$ = ReferenceError.prototype.getStackTrace$ = function() { return Clazz._getStackTrace() }
20968+
TypeError.prototype.printStackTrace$ = ReferenceError.prototype.printStackTrace$ = function() { printStackTrace(this,System.err) }
20969+
ReferenceError.prototype.printStackTrace$java_io_PrintStream = TypeError.prototype.printStackTrace$java_io_PrintStream = function(stream){stream.println$S(this + "\n" + this.stack);};
20970+
ReferenceError.prototype.printStackTrace$java_io_PrintWriter = TypeError.prototype.printStackTrace$java_io_PrintWriter = function(printer){printer.println$S(this + "\n" + this.stack);};
20971+
2096320972
Clazz.Error = Error;
2096420973

2096520974
var declareType = function(prefix, name, clazzSuper, interfacez) {

0 commit comments

Comments
 (0)