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

Skip to content

Commit 849296c

Browse files
author
zhourenjian
committed
1. Add org.eclipse.jface.* support in Java2Script compiler
2. Fix bug of boxing "char" arguments for "int" 3. Add "/-* ... *-/" for @j2sNative's "/* ... */"
1 parent c2529bd commit 849296c

File tree

6 files changed

+187
-87
lines changed

6 files changed

+187
-87
lines changed

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

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.util.ArrayList;
1515
import java.util.Iterator;
1616
import java.util.List;
17+
import java.util.regex.Pattern;
18+
1719
import org.eclipse.jdt.core.dom.ASTNode;
1820
import org.eclipse.jdt.core.dom.Block;
1921
import org.eclipse.jdt.core.dom.BodyDeclaration;
@@ -143,20 +145,7 @@ boolean visitNativeJavadoc(Javadoc javadoc, Block node, boolean superVisit) {
143145
TagElement tagEl = (TagElement) iter.next();
144146
if ("@j2sDebug".equals(tagEl.getTagName())) {
145147
if (superVisit) super.visit(node);
146-
List fragments = tagEl.fragments();
147-
boolean isFirstLine = true;
148-
for (Iterator iterator = fragments.iterator(); iterator
149-
.hasNext();) {
150-
TextElement commentEl = (TextElement) iterator.next();
151-
String text = commentEl.getText().trim();
152-
if (isFirstLine) {
153-
if (text.length() == 0) {
154-
continue;
155-
}
156-
}
157-
buffer.append(text);
158-
buffer.append("\r\n");
159-
}
148+
visitJavadocJ2SSource(tagEl);
160149
return false;
161150
}
162151
}
@@ -168,20 +157,7 @@ boolean visitNativeJavadoc(Javadoc javadoc, Block node, boolean superVisit) {
168157
TagElement tagEl = (TagElement) iter.next();
169158
if ("@j2sNativeSrc".equals(tagEl.getTagName())) {
170159
if (superVisit) super.visit(node);
171-
List fragments = tagEl.fragments();
172-
boolean isFirstLine = true;
173-
for (Iterator iterator = fragments.iterator(); iterator
174-
.hasNext();) {
175-
TextElement commentEl = (TextElement) iterator.next();
176-
String text = commentEl.getText().trim();
177-
if (isFirstLine) {
178-
if (text.length() == 0) {
179-
continue;
180-
}
181-
}
182-
buffer.append(text);
183-
buffer.append("\r\n");
184-
}
160+
visitJavadocJ2SSource(tagEl);
185161
return false;
186162
}
187163
}
@@ -190,20 +166,7 @@ boolean visitNativeJavadoc(Javadoc javadoc, Block node, boolean superVisit) {
190166
TagElement tagEl = (TagElement) iter.next();
191167
if ("@j2sNative".equals(tagEl.getTagName())) {
192168
if (superVisit) super.visit(node);
193-
List fragments = tagEl.fragments();
194-
boolean isFirstLine = true;
195-
for (Iterator iterator = fragments.iterator(); iterator
196-
.hasNext();) {
197-
TextElement commentEl = (TextElement) iterator.next();
198-
String text = commentEl.getText().trim();
199-
if (isFirstLine) {
200-
if (text.length() == 0) {
201-
continue;
202-
}
203-
}
204-
buffer.append(text);
205-
buffer.append("\r\n");
206-
}
169+
visitJavadocJ2SSource(tagEl);
207170
return false;
208171
}
209172
}
@@ -212,7 +175,34 @@ boolean visitNativeJavadoc(Javadoc javadoc, Block node, boolean superVisit) {
212175
return true;
213176
}
214177

178+
private void visitJavadocJ2SSource(TagElement tagEl) {
179+
List fragments = tagEl.fragments();
180+
boolean isFirstLine = true;
181+
StringBuffer buf = new StringBuffer();
182+
for (Iterator iterator = fragments.iterator(); iterator
183+
.hasNext();) {
184+
TextElement commentEl = (TextElement) iterator.next();
185+
String text = commentEl.getText().trim();
186+
if (isFirstLine) {
187+
if (text.length() == 0) {
188+
continue;
189+
}
190+
}
191+
buf.append(text);
192+
buf.append("\r\n");
193+
}
194+
buffer.append(fixCommentBlock(buf.toString()));
195+
}
215196

