Skip to content

Commit 583bf3a

Browse files
committed
*=,+=, etc.; Clazz.doubleToInt deprecated -- instead, .intValue()
1 parent 60cf7f4 commit 583bf3a

File tree

8 files changed

+387
-531
lines changed

8 files changed

+387
-531
lines changed

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

Lines changed: 331 additions & 314 deletions
Large diffs are not rendered by default.

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

Lines changed: 5 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.eclipse.jdt.core.dom.IMethodBinding;
3434
import org.eclipse.jdt.core.dom.ITypeBinding;
3535
import org.eclipse.jdt.core.dom.IVariableBinding;
36-
import org.eclipse.jdt.core.dom.InfixExpression;
3736
import org.eclipse.jdt.core.dom.Initializer;
3837
import org.eclipse.jdt.core.dom.MethodDeclaration;
3938
import org.eclipse.jdt.core.dom.MethodInvocation;
@@ -57,11 +56,13 @@
5756
import org.eclipse.jdt.core.dom.TypeLiteral;
5857
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
5958

60-
// TODO: assert
61-
6259
// TODO: static calls to static methods do not trigger "musts" dependency
6360

64-
// BH 9/8/2017 -- primitive numeric casting -- (byte) was ignored so that (byte) 0xFF remained 0xFF.
61+
62+
//TODO: doubleToInt; not just /=
63+
64+
// BH 9/7/2017 -- primitive casting for *=,/=,+=,-=,&=,|=,^=
65+
// BH 9/7/2017 -- primitive numeric casting -- (byte) was ignored so that (byte) 0xFF remained 0xFF.
6566
// BH 9/7/2017 -- fixed multiple issues with char and Character
6667
// BH 9/4/2017 -- java.awt, javax.swing, swingjs code added; additional fixes required
6768
// BH 8/30/2017 -- all i/o working, including printf and FileOutputStream
@@ -249,10 +250,6 @@ protected boolean checkSameName(ITypeBinding binding, String name) {
249250
return ((ASTJ2SMapVisitor) getAdaptable(ASTJ2SMapVisitor.class)).checkSameName(binding, name);
250251
}
251252

252-
public boolean isPrimNumberType(String type) {
253-
return ((ASTTypeVisitor) getAdaptable(ASTTypeVisitor.class)).isNumericType(type);
254-
}
255-
256253
public String getClassName() {
257254
return ((ASTTypeVisitor) getAdaptable(ASTTypeVisitor.class)).getClassName();
258255
}
@@ -838,25 +835,6 @@ public boolean visit(EnumConstantDeclaration node) {
838835
return super.visit(node);
839836
}
840837

841-
public boolean visit(FieldAccess node) {
842-
// Expression . Identifier
843-
// TODO: more complicated rules should be considered. read the JavaDoc
844-
IVariableBinding varBinding = node.resolveFieldBinding();
845-
Expression expression = node.getExpression();
846-
if (checkStaticBinding(varBinding)) {
847-
buffer.append('(');
848-
expression.accept(this);
849-
buffer.append(", ");
850-
buffer.append(assureQualifiedName(removeJavaLang(varBinding.getDeclaringClass().getQualifiedName())));
851-
buffer.append(')');
852-
} else {
853-
expression.accept(this);
854-
}
855-
buffer.append(".");
856-
node.getName().accept(this);
857-
return false;
858-
}
859-
860838
public boolean visit(FieldDeclaration node) {
861839
if (isStatic(node))
862840
return false;
@@ -916,173 +894,10 @@ private ITypeBinding resolveParentBinding(ASTNode xparent) {
916894
: null);
917895
}
918896

