Skip to content

Commit 3fb662c

Browse files
author
zhourenjian@gmail.com
committed
Fixing bug of incorrect cast expression compiling
1 parent d1888fa commit 3fb662c

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

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

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.eclipse.jdt.core.dom.Modifier;
4242
import org.eclipse.jdt.core.dom.Name;
4343
import org.eclipse.jdt.core.dom.NullLiteral;
44+
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
4445
import org.eclipse.jdt.core.dom.PrimitiveType;
4546
import org.eclipse.jdt.core.dom.QualifiedName;
4647
import org.eclipse.jdt.core.dom.ReturnStatement;
@@ -447,12 +448,29 @@ public boolean visit(CastExpression node) {
447448
|| pType.getPrimitiveTypeCode() == PrimitiveType.LONG) {
448449
if ("char".equals(name)) {
449450
buffer.append("(");
450-
expression.accept(this);
451+
if (expression instanceof ParenthesizedExpression) {
452+
ParenthesizedExpression pe = (ParenthesizedExpression) expression;
453+
pe.getExpression().accept(this);
454+
} else {
455+
expression.accept(this);
456+
}
451457
buffer.append (").charCodeAt (0)");
452458
return false;
453459
} else if ("float".equals(name) || "double".equals(name)) {
454-
buffer.append("Math.round (");
455-
expression.accept(this);
460+
//buffer.append("Math.round (");
461+
buffer.append("Clazz.");
462+
buffer.append(name);
463+
buffer.append("To");
464+
String targetType = pType.getPrimitiveTypeCode().toString();
465+
buffer.append(targetType.substring(0, 1).toUpperCase());
466+
buffer.append(targetType.substring(1));
467+
buffer.append (" (");
468+
if (expression instanceof ParenthesizedExpression) {
469+
ParenthesizedExpression pe = (ParenthesizedExpression) expression;
470+
pe.getExpression().accept(this);
471+
} else {
472+
expression.accept(this);
473+
}
456474
buffer.append (")");
457475
return false;
458476
}
@@ -465,10 +483,18 @@ public boolean visit(CastExpression node) {
465483
// return false;
466484
} else if ("float".equals(name) || "double".equals(name)) {
467485
// TODO:
468-
buffer.append("String.fromCharCode (");
469-
buffer.append("Math.round (");
470-
expression.accept(this);
471-
buffer.append (")");
486+
buffer.append("Clazz.");
487+
buffer.append(name);
488+
buffer.append("ToChar (");
489+
// buffer.append("String.fromCharCode (");
490+
// buffer.append("Math.round (");
491+
if (expression instanceof ParenthesizedExpression) {
492+
ParenthesizedExpression pe = (ParenthesizedExpression) expression;
493+
pe.getExpression().accept(this);
494+
} else {
495+
expression.accept(this);
496+
}
497+
// buffer.append (")");
472498
buffer.append (")");
473499
return false;
474500
} else if ("int".equals(name) || "byte".equals(name)
@@ -499,7 +525,12 @@ public boolean visit(CastExpression node) {
499525
}
500526
}
501527
buffer.append("String.fromCharCode (");
502-
expression.accept(this);
528+
if (expression instanceof ParenthesizedExpression) {
529+
ParenthesizedExpression pe = (ParenthesizedExpression) expression;
530+
pe.getExpression().accept(this);
531+
} else {
532+
expression.accept(this);
533+
}
503534
buffer.append (")");
504535
return false;
505536
}
@@ -615,7 +646,9 @@ public boolean visit(ClassInstanceCreation node) {
615646
currentBlockForVisit = blockLevel;
616647
visitedVars = variableVisitor.visitedVars = new ArrayList();
617648
variableVisitor.normalVars = new ArrayList();
649+
methodDeclareStack.push(binding.getKey());
618650
anonDeclare.accept(this);
651+
methodDeclareStack.pop();
619652
buffer.append(", ");
620653

621654
buffer.append("Clazz.innerTypeInstance (");
@@ -1429,7 +1462,9 @@ public boolean visit(InfixExpression node) {
14291462
StringBuffer tmpBuffer = buffer;
14301463
buffer = new StringBuffer();
14311464

1432-
buffer.append("Math.floor (");
1465+
//buffer.append("Math.floor (");
1466+
// TODO
1467+
buffer.append("Clazz.doubleToInt (");
14331468
charVisit(left, beCare);
14341469
buffer.append(' ');
14351470
buffer.append(operator);
@@ -1445,7 +1480,8 @@ public boolean visit(InfixExpression node) {
14451480
Expression exp = (Expression) element;
14461481
ITypeBinding expBinding = exp.resolveTypeBinding();
14471482
if (isIntegerType(expBinding.getName())) {
1448-
buffer.insert(0, "Math.floor (");
1483+
//buffer.insert(0, "Math.floor (");
1484+
buffer.insert(0, "Clazz.doubleToInt (");
14491485
is2Floor = true;
14501486
}
14511487
}

sources/net.sf.j2s.core/src/net/sf/j2s/core/compiler/Java2ScriptCompiler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,16 @@ public static String[] getClazzAbbrMap() {
539539
"Clazz.newCharArray", "AC", //
540540
"Clazz.newBooleanArray", "Ab", //
541541
//"Clazz.newStringArray", "AX", //
542+
"Clazz.floatToInt", "fI", //
543+
"Clazz.floatToByte", "fB", //
544+
"Clazz.floatToShort", "fS", //
545+
"Clazz.floatToLong", "fL", //
546+
"Clazz.floatToChar", "fC", //
547+
"Clazz.doubleToInt", "dI", //
548+
"Clazz.doubleToByte", "dB", //
549+
"Clazz.doubleToShort", "dS", //
550+
"Clazz.doubleToLong", "dL", //
551+
"Clazz.doubleToChar", "dC", //
542552
"Clazz.instanceOf", "O", //
543553
"Clazz.exceptionOf", "e", //sgurin
544554
"Clazz.inheritArgs", "G", //

0 commit comments

Comments
 (0)