Skip to content

Commit 2e64e57

Browse files
committed
bug fixes working on ReturnStatement
1 parent 9036c23 commit 2e64e57

File tree

4 files changed

+203
-44
lines changed

4 files changed

+203
-44
lines changed

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTKeywordVisitor.java

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,43 +1219,68 @@ public boolean visit(QualifiedName node) {
12191219
return false;
12201220
}
12211221

1222+
@SuppressWarnings("null")
12221223
public boolean visit(ReturnStatement node) {
12231224
buffer.append("return");
12241225
Expression expression = node.getExpression();
1225-
if (expression != null) {
1226-
buffer.append(' ');
1227-
boolean needCharWrapping = false;
1228-
ASTNode parent = node.getParent();
1229-
while (parent != null && !(parent instanceof MethodDeclaration)) {
1230-
parent = parent.getParent();
1231-
}
1232-
if (parent != null) {
1233-
MethodDeclaration m = (MethodDeclaration) parent;
1234-
IMethodBinding binding = m.resolveBinding();
1235-
if (binding != null) {
1236-
ITypeBinding returnType = binding.getReturnType();
1237-
if (returnType != null && "char".equals(returnType.getName())) {
1238-
needCharWrapping = true;
1239-
}
1240-
}
1241-
}
1242-
if (needCharWrapping) {
1243-
ITypeBinding tBinding = expression.resolveTypeBinding();
1244-
if (tBinding != null && !("char".equals(tBinding.getName()))) {
1245-
buffer.append("String.fromCharCode (");
1226+
if (expression == null)
1227+
return false;
1228+
buffer.append(' ');
1229+
ASTNode parent = node.getParent();
1230+
while (parent != null && !(parent instanceof MethodDeclaration)) {
1231+
parent = parent.getParent();
1232+
}
1233+
ITypeBinding expType = expression.resolveTypeBinding();
1234+
IMethodBinding mBinding = (parent == null ? null : ((MethodDeclaration) parent).resolveBinding());
1235+
ITypeBinding retType = (mBinding == null || expType == null ? null : mBinding.getReturnType());
1236+
boolean done = false;
1237+
if (expression.resolveUnboxing() || expression.resolveUnboxing()) {
1238+
boxingNode(expression);
1239+
done = true;
1240+
}
1241+
if (!done && retType != null && retType != expType) {
1242+
String retName = retType.getName();
1243+
String expName = expType.getName();
1244+
//buffer.append("<<" + retName + " f(){return " + expName + " " + expression + "}");
1245+
switch (retName) {
1246+
case "char":
1247+
// char f() { return ... }
1248+
if (!("char".equals(expName))) {
1249+
buffer.append("String.fromCharCode(");
12461250
expression.accept(this);
12471251
buffer.append(")");
1248-
} else {
1249-
expression.accept(this);
1252+
done = true;
12501253
}
1251-
} else {
1252-
expression.accept(this);
1254+
break;
1255+
case "String":
1256+
break;
1257+
default:
1258+
if (("char".equals(expName))) {
1259+
if ("Character".equals(retName)) {
1260+
buffer.append("new Character(");
1261+
expression.accept(this);
1262+
buffer.append(")");
1263+
} else {
1264+
buffer.append("(");
1265+
expression.accept(this);
1266+
buffer.append(").charCodeAt(0)");
1267+
}
1268+
done = true;
1269+
}
1270+
break;
12531271
}
1272+
1273+
}
1274+
if (!done) {
1275+
expression.accept(this);
12541276
}
1255-
buffer.append(";\r\n");
12561277
return false;
12571278
}
12581279

1280+
public void endVisit(ReturnStatement node) {
1281+
buffer.append(";\r\n");
1282+
}
1283+
12591284
public boolean visit(StringLiteral node) {
12601285
buffer.append(node.getEscapedValue());
12611286
return false;

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTScriptVisitor.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.eclipse.jdt.core.dom.PrimitiveType;
4646
import org.eclipse.jdt.core.dom.PrimitiveType.Code;
4747
import org.eclipse.jdt.core.dom.QualifiedName;
48+
import org.eclipse.jdt.core.dom.ReturnStatement;
4849
import org.eclipse.jdt.core.dom.SimpleName;
4950
import org.eclipse.jdt.core.dom.SimpleType;
5051
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
@@ -76,6 +77,101 @@
7677
// DONE: array handling in instanceof and reflection
7778
// DONE: String + double/float/Double/Float --> new Double/Float().toString()
7879
// TODO: Q: Good assumption that generic parameterization can be ignored? put<K,V> vs put<K>?
80+
/*
81+
*
82+
83+
parameter and return values are not indicated as boxed or unboxed but actually are.
84+
85+
Test_Char
86+
System.out.println(getCharacterFromChar1('c') == 'c');
87+
System.out.println(getCharacterFromChar2('c') == 'c');
88+
System.out.println(getCharFromCharacter1('c') == 'c');
89+
System.out.println(getCharFromCharacter2('c') == 'c');
90+
91+
System.out.println$Z((test.Test_Char.getCharacterFromChar1$C('c')).charValue () === 99);
92+
System.out.println$Z((test.Test_Char.getCharacterFromChar2$C('c')).charValue () === 99);
93+
System.out.println$Z(test.Test_Char.getCharFromCharacter1$Character(99) == 'c');
94+
System.out.println$Z(test.Test_Char.getCharFromCharacter2$Character(99) == 'c');
95+
96+
should be
97+
98+
System.out.println$Z((test.Test_Char.getCharacterFromChar1$C('c')).charValue () === 'c');
99+
System.out.println$Z((test.Test_Char.getCharacterFromChar2$C('c')).charValue () === 'c');
100+
System.out.println$Z(test.Test_Char.getCharFromCharacter1$Character(new Character('c')) == 'c');
101+
System.out.println$Z(test.Test_Char.getCharFromCharacter2$Character(new Character('c')) == 'c');
102+
103+
104+
105+
TODO #25 for int f() { return 'o' } -- will return a string, not int
106+
107+
TODO #24 in a file starting with an interface and also including a class, only the class is found.
108+
109+
interface Editable {
110+
EditInfo getEditInfo(int n);
111+
void setEditValue(int n, EditInfo ei);
112+
}
113+
114+
class EditDialog extends Dialog implements AdjustmentListener, ActionListener, ItemListener {
115+
...
116+
117+
TODO #21 (byte) ignored so that 0xFF remains 0xFF.
118+
119+
With the implementation of Int8Array in j2sJmol, it becomes more important to consider
120+
the issue of no byte type in JavaScript. In particular, the construction
121+
122+
bytes[3] == (byte) 0xFF
123+
124+
is being translated as
125+
126+
bytes[3] == 0xFF
127+
128+
and will evaluate FALSE since JavaScript Int8Array elements cannot have value 255.
129+
130+
The solution is to recast the byte as an integer, not the other way around:
131+
132+
(bytes[3] & 0xFF) == 0xFF
133+
134+
which now works for both Java and JavaScript.
135+
136+
137+
TODO #16 when an inner public class is called by another class using instanceOf, that inner class becomes an optional load.
138+
but optional loads must still be loaded, and unless declared in package.js, J2S will look for xxx.xxx.Outer/Inner.js
139+
because the inner classes are not fully declared.
140+
141+
Solution is to switch to requiring the outer class, not the inner class:
142+
143+
@J2SRequireImport(NumberFormat.class)
144+
@J2SIgnoreImport(NumberFormat.Field.class)
145+
public class NumberFormatter extends InternationalFormatter...
146+
147+
148+
149+
TODO #14 in java.awt.image.Raster, we have a static block that
150+
creates new Objects. In that case, we need to add the annotation:
151+
152+
@J2SRequireImport({ jsjava.awt.image.SinglePixelPackedSampleModel.class, jssun.awt.image.IntegerInterleavedRaster.class, jssun.awt.image.ByteInterleavedRaster.class })
153+
154+
155+
TODO #12 Inner classes must not call other inner classes defined after them in a file.
156+
This showed up in java.awt.geom.Path2D.Float.CopyIterator, which extends
157+
java.awt.geom.Path2D.Iterator. Since the Iterator is in the code after CopyIterator,
158+
the reference to java.awt.geom.Path2D.Iterator in
159+
160+
c$ = Clazz.decorateAsClass (function () {
161+
this.floatCoords = null;
162+
Clazz.instantialize (this, arguments);
163+
}, java.awt.geom.Path2D.Float, "CopyIterator", java.awt.geom.Path2D.Iterator);
164+
165+
is null, and then CopyIterator does not extend Iterator.
166+
167+
TODO #4 @J2SRequireImport({jsjava.util.PropertyResourceBundle.class})
168+
169+
is required for public abstract class ResourceBundle because the inner class
170+
ResourceBundle.Control requires it, but for some reason it is not included in the
171+
MUST list in the Clazz.load() call.
172+
173+
174+
*/
79175

80176
/**
81177
*

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

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,65 @@
33
import java.util.Properties;
44

55
class Test_Char {
6+
7+
public static void main(String[] args) {
8+
System.out.println("'K'=" + (char) 75 + " Character('K')=" + new Character('K'));
9+
test((char) 75);
10+
String s = new String("\ud800\udf84");
11+
char c = (char) 0xdf84;
12+
int i = (int) c;
13+
c = (char) 0xd800df84;
14+
System.out.println(i == 57220);
15+
System.out.println(s.length() + " " + Character.charCount(s.codePointAt(0)) + " " + Character.getName(c));
16+
17+
System.out.println(getIntFromChar1('c') == 99);
18+
System.out.println(getIntFromChar2('c') == 99);
19+
20+
System.out.println(getCharacterFromChar1('c') == 'c');
21+
System.out.println(getCharacterFromChar2('c') == 'c');
22+
23+
System.out.println(getCharFromCharacter1('c') == 'c');
24+
System.out.println(getCharFromCharacter2('c') == 'c');
25+
26+
System.out.println(getIntFromInteger(99) == 'c');
27+
28+
System.out.println(getIntegerFromInt(Integer.valueOf(99)) == 'c');
29+
}
30+
31+
private static int getIntFromInteger(Integer i) {
32+
return i;
33+
}
634

7-
public static void main(String[] args) {
8-
System.out.println("'K'=" + (char)75 + " Character('K')=" + new Character('K'));
9-
test((char) 75);
10-
String s = new String("\ud800\udf84");
11-
char c = (char)0xdf84;
12-
int i = (int) c;
13-
c = (char) 0xd800df84;
14-
System.out.println("c=" + c + " i = " + i);
15-
System.out.println(s.length() + " " + Character.charCount(s.codePointAt(0)) + " " + Character.getName(c));
16-
}
35+
private static int getIntegerFromInt(int i) {
36+
return i;
37+
}
38+
39+
private static int getIntFromChar1(char c) {
40+
return (int) c;
41+
}
42+
43+
private static int getIntFromChar2(char c) {
44+
return c;
45+
}
46+
47+
private static Character getCharacterFromChar1(char c) {
48+
return new Character(c);
49+
}
50+
51+
private static Character getCharacterFromChar2(char c) {
52+
return c;
53+
}
54+
55+
private static char getCharFromCharacter1(Character c) {
56+
return c.charValue();
57+
}
58+
59+
private static char getCharFromCharacter2(Character c) {
60+
return c;
61+
}
1762

1863
private static void test(char c) {
1964
System.out.println("testing char " + c);
2065
}
21-
66+
2267
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,5 @@ public static void main(String[] args) {
1414
new Test_Ints();
1515
new Test_Ints(3,5,6);
1616
new Test_Ints(3.5f,5.5f,6.5f);
17-
System.out.println(getIntFromChar('c') == 99);
18-
}
19-
20-
private static int getIntFromChar(char c) {
21-
return c;
22-
23-
FAILS
2417
}
2518
}

0 commit comments

Comments
 (0)