919-
/**
920-
* The left operand is primitive boolean. Check to see if the operator is ^,
921-
* |, or &, or if the left or right operand is such an expression.
922-
*
923-
* If so, we are going to box this all as a Boolean(....).valueOf()
924-
*
925-
* @param node
926-
* @return
927-
*/
928-
private boolean isBitwiseBinaryOperator(InfixExpression node) {
929-
if (checkSimpleBooleanOperator(node.getOperator().toString())) {
930-
return true;
931-
}
932-
Expression left = node.getLeftOperand();
933-
if (left instanceof InfixExpression) {
934-
if (isBitwiseBinaryOperator((InfixExpression) left)) {
935-
return true;
936-
}
937-
}
938-
Expression right = node.getRightOperand();
939-
if (right instanceof InfixExpression) {
940-
if (isBitwiseBinaryOperator((InfixExpression) right)) {
941-
return true;
942-
}
943-
}
944-
return false;
945-
}
946-
947-
private boolean checkSimpleBooleanOperator(String op) {
948-
return (op.equals("^") || op.equals("|") || op.equals("&"));
949-
}
950-
951-
public boolean visit(InfixExpression node) {
952-
// TODO check for String + Double/Float --> String + ... .toString()
953-
// and change to String + double/float --> String + new
954-
// Double/Float(...).toString()
955-
String constValue = checkConstantValue(node);
956-
if (constValue != null) {
957-
buffer.append(constValue);
958-
return false;
959-
}
960-
ITypeBinding expTypeBinding = node.resolveTypeBinding();
961-
if (expTypeBinding == null)
962-
return false;
963-
boolean toString = (expTypeBinding.getName().indexOf("String") >= 0);
964-
String operator = node.getOperator().toString();
965-
Expression left = node.getLeftOperand();
966-
Expression right = node.getRightOperand();
967-
ITypeBinding leftTypeBinding = left.resolveTypeBinding();
968-
ITypeBinding rightTypeBinding = right.resolveTypeBinding();
969-
if (leftTypeBinding == null || rightTypeBinding == null)
970-
return false;
971-
String leftName = leftTypeBinding.getName();
972-
String rightName = rightTypeBinding.getName();
973-
if ("!==<=>=".indexOf(operator) >= 0) {
974-
// < > <= >= == !=
975-
switch (leftName) {
976-
case "char":
977-
case "Character":
978-
switch (rightName) {
979-
case "char":
980-
case "Character":
981-
boxingNode(left);
982-
buffer.append(' ');
983-
buffer.append(operator);
984-
buffer.append(' ');
985-
boxingNode(right);
986-
return false;
987-
default:
988-
}
989-
break;
990-
default:
991-
break;
992-
}
993-
} else if ("/".equals(operator)) {
994-
// what about CHAR here?
995-
if (leftTypeBinding.isPrimitive() && isPrimNumberType(leftName) && isPrimNumberType(rightName)) {
996-
// left and right are some sort of primitive byte, short, int,
997-
// or, long
998-
// division must take care of this.
999-
// TODO more issues here...
1000-
StringBuffer oldBuffer = buffer;
1001-
buffer = new StringBuffer();
1002-
buffer.append("Clazz.doubleToInt (");
1003-
{
1004-
addOperand(left, toString);
1005-
buffer.append(" / ");
1006-
addOperand(right, toString);
1007-
}
1008-
buffer.append(')');
1009-
List<?> extendedOperands = node.extendedOperands();
1010-
if (extendedOperands.size() > 0) {
1011-
for (Iterator<?> iter = extendedOperands.iterator(); iter.hasNext();) {
1012-
ASTNode element = (ASTNode) iter.next();
1013-
boolean is2Floor = false;
1014-
if (element instanceof Expression) {
1015-
Expression exp = (Expression) element;
1016-
ITypeBinding expBinding = exp.resolveTypeBinding();
1017-
if (isPrimNumberType(expBinding.getName())) {
1018-
buffer.insert(0, "Clazz.doubleToInt (");
1019-
is2Floor = true;
1020-
}
1021-
}
1022-
buffer.append(" / ");
1023-
addOperand((Expression) element, toString);
1024-
if (is2Floor) {
1025-
buffer.append(')');
1026-
}
1027-
}
1028-
}
1029-
oldBuffer.append(buffer);
1030-
buffer = oldBuffer;
1031-
oldBuffer = null;
1032-
return false;
1033-
}
1034-
}
1035-
boolean toBoolean = false;
1036-
if (leftTypeBinding.isPrimitive() && "boolean".equals(leftName) && isBitwiseBinaryOperator(node)) {
1037-
// box this as Boolean(...).valueOf();
1038-
buffer.append(" new Boolean (");
1039-
toBoolean = true;
1040-
}
1041-
// left
1042-
addOperand(left, toString);
1043-
buffer.append(' ');
1044-
// op
1045-
buffer.append(operator);
1046-
if (("==".equals(operator) || "!=".equals(operator)) && !leftTypeBinding.isPrimitive()
1047-
&& !(left instanceof NullLiteral) && !(right instanceof NullLiteral)) {
1048-
buffer.append('=');
1049-
}
1050-
buffer.append(' ');
1051-
// right
1052-
addOperand(right, toString);
1053-
1054-
// ok, this is trouble
1055-
// The extended operands is the preferred way of representing deeply
1056-
// nested expressions of the form L op R op R2 op R3... where the same
1057-
// operator appears between all the operands (the most common case being
1058-
// lengthy string concatenation expressions). Using the extended
1059-
// operands keeps the trees from getting too deep; this decreases the
1060-
// risk is running out of thread stack space at runtime when traversing
1061-
// such trees. ((a + b) + c) + d would be translated to: leftOperand: a
1062-
// rightOperand: b extendedOperands: {c, d} operator: +
1063-
1064-
List<?> extendedOperands = node.extendedOperands();
1065-
if (extendedOperands.size() > 0) {
1066-
for (Iterator<?> iter = extendedOperands.iterator(); iter.hasNext();) {
1067-
buffer.append(' ');
1068-
buffer.append(operator);
1069-
buffer.append(' ');
1070-
ASTNode element = (ASTNode) iter.next();
1071-
addOperand((Expression) element, toString);
1072-
}
1073-
}
1074-
if (toBoolean) {
1075-
// unbox Boolean(...)
1076-
buffer.append(").valueOf()");
1077-
}
1078-
return false;
1079-
}
1080-
1081897
public boolean visit(Initializer node) {
1082898
if (checkj2sIgnore(node)) {
1083899
return false;
1084900
}
1085-
// visitList(node.getBody().statements(), "\r\n");
1086901
node.getBody().accept(this);
1087902
buffer.append("\r\n");
1088903
return false;

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,4 @@ public String assureQualifiedName(String name) {
271271
return name;
272272
}
273273

274-
public boolean isNumericType(String type) {
275-
// BH -- removed char
276-
if ("int".equals(type)
277-
|| "long".equals(type)
278-
|| "byte".equals(type)
279-
|| "short".equals(type)) {
280-
return true;
281-
}
282-
return false;
283-
}
284-
285274
}

