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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/Context.java
diff --git a/editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/Context.java b/editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/Context.java
index f8c256b4e8d8b0b0e47f8f88a2b924231c936768..3b630adaa65b681143a0f121837b55eaaa9f74c2 100644
--- a/editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/Context.java
+++ b/editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/Context.java
@@ -68,6 +68,7 @@ import static com.google.dart.java2dart.util.TokenFactory.token;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
@@ -595,6 +596,7 @@ public class Context {
ensureFieldInitializers(dartUniverse);
dontUseThisInFieldInitializers(dartUniverse);
ensureUniqueClassMemberNames(dartUniverse);
+ renameAnonymousClassDeclarations();
applyLocalVariableSemanticChanges(dartUniverse);
new ConstructorSemanticProcessor(this).process(dartUniverse);
renameConstructors(dartUniverse);
@@ -851,6 +853,68 @@ public class Context {
return units;
}
+ /**
+ * Improves names for anonymous {@link ClassDeclaration}s.
+ */
+ private void renameAnonymousClassDeclarations() {
+ // prepare unused top-level names
+ Set<String> usedTopNames = Sets.newHashSet();
+ for (CompilationUnitMember unitMember : dartUniverse.getDeclarations()) {
+ if (unitMember instanceof ClassDeclaration) {
+ ClassDeclaration classDeclaration = (ClassDeclaration) unitMember;
+ String name = classDeclaration.getName().getName();
+ usedTopNames.add(name);
+ }
+ }
+ // rename anonymous types
+ for (Entry<InstanceCreationExpression, ClassDeclaration> entry : anonymousDeclarations.entrySet()) {
+ // prepare enclosing information
+ InstanceCreationExpression creation = entry.getKey();
+ ClassDeclaration enclosingClass = creation.getAncestor(ClassDeclaration.class);
+ //
+ SimpleIdentifier enclosingClassMemberName = null;
+ if (enclosingClassMemberName == null) {
+ MethodDeclaration enclosingMethod = creation.getAncestor(MethodDeclaration.class);
+ if (enclosingMethod != null) {
+ enclosingClassMemberName = enclosingMethod.getName();
+ }
+ }
+ if (enclosingClassMemberName == null) {
+ VariableDeclaration enclosingField = creation.getAncestor(VariableDeclaration.class);
+ if (enclosingField != null) {
+ enclosingClassMemberName = enclosingField.getName();
+ }
+ }
+ // prepare new name for anonymous class
+ ClassDeclaration classDeclaration = entry.getValue();
+ SimpleIdentifier nameNode = classDeclaration.getName();
+ String name = nameNode.getName();
+ name = StringUtils.substringBeforeLast(name, "_");
+ {
+ String enclosingClassName = enclosingClass.getName().getName();
+ if (!enclosingClassName.equals(name)) {
+ name = name + "_" + enclosingClassName;
+ }
+ }
+ if (enclosingClassMemberName != null) {
+ name += "_" + enclosingClassMemberName.getName();
+ }
+ // ensure unique name
+ if (!usedTopNames.add(name)) {
+ int index = 2;
+ while (true) {
+ String newName = name + "_" + index++;
+ if (usedTopNames.add(newName)) {
+ name = newName;
+ break;
+ }
+ }
+ }
+ // rename
+ renameIdentifier(nameNode, name);
+ }
+ }
+
private void renameConstructors(CompilationUnit unit) {
unit.accept(new RecursiveASTVisitor<Void>() {
private final Set<String> memberNamesInClass = Sets.newHashSet();
« 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