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

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

Issue 14161021: Generate unique names less aggressively. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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_test/src/com/google/dart/java2dart/SemanticTest.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 4e5285ccb39c0962e5e876d394219ea8b411299b..de48d8f684180035ccb431acd354691c973ba92e 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
@@ -209,22 +209,56 @@ public class Context {
}
});
if (hasHiding.get()) {
- String newName = generateUniqueName(parameterName);
+ Set<String> used = getSuperMembersNames(node);
+ String newName = generateUniqueParameterName(used, parameterName);
renameIdentifier(parameter.getIdentifier(), newName);
}
}
}
return super.visitMethodDeclaration(node);
}
+
+ private String generateUniqueParameterName(Set<String> used, String name) {
Brian Wilkerson 2013/04/18 00:00:32 There's probably a good reason for doing it this w
scheglov 2013/04/18 00:09:51 In general this is good idea, but in this case we
+ int index = 2;
+ while (true) {
+ String newName = name + index;
+ if (!used.contains(newName)) {
+ return newName;
+ }
+ index++;
+ }
+ }
});
}
public void ensureNoVariableNameReferenceFromInitializer(CompilationUnit unit) {
unit.accept(new RecursiveASTVisitor<Void>() {
+ private Set<String> hierarchyNames;
+ private Set<String> methodNames;
private String currentVariableName = null;
private boolean hasNameReference = false;
@Override
+ public Void visitClassDeclaration(ClassDeclaration node) {
+ hierarchyNames = null;
+ try {
+ return super.visitClassDeclaration(node);
+ } finally {
+ hierarchyNames = null;
+ }
+ }
+
+ @Override
+ public Void visitMethodDeclaration(MethodDeclaration node) {
+ methodNames = null;
+ try {
+ return super.visitMethodDeclaration(node);
+ } finally {
+ methodNames = null;
+ }
+ }
+
+ @Override
public Void visitSimpleIdentifier(SimpleIdentifier node) {
hasNameReference |= node.getName().equals(currentVariableName);
return super.visitSimpleIdentifier(node);
@@ -241,7 +275,9 @@ public class Context {
initializer.accept(this);
}
if (hasNameReference || forbiddenNames.contains(currentVariableName)) {
- String newName = generateUniqueName(currentVariableName);
+ ensureHierarchyNames(node);
+ ensureMethodNames(node);
+ String newName = generateUniqueVariableName(currentVariableName);
renameIdentifier(node.getName(), newName);
}
} finally {
@@ -249,6 +285,44 @@ public class Context {
}
return null;
}
+
+ private void ensureHierarchyNames(ASTNode node) {
+ if (hierarchyNames != null) {
+ return;
+ }
+ hierarchyNames = getSuperMembersNames(node);
+ }
+
+ private void ensureMethodNames(ASTNode node) {
+ methodNames = Sets.newHashSet();
+ MethodDeclaration method = node.getAncestor(MethodDeclaration.class);
+ if (method != null) {
+ method.accept(new RecursiveASTVisitor<Void>() {
+ @Override
+ public Void visitVariableDeclaration(VariableDeclaration node) {
+ methodNames.add(node.getName().getName());
+ return super.visitVariableDeclaration(node);
+ }
+ });
+ }
+ }
+
+ /**
+ * @return the new name for variable which does not conflict with name of any member in super
+ * classes - {@link #hierarchyNames}.
+ */
+ private String generateUniqueVariableName(String name) {
+ int index = 2;
+ while (true) {
+ String newName = name + index;
+ if (!hierarchyNames.contains(newName) && !methodNames.contains(newName)
+ && !forbiddenNames.contains(newName)) {
+ methodNames.add(newName);
+ return newName;
+ }
+ index++;
+ }
+ }
});
}
@@ -721,21 +795,26 @@ public class Context {
}
/**
- * @return the globally unique name, based on the given one.
+ * @return the name of member declared in enclosing {@link ClassDeclaration} and its super
+ * classes.
*/
- private String generateUniqueName(String name) {
- if (usedNames.contains(name) || forbiddenNames.contains(name)) {
- int index = 2;
- while (true) {
- String newName = name + index;
- if (!usedNames.contains(newName) && !forbiddenNames.contains(newName)) {
- usedNames.add(newName);
- return newName;
+ private Set<String> getSuperMembersNames(ASTNode node) {
+ Set<String> hierarchyNames = Sets.newHashSet();
+ ClassDeclaration classDeclaration = node.getAncestor(ClassDeclaration.class);
+ org.eclipse.jdt.core.dom.ITypeBinding binding = getNodeTypeBinding(classDeclaration);
+ if (binding != null) {
+ binding = binding.getSuperclass();
+ while (binding != null) {
+ for (org.eclipse.jdt.core.dom.IVariableBinding field : binding.getDeclaredFields()) {
+ hierarchyNames.add(field.getName());
}
- index++;
+ for (org.eclipse.jdt.core.dom.IMethodBinding method : binding.getDeclaredMethods()) {
+ hierarchyNames.add(method.getName());
+ }
+ binding = binding.getSuperclass();
}
}
- return name;
+ return hierarchyNames;
}
/**
@@ -874,7 +953,6 @@ public class Context {
}
}
- // XXX
private void unwrapVarArgIfAlreadyArray(CompilationUnit unit) {
unit.accept(new RecursiveASTVisitor<Void>() {
@Override
« no previous file with comments | « no previous file | editor/util/plugins/com.google.dart.java2dart_test/src/com/google/dart/java2dart/SemanticTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698