197+
private String fixCommentBlock(String text) {
198+
if (text == null || text.length() == 0) {
199+
return text;
200+
}
201+
return Pattern.compile("\\/-\\*(.*)\\*-\\/",
202+
Pattern.MULTILINE | Pattern.DOTALL)
203+
.matcher(text).replaceAll("/*$1*/");
204+
}
205+
216206
private void checkJavadocs(ASTNode root) {
217207
if (root != javadocRoot) {
218208
nativeJavadoc = null;

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

Lines changed: 134 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,13 @@ public boolean visit(ClassInstanceCreation node) {
474474
}
475475
}
476476
buffer.append(" (");
477-
visitList(node.arguments(), ", ");
477+
IMethodBinding methodDeclaration = null;
478+
IMethodBinding constructorBinding = node.resolveConstructorBinding();
479+
if (constructorBinding != null) {
480+
methodDeclaration = constructorBinding.getMethodDeclaration();
481+
}
482+
visitMethodParameterList(node.arguments(), methodDeclaration);
483+
//visitList(node.arguments(), ", ");
478484
buffer.append(")");
479485
} else {
480486
int anonCount = ((ASTTypeVisitor) getAdaptable(ASTTypeVisitor.class)).getAnonymousCount() + 1;
@@ -530,7 +536,14 @@ public boolean visit(ClassInstanceCreation node) {
530536
if (argSize > 0) {
531537
buffer.append(", ");
532538
}
533-
visitList(arguments, ", ");
539+
IMethodBinding methodDeclaration = null;
540+
IMethodBinding constructorBinding = node.resolveConstructorBinding();
541+
if (constructorBinding != null) {
542+
methodDeclaration = constructorBinding.getMethodDeclaration();
543+
}
544+
visitMethodParameterList(node.arguments(), methodDeclaration);
545+
546+
//visitList(arguments, ", ");
534547
buffer.append(", ");
535548
String scope = null;
536549
if (methodDeclareStack.size() != 0) {
@@ -561,9 +574,113 @@ public boolean visit(ClassInstanceCreation node) {
561574
return false;
562575
}
563576

577+
protected void visitMethodParameterList(List arguments,
578+
IMethodBinding methodDeclaration, int begin, int end) {
579+
ITypeBinding[] parameterTypes = null;
580+
String clazzName = null;
581+
String methodName = null;
582+
/*
583+
Object[] ignores = new Object[] {
584+
"java.lang.String", "indexOf", new int[] { 0 },
585+
"java.lang.String", "lastIndexOf", new int[] { 0 },
586+
"java.lang.StringBuffer", "append", new int[] { 0 },
587+
"java.lang.StringBuilder", "append", new int[] { 0 }
588+
};
589+
*/
590+
if (methodDeclaration != null) {
591+
parameterTypes = methodDeclaration.getParameterTypes();
592+
ITypeBinding declaringClass = methodDeclaration.getDeclaringClass();
593+
if (declaringClass != null) {
594+
clazzName = declaringClass.getQualifiedName();
595+
}
596+
methodName = methodDeclaration.getName();
597+
}
598+
for (int i = begin; i < end; i++) {
599+
ASTNode element = (ASTNode) arguments.get(i);
600+
String typeStr = null;
601+
if (element instanceof CastExpression) {
602+
CastExpression castExp = (CastExpression) element;
603+
Expression exp = castExp.getExpression();
604+
if (exp instanceof NullLiteral) {
605+
ITypeBinding nullTypeBinding = castExp.resolveTypeBinding();
606+
if (nullTypeBinding != null) {
607+
if (nullTypeBinding.isArray()) {
608+
typeStr = "Array";
609+
} else if (nullTypeBinding.isPrimitive()) {
610+
Code code = PrimitiveType.toCode(nullTypeBinding.getName());
611+
if (code == PrimitiveType.BOOLEAN) {
612+
typeStr = "Boolean";
613+
} else{
614+
typeStr = "Number";
615+
}
616+
} else if (!nullTypeBinding.isTypeVariable()) {
617+
typeStr = assureQualifiedName(shortenQualifiedName(nullTypeBinding.getQualifiedName()));
618+
}
619+
}
620+
}
621+
}
622+
if (typeStr != null) {
623+
buffer.append("Clazz.castNullAs (\"");
624+
buffer.append(typeStr);
625+
buffer.append("\")");
626+
} else {
627+
boxingNode(element);
628+
Expression exp = (Expression) element;
629+
ITypeBinding typeBinding = exp.resolveTypeBinding();
630+
String typeName = typeBinding.getName();
631+
String parameterTypeName = null;
632+
if (parameterTypes != null) {
633+
parameterTypeName = parameterTypes[i].getName();
634+
}
635+
if ("char".equals(typeName) && "int".equals(parameterTypeName)) {
636+
boolean ignored = false;
637+
/*
638+
for (int j = 0; j < ignores.length / 3; j++) {
639+
int[] indexes = (int[]) ignores[i + i + i + 2];
640+
boolean existed = false;
641+
for (int k = 0; k < indexes.length; k++) {
642+
if (indexes[k] == i) {
643+
existed = true;
644+
break;
645+
}
646+
}
647+
if (existed) {
648+
if (ignores[0].equals(Bindings.removeBrackets(clazzName)) && ignores[1].equals(methodName)) {
649+
ignored = true;
650+
break;
651+
}
652+
}
653+
}
654+
*/
655+
// Keep String#indexOf(int) and String#lastIndexOf(int)'s first char argument
656+
ignored = (i == 0
657+
&& (/*"append".equals(methodName) || */"indexOf".equals(methodName) || "lastIndexOf".equals(methodName))
658+
&& ("java.lang.String".equals(Bindings.removeBrackets(clazzName))));
659+
if (!ignored) {
660+
buffer.append(".charCodeAt (0)");
661+
}
662+
}
663+
}
664+
if (i < end - 1) {
665+
buffer.append(", ");
666+
}
667+
}
668+
}
669+
670+
protected void visitMethodParameterList(List arguments,
671+
IMethodBinding methodDeclaration) {
672+
visitMethodParameterList(arguments, methodDeclaration, 0, arguments.size());
673+
}
674+
564675
public boolean visit(ConstructorInvocation node) {
565676
buffer.append("this.construct (");
566-
visitList(node.arguments(), ", ");
677+
IMethodBinding methodDeclaration = null;
678+
IMethodBinding constructorBinding = node.resolveConstructorBinding();
679+
if (constructorBinding != null) {
680+
methodDeclaration = constructorBinding.getMethodDeclaration();
681+
}
682+
visitMethodParameterList(node.arguments(), methodDeclaration);
683+
//visitList(node.arguments(), ", ");
567684
buffer.append(");\r\n");
568685
return false;
569686
}
@@ -1586,8 +1703,8 @@ public boolean visit(MethodInvocation node) {
15861703
buffer.append(" (");
15871704

15881705
IMethodBinding methodBinding = node.resolveMethodBinding();
1706+
ITypeBinding[] paramTypes = methodBinding.getParameterTypes();
15891707
if (methodBinding != null && methodBinding.isVarargs()) {
1590-
ITypeBinding[] paramTypes = methodBinding.getParameterTypes();
15911708
visitList(args, ", ", 0, paramTypes.length - 1);
15921709
if (paramTypes.length - 1 > 0) {
15931710
buffer.append(", ");
@@ -1601,44 +1718,13 @@ public boolean visit(MethodInvocation node) {
16011718
}
16021719
//}
16031720
if (needBrackets) buffer.append("[");
1721+
//IMethodBinding methodDeclaration = node.resolveMethodBinding();
1722+
//visitMethodParameterList(node.arguments(), methodDeclaration, paramTypes.length - 1, size);
16041723
visitList(args, ", ", paramTypes.length - 1, size);
16051724
if (needBrackets) buffer.append("]");
16061725
} else {
1607-
for (Iterator iter = args.iterator(); iter.hasNext();) {
1608-
ASTNode element = (ASTNode) iter.next();
1609-
String typeStr = null;
1610-
if (element instanceof CastExpression) {
1611-
CastExpression castExp = (CastExpression) element;
1612-
Expression exp = castExp.getExpression();
1613-
if (exp instanceof NullLiteral) {
1614-
ITypeBinding nullTypeBinding = castExp.resolveTypeBinding();
1615-
if (nullTypeBinding != null) {
1616-
if (nullTypeBinding.isArray()) {
1617-
typeStr = "Array";
1618-
} else if (nullTypeBinding.isPrimitive()) {
1619-
Code code = PrimitiveType.toCode(nullTypeBinding.getName());
1620-
if (code == PrimitiveType.BOOLEAN) {
1621-
typeStr = "Boolean";
1622-
} else{
1623-
typeStr = "Number";
1624-
}
1625-
} else if (!nullTypeBinding.isTypeVariable()) {
1626-
typeStr = assureQualifiedName(shortenQualifiedName(nullTypeBinding.getQualifiedName()));
1627-
}
1628-
}
1629-
}
1630-
}
1631-
if (typeStr != null) {
1632-
buffer.append("Clazz.castNullAs (\"");
1633-
buffer.append(typeStr);
1634-
buffer.append("\")");
1635-
} else {
1636-
boxingNode(element);
1637-
}
1638-
if (iter.hasNext()) {
1639-
buffer.append(", ");
1640-
}
1641-
}
1726+
IMethodBinding methodDeclaration = node.resolveMethodBinding();
1727+
visitMethodParameterList(node.arguments(), methodDeclaration);
16421728
//visitList(args, ", ");
16431729
}
16441730
buffer.append(")");
@@ -2020,7 +2106,12 @@ public boolean visit(SuperConstructorInvocation node) {
20202106
List arguments = node.arguments();
20212107
if (arguments.size() > 0) {
20222108
buffer.append(", [");
2023-
visitList(arguments, ", ");
2109+
IMethodBinding methodDeclaration = null;
2110+
if (constructorBinding != null) {
2111+
methodDeclaration = constructorBinding.getMethodDeclaration();
2112+
}
2113+
visitMethodParameterList(arguments, methodDeclaration);
2114+
//visitList(arguments, ", ");
20242115
buffer.append("]");
20252116
}
20262117
buffer.append(");\r\n");
@@ -2078,7 +2169,9 @@ public boolean visit(SuperMethodInvocation node) {
20782169
String name = getJ2SName(node.getName());
20792170
buffer.append(name);
20802171
buffer.append("\", [");
2081-
visitList(node.arguments(), ", ");
2172+
IMethodBinding methodDeclaration = node.resolveMethodBinding();
2173+
visitMethodParameterList(node.arguments(), methodDeclaration);
2174+
//visitList(node.arguments(), ", ");
20822175
buffer.append("])");
20832176
return false;
20842177
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,10 @@ protected String checkConstantValue(Expression node) {
208208
StringBuffer buffer = new StringBuffer();
209209
String str = (String) constValue;
210210
int length = str.length();
211+
/*
211212
if (length > 20) {
212213
return null;
213-
}
214+
}*/
214215
buffer.append("\"");
215216
for (int i = 0; i < length; i++) {
216217
char c = str.charAt(i);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,9 @@ public static boolean isMethodInvoking(Expression exp, String className, String
14291429
}
14301430

14311431
public static String removeBrackets(String qName) {
1432+
if (qName == null) {
1433+
return qName;
1434+
}
14321435
int length = qName.length();
14331436
StringBuffer buf = new StringBuffer();
14341437
int ltCount = 0;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ public boolean visit(QualifiedName node) {
668668
Object constValue = node.resolveConstantExpressionValue();
669669
if (constValue != null && (constValue instanceof Number
670670
|| constValue instanceof Character
671+
|| constValue instanceof String
671672
|| constValue instanceof Boolean)
672673
&& isSimpleQualified(node)) {
673674
//buffer.append(constValue);

0 commit comments

Comments
 (0)