|
31 | 31 | importorg.eclipse.jdt.core.dom.IMethodBinding; |
32 | 32 | importorg.eclipse.jdt.core.dom.ITypeBinding; |
33 | 33 | importorg.eclipse.jdt.core.dom.IVariableBinding; |
| 34 | +importorg.eclipse.jdt.core.dom.IfStatement; |
34 | 35 | importorg.eclipse.jdt.core.dom.InfixExpression; |
35 | 36 | importorg.eclipse.jdt.core.dom.Initializer; |
36 | 37 | importorg.eclipse.jdt.core.dom.Javadoc; |
|
45 | 46 | importorg.eclipse.jdt.core.dom.SimpleName; |
46 | 47 | importorg.eclipse.jdt.core.dom.SimpleType; |
47 | 48 | importorg.eclipse.jdt.core.dom.SingleVariableDeclaration; |
| 49 | +importorg.eclipse.jdt.core.dom.Statement; |
48 | 50 | importorg.eclipse.jdt.core.dom.SuperConstructorInvocation; |
49 | 51 | importorg.eclipse.jdt.core.dom.SuperFieldAccess; |
50 | 52 | importorg.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 | +protectedString[]getFilterMethods() { |
| 1120 | +returnnewString[0]; |
| 1121 | +} |
1116 | 1122 |
|
1117 | 1123 | publicbooleanvisit(MethodDeclarationnode) { |
1118 | 1124 | if (getJ2SDocTag(node,"@j2sIgnore") !=null) { |
@@ -1144,8 +1150,9 @@ public boolean visit(MethodDeclaration node) { |
1144 | 1150 | Blockbody =node.getBody(); |
1145 | 1151 | booleanneedToCheckArgs =false; |
1146 | 1152 | ListargsList =null; |
1147 | | -if (body !=null &&body.statements().size() ==1) { |
1148 | | -Objectstatement =body.statements().get(0); |
| 1153 | +if (body !=null &&containsOnlySuperCall(body)) { |
| 1154 | +Liststs =body.statements(); |
| 1155 | +Objectstatement =sts.get(sts.size() -1); |
1149 | 1156 | if (statementinstanceofReturnStatement) { |
1150 | 1157 | ReturnStatementret = (ReturnStatement)statement; |
1151 | 1158 | Expressionexp =ret.getExpression(); |
@@ -1390,6 +1397,81 @@ public boolean visit(MethodDeclaration node) { |
1390 | 1397 | returnfalse; |
1391 | 1398 | } |
1392 | 1399 |
|
| 1400 | +privatebooleancontainsOnlySuperCall(Blockbody) { |
| 1401 | +booleanisOnlyOneCall =false; |
| 1402 | +Listss =body.statements(); |
| 1403 | +intsize =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 | +Objectobj =ss.get(size -1); |
| 1418 | +if (objinstanceofExpressionStatement) { |
| 1419 | +ExpressionStatementsmt = (ExpressionStatement)obj; |
| 1420 | +Expressione =smt.getExpression(); |
| 1421 | +if (einstanceofSuperMethodInvocation) {// the last is super call |
| 1422 | +isOnlyOneCall =true; |
| 1423 | +for (inti =0;i <size -1;i++) {// check previous calls |
| 1424 | +Objectstatement =ss.get(i); |
| 1425 | +MethodInvocationmethod =null; |
| 1426 | +if (statementinstanceofExpressionStatement) { |
| 1427 | +ExpressionStatementsttmt = (ExpressionStatement)statement; |
| 1428 | +Expressionexp =sttmt.getExpression(); |
| 1429 | +if (expinstanceofMethodInvocation) { |
| 1430 | +method = (MethodInvocation)exp; |
| 1431 | +} |
| 1432 | +}elseif (statementinstanceofIfStatement) {// if (...) checkWidget(); |
| 1433 | +IfStatementifSss = (IfStatement)statement; |
| 1434 | +if (ifSss.getElseStatement() ==null) { |
| 1435 | +StatementthenStatement =ifSss.getThenStatement(); |
| 1436 | +if (thenStatementinstanceofBlock) { |
| 1437 | +Blockblock = (Block)thenStatement; |
| 1438 | +Liststatements =block.statements(); |
| 1439 | +if (statements.size() ==1) { |
| 1440 | +thenStatement = (Statement)statements.get(0); |
| 1441 | +} |
| 1442 | +} |
| 1443 | +if (thenStatementinstanceofExpressionStatement) { |
| 1444 | +ExpressionStatementexpStmt = (ExpressionStatement)thenStatement; |
| 1445 | +Expressionexp =expStmt.getExpression(); |
| 1446 | +if (expinstanceofMethodInvocation) { |
| 1447 | +method = (MethodInvocation)exp; |
| 1448 | +} |
| 1449 | +} |
| 1450 | +} |
| 1451 | +} |
| 1452 | +if (method !=null) { |
| 1453 | +booleanisFiltered =false; |
| 1454 | +IMethodBindingmethodBinding =method.resolveMethodBinding(); |
| 1455 | +for (intj =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 | +returnisOnlyOneCall; |
| 1473 | +} |
| 1474 | + |
1393 | 1475 | publicbooleanvisit(MethodInvocationnode) { |
1394 | 1476 | Expressionexpression =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 | +booleanneedBrackets =true; |
| 1521 | +//if (args.size() == 1) { |
| 1522 | +Expressionarg = (Expression)args.get(args.size() -1); |
| 1523 | +ITypeBindingresolveTypeBinding =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 (Iteratoriter =args.iterator();iter.hasNext();) { |
1443 | 1533 | ASTNodeelement = (ASTNode)iter.next(); |
@@ -1921,9 +2011,25 @@ public boolean visit(SuperMethodInvocation node) { |
1921 | 2011 | publicbooleanvisit(ThisExpressionnode) { |
1922 | 2012 | Namequalifier =node.getQualifier(); |
1923 | 2013 | if (qualifier !=null) { |
1924 | | -buffer.append("this.callbacks[\""); |
1925 | | -qualifier.accept(this); |
1926 | | -buffer.append("\"]"); |
| 2014 | +ASTNodexparent =node.getParent(); |
| 2015 | +while (xparent !=null |
| 2016 | +&& !(xparentinstanceofAbstractTypeDeclaration) |
| 2017 | +&& !(xparentinstanceofAnonymousClassDeclaration)) { |
| 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 | publicbooleanvisit(TypeLiteralnode) { |
2551 | | -node.getType().accept(this); |
| 2657 | +Typetype =node.getType(); |
| 2658 | +if (type.isPrimitiveType()) { |
| 2659 | +ITypeBindingresolveBinding =type.resolveBinding(); |
| 2660 | +Stringname =resolveBinding.getName(); |
| 2661 | +if ("boolean".equals(name)) { |
| 2662 | +buffer.append("Boolean"); |
| 2663 | +returnfalse; |
| 2664 | +}else {// TODO: More types? Integer, Long, Double, ... ? |
| 2665 | +buffer.append("Number"); |
| 2666 | +returnfalse; |
| 2667 | +} |
| 2668 | +}elseif (type.isArrayType()) { |
| 2669 | +buffer.append("Array"); |
| 2670 | +returnfalse; |
| 2671 | +} |
| 2672 | +type.accept(this); |
2552 | 2673 | returnfalse; |
2553 | 2674 | } |
2554 | 2675 |
|
|