|
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) {
|
@@ -1435,9 +1517,17 @@ public boolean visit(MethodInvocation node) {
|
1435 | 1517 | if (paramTypes.length - 1 > 0) {
|
1436 | 1518 | buffer.append(", ");
|
1437 | 1519 | }
|
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("["); |
1439 | 1529 | visitList(args, ", ", paramTypes.length - 1, size);
|
1440 |
| - buffer.append("]"); |
| 1530 | + if (needBrackets) buffer.append("]"); |
1441 | 1531 | } else {
|
1442 | 1532 | for (Iterator iter = args.iterator(); iter.hasNext();) {
|
1443 | 1533 | ASTNode element = (ASTNode) iter.next();
|
@@ -1921,9 +2011,25 @@ public boolean visit(SuperMethodInvocation node) {
|
1921 | 2011 | public boolean visit(ThisExpression node) {
|
1922 | 2012 | Name qualifier = node.getQualifier();
|
1923 | 2013 | 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 | + } |
1927 | 2033 | } else {
|
1928 | 2034 | buffer.append("this");
|
1929 | 2035 | }
|
@@ -2548,7 +2654,22 @@ public static void main(String[] args) {
|
2548 | 2654 | }
|
2549 | 2655 |
|
2550 | 2656 | 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); |
2552 | 2673 | return false;
|
2553 | 2674 | }
|
2554 | 2675 |
|
|
0 commit comments