Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/Context.java

Issue 18129004: Simplify constructors translation, improve code style. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 22 matching lines...) Expand all
33 import com.google.dart.engine.ast.ForEachStatement; 33 import com.google.dart.engine.ast.ForEachStatement;
34 import com.google.dart.engine.ast.FormalParameter; 34 import com.google.dart.engine.ast.FormalParameter;
35 import com.google.dart.engine.ast.FormalParameterList; 35 import com.google.dart.engine.ast.FormalParameterList;
36 import com.google.dart.engine.ast.Identifier; 36 import com.google.dart.engine.ast.Identifier;
37 import com.google.dart.engine.ast.InstanceCreationExpression; 37 import com.google.dart.engine.ast.InstanceCreationExpression;
38 import com.google.dart.engine.ast.ListLiteral; 38 import com.google.dart.engine.ast.ListLiteral;
39 import com.google.dart.engine.ast.MethodDeclaration; 39 import com.google.dart.engine.ast.MethodDeclaration;
40 import com.google.dart.engine.ast.MethodInvocation; 40 import com.google.dart.engine.ast.MethodInvocation;
41 import com.google.dart.engine.ast.NodeList; 41 import com.google.dart.engine.ast.NodeList;
42 import com.google.dart.engine.ast.PropertyAccess; 42 import com.google.dart.engine.ast.PropertyAccess;
43 import com.google.dart.engine.ast.RedirectingConstructorInvocation;
43 import com.google.dart.engine.ast.SimpleIdentifier; 44 import com.google.dart.engine.ast.SimpleIdentifier;
44 import com.google.dart.engine.ast.SuperConstructorInvocation; 45 import com.google.dart.engine.ast.SuperConstructorInvocation;
45 import com.google.dart.engine.ast.ThisExpression; 46 import com.google.dart.engine.ast.ThisExpression;
46 import com.google.dart.engine.ast.VariableDeclaration; 47 import com.google.dart.engine.ast.VariableDeclaration;
47 import com.google.dart.engine.ast.VariableDeclarationList; 48 import com.google.dart.engine.ast.VariableDeclarationList;
48 import com.google.dart.engine.ast.visitor.GeneralizingASTVisitor; 49 import com.google.dart.engine.ast.visitor.GeneralizingASTVisitor;
49 import com.google.dart.engine.ast.visitor.RecursiveASTVisitor; 50 import com.google.dart.engine.ast.visitor.RecursiveASTVisitor;
50 import com.google.dart.engine.scanner.Keyword; 51 import com.google.dart.engine.scanner.Keyword;
51 import com.google.dart.engine.scanner.KeywordToken; 52 import com.google.dart.engine.scanner.KeywordToken;
52 import com.google.dart.engine.scanner.TokenType; 53 import com.google.dart.engine.scanner.TokenType;
54 import com.google.dart.java2dart.processor.ConstructorSemanticProcessor;
53 import com.google.dart.java2dart.util.Bindings; 55 import com.google.dart.java2dart.util.Bindings;
54 import com.google.dart.java2dart.util.JavaUtils; 56 import com.google.dart.java2dart.util.JavaUtils;
55 57
56 import static com.google.dart.java2dart.util.ASTFactory.assignmentExpression; 58 import static com.google.dart.java2dart.util.ASTFactory.assignmentExpression;
57 import static com.google.dart.java2dart.util.ASTFactory.block; 59 import static com.google.dart.java2dart.util.ASTFactory.block;
58 import static com.google.dart.java2dart.util.ASTFactory.blockFunctionBody; 60 import static com.google.dart.java2dart.util.ASTFactory.blockFunctionBody;
59 import static com.google.dart.java2dart.util.ASTFactory.compilationUnit; 61 import static com.google.dart.java2dart.util.ASTFactory.compilationUnit;
60 import static com.google.dart.java2dart.util.ASTFactory.constructorDeclaration; 62 import static com.google.dart.java2dart.util.ASTFactory.constructorDeclaration;
61 import static com.google.dart.java2dart.util.ASTFactory.expressionStatement; 63 import static com.google.dart.java2dart.util.ASTFactory.expressionStatement;
62 import static com.google.dart.java2dart.util.ASTFactory.formalParameterList; 64 import static com.google.dart.java2dart.util.ASTFactory.formalParameterList;
(...skipping 23 matching lines...) Expand all
86 import java.util.Set; 88 import java.util.Set;
87 import java.util.concurrent.atomic.AtomicBoolean; 89 import java.util.concurrent.atomic.AtomicBoolean;
88 90
89 /** 91 /**
90 * Context information for Java to Dart translation. 92 * Context information for Java to Dart translation.
91 */ 93 */
92 public class Context { 94 public class Context {
93 /** 95 /**
94 * Information about constructor and its usages. 96 * Information about constructor and its usages.
95 */ 97 */
96 class ConstructorDescription { 98 public static class ConstructorDescription {
97 final IMethodBinding binding; 99 final IMethodBinding binding;
98 final List<SuperConstructorInvocation> superInvocations = Lists.newArrayList (); 100 public final List<RedirectingConstructorInvocation> redirectingInvocations = Lists.newArrayList();
99 final List<InstanceCreationExpression> instanceCreations = Lists.newArrayLis t(); 101 public final List<SuperConstructorInvocation> superInvocations = Lists.newAr rayList();
100 final List<SimpleIdentifier> implInvocations = Lists.newArrayList(); 102 public final List<InstanceCreationExpression> instanceCreations = Lists.newA rrayList();
103 public boolean isEnum;
101 String declName; 104 String declName;
102 String implName;
103 105
104 public ConstructorDescription(IMethodBinding binding) { 106 public ConstructorDescription(IMethodBinding binding) {
105 this.binding = binding; 107 this.binding = binding;
106 } 108 }
107 } 109 }
108 110
109 private static final String[] JAVA_EXTENSION = {"java"}; 111 private static final String[] JAVA_EXTENSION = {"java"};
110 private final List<File> classpathFiles = Lists.newArrayList(); 112 private final List<File> classpathFiles = Lists.newArrayList();
111 private final List<File> sourceFolders = Lists.newArrayList(); 113 private final List<File> sourceFolders = Lists.newArrayList();
112 private final List<File> sourceFiles = Lists.newArrayList(); 114 private final List<File> sourceFiles = Lists.newArrayList();
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 } 543 }
542 544
543 /** 545 /**
544 * @return the artificial {@link ClassDeclaration}created for Java creation of anonymous class 546 * @return the artificial {@link ClassDeclaration}created for Java creation of anonymous class
545 * declaration. 547 * declaration.
546 */ 548 */
547 public ClassDeclaration getAnonymousDeclaration(InstanceCreationExpression cre ation) { 549 public ClassDeclaration getAnonymousDeclaration(InstanceCreationExpression cre ation) {
548 return anonymousDeclarations.get(creation); 550 return anonymousDeclarations.get(creation);
549 } 551 }
550 552
553 /**
554 * @return the not <code>null</code> {@link ConstructorDescription}, may be ju st added.
555 */
556 public ConstructorDescription getConstructorDescription(IMethodBinding binding ) {
557 ConstructorDescription description = bindingToConstructor.get(binding);
558 if (description == null) {
559 description = new ConstructorDescription(binding);
560 bindingToConstructor.put(binding, description);
561 }
562 return description;
563 }
564
551 public Map<File, List<CompilationUnitMember>> getFileToMembers() { 565 public Map<File, List<CompilationUnitMember>> getFileToMembers() {
552 return fileToMembers; 566 return fileToMembers;
553 } 567 }
554 568
555 /** 569 /**
556 * We rename {@link SimpleIdentifier}s, but sometimes we need to know original name. 570 * We rename {@link SimpleIdentifier}s, but sometimes we need to know original name.
557 */ 571 */
558 public String getIdentifierOriginalName(SimpleIdentifier identifier) { 572 public String getIdentifierOriginalName(SimpleIdentifier identifier) {
559 String name = identifierToName.get(identifier); 573 String name = identifierToName.get(identifier);
560 if (name == null) { 574 if (name == null) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 if (name == null) { 618 if (name == null) {
605 newIdentifier = null; 619 newIdentifier = null;
606 } else { 620 } else {
607 newIdentifier = identifier(name); 621 newIdentifier = identifier(name);
608 } 622 }
609 // rename constructor 623 // rename constructor
610 node.setName(newIdentifier); 624 node.setName(newIdentifier);
611 // update references 625 // update references
612 ConstructorDescription constructorDescription = bindingToConstructor.get(bin ding); 626 ConstructorDescription constructorDescription = bindingToConstructor.get(bin ding);
613 if (constructorDescription != null) { 627 if (constructorDescription != null) {
614 // set name in InstanceCreationExpression 628 // set name in RedirectingConstructorInvocation
615 { 629 {
616 List<InstanceCreationExpression> creations = constructorDescription.inst anceCreations; 630 List<RedirectingConstructorInvocation> invocations = constructorDescript ion.redirectingInvocations;
617 for (InstanceCreationExpression creation : creations) { 631 for (RedirectingConstructorInvocation invocation : invocations) {
618 creation.getConstructorName().setName(newIdentifier); 632 invocation.setConstructorName(newIdentifier);
619 } 633 }
620 } 634 }
621 // set name in SuperConstructorInvocation 635 // set name in SuperConstructorInvocation
622 { 636 {
623 List<SuperConstructorInvocation> invocations = constructorDescription.su perInvocations; 637 List<SuperConstructorInvocation> invocations = constructorDescription.su perInvocations;
624 for (SuperConstructorInvocation invocation : invocations) { 638 for (SuperConstructorInvocation invocation : invocations) {
625 invocation.setConstructorName(newIdentifier); 639 invocation.setConstructorName(newIdentifier);
626 } 640 }
627 } 641 }
628 // set name in invocation of implementation 642 // set name in InstanceCreationExpression
629 { 643 {
630 List<SimpleIdentifier> invocations = constructorDescription.implInvocati ons; 644 List<InstanceCreationExpression> creations = constructorDescription.inst anceCreations;
631 for (SimpleIdentifier identifier : invocations) { 645 for (InstanceCreationExpression creation : creations) {
632 identifier.setToken(token(TokenType.IDENTIFIER, constructorDescription .implName)); 646 creation.getConstructorName().setName(newIdentifier);
633 } 647 }
634 } 648 }
635 } 649 }
636 } 650 }
637 651
638 /** 652 /**
639 * Sets the {@link SimpleIdentifier} name and updates all references. 653 * Sets the {@link SimpleIdentifier} name and updates all references.
640 */ 654 */
641 public void renameIdentifier(SimpleIdentifier declarationIdentifier, String ne wName) { 655 public void renameIdentifier(SimpleIdentifier declarationIdentifier, String ne wName) {
642 // move identifiers to the new signature 656 // move identifiers to the new signature
(...skipping 24 matching lines...) Expand all
667 } 681 }
668 // run processors 682 // run processors
669 { 683 {
670 replaceInnerClassReferences(dartUniverse); 684 replaceInnerClassReferences(dartUniverse);
671 unwrapVarArgIfAlreadyArray(dartUniverse); 685 unwrapVarArgIfAlreadyArray(dartUniverse);
672 ensureFieldInitializers(dartUniverse); 686 ensureFieldInitializers(dartUniverse);
673 dontUseThisInFieldInitializers(dartUniverse); 687 dontUseThisInFieldInitializers(dartUniverse);
674 ensureUniqueClassMemberNames(dartUniverse); 688 ensureUniqueClassMemberNames(dartUniverse);
675 ensureNoVariableNameReferenceFromInitializer(dartUniverse); 689 ensureNoVariableNameReferenceFromInitializer(dartUniverse);
676 ensureMethodParameterDoesNotHide(dartUniverse); 690 ensureMethodParameterDoesNotHide(dartUniverse);
691 new ConstructorSemanticProcessor(this).process(dartUniverse);
677 renameConstructors(dartUniverse); 692 renameConstructors(dartUniverse);
678 } 693 }
679 // done 694 // done
680 return dartUniverse; 695 return dartUniverse;
681 } 696 }
682 697
683 /** 698 /**
684 * @return the "technical" name for the top-level Dart class for Java anonymou s class. 699 * @return the "technical" name for the top-level Dart class for Java anonymou s class.
685 */ 700 */
686 int generateTechnicalAnonymousClassIndex() { 701 int generateTechnicalAnonymousClassIndex() {
687 return technicalAnonymousClassIndex++; 702 return technicalAnonymousClassIndex++;
688 } 703 }
689 704
690 /** 705 /**
691 * @return the "technical" name for the Dart constructor. 706 * @return the "technical" name for the Dart constructor.
692 */ 707 */
693 String generateTechnicalConstructorName() { 708 String generateTechnicalConstructorName() {
694 return "jtd_constructor_" + technicalConstructorIndex++; 709 return "jtd_constructor_" + technicalConstructorIndex++;
695 } 710 }
696 711
697 /** 712 /**
698 * @return the "technical" name for the top-level Dart class for Java inner cl ass. 713 * @return the "technical" name for the top-level Dart class for Java inner cl ass.
699 */ 714 */
700 String generateTechnicalInnerClassName() { 715 String generateTechnicalInnerClassName() {
701 return "JtdClass_" + technicalInnerClassIndex++; 716 return "JtdClass_" + technicalInnerClassIndex++;
702 } 717 }
703 718
704 /** 719 /**
705 * @return the not <code>null</code> {@link ConstructorDescription}, may be ju st added.
706 */
707 ConstructorDescription getConstructorDescription(IMethodBinding binding) {
708 ConstructorDescription description = bindingToConstructor.get(binding);
709 if (description == null) {
710 description = new ConstructorDescription(binding);
711 bindingToConstructor.put(binding, description);
712 }
713 return description;
714 }
715
716 /**
717 * Remembers artificial {@link ClassDeclaration} created for Java creation of anonymous class 720 * Remembers artificial {@link ClassDeclaration} created for Java creation of anonymous class
718 * declaration. 721 * declaration.
719 */ 722 */
720 void putAnonymousDeclaration(InstanceCreationExpression creation, ClassDeclara tion declaration) { 723 void putAnonymousDeclaration(InstanceCreationExpression creation, ClassDeclara tion declaration) {
721 if (declaration != null) { 724 if (declaration != null) {
722 anonymousDeclarations.put(creation, declaration); 725 anonymousDeclarations.put(creation, declaration);
723 } 726 }
724 } 727 }
725 728
726 /** 729 /**
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
825 return super.visitThisExpression(node); 828 return super.visitThisExpression(node);
826 } 829 }
827 }); 830 });
828 return result.get(); 831 return result.get();
829 } 832 }
830 }); 833 });
831 // add field assignment for each "this" field initializer 834 // add field assignment for each "this" field initializer
832 if (thisInitializers.isEmpty()) { 835 if (thisInitializers.isEmpty()) {
833 return; 836 return;
834 } 837 }
835 ConstructorDeclaration singleConstructor = null; 838 boolean hasConstructor = false;
836 boolean hasImpl = false;
837 for (ClassMember classMember : classDeclaration.getMembers()) { 839 for (ClassMember classMember : classDeclaration.getMembers()) {
838 if (classMember instanceof ConstructorDeclaration) { 840 if (classMember instanceof ConstructorDeclaration) {
839 singleConstructor = (ConstructorDeclaration) classMember; 841 ConstructorDeclaration constructor = (ConstructorDeclaration) classM ember;
840 } 842 hasConstructor = true;
841 if (classMember instanceof MethodDeclaration) { 843 Block block = ((BlockFunctionBody) constructor.getBody()).getBlock() ;
842 MethodDeclaration method = (MethodDeclaration) classMember; 844 addAssignmentsToBlock(block, thisInitializers);
843 String methodName = method.getName().getName();
844 if (methodName.startsWith("_jtd_constructor_") && methodName.endsWit h("_impl")) {
845 hasImpl = true;
846 Block block = ((BlockFunctionBody) method.getBody()).getBlock();
847 addAssignmentsToBlock(block, thisInitializers);
848 }
849 } 845 }
850 } 846 }
851 // no "_impl", add assignments to the single constructor 847 // no constructors, generate default constructor
852 if (!hasImpl && singleConstructor != null) { 848 if (!hasConstructor) {
853 Block block = ((BlockFunctionBody) singleConstructor.getBody()).getBlo ck();
854 addAssignmentsToBlock(block, thisInitializers);
855 }
856 // no "_impl", generate default constructor
857 if (singleConstructor == null) {
858 Block block = block(); 849 Block block = block();
859 addAssignmentsToBlock(block, thisInitializers); 850 addAssignmentsToBlock(block, thisInitializers);
860 ConstructorDeclaration constructor = constructorDeclaration( 851 ConstructorDeclaration constructor = constructorDeclaration(
861 classDeclaration.getName(), 852 classDeclaration.getName(),
862 null, 853 null,
863 formalParameterList(), 854 formalParameterList(),
864 null, 855 null,
865 blockFunctionBody(block)); 856 blockFunctionBody(block));
866 classDeclaration.getMembers().add(constructor); 857 classDeclaration.getMembers().add(constructor);
867 } 858 }
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
1098 } 1089 }
1099 } 1090 }
1100 } 1091 }
1101 } 1092 }
1102 } 1093 }
1103 } 1094 }
1104 } 1095 }
1105 }); 1096 });
1106 } 1097 }
1107 } 1098 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698