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

Skip to content

Commit b4d8ecd

Browse files
author
jossonsmith
committed
Merge with /trunks for SWT Dialog improvement and Simple RPC implementation
1 parent 44390a7 commit b4d8ecd

File tree

5 files changed

+153
-11
lines changed

5 files changed

+153
-11
lines changed

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,16 +1267,27 @@ public void endVisit(InstanceofExpression node) {
12671267
}
12681268

12691269
public boolean visit(InstanceofExpression node) {
1270-
buffer.append("Clazz.instanceOf (");
1271-
node.getLeftOperand().accept(this);
1272-
buffer.append(", ");
12731270
Type right = node.getRightOperand();
1274-
if (right instanceof ArrayType) {
1275-
buffer.append("Array");
1276-
} else {
1277-
right.accept(this);
1278-
}
1279-
buffer.append(")");
1271+
//ITypeBinding binding = right.resolveBinding();
1272+
//if (binding.isInterface()) { // only interfaces need wrapping
1273+
buffer.append("Clazz.instanceOf (");
1274+
node.getLeftOperand().accept(this);
1275+
buffer.append(", ");
1276+
if (right instanceof ArrayType) {
1277+
buffer.append("Array");
1278+
} else {
1279+
right.accept(this);
1280+
}
1281+
buffer.append(")");
1282+
/*} else {
1283+
node.getLeftOperand().accept(this);
1284+
buffer.append(" instanceof ");
1285+
if (right instanceof ArrayType) {
1286+
buffer.append("Array");
1287+
} else {
1288+
right.accept(this);
1289+
}
1290+
}*/
12801291
return false;
12811292
}
12821293

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

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
*******************************************************************************/
1111
package net.sf.j2s.core.astvisitors;
1212
import java.util.ArrayList;
13+
import java.util.HashSet;
1314
import java.util.Iterator;
1415
import java.util.List;
16+
import java.util.Set;
1517

1618
import org.eclipse.jdt.core.dom.AST;
1719
import org.eclipse.jdt.core.dom.ASTNode;
1820
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
1921
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
22+
import org.eclipse.jdt.core.dom.ArrayType;
2023
import org.eclipse.jdt.core.dom.Block;
2124
import org.eclipse.jdt.core.dom.CastExpression;
2225
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
@@ -56,6 +59,7 @@
5659
import org.eclipse.jdt.core.dom.TypeDeclarationStatement;
5760
import org.eclipse.jdt.core.dom.TypeLiteral;
5861
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
62+
import org.eclipse.jdt.core.dom.PrimitiveType.Code;
5963

