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

Skip to content

Commit 00ac1f1

Browse files
author
zhourenjian
committed
Merge from branch 3.3
1 parent 1287bf4 commit 00ac1f1

File tree

5 files changed

+225
-50
lines changed

5 files changed

+225
-50
lines changed

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

Lines changed: 129 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.eclipse.jdt.core.dom.IMethodBinding;
3232
import org.eclipse.jdt.core.dom.ITypeBinding;
3333
import org.eclipse.jdt.core.dom.IVariableBinding;
34+
import org.eclipse.jdt.core.dom.IfStatement;
3435
import org.eclipse.jdt.core.dom.InfixExpression;
3536
import org.eclipse.jdt.core.dom.Initializer;
3637
import org.eclipse.jdt.core.dom.Javadoc;
@@ -45,6 +46,7 @@
4546
import org.eclipse.jdt.core.dom.SimpleName;
4647
import org.eclipse.jdt.core.dom.SimpleType;
4748
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
49+
import org.eclipse.jdt.core.dom.Statement;
4850
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
4951
import org.eclipse.jdt.core.dom.SuperFieldAccess;
5052
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
@@ -1113,6 +1115,10 @@ public void endVisit(MethodDeclaration node) {
11131115
}
11141116
super.endVisit(node);
11151117
}
1118+
1119+
protected String[] getFilterMethods() {
1120+
return new String[0];
1121+
}
11161122

