| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012, the Dart project authors. | 2 * Copyright (c) 2012, 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 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 node.accept(new UnifyingAstVisitor<Void>() { | 225 node.accept(new UnifyingAstVisitor<Void>() { |
| 226 @Override | 226 @Override |
| 227 public Void visitNode(AstNode node) { | 227 public Void visitNode(AstNode node) { |
| 228 clearNode(node); | 228 clearNode(node); |
| 229 return super.visitNode(node); | 229 return super.visitNode(node); |
| 230 } | 230 } |
| 231 }); | 231 }); |
| 232 } | 232 } |
| 233 } | 233 } |
| 234 | 234 |
| 235 public void ensureUniqueClassMemberNames(CompilationUnit unit) { | 235 public void ensureUniqueClassMemberNames() { |
| 236 unit.accept(new RecursiveAstVisitor<Void>() { | 236 dartUniverse.accept(new RecursiveAstVisitor<Void>() { |
| 237 private final Set<ClassMember> untouchableMethods = Sets.newHashSet(); | 237 private final Set<ClassMember> untouchableMethods = Sets.newHashSet(); |
| 238 private final Map<String, ClassMember> usedClassMembers = Maps.newHashMap(
); | 238 private final Map<String, ClassMember> usedClassMembers = Maps.newHashMap(
); |
| 239 private final Set<String> superNames = Sets.newHashSet(); | 239 private final Set<String> superNames = Sets.newHashSet(); |
| 240 private final Map<String, List<IMethodBinding>> superMembers = Maps.newHas
hMap(); | 240 private final Map<String, List<IMethodBinding>> superMembers = Maps.newHas
hMap(); |
| 241 | 241 |
| 242 @Override | 242 @Override |
| 243 public Void visitClassDeclaration(ClassDeclaration node) { | 243 public Void visitClassDeclaration(ClassDeclaration node) { |
| 244 untouchableMethods.clear(); | 244 untouchableMethods.clear(); |
| 245 usedClassMembers.clear(); | 245 usedClassMembers.clear(); |
| 246 superNames.clear(); | 246 superNames.clear(); |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 | 378 |
| 379 /** | 379 /** |
| 380 * @return the artificial {@link ClassDeclaration}created for Java creation of
anonymous class | 380 * @return the artificial {@link ClassDeclaration}created for Java creation of
anonymous class |
| 381 * declaration. | 381 * declaration. |
| 382 */ | 382 */ |
| 383 public ClassDeclaration getAnonymousDeclaration(InstanceCreationExpression cre
ation) { | 383 public ClassDeclaration getAnonymousDeclaration(InstanceCreationExpression cre
ation) { |
| 384 return anonymousDeclarations.get(creation); | 384 return anonymousDeclarations.get(creation); |
| 385 } | 385 } |
| 386 | 386 |
| 387 /** | 387 /** |
| 388 * @return some Java binding for the given Dart {@link ConstructorDeclaration}
. |
| 389 */ |
| 390 public IMethodBinding getConstructorBinding(ConstructorDeclaration node) { |
| 391 return constructorToBinding.get(node); |
| 392 } |
| 393 |
| 394 /** |
| 388 * @return the not <code>null</code> {@link ConstructorDescription}, may be ju
st added. | 395 * @return the not <code>null</code> {@link ConstructorDescription}, may be ju
st added. |
| 389 */ | 396 */ |
| 390 public ConstructorDescription getConstructorDescription(ConstructorDeclaration
node) { | 397 public ConstructorDescription getConstructorDescription(ConstructorDeclaration
node) { |
| 391 IMethodBinding binding = constructorToBinding.get(node); | 398 IMethodBinding binding = getConstructorBinding(node); |
| 392 return getConstructorDescription(binding); | 399 return getConstructorDescription(binding); |
| 393 } | 400 } |
| 394 | 401 |
| 395 /** | 402 /** |
| 396 * @return the not <code>null</code> {@link ConstructorDescription}, may be ju
st added. | 403 * @return the not <code>null</code> {@link ConstructorDescription}, may be ju
st added. |
| 397 */ | 404 */ |
| 398 public ConstructorDescription getConstructorDescription(IMethodBinding binding
) { | 405 public ConstructorDescription getConstructorDescription(IMethodBinding binding
) { |
| 399 binding = (IMethodBinding) Bindings.getDeclaration(binding); | 406 binding = (IMethodBinding) Bindings.getDeclaration(binding); |
| 400 ConstructorDescription description = bindingToConstructor.get(binding); | 407 ConstructorDescription description = bindingToConstructor.get(binding); |
| 401 if (description == null) { | 408 if (description == null) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 413 * We rename {@link SimpleIdentifier}s, but sometimes we need to know original
name. | 420 * We rename {@link SimpleIdentifier}s, but sometimes we need to know original
name. |
| 414 */ | 421 */ |
| 415 public String getIdentifierOriginalName(SimpleIdentifier identifier) { | 422 public String getIdentifierOriginalName(SimpleIdentifier identifier) { |
| 416 String name = identifierToName.get(identifier); | 423 String name = identifierToName.get(identifier); |
| 417 if (name == null) { | 424 if (name == null) { |
| 418 name = identifier.getName(); | 425 name = identifier.getName(); |
| 419 } | 426 } |
| 420 return name; | 427 return name; |
| 421 } | 428 } |
| 422 | 429 |
| 430 public List<MethodInvocation> getInvocations(MethodDeclaration method) { |
| 431 List<MethodInvocation> invocations = Lists.newArrayList(); |
| 432 SimpleIdentifier methodName = method.getName(); |
| 433 List<SimpleIdentifier> references = getReferences(methodName); |
| 434 for (SimpleIdentifier reference : references) { |
| 435 if (reference.getParent() instanceof MethodInvocation) { |
| 436 MethodInvocation invocation = (MethodInvocation) reference.getParent(); |
| 437 if (invocation.getMethodName() == reference) { |
| 438 invocations.add(invocation); |
| 439 } |
| 440 } |
| 441 } |
| 442 return invocations; |
| 443 } |
| 444 |
| 423 public Map<CompilationUnitMember, File> getMemberToFile() { | 445 public Map<CompilationUnitMember, File> getMemberToFile() { |
| 424 return memberToFile; | 446 return memberToFile; |
| 425 } | 447 } |
| 426 | 448 |
| 427 /** | 449 /** |
| 428 * Returns all node annotations. Used for testing. | 450 * Returns all node annotations. Used for testing. |
| 429 */ | 451 */ |
| 430 public Map<AstNode, List<ParsedAnnotation>> getNodeAnnotations() { | 452 public Map<AstNode, List<ParsedAnnotation>> getNodeAnnotations() { |
| 431 return nodeAnnotations; | 453 return nodeAnnotations; |
| 432 } | 454 } |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 */ | 553 */ |
| 532 public void removeReference(SimpleIdentifier identifier) { | 554 public void removeReference(SimpleIdentifier identifier) { |
| 533 IBinding binding = getNodeBinding(identifier); | 555 IBinding binding = getNodeBinding(identifier); |
| 534 List<SimpleIdentifier> identifiers = bindingToIdentifiers.get(binding); | 556 List<SimpleIdentifier> identifiers = bindingToIdentifiers.get(binding); |
| 535 if (identifiers != null) { | 557 if (identifiers != null) { |
| 536 identifiers.remove(identifier); | 558 identifiers.remove(identifier); |
| 537 } | 559 } |
| 538 } | 560 } |
| 539 | 561 |
| 540 public void renameConstructor(ConstructorDeclaration node, String name) { | 562 public void renameConstructor(ConstructorDeclaration node, String name) { |
| 541 IMethodBinding binding = constructorToBinding.get(node); | 563 IMethodBinding binding = getConstructorBinding(node); |
| 542 // | 564 // |
| 543 SimpleIdentifier newIdentifier; | 565 SimpleIdentifier newIdentifier; |
| 544 if (name == null) { | 566 if (name == null) { |
| 545 newIdentifier = null; | 567 newIdentifier = null; |
| 546 } else { | 568 } else { |
| 547 newIdentifier = identifier(name); | 569 newIdentifier = identifier(name); |
| 548 } | 570 } |
| 549 // rename constructor | 571 // rename constructor |
| 550 node.setName(newIdentifier); | 572 node.setName(newIdentifier); |
| 551 // update references | 573 // update references |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 } | 628 } |
| 607 } | 629 } |
| 608 // run processors | 630 // run processors |
| 609 { | 631 { |
| 610 replaceInnerClassReferences(dartUniverse); | 632 replaceInnerClassReferences(dartUniverse); |
| 611 unwrapVarArgIfAlreadyArray(dartUniverse); | 633 unwrapVarArgIfAlreadyArray(dartUniverse); |
| 612 ensureFieldInitializers(dartUniverse); | 634 ensureFieldInitializers(dartUniverse); |
| 613 dontUseThisInFieldInitializers(dartUniverse); | 635 dontUseThisInFieldInitializers(dartUniverse); |
| 614 renameAnonymousClassDeclarations(); | 636 renameAnonymousClassDeclarations(); |
| 615 renamePrivateClassMembers(); | 637 renamePrivateClassMembers(); |
| 616 ensureUniqueClassMemberNames(dartUniverse); | |
| 617 applyLocalVariableSemanticChanges(dartUniverse); | |
| 618 new ConstructorSemanticProcessor(this).process(dartUniverse); | 638 new ConstructorSemanticProcessor(this).process(dartUniverse); |
| 619 renameConstructors(dartUniverse); | |
| 620 insertEnclosingTypeForInstanceCreationArguments(dartUniverse); | 639 insertEnclosingTypeForInstanceCreationArguments(dartUniverse); |
| 621 } | 640 } |
| 622 // done | 641 // done |
| 623 return dartUniverse; | 642 return dartUniverse; |
| 624 } | 643 } |
| 625 | 644 |
| 626 /** | 645 /** |
| 627 * @return the "technical" name for the top-level Dart class for Java anonymou
s class. | 646 * @return the "technical" name for the top-level Dart class for Java anonymou
s class. |
| 628 */ | 647 */ |
| 629 int generateTechnicalAnonymousClassIndex() { | 648 int generateTechnicalAnonymousClassIndex() { |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 940 name = newName; | 959 name = newName; |
| 941 break; | 960 break; |
| 942 } | 961 } |
| 943 } | 962 } |
| 944 } | 963 } |
| 945 // rename | 964 // rename |
| 946 renameIdentifier(nameNode, name); | 965 renameIdentifier(nameNode, name); |
| 947 } | 966 } |
| 948 } | 967 } |
| 949 | 968 |
| 950 private void renameConstructors(CompilationUnit unit) { | |
| 951 unit.accept(new RecursiveAstVisitor<Void>() { | |
| 952 private final Set<String> memberNamesInClass = Sets.newHashSet(); | |
| 953 private int numConstructors; | |
| 954 | |
| 955 @Override | |
| 956 public Void visitClassDeclaration(ClassDeclaration node) { | |
| 957 memberNamesInClass.clear(); | |
| 958 numConstructors = 0; | |
| 959 NodeList<ClassMember> members = node.getMembers(); | |
| 960 for (ClassMember member : members) { | |
| 961 if (member instanceof ConstructorDeclaration) { | |
| 962 numConstructors++; | |
| 963 } | |
| 964 if (member instanceof MethodDeclaration) { | |
| 965 String name = ((MethodDeclaration) member).getName().getName(); | |
| 966 memberNamesInClass.add(name); | |
| 967 } | |
| 968 if (member instanceof FieldDeclaration) { | |
| 969 FieldDeclaration fieldDeclaration = (FieldDeclaration) member; | |
| 970 NodeList<VariableDeclaration> variables = fieldDeclaration.getFields
().getVariables(); | |
| 971 for (VariableDeclaration variable : variables) { | |
| 972 String name = variable.getName().getName(); | |
| 973 memberNamesInClass.add(name); | |
| 974 } | |
| 975 } | |
| 976 } | |
| 977 return super.visitClassDeclaration(node); | |
| 978 } | |
| 979 | |
| 980 @Override | |
| 981 public Void visitConstructorDeclaration(ConstructorDeclaration node) { | |
| 982 IMethodBinding binding = constructorToBinding.get(node); | |
| 983 String bindingSignature = JavaUtils.getJdtSignature(binding); | |
| 984 // prepare name | |
| 985 String name = renameMap.get(bindingSignature); | |
| 986 if (name == null) { | |
| 987 if (numConstructors == 1 || node.getParameters().getParameters().isEmp
ty()) { | |
| 988 // don't set name, use unnamed constructor | |
| 989 } else { | |
| 990 int index = 1; | |
| 991 while (true) { | |
| 992 name = "con" + index++; | |
| 993 if (!memberNamesInClass.contains(name)) { | |
| 994 break; | |
| 995 } | |
| 996 } | |
| 997 } | |
| 998 } | |
| 999 memberNamesInClass.add(name); | |
| 1000 // apply name | |
| 1001 if ("<empty>".equals(name)) { | |
| 1002 name = null; | |
| 1003 } | |
| 1004 renameConstructor(node, name); | |
| 1005 // continue | |
| 1006 return super.visitConstructorDeclaration(node); | |
| 1007 } | |
| 1008 }); | |
| 1009 } | |
| 1010 | |
| 1011 private void renamePrivateClassMembers() { | 969 private void renamePrivateClassMembers() { |
| 1012 for (ClassMember member : privateClassMembers) { | 970 for (ClassMember member : privateClassMembers) { |
| 1013 if (member instanceof FieldDeclaration) { | 971 if (member instanceof FieldDeclaration) { |
| 1014 FieldDeclaration fieldDeclaration = (FieldDeclaration) member; | 972 FieldDeclaration fieldDeclaration = (FieldDeclaration) member; |
| 1015 NodeList<VariableDeclaration> variables = fieldDeclaration.getFields().g
etVariables(); | 973 NodeList<VariableDeclaration> variables = fieldDeclaration.getFields().g
etVariables(); |
| 1016 for (VariableDeclaration field : variables) { | 974 for (VariableDeclaration field : variables) { |
| 1017 SimpleIdentifier nameNode = field.getName(); | 975 SimpleIdentifier nameNode = field.getName(); |
| 1018 String name = nameNode.getName(); | 976 String name = nameNode.getName(); |
| 1019 renameIdentifier(nameNode, "_" + name); | 977 renameIdentifier(nameNode, "_" + name); |
| 1020 } | 978 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1099 } | 1057 } |
| 1100 } | 1058 } |
| 1101 } | 1059 } |
| 1102 } | 1060 } |
| 1103 } | 1061 } |
| 1104 } | 1062 } |
| 1105 } | 1063 } |
| 1106 }); | 1064 }); |
| 1107 } | 1065 } |
| 1108 } | 1066 } |
| OLD | NEW |