Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 975177e

Browse files
Add support of overriding constructor
Generating cleaner sources for those char related operations Make array related operations into separated method Add configuration "j2s.compiler.utf8bom=true/false" to control whether UTF-8 header is generated or not
1 parent b2d65cf commit 975177e

File tree

4 files changed

+176
-28
lines changed

4 files changed

+176
-28
lines changed

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

Lines changed: 130 additions & 19 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 (");
@@ -774,14 +807,14 @@ private void visitArgumentItem(ASTNode element,
774807
buffer.append(typeStr.replaceFirst("^\\$wt.", "org.eclipse.swt."));
775808
buffer.append("\")");
776809
} else {
777-
boxingNode(element);
778810
Expression exp = (Expression) element;
779811
ITypeBinding typeBinding = exp.resolveTypeBinding();
780812
String typeName = null;
781813
if (typeBinding != null) {
782814
typeName = typeBinding.getName();
783815
}
784-
if ("char".equals(typeName) && "int".equals(parameterTypeName)) {
816+
int idx1 = buffer.length();
817+
if ("char".equals(typeName) && !"char".equals(parameterTypeName)) {
785818
boolean ignored = false;
786819
/*
787820
for (int j = 0; j < ignores.length / 3; j++) {
@@ -805,9 +838,38 @@ private void visitArgumentItem(ASTNode element,
805838
ignored = (position == 0
806839
&& (/*"append".equals(methodName) || */"indexOf".equals(methodName) || "lastIndexOf".equals(methodName))
807840
&& ("java.lang.String".equals(Bindings.removeBrackets(clazzName))));
841+
842+
if (!ignored && exp instanceof CharacterLiteral) {
843+
CharacterLiteral cl = (CharacterLiteral) exp;
844+
buffer.append(0 + cl.charValue());
845+
ignored = true;
846+
} else {
847+
boxingNode(element);
848+
}
808849
if (!ignored) {
809-
buffer.append(".charCodeAt (0)");
850+
boolean appendingCode = true;
851+
int length = buffer.length();
852+
if (exp instanceof MethodInvocation) {
853+
MethodInvocation m = (MethodInvocation) exp;
854+
if ("charAt".equals(m.getName().toString())) {
855+
int idx2 = buffer.indexOf(".charAt ", idx1);
856+
if (idx2 != -1) {
857+
StringBuffer newMethodBuffer = new StringBuffer();
858+
newMethodBuffer.append(buffer.substring(idx1, idx2));
859+
newMethodBuffer.append(".charCodeAt ");
860+
newMethodBuffer.append(buffer.substring(idx2 + 8, length));
861+
buffer.delete(idx1, length);
862+
buffer.append(newMethodBuffer.toString());
863+
appendingCode = false;
864+
}
865+
}
866+
}
867+
if (appendingCode) {
868+
buffer.append(".charCodeAt (0)");
869+
}
810870
}
871+
} else {
872+
boxingNode(element);
811873
}
812874
}
813875
}
@@ -1272,6 +1334,8 @@ && checkSameName(typeBinding, fieldName)) {
12721334
PrimitiveType pType = (PrimitiveType) node.getType();
12731335
if (pType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) {
12741336
buffer.append("false");
1337+
} else if (pType.getPrimitiveTypeCode() == PrimitiveType.CHAR) {
1338+
buffer.append("'\\0'");
12751339
} else {
12761340
buffer.append("0");
12771341
}
@@ -1319,7 +1383,8 @@ private void charVisit(ASTNode node, boolean beCare) {
13191383
boxingNode(node);
13201384
return ;
13211385
}
1322-
ITypeBinding binding = ((Expression) node).resolveTypeBinding();
1386+
Expression exp = (Expression) node;
1387+
ITypeBinding binding = exp.resolveTypeBinding();
13231388
if (binding.isPrimitive() && "char".equals(binding.getName())) {
13241389
if (node instanceof CharacterLiteral) {
13251390
CharacterLiteral cl = (CharacterLiteral) node;
@@ -1328,9 +1393,31 @@ private void charVisit(ASTNode node, boolean beCare) {
13281393
boxingNode(node);
13291394
buffer.append(".charCodeAt (0)");
13301395
} else {
1396+
int idx1 = buffer.length();
13311397
buffer.append("(");
13321398
boxingNode(node);
1333-
buffer.append(").charCodeAt (0)");
1399+
buffer.append(")");
1400+
1401+
boolean appendingCode = true;
1402+
int length = buffer.length();
1403+
if (exp instanceof MethodInvocation) {
1404+
MethodInvocation m = (MethodInvocation) exp;
1405+
if ("charAt".equals(m.getName().toString())) {
1406+
int idx2 = buffer.indexOf(".charAt ", idx1);
1407+
if (idx2 != -1) {
1408+
StringBuffer newMethodBuffer = new StringBuffer();
1409+
newMethodBuffer.append(buffer.substring(idx1 + 1, idx2));
1410+
newMethodBuffer.append(".charCodeAt ");
1411+
newMethodBuffer.append(buffer.substring(idx2 + 8, length - 1));
1412+
buffer.delete(idx1, length);
1413+
buffer.append(newMethodBuffer.toString());
1414+
appendingCode = false;
1415+
}
1416+
}
1417+
}
1418+
if (appendingCode) {
1419+
buffer.append(".charCodeAt (0)");
1420+
}
13341421
}
13351422
} else {
13361423
boxingNode(node);
@@ -1353,8 +1440,9 @@ public boolean visit(InfixExpression node) {
13531440
Expression right = node.getRightOperand();
13541441
ITypeBinding typeBinding = left.resolveTypeBinding();
13551442

1356-
if ((left instanceof SimpleName || left instanceof CharacterLiteral) && (right instanceof SimpleName || right instanceof CharacterLiteral)
1357-
&& (">".equals(operator) || "<".equals(operator) || ">=".equals(operator) || "<=".equals(operator))) {
1443+
if (/*(left instanceof SimpleName || left instanceof CharacterLiteral) && (right instanceof SimpleName || right instanceof CharacterLiteral)
1444+
&& */(">".equals(operator) || "<".equals(operator) || ">=".equals(operator) || "<=".equals(operator)
1445+
|| "==".equals(operator) || "!=".equals(operator))) {
13581446
ITypeBinding rightBinding = right.resolveTypeBinding();
13591447
if (typeBinding.isPrimitive() && "char".equals(typeBinding.getName())
13601448
&& rightBinding.isPrimitive() && "char".equals(rightBinding.getName())) {
@@ -1374,7 +1462,9 @@ public boolean visit(InfixExpression node) {
13741462
StringBuffer tmpBuffer = buffer;
13751463
buffer = new StringBuffer();
13761464

1377-
buffer.append("Math.floor (");
1465+
//buffer.append("Math.floor (");
1466+
// TODO
1467+
buffer.append("Clazz.doubleToInt (");
13781468
charVisit(left, beCare);
13791469
buffer.append(' ');
13801470
buffer.append(operator);
@@ -1390,7 +1480,8 @@ public boolean visit(InfixExpression node) {
13901480
Expression exp = (Expression) element;
13911481
ITypeBinding expBinding = exp.resolveTypeBinding();
13921482
if (isIntegerType(expBinding.getName())) {
1393-
buffer.insert(0, "Math.floor (");
1483+
//buffer.insert(0, "Math.floor (");
1484+
buffer.insert(0, "Clazz.doubleToInt (");
13941485
is2Floor = true;
13951486
}
13961487
}
@@ -1650,7 +1741,11 @@ public boolean visit(MethodDeclaration node) {
16501741
}
16511742

16521743
if (node.isConstructor()) {
1653-
buffer.append("Clazz.makeConstructor (");
1744+
if (getJ2STag(node, "@j2sOverride") != null) {
1745+
buffer.append("Clazz.overrideConstructor (");
1746+
} else {
1747+
buffer.append("Clazz.makeConstructor (");
1748+
}
16541749
} else {
16551750
if ((node.getModifiers() & Modifier.STATIC) != 0) {
16561751
/* replace full class name with short variable name */
@@ -3011,7 +3106,7 @@ private String prepareSimpleSerializable(TypeDeclaration node, List bodyDeclarat
30113106
if (isSimpleSerializable) {
30123107
List fragments = fieldDeclaration.fragments();
30133108
int modifiers = fieldDeclaration.getModifiers();
3014-
if ((Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers))
3109+
if ((Modifier.isPublic(modifiers)/* || Modifier.isProtected(modifiers)*/)
30153110
&& !Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) {
30163111
Type type = fieldDeclaration.getType();
30173112
int dims = 0;
@@ -3044,6 +3139,22 @@ private String prepareSimpleSerializable(TypeDeclaration node, List bodyDeclarat
30443139
ITypeBinding resolveBinding = type.resolveBinding();
30453140
if ("java.lang.String".equals(resolveBinding.getQualifiedName())) {
30463141
mark = "s";
3142+
} else {
3143+
ITypeBinding t = resolveBinding;
3144+
do {
3145+
String typeName = t.getQualifiedName();
3146+
if ("java.lang.Object".equals(typeName)) {
3147+
break;
3148+
}
3149+
if ("net.sf.j2s.ajax.SimpleSerializable".equals(typeName)) {
3150+
mark = "O";
3151+
break;
3152+
}
3153+
t = t.getSuperclass();
3154+
if (t == null) {
3155+
break;
3156+
}
3157+
} while (true);
30473158
}
30483159
if (mark != null) {
30493160
for (Iterator xiter = fragments.iterator(); xiter.hasNext();) {

src/net/sf/j2s/core/astvisitors/ASTVariableVisitor.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,21 @@ protected String checkConstantValue(Expression node) {
196196
}
197197
buffer.append(hexStr);
198198
} else {
199-
buffer.append(constValue);
199+
char c = charValue;
200+
if (c == '\\' || c == '\'' || c == '\"') {
201+
buffer.append('\\');
202+
buffer.append(c);
203+
} else if (c == '\r') {
204+
buffer.append("\\r");
205+
} else if (c == '\n') {
206+
buffer.append("\\n");
207+
} else if (c == '\t') {
208+
buffer.append("\\t");
209+
} else if (c == '\f') {
210+
buffer.append("\\f");
211+
} else {
212+
buffer.append(constValue);
213+
}
200214
}
201215
buffer.append('\'');
202216
} else {

src/net/sf/j2s/core/astvisitors/DependencyASTVisitor.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Iterator;
1919
import java.util.List;
2020
import java.util.Set;
21-
2221
import org.eclipse.jdt.core.dom.ASTNode;
2322
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
2423
import org.eclipse.jdt.core.dom.Annotation;
@@ -347,8 +346,8 @@ public boolean visit(PackageDeclaration node) {
347346
ASTPackageVisitor packageVisitor = ((ASTPackageVisitor) getAdaptable(ASTPackageVisitor.class));
348347
packageVisitor.setPackageName("" + node.getName());
349348
return false;
350-
}
351-
349+
}
350+
352351
//sgurin - fix for bug http://sourceforge.net/tracker/?func=detail&aid=3037341&group_id=155436&atid=795800 with static imports
353352
public void endVisit(ImportDeclaration node) {
354353
super.endVisit(node);
@@ -359,7 +358,7 @@ public void endVisit(ImportDeclaration node) {
359358
musts.add(qnameStr);
360359
}
361360
}
362-
}
361+
}
363362
}
364363

365364
protected void readClasses(Annotation annotation, Set set) {
@@ -938,9 +937,6 @@ public boolean visit(ClassInstanceCreation node) {
938937
// return super.visit(node);
939938
// }
940939

941-
942-
943-
944940
/*
945941
* (non-Javadoc)
946942
*

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ public static void outputJavaScript(ASTScriptVisitor visitor, DependencyASTVisit
315315
String js = dvisitor.getDependencyScript(visitor.getBuffer());
316316
String lineBreak = props.getProperty("j2s.compiler.linebreak");
317317
String whiteSpace = props.getProperty("j2s.compiler.whitespace");
318+
String utf8Header = props.getProperty("j2s.compiler.utf8bom");
319+
boolean addUTF8Header = false;
320+
if (utf8Header != null && utf8Header.equals("true")) {
321+
addUTF8Header = true;
322+
}
318323
if (lineBreak != null && whiteSpace != null
319324
&& lineBreak.length() == 0 && whiteSpace.equals("false")) {
320325
js = RegExCompress.regexCompress(js);
@@ -414,7 +419,9 @@ public static void outputJavaScript(ASTScriptVisitor visitor, DependencyASTVisit
414419
File jsFile = new File(folderPath, elementName + ".js"); //$NON-NLS-1$
415420
try {
416421
FileOutputStream fos = new FileOutputStream(jsFile);
417-
fos.write(new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}); // UTF-8 header!
422+
if (addUTF8Header) {
423+
fos.write(new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}); // UTF-8 header!
424+
}
418425
fos.write(js.getBytes("UTF-8"));
419426
fos.close();
420427
} catch (IOException e) {
@@ -521,6 +528,7 @@ public static String[] getClazzAbbrMap() {
521528
"Clazz.prepareCallback", "B", //
522529
"Clazz.innerTypeInstance", "N", //
523530
"Clazz.makeConstructor", "K", //
531+
"Clazz.overrideConstructor", "k", //
524532
"Clazz.superCall", "U", //
525533
"Clazz.superConstructor", "R", //
526534
"Clazz.defineMethod", "M", //
@@ -530,6 +538,25 @@ public static String[] getClazzAbbrMap() {
530538
"Clazz.cloneFinals", "F", //
531539
"Clazz.prepareFields", "Y", //
532540
"Clazz.newArray", "A", //
541+
"Clazz.newIntArray", "AI", //
542+
"Clazz.newFloatArray", "AF", //
543+
"Clazz.newDoubleArray", "AD", //
544+
"Clazz.newByteArray", "AB", //
545+
"Clazz.newLongArray", "AL", //
546+
"Clazz.newShortArray", "AS", //
547+
"Clazz.newCharArray", "AC", //
548+
"Clazz.newBooleanArray", "Ab", //
549+
//"Clazz.newStringArray", "AX", //
550+
"Clazz.floatToInt", "fI", //
551+
"Clazz.floatToByte", "fB", //
552+
"Clazz.floatToShort", "fS", //
553+
"Clazz.floatToLong", "fL", //
554+
"Clazz.floatToChar", "fC", //
555+
"Clazz.doubleToInt", "dI", //
556+
"Clazz.doubleToByte", "dB", //
557+
"Clazz.doubleToShort", "dS", //
558+
"Clazz.doubleToLong", "dL", //
559+
"Clazz.doubleToChar", "dC", //
533560
"Clazz.instanceOf", "O", //
534561
"Clazz.exceptionOf", "e", //sgurin
535562
"Clazz.inheritArgs", "G", //

0 commit comments

Comments
 (0)