Skip to content

Commit 912c114

Browse files
author
jossonsmith
committed
Fixed a bug that (char/byte/int/long/float) (...) is not converted as expected.
For example (char) (int_number) should be converted to String.fromCharCode (int_number), and (int) (char_number) should be converted to char_number.charCodeAt (0);
1 parent 038e947 commit 912c114

1 file changed

Lines changed: 42 additions & 8 deletions

File tree

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

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,21 +308,55 @@ public boolean visit(CastExpression node) {
308308
* TODO: some casting should have its meaning!
309309
*/
310310
if (type.isPrimitiveType()) {
311+
ITypeBinding resolveTypeBinding = node.getExpression().resolveTypeBinding();
312+
String name = resolveTypeBinding.getName();
311313
PrimitiveType pType = (PrimitiveType) type;
312314
if (pType.getPrimitiveTypeCode() == PrimitiveType.INT
313315
|| pType.getPrimitiveTypeCode() == PrimitiveType.BYTE
314316
|| pType.getPrimitiveTypeCode() == PrimitiveType.SHORT
315317
|| pType.getPrimitiveTypeCode() == PrimitiveType.LONG) {
316-
buffer.append("parseInt (");
317-
node.getExpression().accept(this);
318-
buffer.append (")");
319-
return false;
318+
if ("char".equals(name)) {
319+
buffer.append("(");
320+
node.getExpression().accept(this);
321+
buffer.append (").charCodeAt (0)");
322+
return false;
323+
} else if ("float".equals(name) || "double".equals(name)) {
324+
// TODO:
325+
buffer.append("Math.round (");
326+
node.getExpression().accept(this);
327+
buffer.append (")");
328+
// } else if ("int".equals(name) || "byte".equals(name)
329+
// || "double".equals(name) || "float".equals(name)
330+
// || "short".equals(name) || "long".equals(name)) {
331+
332+
}
333+
// buffer.append("parseInt (");
334+
// node.getExpression().accept(this);
335+
// buffer.append (")");
336+
// return false;
320337
}
321338
if (pType.getPrimitiveTypeCode() == PrimitiveType.CHAR) {
322-
buffer.append("String.fromCharCode (");
323-
node.getExpression().accept(this);
324-
buffer.append (")");
325-
return false;
339+
if ("char".equals(name)) {
340+
// buffer.append("(");
341+
// node.getExpression().accept(this);
342+
// buffer.append (").charCodeAt (0)");
343+
// return false;
344+
} else if ("float".equals(name) || "double".equals(name)) {
345+
// TODO:
346+
buffer.append("String.fromCharCode (");
347+
buffer.append("Math.round (");
348+
node.getExpression().accept(this);
349+
buffer.append (")");
350+
buffer.append (")");
351+
return false;
352+
} else if ("int".equals(name) || "byte".equals(name)
353+
|| "double".equals(name) || "float".equals(name)
354+
|| "short".equals(name) || "long".equals(name)) {
355+
buffer.append("String.fromCharCode (");
356+
node.getExpression().accept(this);
357+
buffer.append (")");
358+
return false;
359+
}
326360
}
327361
}
328362
node.getExpression().accept(this);

0 commit comments

Comments
 (0)