11171123
public boolean visit(MethodDeclaration node) {
11181124
if (getJ2SDocTag(node, "@j2sIgnore") != null) {
@@ -1144,8 +1150,9 @@ public boolean visit(MethodDeclaration node) {
11441150
Block body = node.getBody();
11451151
boolean needToCheckArgs = false;
11461152
List argsList = null;
1147-
if (body != null && body.statements().size() == 1) {
1148-
Object statement = body.statements().get(0);
1153+
if (body != null && containsOnlySuperCall(body)) {
1154+
List sts = body.statements();
1155+
Object statement = sts.get(sts.size() - 1);
11491156
if (statement instanceof ReturnStatement) {
11501157
ReturnStatement ret = (ReturnStatement) statement;
11511158
Expression exp = ret.getExpression();
@@ -1390,6 +1397,81 @@ public boolean visit(MethodDeclaration node) {
13901397
return false;
13911398
}
13921399

1400+
private boolean containsOnlySuperCall(Block body) {
1401+
boolean isOnlyOneCall = false;
1402+
List ss = body.statements();
1403+
int size = ss.size();
1404+
if (size == 1) {
1405+
isOnlyOneCall = true;
1406+
} else {
1407+
/*
1408+
* If all method invocations before super call is filtered, then super call
1409+
* is still considered as the only one.
1410+
*
1411+
* For example, the filtered methods may be:
1412+
* checkWidget();
1413+
* checkDevice();
1414+
*/
1415+
String[] filterMethods = getFilterMethods();
1416+
if (filterMethods.length > 0 && size > 1) {
1417+
Object obj = ss.get(size - 1);
1418+
if (obj instanceof ExpressionStatement) {
1419+
ExpressionStatement smt = (ExpressionStatement) obj;
1420+
Expression e = smt.getExpression();
1421+
if (e instanceof SuperMethodInvocation) { // the last is super call
1422+
isOnlyOneCall = true;
1423+
for (int i = 0; i < size - 1; i++) { // check previous calls
1424+
Object statement = ss.get(i);
1425+
MethodInvocation method = null;
1426+
if (statement instanceof ExpressionStatement) {
1427+
ExpressionStatement sttmt = (ExpressionStatement) statement;
1428+
Expression exp = sttmt.getExpression();
1429+
if (exp instanceof MethodInvocation) {
1430+
method = (MethodInvocation) exp;
1431+
}
1432+
} else if (statement instanceof IfStatement) { // if (...) checkWidget();
1433+
IfStatement ifSss = (IfStatement) statement;
1434+
if (ifSss.getElseStatement() == null) {
1435+
Statement thenStatement = ifSss.getThenStatement();
1436+
if (thenStatement instanceof Block) {
1437+
Block block = (Block) thenStatement;
1438+
List statements = block.statements();
1439+
if (statements.size() == 1) {
1440+
thenStatement = (Statement) statements.get(0);
1441+
}
1442+
}
1443+
if (thenStatement instanceof ExpressionStatement) {
1444+
ExpressionStatement expStmt = (ExpressionStatement) thenStatement;
1445+
Expression exp = expStmt.getExpression();
1446+
if (exp instanceof MethodInvocation) {
1447+
method = (MethodInvocation) exp;
1448+
}
1449+
}
1450+
}
1451+
}
1452+
if (method != null) {
1453+
boolean isFiltered = false;
1454+
IMethodBinding methodBinding = method.resolveMethodBinding();
1455+
for (int j = 0; j < filterMethods.length; j += 2) {
1456+
if (Bindings.isMethodInvoking(methodBinding, filterMethods[j], filterMethods[j + 1])) {
1457+
isFiltered = true;
1458+
break;
1459+
}
1460+
}
1461+
if (isFiltered) {
1462+
continue;
1463+
}
1464+
}
1465+
isOnlyOneCall = false;
1466+
break;
1467+
}
1468+
}
1469+
}
1470+
}
1471+
}
1472+
return isOnlyOneCall;
1473+
}
1474+
13931475
public boolean visit(MethodInvocation node) {
13941476
Expression expression = node.getExpression();
13951477
if (expression != null) {
@@ -1435,9 +1517,17 @@ public boolean visit(MethodInvocation node) {
14351517
if (paramTypes.length - 1 > 0) {
14361518
buffer.append(", ");
14371519
}
1438-
buffer.append("[");
1520+
boolean needBrackets = true;
1521+
//if (args.size() == 1) {
1522+
Expression arg = (Expression) args.get(args.size() - 1);
1523+
ITypeBinding resolveTypeBinding = arg.resolveTypeBinding();
1524+
if (resolveTypeBinding.isArray()) {
1525+
needBrackets = false;
1526+
}
1527+
//}
1528+
if (needBrackets) buffer.append("[");
14391529
visitList(args, ", ", paramTypes.length - 1, size);
1440-
buffer.append("]");
1530+
if (needBrackets) buffer.append("]");
14411531
} else {
14421532
for (Iterator iter = args.iterator(); iter.hasNext();) {
14431533
ASTNode element = (ASTNode) iter.next();
@@ -1921,9 +2011,25 @@ public boolean visit(SuperMethodInvocation node) {
19212011
public boolean visit(ThisExpression node) {
19222012
Name qualifier = node.getQualifier();
19232013
if (qualifier != null) {
1924-
buffer.append("this.callbacks[\"");
1925-
qualifier.accept(this);
1926-
buffer.append("\"]");
2014+
ASTNode xparent = node.getParent();
2015+
while (xparent != null
2016+
&& !(xparent instanceof AbstractTypeDeclaration)
2017+
&& !(xparent instanceof AnonymousClassDeclaration)) {
2018+
xparent = xparent.getParent();
2019+
}
2020+
if (xparent == null
2021+
|| xparent.getParent() == null // CompilationUnit
2022+
|| xparent.getParent().getParent() == null) {
2023+
buffer.append("this");
2024+
} else {
2025+
/*
2026+
* only need callbacks wrapper in inner classes
2027+
* or anonymous classes.
2028+
*/
2029+
buffer.append("this.callbacks[\"");
2030+
qualifier.accept(this);
2031+
buffer.append("\"]");
2032+
}
19272033
} else {
19282034
buffer.append("this");
19292035
}
@@ -2548,7 +2654,22 @@ public static void main(String[] args) {
25482654
}
25492655

25502656
public boolean visit(TypeLiteral node) {
2551-
node.getType().accept(this);
2657+
Type type = node.getType();
2658+
if (type.isPrimitiveType()) {
2659+
ITypeBinding resolveBinding = type.resolveBinding();
2660+
String name = resolveBinding.getName();
2661+
if ("boolean".equals(name)) {
2662+
buffer.append("Boolean");
2663+
return false;
2664+
} else { // TODO: More types? Integer, Long, Double, ... ?
2665+
buffer.append("Number");
2666+
return false;
2667+
}
2668+
} else if (type.isArrayType()) {
2669+
buffer.append("Array");
2670+
return false;
2671+
}
2672+
type.accept(this);
25522673
return false;
25532674
}
25542675

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

Lines changed: 68 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected void checkSuperType(Set set) {
108108
for (Iterator iterator = classBindingSet.iterator(); iterator
109109
.hasNext();) {
110110
ITypeBinding binding = (ITypeBinding) iterator.next();
111-
if (Bindings.isSuperType(binding, qn.binding)) {
111+
if (qn.binding != null && Bindings.isSuperType(binding, qn.binding)) {
112112
removed.add(qn);
113113
isRemoved = true;
114114
break;
@@ -126,11 +126,38 @@ protected void checkSuperType(Set set) {
126126
set.add(qn.qualifiedName);
127127
}
128128
}
129+
130+
131+
protected void remedyReflectionDependency(Set set) {
132+
boolean needRemedy = false;;
133+
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
134+
Object next = iterator.next();
135+
String name = null;
136+
if (next instanceof QNTypeBinding) {
137+
QNTypeBinding qn = (QNTypeBinding) next;
138+
name = qn.qualifiedName;
139+
} else {
140+
name = (String) next;
141+
}
142+
if ("net.sf.j2s.ajax.AClass".equals(name)
143+
|| "net.sf.j2s.ajax.ASWTClass".equals(name)) {
144+
needRemedy = true;
145+
break;
146+
}
147+
}
148+
if (needRemedy) {
149+
set.add("java.lang.reflect.Constructor");
150+
}
151+
}
152+
129153
public String getDependencyScript(StringBuffer mainJS) {
130154
checkSuperType(musts);
131155
checkSuperType(requires);
132156
checkSuperType(optionals);
133-
157+
remedyReflectionDependency(musts);
158+
remedyReflectionDependency(requires);
159+
remedyReflectionDependency(optionals);
160+
134161
musts.remove("");
135162
requires.remove("");
136163
optionals.remove("");
@@ -449,6 +476,15 @@ public boolean isClassKnown(String qualifiedName) {
449476
public boolean isQualifiedNameOK(String qualifiedName, ASTNode node) {
450477
if (qualifiedName != null
451478
&& !isClassKnown(qualifiedName)
479+
&& qualifiedName.indexOf('[') == -1
480+
&& !"int".equals(qualifiedName)
481+
&& !"float".equals(qualifiedName)
482+
&& !"double".equals(qualifiedName)
483+
&& !"long".equals(qualifiedName)
484+
&& !"short".equals(qualifiedName)
485+
&& !"byte".equals(qualifiedName)
486+
&& !"char".equals(qualifiedName)
487+
&& !"boolean".equals(qualifiedName)
452488
&& !qualifiedName.startsWith("org.w3c.dom.")
453489
&& !qualifiedName.startsWith("org.eclipse.swt.internal.xhtml.")) {
454490
ASTNode root = node.getRoot();
@@ -573,7 +609,7 @@ protected void visitForRequires(AbstractTypeDeclaration node) {
573609
}
574610
}
575611
}
576-
612+
577613
private DependencyASTVisitor getSelfVisitor() {
578614
try {
579615
Object obj = this.getClass().getConstructor(new Class[0]).newInstance(new Object[0]);
@@ -715,41 +751,41 @@ public boolean visit(ClassInstanceCreation node) {
715751
return super.visit(node);
716752
}
717753

718-
/* (non-Javadoc)
719-
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.ArrayCreation)
720-
*/
721-
public boolean visit(ArrayCreation node) {
722-
ArrayType type = node.getType();
723-
Type elementType = type.getElementType();
724-
if (!elementType.isPrimitiveType()) {
725-
ITypeBinding resolveTypeBinding = elementType.resolveBinding();
726-
ITypeBinding declaringClass = resolveTypeBinding.getDeclaringClass();
727-
QNTypeBinding qn = new QNTypeBinding();
728-
String qualifiedName = null;
729-
if (declaringClass != null) {
730-
qualifiedName = declaringClass.getQualifiedName();
731-
qn.binding = declaringClass;
732-
} else {
733-
qualifiedName = resolveTypeBinding.getQualifiedName();
734-
qn.binding = resolveTypeBinding;
735-
}
736-
qualifiedName = discardGenericType(qualifiedName);
737-
qn.qualifiedName = qualifiedName;
738-
if (isQualifiedNameOK(qualifiedName, node)
739-
&& !musts.contains(qn)
740-
&& !requires.contains(qn)) {
741-
optionals.add(qn);
742-
}
743-
}
744-
return super.visit(node);
745-
}
754+
// /* (non-Javadoc)
755+
// * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.ArrayCreation)
756+
// */
757+
// public boolean visit(ArrayCreation node) {
758+
// ArrayType type = node.getType();
759+
// Type elementType = type.getElementType();
760+
// if (!elementType.isPrimitiveType()) {
761+
// ITypeBinding resolveTypeBinding = elementType.resolveBinding();
762+
// ITypeBinding declaringClass = resolveTypeBinding.getDeclaringClass();
763+
// QNTypeBinding qn = new QNTypeBinding();
764+
// String qualifiedName = null;
765+
// if (declaringClass != null) {
766+
// qualifiedName = declaringClass.getQualifiedName();
767+
// qn.binding = declaringClass;
768+
// } else {
769+
// qualifiedName = resolveTypeBinding.getQualifiedName();
770+
// qn.binding = resolveTypeBinding;
771+
// }
772+
// qualifiedName = discardGenericType(qualifiedName);
773+
// qn.qualifiedName = qualifiedName;
774+
// if (isQualifiedNameOK(qualifiedName, node)
775+
// && !musts.contains(qn)
776+
// && !requires.contains(qn)) {
777+
// optionals.add(qn);
778+
// }
779+
// }
780+
// return super.visit(node);
781+
// }
746782

747783
/* (non-Javadoc)
748784
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.MethodInvocation)
749785
*/
750786
public boolean visit(MethodInvocation node) {
751787
IMethodBinding resolveMethodBinding = node.resolveMethodBinding();
752-
if (Modifier.isStatic(resolveMethodBinding.getModifiers())) {
788+
if (resolveMethodBinding != null && Modifier.isStatic(resolveMethodBinding.getModifiers())) {
753789
Expression expression = node.getExpression();
754790
if (expression instanceof Name) {
755791
Name name = (Name) expression;

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ public static boolean checkReference(ASTNode node, String methodSignature) {
5151
*/
5252
public boolean visit(ClassInstanceCreation node) {
5353
IMethodBinding constructorBinding = node.resolveConstructorBinding();
54-
if (methodSignature.equals(constructorBinding.getKey())) {
54+
String key = constructorBinding.getKey();
55+
if (key != null) {
56+
key = key.replaceAll("<[^>]+>", "");
57+
}
58+
if (methodSignature.equals(key)) {
5559
isReferenced = true;
5660
return false;
5761
}
@@ -63,7 +67,11 @@ public boolean visit(ClassInstanceCreation node) {
6367
*/
6468
public boolean visit(ConstructorInvocation node) {
6569
IMethodBinding constructorBinding = node.resolveConstructorBinding();
66-
if (methodSignature.equals(constructorBinding.getKey())) {
70+
String key = constructorBinding.getKey();
71+
if (key != null) {
72+
key = key.replaceAll("<[^>]+>", "");
73+
}
74+
if (methodSignature.equals(key)) {
6775
isReferenced = true;
6876
return false;
6977
}
@@ -75,7 +83,11 @@ public boolean visit(ConstructorInvocation node) {
7583
*/
7684
public boolean visit(MethodInvocation node) {
7785
IMethodBinding methodBinding = node.resolveMethodBinding();
78-
if (methodSignature.equals(methodBinding.getKey())) {
86+
String key = methodBinding.getKey();
87+
if (key != null) {
88+
key = key.replaceAll("<[^>]+>", "");
89+
}
90+
if (methodSignature.equals(key)) {
7991
isReferenced = true;
8092
return false;
8193
}
@@ -87,7 +99,11 @@ public boolean visit(MethodInvocation node) {
8799
*/
88100
public boolean visit(SuperMethodInvocation node) {
89101
IMethodBinding methodBinding = node.resolveMethodBinding();
90-
if (methodSignature.equals(methodBinding.getKey())) {
102+
String key = methodBinding.getKey();
103+
if (key != null) {
104+
key = key.replaceAll("<[^>]+>", "");
105+
}
106+
if (methodSignature.equals(key)) {
91107
isReferenced = true;
92108
return false;
93109
}

0 commit comments

Comments
 (0)