| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013, the Dart project authors. | 2 * Copyright (c) 2013, the Dart project authors. |
| 3 * | 3 * |
| 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except | 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except |
| 5 * in compliance with the License. You may obtain a copy of the License at | 5 * in compliance with the License. You may obtain a copy of the License at |
| 6 * | 6 * |
| 7 * http://www.eclipse.org/legal/epl-v10.html | 7 * http://www.eclipse.org/legal/epl-v10.html |
| 8 * | 8 * |
| 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License | 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License |
| 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express | 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 import com.google.dart.engine.ast.StringInterpolation; | 35 import com.google.dart.engine.ast.StringInterpolation; |
| 36 import com.google.dart.engine.ast.SuperConstructorInvocation; | 36 import com.google.dart.engine.ast.SuperConstructorInvocation; |
| 37 import com.google.dart.engine.ast.TypeName; | 37 import com.google.dart.engine.ast.TypeName; |
| 38 import com.google.dart.engine.ast.VariableDeclaration; | 38 import com.google.dart.engine.ast.VariableDeclaration; |
| 39 import com.google.dart.engine.ast.visitor.GeneralizingASTVisitor; | 39 import com.google.dart.engine.ast.visitor.GeneralizingASTVisitor; |
| 40 import com.google.dart.engine.scanner.Keyword; | 40 import com.google.dart.engine.scanner.Keyword; |
| 41 import com.google.dart.engine.scanner.TokenType; | 41 import com.google.dart.engine.scanner.TokenType; |
| 42 import com.google.dart.java2dart.Context; | 42 import com.google.dart.java2dart.Context; |
| 43 import com.google.dart.java2dart.util.JavaUtils; | 43 import com.google.dart.java2dart.util.JavaUtils; |
| 44 | 44 |
| 45 import static com.google.dart.java2dart.util.ASTFactory.argumentList; |
| 45 import static com.google.dart.java2dart.util.ASTFactory.assignmentExpression; | 46 import static com.google.dart.java2dart.util.ASTFactory.assignmentExpression; |
| 46 import static com.google.dart.java2dart.util.ASTFactory.binaryExpression; | 47 import static com.google.dart.java2dart.util.ASTFactory.binaryExpression; |
| 47 import static com.google.dart.java2dart.util.ASTFactory.booleanLiteral; | 48 import static com.google.dart.java2dart.util.ASTFactory.booleanLiteral; |
| 48 import static com.google.dart.java2dart.util.ASTFactory.identifier; | 49 import static com.google.dart.java2dart.util.ASTFactory.identifier; |
| 49 import static com.google.dart.java2dart.util.ASTFactory.instanceCreationExpressi
on; | 50 import static com.google.dart.java2dart.util.ASTFactory.instanceCreationExpressi
on; |
| 50 import static com.google.dart.java2dart.util.ASTFactory.integer; | 51 import static com.google.dart.java2dart.util.ASTFactory.integer; |
| 51 import static com.google.dart.java2dart.util.ASTFactory.interpolationExpression; | 52 import static com.google.dart.java2dart.util.ASTFactory.interpolationExpression; |
| 52 import static com.google.dart.java2dart.util.ASTFactory.interpolationString; | 53 import static com.google.dart.java2dart.util.ASTFactory.interpolationString; |
| 53 import static com.google.dart.java2dart.util.ASTFactory.methodInvocation; | 54 import static com.google.dart.java2dart.util.ASTFactory.methodInvocation; |
| 54 import static com.google.dart.java2dart.util.ASTFactory.namedExpression; | 55 import static com.google.dart.java2dart.util.ASTFactory.namedExpression; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 Expression rightExpr = node.getRightHandSide(); | 150 Expression rightExpr = node.getRightHandSide(); |
| 150 replaceNode( | 151 replaceNode( |
| 151 node, | 152 node, |
| 152 assignmentExpression( | 153 assignmentExpression( |
| 153 leftExpr, | 154 leftExpr, |
| 154 TokenType.EQ, | 155 TokenType.EQ, |
| 155 methodInvocation("javaBooleanOr", leftExpr, rightExpr))); | 156 methodInvocation("javaBooleanOr", leftExpr, rightExpr))); |
| 156 return null; | 157 return null; |
| 157 } | 158 } |
| 158 } | 159 } |
| 160 // Dart has no "bool &=" operator |
| 161 if (node.getOperator().getType() == TokenType.AMPERSAND_EQ) { |
| 162 Expression leftExpr = node.getLeftHandSide(); |
| 163 ITypeBinding argTypeBinding = context.getNodeTypeBinding(leftExpr); |
| 164 if (JavaUtils.isTypeNamed(argTypeBinding, "boolean")) { |
| 165 Expression rightExpr = node.getRightHandSide(); |
| 166 replaceNode( |
| 167 node, |
| 168 assignmentExpression( |
| 169 leftExpr, |
| 170 TokenType.EQ, |
| 171 methodInvocation("javaBooleanAnd", leftExpr, rightExpr))); |
| 172 return null; |
| 173 } |
| 174 } |
| 159 // String += 'c' | 175 // String += 'c' |
| 160 if (node.getOperator().getType() == TokenType.PLUS_EQ) { | 176 if (node.getOperator().getType() == TokenType.PLUS_EQ) { |
| 161 Expression leftExpr = node.getLeftHandSide(); | 177 Expression leftExpr = node.getLeftHandSide(); |
| 162 ITypeBinding argTypeBinding = context.getNodeTypeBinding(leftExpr); | 178 ITypeBinding argTypeBinding = context.getNodeTypeBinding(leftExpr); |
| 163 if (JavaUtils.isTypeNamed(argTypeBinding, "java.lang.String")) { | 179 if (JavaUtils.isTypeNamed(argTypeBinding, "java.lang.String")) { |
| 164 Expression rightExpr = node.getRightHandSide(); | 180 Expression rightExpr = node.getRightHandSide(); |
| 165 replaceCharWithString(rightExpr); | 181 replaceCharWithString(rightExpr); |
| 166 return null; | 182 return null; |
| 167 } | 183 } |
| 168 } | 184 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 195 } else { | 211 } else { |
| 196 elements.add(interpolationExpression(expression)); | 212 elements.add(interpolationExpression(expression)); |
| 197 } | 213 } |
| 198 } | 214 } |
| 199 elements.add(interpolationString("\"", "")); | 215 elements.add(interpolationString("\"", "")); |
| 200 StringInterpolation interpolation = string(elements); | 216 StringInterpolation interpolation = string(elements); |
| 201 replaceNode(node, interpolation); | 217 replaceNode(node, interpolation); |
| 202 return null; | 218 return null; |
| 203 } | 219 } |
| 204 } | 220 } |
| 221 // in Java "true | false" will compute both operands |
| 222 if (node.getOperator().getType() == TokenType.BAR) { |
| 223 ITypeBinding argTypeBinding = context.getNodeTypeBinding(node.getLeftO
perand()); |
| 224 super.visitBinaryExpression(node); |
| 225 if (JavaUtils.isTypeNamed(argTypeBinding, "boolean")) { |
| 226 replaceNode( |
| 227 node, |
| 228 methodInvocation("javaBooleanOr", node.getLeftOperand(), node.ge
tRightOperand())); |
| 229 return null; |
| 230 } |
| 231 } |
| 232 // in Java "true & false" will compute both operands |
| 233 if (node.getOperator().getType() == TokenType.AMPERSAND) { |
| 234 ITypeBinding argTypeBinding = context.getNodeTypeBinding(node.getLeftO
perand()); |
| 235 super.visitBinaryExpression(node); |
| 236 if (JavaUtils.isTypeNamed(argTypeBinding, "boolean")) { |
| 237 replaceNode( |
| 238 node, |
| 239 methodInvocation("javaBooleanAnd", node.getLeftOperand(), node.g
etRightOperand())); |
| 240 return null; |
| 241 } |
| 242 } |
| 205 // super | 243 // super |
| 206 super.visitBinaryExpression(node); | 244 super.visitBinaryExpression(node); |
| 207 // in Java "true | false" will compute both operands | |
| 208 if (node.getOperator().getType() == TokenType.BAR) { | |
| 209 Expression leftOperand = node.getLeftOperand(); | |
| 210 ITypeBinding argTypeBinding = context.getNodeTypeBinding(leftOperand); | |
| 211 if (JavaUtils.isTypeNamed(argTypeBinding, "boolean")) { | |
| 212 replaceNode( | |
| 213 node, | |
| 214 methodInvocation("javaBooleanOr", leftOperand, node.getRightOper
and())); | |
| 215 return null; | |
| 216 } | |
| 217 } | |
| 218 // done | 245 // done |
| 219 return null; | 246 return null; |
| 220 } | 247 } |
| 221 | 248 |
| 222 @Override | 249 @Override |
| 223 public Void visitInstanceCreationExpression(InstanceCreationExpression nod
e) { | 250 public Void visitInstanceCreationExpression(InstanceCreationExpression nod
e) { |
| 224 super.visitInstanceCreationExpression(node); | 251 super.visitInstanceCreationExpression(node); |
| 225 ITypeBinding typeBinding = context.getNodeTypeBinding(node); | 252 ITypeBinding typeBinding = context.getNodeTypeBinding(node); |
| 226 IMethodBinding binding = (IMethodBinding) context.getNodeBinding(node); | 253 IMethodBinding binding = (IMethodBinding) context.getNodeBinding(node); |
| 227 List<Expression> args = node.getArgumentList().getArguments(); | 254 List<Expression> args = node.getArgumentList().getArguments(); |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 return null; | 603 return null; |
| 577 } | 604 } |
| 578 } | 605 } |
| 579 } | 606 } |
| 580 return super.visitPropertyAccess(node); | 607 return super.visitPropertyAccess(node); |
| 581 } | 608 } |
| 582 | 609 |
| 583 @Override | 610 @Override |
| 584 public Void visitSuperConstructorInvocation(SuperConstructorInvocation nod
e) { | 611 public Void visitSuperConstructorInvocation(SuperConstructorInvocation nod
e) { |
| 585 super.visitSuperConstructorInvocation(node); | 612 super.visitSuperConstructorInvocation(node); |
| 613 NodeList<Expression> args = node.getArgumentList().getArguments(); |
| 586 IMethodBinding binding = (IMethodBinding) context.getNodeBinding(node); | 614 IMethodBinding binding = (IMethodBinding) context.getNodeBinding(node); |
| 587 if (isMethodInClass2(binding, "<init>(java.lang.Throwable)", "java.lang.
Exception")) { | 615 if (isMethodInExactClass(binding, "<init>(java.lang.Throwable)", "java.l
ang.Exception")) { |
| 588 node.setConstructorName(identifier("withCause")); | 616 node.setConstructorName(identifier("withCause")); |
| 589 } | 617 } |
| 618 if (isMethodInExactClass( |
| 619 binding, |
| 620 "<init>(java.lang.Throwable)", |
| 621 "java.lang.RuntimeException")) { |
| 622 node.setArgumentList(argumentList(namedExpression("cause", args.get(0)
))); |
| 623 } |
| 624 if (isMethodInExactClass(binding, "<init>(java.lang.String)", "java.lang
.RuntimeException")) { |
| 625 node.setArgumentList(argumentList(namedExpression("message", args.get(
0)))); |
| 626 } |
| 627 if (isMethodInExactClass( |
| 628 binding, |
| 629 "<init>(java.lang.String,java.lang.Throwable)", |
| 630 "java.lang.RuntimeException")) { |
| 631 node.setArgumentList(argumentList( |
| 632 namedExpression("message", args.get(0)), |
| 633 namedExpression("cause", args.get(0)))); |
| 634 } |
| 590 return null; | 635 return null; |
| 591 } | 636 } |
| 592 | 637 |
| 593 @Override | 638 @Override |
| 594 public Void visitTypeName(TypeName node) { | 639 public Void visitTypeName(TypeName node) { |
| 595 ITypeBinding typeBinding = (ITypeBinding) context.getNodeBinding(node); | 640 ITypeBinding typeBinding = (ITypeBinding) context.getNodeBinding(node); |
| 596 // replace by name | 641 // replace by name |
| 597 if (node.getName() instanceof SimpleIdentifier) { | 642 if (node.getName() instanceof SimpleIdentifier) { |
| 598 SimpleIdentifier nameNode = (SimpleIdentifier) node.getName(); | 643 SimpleIdentifier nameNode = (SimpleIdentifier) node.getName(); |
| 599 String name = nameNode.getName(); | 644 String name = nameNode.getName(); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 683 ITypeBinding leftBinding = parameterType; | 728 ITypeBinding leftBinding = parameterType; |
| 684 ITypeBinding rightBinding = context.getNodeTypeBinding(arg); | 729 ITypeBinding rightBinding = context.getNodeTypeBinding(arg); |
| 685 if (JavaUtils.isTypeNamed(leftBinding, "java.lang.CharSequence") | 730 if (JavaUtils.isTypeNamed(leftBinding, "java.lang.CharSequence") |
| 686 && JavaUtils.isTypeNamed(rightBinding, "java.lang.String")) { | 731 && JavaUtils.isTypeNamed(rightBinding, "java.lang.String")) { |
| 687 arg = instanceCreationExpression(Keyword.NEW, typeName("CharSequence"),
arg); | 732 arg = instanceCreationExpression(Keyword.NEW, typeName("CharSequence"),
arg); |
| 688 args.set(i, arg); | 733 args.set(i, arg); |
| 689 } | 734 } |
| 690 } | 735 } |
| 691 } | 736 } |
| 692 } | 737 } |
| OLD | NEW |