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

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

Issue 136223002: Generate names for anonymous Java classes using enclosing classes and members. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 11 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
« no previous file with comments | « no previous file | editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/SyntaxTranslator.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 import static com.google.dart.java2dart.util.ASTFactory.expressionStatement; 61 import static com.google.dart.java2dart.util.ASTFactory.expressionStatement;
62 import static com.google.dart.java2dart.util.ASTFactory.formalParameterList; 62 import static com.google.dart.java2dart.util.ASTFactory.formalParameterList;
63 import static com.google.dart.java2dart.util.ASTFactory.identifier; 63 import static com.google.dart.java2dart.util.ASTFactory.identifier;
64 import static com.google.dart.java2dart.util.ASTFactory.propertyAccess; 64 import static com.google.dart.java2dart.util.ASTFactory.propertyAccess;
65 import static com.google.dart.java2dart.util.ASTFactory.thisExpression; 65 import static com.google.dart.java2dart.util.ASTFactory.thisExpression;
66 import static com.google.dart.java2dart.util.TokenFactory.token; 66 import static com.google.dart.java2dart.util.TokenFactory.token;
67 67
68 import org.apache.commons.io.Charsets; 68 import org.apache.commons.io.Charsets;
69 import org.apache.commons.io.FileUtils; 69 import org.apache.commons.io.FileUtils;
70 import org.apache.commons.lang3.ArrayUtils; 70 import org.apache.commons.lang3.ArrayUtils;
71 import org.apache.commons.lang3.StringUtils;
71 import org.eclipse.core.runtime.Assert; 72 import org.eclipse.core.runtime.Assert;
72 import org.eclipse.jdt.core.JavaCore; 73 import org.eclipse.jdt.core.JavaCore;
73 import org.eclipse.jdt.core.dom.AST; 74 import org.eclipse.jdt.core.dom.AST;
74 import org.eclipse.jdt.core.dom.ASTParser; 75 import org.eclipse.jdt.core.dom.ASTParser;
75 import org.eclipse.jdt.core.dom.FileASTRequestor; 76 import org.eclipse.jdt.core.dom.FileASTRequestor;
76 import org.eclipse.jdt.core.dom.IBinding; 77 import org.eclipse.jdt.core.dom.IBinding;
77 import org.eclipse.jdt.core.dom.IMethodBinding; 78 import org.eclipse.jdt.core.dom.IMethodBinding;
78 import org.eclipse.jdt.core.dom.ITypeBinding; 79 import org.eclipse.jdt.core.dom.ITypeBinding;
79 import org.eclipse.jdt.core.dom.IVariableBinding; 80 import org.eclipse.jdt.core.dom.IVariableBinding;
80 81
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 } 589 }
589 } 590 }
590 } 591 }
591 // run processors 592 // run processors
592 { 593 {
593 replaceInnerClassReferences(dartUniverse); 594 replaceInnerClassReferences(dartUniverse);
594 unwrapVarArgIfAlreadyArray(dartUniverse); 595 unwrapVarArgIfAlreadyArray(dartUniverse);
595 ensureFieldInitializers(dartUniverse); 596 ensureFieldInitializers(dartUniverse);
596 dontUseThisInFieldInitializers(dartUniverse); 597 dontUseThisInFieldInitializers(dartUniverse);
597 ensureUniqueClassMemberNames(dartUniverse); 598 ensureUniqueClassMemberNames(dartUniverse);
599 renameAnonymousClassDeclarations();
598 applyLocalVariableSemanticChanges(dartUniverse); 600 applyLocalVariableSemanticChanges(dartUniverse);
599 new ConstructorSemanticProcessor(this).process(dartUniverse); 601 new ConstructorSemanticProcessor(this).process(dartUniverse);
600 renameConstructors(dartUniverse); 602 renameConstructors(dartUniverse);
601 insertEnclosingTypeForInstanceCreationArguments(dartUniverse); 603 insertEnclosingTypeForInstanceCreationArguments(dartUniverse);
602 } 604 }
603 // done 605 // done
604 return dartUniverse; 606 return dartUniverse;
605 } 607 }
606 608
607 /** 609 /**
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 units.put(astFile, dartUnit); 846 units.put(astFile, dartUnit);
845 } catch (Throwable e) { 847 } catch (Throwable e) {
846 throw new Error(e); 848 throw new Error(e);
847 } 849 }
848 } 850 }
849 }, 851 },
850 null); 852 null);
851 return units; 853 return units;
852 } 854 }
853 855
856 /**
857 * Improves names for anonymous {@link ClassDeclaration}s.
858 */
859 private void renameAnonymousClassDeclarations() {
860 // prepare unused top-level names
861 Set<String> usedTopNames = Sets.newHashSet();
862 for (CompilationUnitMember unitMember : dartUniverse.getDeclarations()) {
863 if (unitMember instanceof ClassDeclaration) {
864 ClassDeclaration classDeclaration = (ClassDeclaration) unitMember;
865 String name = classDeclaration.getName().getName();
866 usedTopNames.add(name);
867 }
868 }
869 // rename anonymous types
870 for (Entry<InstanceCreationExpression, ClassDeclaration> entry : anonymousDe clarations.entrySet()) {
871 // prepare enclosing information
872 InstanceCreationExpression creation = entry.getKey();
873 ClassDeclaration enclosingClass = creation.getAncestor(ClassDeclaration.cl ass);
874 //
875 SimpleIdentifier enclosingClassMemberName = null;
876 if (enclosingClassMemberName == null) {
877 MethodDeclaration enclosingMethod = creation.getAncestor(MethodDeclarati on.class);
878 if (enclosingMethod != null) {
879 enclosingClassMemberName = enclosingMethod.getName();
880 }
881 }
882 if (enclosingClassMemberName == null) {
883 VariableDeclaration enclosingField = creation.getAncestor(VariableDeclar ation.class);
884 if (enclosingField != null) {
885 enclosingClassMemberName = enclosingField.getName();
886 }
887 }
888 // prepare new name for anonymous class
889 ClassDeclaration classDeclaration = entry.getValue();
890 SimpleIdentifier nameNode = classDeclaration.getName();
891 String name = nameNode.getName();
892 name = StringUtils.substringBeforeLast(name, "_");
893 {
894 String enclosingClassName = enclosingClass.getName().getName();
895 if (!enclosingClassName.equals(name)) {
896 name = name + "_" + enclosingClassName;
897 }
898 }
899 if (enclosingClassMemberName != null) {
900 name += "_" + enclosingClassMemberName.getName();
901 }
902 // ensure unique name
903 if (!usedTopNames.add(name)) {
904 int index = 2;
905 while (true) {
906 String newName = name + "_" + index++;
907 if (usedTopNames.add(newName)) {
908 name = newName;
909 break;
910 }
911 }
912 }
913 // rename
914 renameIdentifier(nameNode, name);
915 }
916 }
917
854 private void renameConstructors(CompilationUnit unit) { 918 private void renameConstructors(CompilationUnit unit) {
855 unit.accept(new RecursiveASTVisitor<Void>() { 919 unit.accept(new RecursiveASTVisitor<Void>() {
856 private final Set<String> memberNamesInClass = Sets.newHashSet(); 920 private final Set<String> memberNamesInClass = Sets.newHashSet();
857 private int numConstructors; 921 private int numConstructors;
858 922
859 @Override 923 @Override
860 public Void visitClassDeclaration(ClassDeclaration node) { 924 public Void visitClassDeclaration(ClassDeclaration node) {
861 memberNamesInClass.clear(); 925 memberNamesInClass.clear();
862 numConstructors = 0; 926 numConstructors = 0;
863 NodeList<ClassMember> members = node.getMembers(); 927 NodeList<ClassMember> members = node.getMembers();
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
983 } 1047 }
984 } 1048 }
985 } 1049 }
986 } 1050 }
987 } 1051 }
988 } 1052 }
989 } 1053 }
990 }); 1054 });
991 } 1055 }
992 } 1056 }
OLDNEW
« no previous file with comments | « no previous file | editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/SyntaxTranslator.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698