|
31 | 31 | import org.eclipse.jdt.core.dom.IMethodBinding; |
32 | 32 | import org.eclipse.jdt.core.dom.ITypeBinding; |
33 | 33 | import org.eclipse.jdt.core.dom.IVariableBinding; |
| 34 | +import org.eclipse.jdt.core.dom.IfStatement; |
34 | 35 | import org.eclipse.jdt.core.dom.InfixExpression; |
35 | 36 | import org.eclipse.jdt.core.dom.Initializer; |
36 | 37 | import org.eclipse.jdt.core.dom.Javadoc; |
|
45 | 46 | import org.eclipse.jdt.core.dom.SimpleName; |
46 | 47 | import org.eclipse.jdt.core.dom.SimpleType; |
47 | 48 | import org.eclipse.jdt.core.dom.SingleVariableDeclaration; |
| 49 | +import org.eclipse.jdt.core.dom.Statement; |
48 | 50 | import org.eclipse.jdt.core.dom.SuperConstructorInvocation; |
49 | 51 | import org.eclipse.jdt.core.dom.SuperFieldAccess; |
50 | 52 | import org.eclipse.jdt.core.dom.SuperMethodInvocation; |
@@ -1113,6 +1115,10 @@ public void endVisit(MethodDeclaration node) { |
1113 | 1115 | } |
1114 | 1116 | super.endVisit(node); |
1115 | 1117 | } |
| 1118 | + |
| 1119 | + protected String[] getFilterMethods() { |
| 1120 | + return new String[0]; |
| 1121 | + } |
1116 | 1122 |
|
1117 | 1123 | public boolean visit(MethodDeclaration node) { |
1118 | 1124 | if (getJ2SDocTag(node, "@j2sIgnore") != null) { |
@@ -1144,8 +1150,9 @@ public boolean visit(MethodDeclaration node) { |
1144 | 1150 | Block body = node.getBody(); |
1145 | 1151 | boolean needToCheckArgs = false; |
1146 | 1152 | 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); |
1149 | 1156 | if (statement instanceof ReturnStatement) { |
1150 | 1157 | ReturnStatement ret = (ReturnStatement) statement; |
1151 | 1158 | Expression exp = ret.getExpression(); |
@@ -1390,6 +1397,81 @@ public boolean visit(MethodDeclaration node) { |
1390 | 1397 | return false; |
1391 | 1398 | } |
1392 | 1399 |
|
| 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 | + |
1393 | 1475 | public boolean visit(MethodInvocation node) { |
1394 | 1476 | Expression expression = node.getExpression(); |
1395 | 1477 | if (expression != null) { |
|
0 commit comments