sources/net.sf.j2s.core/test/dev/js/j2sSwingJS.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4067,25 +4067,26 @@ Number.compare = function(a,b) { return (a < b ? -1 : a == b ? 0 : 1) };
40674067

40684068
m$(Number,"shortValue",
40694069
function(){
4070-
var x = Math.round(this)&0xffff;
4071-
return (this < 0 && x > 0 || x == 0xffff ? x - 0x10000 : x);
4070+
var x = (this|0)&0xffff;
4071+
return (x >= 0x8000 ? x - 0x10000 : x);
40724072
});
40734073

40744074
m$(Number,"byteValue",
40754075
function(){
4076-
var x = Math.round(this)&0xff;
4077-
return (this < 0 && x > 0 || x == 0xff ? x - 0x100 : x);
4076+
var x = (this|0)&0xff;
4077+
return (x >= 0x80 ? x - 0x100 : x);
40784078
});
40794079

4080+
40804081
m$(Number,"intValue",
40814082
function(){
4082-
var x = Math.round(this)&0xffffffff;
4083-
return (this < 0 && x > 0 || x == 0xffffffff ? x - 0x100000000 : x);
4083+
var x = (this|0)&0xffffffff;
4084+
return (x >= 0x80000000 ? x - 0x100000000 : x);
40844085
});
40854086

40864087
m$(Number,"longValue",
40874088
function(){
4088-
return Math.round(this);
4089+
return (this|0);
40894090
});
40904091

40914092
m$(Number,"floatValue",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
class Test_Boolean {
44

55
public static void main(String[] args) {
6-
System.out.println("false = " + new Boolean("false") + "; true = " + new Boolean("true"));
6+
assert(new Boolean("true"));
7+
assert(!new Boolean("false"));
8+
System.out.println("Test_Boolean OK");
79
}
810

911
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ public static void main(String[] args) {
6161
s += 3 + cc + 5f;
6262

6363
s += new Double(3);
64-
s += new Double(3) + cc + 5;
65-
System.out.println(s);
66-
assert(s.equals("33.53o53o5.03.03.0o5null33.0119119.03.0119.0"));
64+
s += new Double(3) + cc++ + 5;
65+
s += new Double(3) + ++cc + 5;
66+
System.out.println(s + cc);
67+
assert(s.equals("33.53o53o5.03.03.0o5null33.0119119.03.0119.0121.0q"));
6768
d = 333.5;
6869
assert((int)d == 333);
6970
assert((int)-d == -333);

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,29 @@
33
class Test_DivEqual {
44

55
public static void main(String[] args) {
6-
int x = 99;
6+
7+
int x = 100;
78
x /= 3;
8-
System.out.println("x == 33 is " + (x == 33));
9+
assert(x == 33);
910
x /= 3.5;
10-
System.out.println("x == 9 is " + (x == 9));
11+
assert(x == 9);
12+
x = 3;
13+
x *= 3.5;
14+
assert(x == 10);
15+
x = 3;
16+
x += 3.5 - x;
17+
assert(x == 6);
18+
x = 3;
19+
x -= 3.5 + x - 3.5;
20+
assert(x == 0);
21+
byte b = 50;
22+
b *= 20;
23+
assert(b == -24);
24+
b = 50;
25+
b += 205;
26+
assert(b == -1);
27+
System.out.println(b / 33f);
28+
System.out.println("Test_DivEqual OK");
1129
}
1230

1331
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,28 @@
22

33
class Test_Static {
44

5-
static int y;
5+
private static int y;
6+
private static char c = 'c';
7+
static String s;
68
static {
79
int x = 3;
810
y = x;
911
Test_Boolean.main(null);
1012
}
1113

1214
public static void main(String[] args) {
13-
System.out.println(y == 3);
15+
assert(y == 3);
16+
new Test_Static().y++;
17+
new Test_Static().s += "test1" + c++ + 3 + 5.5f + c + 5.5;
18+
assert(s.equals("nulltest1c35.5d5.5"));
19+
int i = 3 + y + (new Test_Static().y++);
20+
assert(i == 11 && y == 4);
21+
i += 3 + y + ++(new Test_Static().y);
22+
assert(y == 5 && i == 25);
23+
String y1 ="0";
24+
y1 += "test" + c + 3 + 5.5f + c + 5.5;
25+
assert(y1.equals("0testd35.5d5.5"));
26+
System.out.println("Test_Static OK");
1427
}
1528

1629
}

0 commit comments

Comments
 (0)