Skip to content

Commit bcc0d87

Browse files
author
zhourenjian
committed
Optimize for super method invocation like:
{ checkWidget(); // ignored! return super.getBounds(); }
1 parent 701b8a2 commit bcc0d87

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

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

Lines changed: 84 additions & 2 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) {

0 commit comments

Comments
 (0)