6064
public class ASTScriptVisitor extends ASTKeywordParser {
6165

@@ -3079,6 +3083,92 @@ replace full class name with short variable name
30793083
}
30803084
// Interface's inner interfaces or classes
30813085
buffer.append(laterBuffer);
3086+
3087+
StringBuffer fieldsSerializables = new StringBuffer();
3088+
ITypeBinding binding = node.resolveBinding();
3089+
boolean isSimpleSerializable = (Bindings.findTypeInHierarchy(binding, "net.sf.j2s.ajax.SimpleSerializable") != null);
3090+
for (Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) {
3091+
ASTNode element = (ASTNode) iter.next();
3092+
if (element instanceof FieldDeclaration) {
3093+
if (node.isInterface()) {
3094+
/*
3095+
* As members of interface should be treated
3096+
* as final and for javascript interface won't
3097+
* get instantiated, so the member will be
3098+
* treated specially.
3099+
*/
3100+
continue;
3101+
}
3102+
FieldDeclaration fieldDeclaration = (FieldDeclaration) element;
3103+
3104+
if (isSimpleSerializable) {
3105+
List fragments = fieldDeclaration.fragments();
3106+
int modifiers = fieldDeclaration.getModifiers();
3107+
if ((Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers))
3108+
&& !Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) {
3109+
Type type = fieldDeclaration.getType();
3110+
int dims = 0;
3111+
if (type.isArrayType()) {
3112+
dims = 1;
3113+
type = ((ArrayType) type).getComponentType();
3114+
}
3115+
String mark = null;
3116+
if (type.isPrimitiveType()) {
3117+
PrimitiveType pType = (PrimitiveType) type;
3118+
Code code = pType.getPrimitiveTypeCode();
3119+
if (code == PrimitiveType.FLOAT) {
3120+
mark = "F";
3121+
} else if (code == PrimitiveType.DOUBLE) {
3122+
mark = "D";
3123+
} else if (code == PrimitiveType.INT) {
3124+
mark = "I";
3125+
} else if (code == PrimitiveType.LONG) {
3126+
mark = "L";
3127+
} else if (code == PrimitiveType.SHORT) {
3128+
mark = "S";
3129+
} else if (code == PrimitiveType.BYTE) {
3130+
mark = "B";
3131+
} else if (code == PrimitiveType.CHAR) {
3132+
mark = "C";
3133+
} else if (code == PrimitiveType.BOOLEAN) {
3134+
mark = "b";
3135+
}
3136+
}
3137+
ITypeBinding resolveBinding = type.resolveBinding();
3138+
if ("java.lang.String".equals(resolveBinding.getQualifiedName())) {
3139+
mark = "s";
3140+
}
3141+
if (mark != null) {
3142+
for (Iterator xiter = fragments.iterator(); xiter.hasNext();) {
3143+
VariableDeclarationFragment var = (VariableDeclarationFragment) xiter.next();
3144+
int curDim = dims + var.getExtraDimensions();
3145+
if (curDim <= 1) {
3146+
if (fieldsSerializables.length() > 0) {
3147+
fieldsSerializables.append(", ");
3148+
}
3149+
fieldsSerializables.append("\"" + var.getName() + "\", \"");
3150+
if (mark.charAt(0) == 's' && curDim == 1) {
3151+
fieldsSerializables.append("AX");
3152+
} else if (curDim == 1) {
3153+
fieldsSerializables.append("A");
3154+
fieldsSerializables.append(mark);
3155+
} else {
3156+
fieldsSerializables.append(mark);
3157+
}
3158+
fieldsSerializables.append("\"");
3159+
}
3160+
}
3161+
}
3162+
}
3163+
}
3164+
}
3165+
}
3166+
if (fieldsSerializables.length() > 0) {
3167+
buffer.append("Clazz.registerSerializableFields(cla$$, ");
3168+
buffer.append(fieldsSerializables.toString());
3169+
buffer.append(");\r\n");
3170+
}
3171+
30823172
Javadoc javadoc = node.getJavadoc();
30833173
if (javadoc != null) {
30843174
List tags = javadoc.tags();
@@ -3126,7 +3216,6 @@ public boolean visit(TypeDeclaration node) {
31263216
// if (thisClassName == null || thisClassName.trim().length() == 0) {
31273217
// thisClassName = node.getName().toString();
31283218
// }
3129-
System.out.println();
31303219

31313220
if ((node != rootTypeNode) && node.getParent() != null
31323221
&& (node.getParent() instanceof AbstractTypeDeclaration

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.eclipse.jdt.core.dom.ITypeBinding;
4040
import org.eclipse.jdt.core.dom.IVariableBinding;
4141
import org.eclipse.jdt.core.dom.IfStatement;
42+
import org.eclipse.jdt.core.dom.ImportDeclaration;
4243
import org.eclipse.jdt.core.dom.Initializer;
4344
import org.eclipse.jdt.core.dom.Javadoc;
4445
import org.eclipse.jdt.core.dom.MethodDeclaration;
@@ -404,6 +405,11 @@ public static void main(String[] args) {
404405
System.out.println(buf.toString().replaceAll(", ", ",\r\n\t"));
405406
}
406407

408+
409+
public boolean visit(ImportDeclaration node) {
410+
return false;
411+
}
412+
407413
public boolean visit(PackageDeclaration node) {
408414
thisPackageName = "" + node.getName();
409415
return false;

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class SWTScriptVisitor extends ASTScriptVisitor {
3838

3939
// private StringBuffer bufferRemoved = new StringBuffer();
4040
private boolean metSWTBlockWhile = false;
41+
private boolean metDialogOpen = false;
4142

4243
public SWTScriptVisitor() {
4344
super();
@@ -368,6 +369,24 @@ protected String[] getFilterMethods() {
368369
*/
369370
public boolean visit(MethodInvocation node) {
370371
IMethodBinding methodBinding = node.resolveMethodBinding();
372+
if ("open".equals(methodBinding.getName()) && methodBinding.getParameterTypes().length == 0) {
373+
if (Bindings.findTypeInHierarchy(methodBinding.getDeclaringClass(), "org.eclipse.swt.widgets.Dialog") != null) {
374+
int lastIndexOf = buffer.lastIndexOf(";\r\n");
375+
if (lastIndexOf == -1) {
376+
lastIndexOf = 0;
377+
}
378+
String s = buffer.substring(lastIndexOf + 3);
379+
buffer.delete(lastIndexOf + 3, buffer.length());
380+
buffer.append("DialogSync2Async.block (");
381+
node.getExpression().accept(this);
382+
buffer.append(", this, function () {\r\n");
383+
buffer.append(s);
384+
node.getExpression().accept(this);
385+
buffer.append(".dialogReturn");
386+
metDialogOpen = true;
387+
return false;
388+
}
389+
}
371390
if ("net.sf.j2s.ajax.junit.AsyncSWT".equals(methodBinding.getDeclaringClass().getQualifiedName())
372391
&& "waitLayout".equals(methodBinding.getName())) {
373392
metSWTBlockWhile = true;
@@ -408,6 +427,11 @@ public void endVisit(MethodDeclaration node) {
408427
*/
409428
public boolean visit(MethodDeclaration node) {
410429
IMethodBinding methodBinding = node.resolveBinding();
430+
// if ("open".equals(methodBinding.getName()) && methodBinding.getParameterTypes().length == 0) {
431+
// if (Bindings.findTypeInHierarchy(methodBinding.getDeclaringClass(), "org.eclipse.swt.widgets.Dialog") != null) {
432+
//
433+
// }
434+
// }
411435
String[] filterMethods = getFilterMethods();
412436
for (int i = 0; i < filterMethods.length; i += 2) {
413437
if (isMethodInvoking(methodBinding, filterMethods[i], filterMethods[i + 1])) {
@@ -419,9 +443,14 @@ public boolean visit(MethodDeclaration node) {
419443

420444
public boolean visit(Block node) {
421445
int swtBlockWhileCount = 0;
446+
int swtDialogOpenCount = 0;
422447
boolean lastSWTBlockWhile = metSWTBlockWhile;
423448
metSWTBlockWhile = false;
449+
boolean lastDialogOpen = metDialogOpen;
450+
metDialogOpen = false;
424451
if (super.visit(node) == false) {
452+
metSWTBlockWhile = lastSWTBlockWhile;
453+
metDialogOpen = lastDialogOpen;
425454
return false;
426455
}
427456
List statements = node.statements();
@@ -447,11 +476,16 @@ public boolean visit(Block node) {
447476
swtBlockWhileCount++;
448477
metSWTBlockWhile = false;
449478
}
479+
if (metDialogOpen) {
480+
swtDialogOpenCount++;
481+
metDialogOpen = false;
482+
}
450483
}
451-
for (int i = 0; i < swtBlockWhileCount; i++) {
484+
for (int i = 0; i < swtBlockWhileCount + swtDialogOpenCount; i++) {
452485
buffer.append("});\r\n");
453486
}
454487
metSWTBlockWhile = lastSWTBlockWhile;
488+
metDialogOpen = lastDialogOpen;
455489
return false;
456490
//return super.visit(node);
457491
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ public static String[] getClazzAbbrMap() {
489489
"Clazz.inheritArgs", "G", //
490490
"Clazz.checkPrivateMethod", "X", //
491491
"Clazz.makeFunction", "Q", //
492+
493+
"Clazz.registerSerializableFields", "s", //
492494
};
493495
return clazzAll;
494496
}

0 commit comments

Comments
 (0)