Chromium Code Reviews| 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 10 matching lines...) Expand all Loading... | |
| 21 import com.google.common.io.Files; | 21 import com.google.common.io.Files; |
| 22 import com.google.dart.engine.ast.ASTNode; | 22 import com.google.dart.engine.ast.ASTNode; |
| 23 import com.google.dart.engine.ast.ArgumentList; | 23 import com.google.dart.engine.ast.ArgumentList; |
| 24 import com.google.dart.engine.ast.Block; | 24 import com.google.dart.engine.ast.Block; |
| 25 import com.google.dart.engine.ast.BlockFunctionBody; | 25 import com.google.dart.engine.ast.BlockFunctionBody; |
| 26 import com.google.dart.engine.ast.ClassDeclaration; | 26 import com.google.dart.engine.ast.ClassDeclaration; |
| 27 import com.google.dart.engine.ast.ClassMember; | 27 import com.google.dart.engine.ast.ClassMember; |
| 28 import com.google.dart.engine.ast.CompilationUnit; | 28 import com.google.dart.engine.ast.CompilationUnit; |
| 29 import com.google.dart.engine.ast.CompilationUnitMember; | 29 import com.google.dart.engine.ast.CompilationUnitMember; |
| 30 import com.google.dart.engine.ast.ConstructorDeclaration; | 30 import com.google.dart.engine.ast.ConstructorDeclaration; |
| 31 import com.google.dart.engine.ast.DeclaredIdentifier; | |
| 32 import com.google.dart.engine.ast.Expression; | 31 import com.google.dart.engine.ast.Expression; |
| 33 import com.google.dart.engine.ast.FieldDeclaration; | 32 import com.google.dart.engine.ast.FieldDeclaration; |
| 34 import com.google.dart.engine.ast.ForEachStatement; | |
| 35 import com.google.dart.engine.ast.FormalParameter; | |
| 36 import com.google.dart.engine.ast.FormalParameterList; | |
| 37 import com.google.dart.engine.ast.Identifier; | 33 import com.google.dart.engine.ast.Identifier; |
| 38 import com.google.dart.engine.ast.InstanceCreationExpression; | 34 import com.google.dart.engine.ast.InstanceCreationExpression; |
| 39 import com.google.dart.engine.ast.ListLiteral; | 35 import com.google.dart.engine.ast.ListLiteral; |
| 40 import com.google.dart.engine.ast.MethodDeclaration; | 36 import com.google.dart.engine.ast.MethodDeclaration; |
| 41 import com.google.dart.engine.ast.MethodInvocation; | 37 import com.google.dart.engine.ast.MethodInvocation; |
| 42 import com.google.dart.engine.ast.NodeList; | 38 import com.google.dart.engine.ast.NodeList; |
| 43 import com.google.dart.engine.ast.PropertyAccess; | |
| 44 import com.google.dart.engine.ast.RedirectingConstructorInvocation; | 39 import com.google.dart.engine.ast.RedirectingConstructorInvocation; |
| 45 import com.google.dart.engine.ast.SimpleIdentifier; | 40 import com.google.dart.engine.ast.SimpleIdentifier; |
| 46 import com.google.dart.engine.ast.SuperConstructorInvocation; | 41 import com.google.dart.engine.ast.SuperConstructorInvocation; |
| 47 import com.google.dart.engine.ast.ThisExpression; | 42 import com.google.dart.engine.ast.ThisExpression; |
| 48 import com.google.dart.engine.ast.VariableDeclaration; | 43 import com.google.dart.engine.ast.VariableDeclaration; |
| 49 import com.google.dart.engine.ast.VariableDeclarationList; | 44 import com.google.dart.engine.ast.VariableDeclarationList; |
| 50 import com.google.dart.engine.ast.visitor.GeneralizingASTVisitor; | 45 import com.google.dart.engine.ast.visitor.GeneralizingASTVisitor; |
| 51 import com.google.dart.engine.ast.visitor.RecursiveASTVisitor; | 46 import com.google.dart.engine.ast.visitor.RecursiveASTVisitor; |
| 52 import com.google.dart.engine.scanner.Keyword; | 47 import com.google.dart.engine.scanner.Keyword; |
| 53 import com.google.dart.engine.scanner.KeywordToken; | 48 import com.google.dart.engine.scanner.KeywordToken; |
| 54 import com.google.dart.engine.scanner.TokenType; | 49 import com.google.dart.engine.scanner.TokenType; |
| 55 import com.google.dart.java2dart.processor.ConstructorSemanticProcessor; | 50 import com.google.dart.java2dart.processor.ConstructorSemanticProcessor; |
| 51 import com.google.dart.java2dart.processor.LocalVariablesSemanticProcessor; | |
| 56 import com.google.dart.java2dart.util.Bindings; | 52 import com.google.dart.java2dart.util.Bindings; |
| 57 import com.google.dart.java2dart.util.JavaUtils; | 53 import com.google.dart.java2dart.util.JavaUtils; |
| 58 | 54 |
| 59 import static com.google.dart.java2dart.util.ASTFactory.assignmentExpression; | 55 import static com.google.dart.java2dart.util.ASTFactory.assignmentExpression; |
| 60 import static com.google.dart.java2dart.util.ASTFactory.block; | 56 import static com.google.dart.java2dart.util.ASTFactory.block; |
| 61 import static com.google.dart.java2dart.util.ASTFactory.blockFunctionBody; | 57 import static com.google.dart.java2dart.util.ASTFactory.blockFunctionBody; |
| 62 import static com.google.dart.java2dart.util.ASTFactory.compilationUnit; | 58 import static com.google.dart.java2dart.util.ASTFactory.compilationUnit; |
| 63 import static com.google.dart.java2dart.util.ASTFactory.constructorDeclaration; | 59 import static com.google.dart.java2dart.util.ASTFactory.constructorDeclaration; |
| 64 import static com.google.dart.java2dart.util.ASTFactory.expressionStatement; | 60 import static com.google.dart.java2dart.util.ASTFactory.expressionStatement; |
| 65 import static com.google.dart.java2dart.util.ASTFactory.formalParameterList; | 61 import static com.google.dart.java2dart.util.ASTFactory.formalParameterList; |
| 66 import static com.google.dart.java2dart.util.ASTFactory.identifier; | 62 import static com.google.dart.java2dart.util.ASTFactory.identifier; |
| 67 import static com.google.dart.java2dart.util.ASTFactory.propertyAccess; | 63 import static com.google.dart.java2dart.util.ASTFactory.propertyAccess; |
| 68 import static com.google.dart.java2dart.util.ASTFactory.thisExpression; | 64 import static com.google.dart.java2dart.util.ASTFactory.thisExpression; |
| 69 import static com.google.dart.java2dart.util.TokenFactory.token; | 65 import static com.google.dart.java2dart.util.TokenFactory.token; |
| 70 | 66 |
| 71 import org.apache.commons.io.Charsets; | 67 import org.apache.commons.io.Charsets; |
| 72 import org.apache.commons.io.FileUtils; | 68 import org.apache.commons.io.FileUtils; |
| 73 import org.apache.commons.lang3.ArrayUtils; | 69 import org.apache.commons.lang3.ArrayUtils; |
| 74 import org.eclipse.core.runtime.Assert; | 70 import org.eclipse.core.runtime.Assert; |
| 75 import org.eclipse.jdt.core.JavaCore; | 71 import org.eclipse.jdt.core.JavaCore; |
| 76 import org.eclipse.jdt.core.dom.AST; | 72 import org.eclipse.jdt.core.dom.AST; |
| 77 import org.eclipse.jdt.core.dom.ASTParser; | 73 import org.eclipse.jdt.core.dom.ASTParser; |
| 78 import org.eclipse.jdt.core.dom.FileASTRequestor; | 74 import org.eclipse.jdt.core.dom.FileASTRequestor; |
| 79 import org.eclipse.jdt.core.dom.IBinding; | 75 import org.eclipse.jdt.core.dom.IBinding; |
| 80 import org.eclipse.jdt.core.dom.IMethodBinding; | 76 import org.eclipse.jdt.core.dom.IMethodBinding; |
| 81 import org.eclipse.jdt.core.dom.ITypeBinding; | 77 import org.eclipse.jdt.core.dom.ITypeBinding; |
| 78 import org.eclipse.jdt.core.dom.IVariableBinding; | |
| 82 | 79 |
| 83 import java.io.File; | 80 import java.io.File; |
| 84 import java.util.Collection; | 81 import java.util.Collection; |
| 85 import java.util.Collections; | 82 import java.util.Collections; |
| 86 import java.util.List; | 83 import java.util.List; |
| 87 import java.util.Map; | 84 import java.util.Map; |
| 88 import java.util.Map.Entry; | 85 import java.util.Map.Entry; |
| 89 import java.util.Set; | 86 import java.util.Set; |
| 90 import java.util.concurrent.atomic.AtomicBoolean; | 87 import java.util.concurrent.atomic.AtomicBoolean; |
| 91 | 88 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 115 private final List<File> sourceFolders = Lists.newArrayList(); | 112 private final List<File> sourceFolders = Lists.newArrayList(); |
| 116 private final List<File> sourceFiles = Lists.newArrayList(); | 113 private final List<File> sourceFiles = Lists.newArrayList(); |
| 117 | 114 |
| 118 private final Map<String, String> renameMap = Maps.newHashMap(); | 115 private final Map<String, String> renameMap = Maps.newHashMap(); |
| 119 private final Set<String> notPropertySet = Sets.newHashSet(); | 116 private final Set<String> notPropertySet = Sets.newHashSet(); |
| 120 | 117 |
| 121 private final CompilationUnit dartUniverse = compilationUnit(); | 118 private final CompilationUnit dartUniverse = compilationUnit(); |
| 122 private final Map<File, List<CompilationUnitMember>> fileToMembers = Maps.newH ashMap(); | 119 private final Map<File, List<CompilationUnitMember>> fileToMembers = Maps.newH ashMap(); |
| 123 private final Map<CompilationUnitMember, File> memberToFile = Maps.newHashMap( ); | 120 private final Map<CompilationUnitMember, File> memberToFile = Maps.newHashMap( ); |
| 124 // information about names | 121 // information about names |
| 125 private static final Set<String> forbiddenNames = Sets.newHashSet(); | 122 public static final Set<String> FORBIDDEN_NAMES = Sets.newHashSet(); |
| 126 private final Set<String> usedNames = Sets.newHashSet(); | 123 private final Set<String> usedNames = Sets.newHashSet(); |
| 127 private final Set<ClassMember> privateClassMembers = Sets.newHashSet(); | 124 private final Set<ClassMember> privateClassMembers = Sets.newHashSet(); |
| 128 private final Map<SimpleIdentifier, String> identifierToName = Maps.newHashMap (); | 125 private final Map<SimpleIdentifier, String> identifierToName = Maps.newHashMap (); |
| 129 private final Map<String, Object> signatureToBinding = Maps.newHashMap(); | 126 private final Map<String, Object> signatureToBinding = Maps.newHashMap(); |
| 130 private final Map<Object, List<SimpleIdentifier>> bindingToIdentifiers = Maps. newHashMap(); | 127 private final Map<Object, List<SimpleIdentifier>> bindingToIdentifiers = Maps. newHashMap(); |
| 131 private final Map<ASTNode, IBinding> nodeToBinding = Maps.newHashMap(); | 128 private final Map<ASTNode, IBinding> nodeToBinding = Maps.newHashMap(); |
| 132 private final Map<ASTNode, ITypeBinding> nodeToTypeBinding = Maps.newHashMap() ; | 129 private final Map<ASTNode, ITypeBinding> nodeToTypeBinding = Maps.newHashMap() ; |
| 133 private final Map<InstanceCreationExpression, ClassDeclaration> anonymousDecla rations = Maps.newHashMap(); | 130 private final Map<InstanceCreationExpression, ClassDeclaration> anonymousDecla rations = Maps.newHashMap(); |
| 134 private final Set<SimpleIdentifier> innerClassNames = Sets.newHashSet(); | 131 private final Set<SimpleIdentifier> innerClassNames = Sets.newHashSet(); |
| 135 // information about constructors | 132 // information about constructors |
| 136 private int technicalConstructorIndex; | 133 private int technicalConstructorIndex; |
| 137 private final Map<IMethodBinding, ConstructorDescription> bindingToConstructor = Maps.newHashMap(); | 134 private final Map<IMethodBinding, ConstructorDescription> bindingToConstructor = Maps.newHashMap(); |
| 138 private final Map<ConstructorDeclaration, IMethodBinding> constructorToBinding = Maps.newHashMap(); | 135 private final Map<ConstructorDeclaration, IMethodBinding> constructorToBinding = Maps.newHashMap(); |
| 139 // information about inner classes | 136 // information about inner classes |
| 140 private int technicalInnerClassIndex; | 137 private int technicalInnerClassIndex; |
| 141 private int technicalAnonymousClassIndex; | 138 private int technicalAnonymousClassIndex; |
| 142 | 139 |
| 143 static { | 140 static { |
| 144 for (Keyword keyword : Keyword.values()) { | 141 for (Keyword keyword : Keyword.values()) { |
| 145 if (!keyword.isPseudoKeyword()) { | 142 if (!keyword.isPseudoKeyword()) { |
| 146 forbiddenNames.add(keyword.getSyntax()); | 143 FORBIDDEN_NAMES.add(keyword.getSyntax()); |
| 147 } | 144 } |
| 148 } | 145 } |
| 149 } | 146 } |
| 150 | 147 |
| 151 /** | 148 /** |
| 152 * Specifies that given {@link File} should be added to Java classpath. | 149 * Specifies that given {@link File} should be added to Java classpath. |
| 153 */ | 150 */ |
| 154 public void addClasspathFile(File file) { | 151 public void addClasspathFile(File file) { |
| 155 Assert.isLegal(file.exists(), "File '" + file + "' does not exist."); | 152 Assert.isLegal(file.exists(), "File '" + file + "' does not exist."); |
| 156 Assert.isLegal(file.isFile(), "File '" + file + "' is not a regular file."); | 153 Assert.isLegal(file.isFile(), "File '" + file + "' is not a regular file."); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 /** | 193 /** |
| 197 * Specifies that given folder is a source folder (root of Java packages hiera rchy). | 194 * Specifies that given folder is a source folder (root of Java packages hiera rchy). |
| 198 */ | 195 */ |
| 199 public void addSourceFolder(File folder) { | 196 public void addSourceFolder(File folder) { |
| 200 Assert.isLegal(folder.exists(), "Folder '" + folder + "' does not exist."); | 197 Assert.isLegal(folder.exists(), "Folder '" + folder + "' does not exist."); |
| 201 Assert.isLegal(folder.isDirectory(), "Folder '" + folder + "' is not a folde r."); | 198 Assert.isLegal(folder.isDirectory(), "Folder '" + folder + "' is not a folde r."); |
| 202 folder = folder.getAbsoluteFile(); | 199 folder = folder.getAbsoluteFile(); |
| 203 sourceFolders.add(folder); | 200 sourceFolders.add(folder); |
| 204 } | 201 } |
| 205 | 202 |
| 203 public void applyLocalVariableSemanticChanges(CompilationUnit unit) { | |
| 204 new LocalVariablesSemanticProcessor(this).process(unit); | |
| 205 } | |
| 206 | |
| 206 /** | 207 /** |
| 207 * @return {@code true} if the method with the given signature (which could be made getter or | 208 * @return {@code true} if the method with the given signature (which could be made getter or |
| 208 * setter) is allowed to be converted into getter/setter. | 209 * setter) is allowed to be converted into getter/setter. |
| 209 */ | 210 */ |
| 210 public boolean canMakeProperty(SimpleIdentifier identifier) { | 211 public boolean canMakeProperty(SimpleIdentifier identifier) { |
| 211 IBinding binding = getNodeBinding(identifier); | 212 IBinding binding = getNodeBinding(identifier); |
| 212 String signature = JavaUtils.getJdtSignature(binding); | 213 String signature = JavaUtils.getJdtSignature(binding); |
| 213 return !notPropertySet.contains(signature); | 214 return !notPropertySet.contains(signature); |
| 214 } | 215 } |
| 215 | 216 |
| 216 /** | 217 /** |
| 217 * In Java we can have method parameter "foo" and invoke method named "foo", a nd parameter will | 218 * In Java we can have method parameter "foo" and invoke method named "foo", a nd parameter will |
| 218 * not shadow invoked method. But in Dart it will. | 219 * not shadow invoked method. But in Dart it will. |
| 219 */ | 220 */ |
| 220 public void ensureMethodParameterDoesNotHide(CompilationUnit unit) { | 221 public void ensureMethodParameterDoesNotHide(CompilationUnit unit) { |
|
Brian Wilkerson
2013/10/01 00:34:55
Do we need to keep this method?
scheglov
2013/10/01 01:18:51
Done.
| |
| 221 unit.accept(new RecursiveASTVisitor<Void>() { | 222 // unit.accept(new RecursiveASTVisitor<Void>() { |
| 222 @Override | 223 // @Override |
| 223 public Void visitMethodDeclaration(MethodDeclaration node) { | 224 // public Void visitMethodDeclaration(MethodDeclaration node) { |
| 224 FormalParameterList parameterList = node.getParameters(); | 225 // FormalParameterList parameterList = node.getParameters(); |
| 225 if (parameterList != null) { | 226 // if (parameterList != null) { |
| 226 for (FormalParameter parameter : parameterList.getParameters()) { | 227 // for (FormalParameter parameter : parameterList.getParameters()) { |
| 227 final String parameterName = parameter.getIdentifier().getName(); | 228 // final String parameterName = parameter.getIdentifier().getName(); |
| 228 final Object parameterBinding = getNodeBinding(parameter.getIdentifi er()); | 229 // final Object parameterBinding = getNodeBinding(parameter.getIdenti fier()); |
| 229 final AtomicBoolean hasHiding = new AtomicBoolean(); | 230 // final AtomicBoolean hasHiding = new AtomicBoolean(); |
| 230 node.accept(new RecursiveASTVisitor<Void>() { | 231 // node.accept(new RecursiveASTVisitor<Void>() { |
| 231 @Override | 232 // @Override |
| 232 public Void visitSimpleIdentifier(SimpleIdentifier node) { | 233 // public Void visitSimpleIdentifier(SimpleIdentifier node) { |
| 233 if (node.getName().equals(parameterName) | 234 // if (node.getName().equals(parameterName) |
| 234 && getNodeBinding(node) != parameterBinding) { | 235 // && getNodeBinding(node) != parameterBinding) { |
| 235 hasHiding.set(true); | 236 // hasHiding.set(true); |
| 236 } | 237 // } |
| 237 return super.visitSimpleIdentifier(node); | 238 // return super.visitSimpleIdentifier(node); |
| 238 } | 239 // } |
| 239 }); | 240 // }); |
| 240 if (hasHiding.get()) { | 241 // if (hasHiding.get()) { |
| 241 Set<String> used = getSuperMembersNames(node); | 242 // Set<String> used = getSuperMembersNames(node); |
| 242 String newName = generateUniqueParameterName(used, parameterName); | 243 // String newName = generateUniqueParameterName(used, parameterName ); |
| 243 renameIdentifier(parameter.getIdentifier(), newName); | 244 // renameIdentifier(parameter.getIdentifier(), newName); |
| 244 } | 245 // } |
| 245 } | 246 // } |
| 246 } | 247 // } |
| 247 return super.visitMethodDeclaration(node); | 248 // return super.visitMethodDeclaration(node); |
| 248 } | 249 // } |
| 249 | 250 // |
| 250 private String generateUniqueParameterName(Set<String> used, String name) { | 251 // private String generateUniqueParameterName(Set<String> used, String name ) { |
| 251 int index = 2; | 252 // int index = 2; |
| 252 while (true) { | 253 // while (true) { |
| 253 String newName = name + index; | 254 // String newName = name + index; |
| 254 if (!used.contains(newName)) { | 255 // if (!used.contains(newName)) { |
| 255 return newName; | 256 // return newName; |
| 256 } | 257 // } |
| 257 index++; | 258 // index++; |
| 258 } | 259 // } |
| 259 } | 260 // } |
| 260 }); | 261 // }); |
| 261 } | |
| 262 | |
| 263 public void ensureNoVariableNameReferenceFromInitializer(CompilationUnit unit) { | |
| 264 unit.accept(new RecursiveASTVisitor<Void>() { | |
| 265 private Set<String> hierarchyNames; | |
| 266 private Set<String> methodNames; | |
| 267 private String currentVariableName = null; | |
| 268 private boolean hasNameReference = false; | |
| 269 | |
| 270 @Override | |
| 271 public Void visitClassDeclaration(ClassDeclaration node) { | |
| 272 hierarchyNames = null; | |
| 273 try { | |
| 274 return super.visitClassDeclaration(node); | |
| 275 } finally { | |
| 276 hierarchyNames = null; | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 @Override | |
| 281 public Void visitForEachStatement(ForEachStatement node) { | |
| 282 DeclaredIdentifier loopVariable = node.getLoopVariable(); | |
| 283 if (loopVariable != null) { | |
| 284 SimpleIdentifier nameNode = loopVariable.getIdentifier(); | |
| 285 String variableName = nameNode.getName(); | |
| 286 if (forbiddenNames.contains(variableName)) { | |
| 287 ensureHierarchyNames(node); | |
| 288 ensureMethodNames(node); | |
| 289 String newName = generateUniqueVariableName(variableName); | |
| 290 renameIdentifier(nameNode, newName); | |
| 291 } | |
| 292 } | |
| 293 return super.visitForEachStatement(node); | |
| 294 } | |
| 295 | |
| 296 @Override | |
| 297 public Void visitMethodDeclaration(MethodDeclaration node) { | |
| 298 methodNames = null; | |
| 299 try { | |
| 300 return super.visitMethodDeclaration(node); | |
| 301 } finally { | |
| 302 methodNames = null; | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 @Override | |
| 307 public Void visitSimpleIdentifier(SimpleIdentifier node) { | |
| 308 if (node.getName().equals(currentVariableName)) { | |
| 309 ASTNode parent = node.getParent(); | |
| 310 // name() | |
| 311 if (parent instanceof MethodInvocation) { | |
| 312 MethodInvocation invocation = (MethodInvocation) parent; | |
| 313 if (invocation.getMethodName() == node) { | |
| 314 // name = target.name() | |
| 315 if (invocation.getTarget() != null) { | |
| 316 return null; | |
| 317 } | |
| 318 // name = name() | |
| 319 hasNameReference = true; | |
| 320 return null; | |
| 321 } | |
| 322 } | |
| 323 // name = target.name | |
| 324 if (parent instanceof PropertyAccess) { | |
| 325 PropertyAccess propertyAccess = (PropertyAccess) parent; | |
| 326 if (propertyAccess.getPropertyName() == node && propertyAccess.getTa rget() != null) { | |
| 327 return null; | |
| 328 } | |
| 329 } | |
| 330 // name = name_whichWasGetMethod_butNowGetter | |
| 331 { | |
| 332 Object bindingObject = getNodeBinding(node); | |
| 333 if (bindingObject instanceof IMethodBinding) { | |
| 334 SyntaxTranslator.replaceNode(parent, node, propertyAccess(thisExpr ession(), node)); | |
| 335 return null; | |
| 336 } | |
| 337 } | |
| 338 // OK, this is really conflict | |
| 339 hasNameReference = true; | |
| 340 } | |
| 341 return null; | |
| 342 } | |
| 343 | |
| 344 @Override | |
| 345 public Void visitVariableDeclaration(VariableDeclaration node) { | |
| 346 String oldVariableName = currentVariableName; | |
| 347 try { | |
| 348 currentVariableName = node.getName().getName(); | |
| 349 hasNameReference = false; | |
| 350 Expression initializer = node.getInitializer(); | |
| 351 if (initializer != null) { | |
| 352 initializer.accept(this); | |
| 353 } | |
| 354 if (hasNameReference || forbiddenNames.contains(currentVariableName)) { | |
| 355 ensureHierarchyNames(node); | |
| 356 ensureMethodNames(node); | |
| 357 String newName = generateUniqueVariableName(currentVariableName); | |
| 358 renameIdentifier(node.getName(), newName); | |
| 359 } | |
| 360 } finally { | |
| 361 currentVariableName = oldVariableName; | |
| 362 } | |
| 363 return null; | |
| 364 } | |
| 365 | |
| 366 private void ensureHierarchyNames(ASTNode node) { | |
| 367 if (hierarchyNames != null) { | |
| 368 return; | |
| 369 } | |
| 370 hierarchyNames = getSuperMembersNames(node); | |
| 371 } | |
| 372 | |
| 373 private void ensureMethodNames(ASTNode node) { | |
| 374 methodNames = Sets.newHashSet(); | |
| 375 MethodDeclaration method = node.getAncestor(MethodDeclaration.class); | |
| 376 if (method != null) { | |
| 377 method.accept(new RecursiveASTVisitor<Void>() { | |
| 378 @Override | |
| 379 public Void visitVariableDeclaration(VariableDeclaration node) { | |
| 380 methodNames.add(node.getName().getName()); | |
| 381 return super.visitVariableDeclaration(node); | |
| 382 } | |
| 383 }); | |
| 384 } | |
| 385 } | |
| 386 | |
| 387 /** | |
| 388 * @return the new name for variable which does not conflict with name of any member in super | |
| 389 * classes - {@link #hierarchyNames}. | |
| 390 */ | |
| 391 private String generateUniqueVariableName(String name) { | |
| 392 int index = 2; | |
| 393 while (true) { | |
| 394 String newName = name + index; | |
| 395 if (!hierarchyNames.contains(newName) && !methodNames.contains(newName ) | |
| 396 && !forbiddenNames.contains(newName)) { | |
| 397 methodNames.add(newName); | |
| 398 return newName; | |
| 399 } | |
| 400 index++; | |
| 401 } | |
| 402 } | |
| 403 }); | |
| 404 } | 262 } |
| 405 | 263 |
| 406 public void ensureUniqueClassMemberNames(CompilationUnit unit) { | 264 public void ensureUniqueClassMemberNames(CompilationUnit unit) { |
| 407 unit.accept(new RecursiveASTVisitor<Void>() { | 265 unit.accept(new RecursiveASTVisitor<Void>() { |
| 408 private final Set<ClassMember> untouchableMethods = Sets.newHashSet(); | 266 private final Set<ClassMember> untouchableMethods = Sets.newHashSet(); |
| 409 private final Map<String, ClassMember> usedClassMembers = Maps.newHashMap( ); | 267 private final Map<String, ClassMember> usedClassMembers = Maps.newHashMap( ); |
| 410 private final Set<String> superNames = Sets.newHashSet(); | 268 private final Set<String> superNames = Sets.newHashSet(); |
| 411 private final Map<String, List<IMethodBinding>> superMembers = Maps.newHas hMap(); | 269 private final Map<String, List<IMethodBinding>> superMembers = Maps.newHas hMap(); |
| 412 | 270 |
| 413 @Override | 271 @Override |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 535 } | 393 } |
| 536 } | 394 } |
| 537 return name; | 395 return name; |
| 538 } | 396 } |
| 539 | 397 |
| 540 private boolean isGloballyUniqueClassMemberName(String name) { | 398 private boolean isGloballyUniqueClassMemberName(String name) { |
| 541 return isUniqueClassMemberName(name) && !usedNames.contains(name); | 399 return isUniqueClassMemberName(name) && !usedNames.contains(name); |
| 542 } | 400 } |
| 543 | 401 |
| 544 private boolean isUniqueClassMemberName(String name) { | 402 private boolean isUniqueClassMemberName(String name) { |
| 545 return !forbiddenNames.contains(name) && !usedClassMembers.containsKey(n ame); | 403 return !FORBIDDEN_NAMES.contains(name) && !usedClassMembers.containsKey( name); |
| 546 } | 404 } |
| 547 }); | 405 }); |
| 548 } | 406 } |
| 549 | 407 |
| 550 /** | 408 /** |
| 551 * @return the artificial {@link ClassDeclaration}created for Java creation of anonymous class | 409 * @return the artificial {@link ClassDeclaration}created for Java creation of anonymous class |
| 552 * declaration. | 410 * declaration. |
| 553 */ | 411 */ |
| 554 public ClassDeclaration getAnonymousDeclaration(InstanceCreationExpression cre ation) { | 412 public ClassDeclaration getAnonymousDeclaration(InstanceCreationExpression cre ation) { |
| 555 return anonymousDeclarations.get(creation); | 413 return anonymousDeclarations.get(creation); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 618 /** | 476 /** |
| 619 * @return all references (actual references and declarations) | 477 * @return all references (actual references and declarations) |
| 620 */ | 478 */ |
| 621 public List<SimpleIdentifier> getReferences(SimpleIdentifier target) { | 479 public List<SimpleIdentifier> getReferences(SimpleIdentifier target) { |
| 622 Object binding = nodeToBinding.get(target); | 480 Object binding = nodeToBinding.get(target); |
| 623 List<SimpleIdentifier> references = bindingToIdentifiers.get(binding); | 481 List<SimpleIdentifier> references = bindingToIdentifiers.get(binding); |
| 624 return references != null ? references : Lists.<SimpleIdentifier> newArrayLi st(); | 482 return references != null ? references : Lists.<SimpleIdentifier> newArrayLi st(); |
| 625 } | 483 } |
| 626 | 484 |
| 627 /** | 485 /** |
| 486 * @return the name of member declared in enclosing {@link ClassDeclaration} a nd its super | |
| 487 * classes. | |
| 488 */ | |
| 489 public Set<String> getSuperMembersNames(ASTNode node) { | |
| 490 Set<String> hierarchyNames = Sets.newHashSet(); | |
| 491 ClassDeclaration classDeclaration = node.getAncestor(ClassDeclaration.class) ; | |
| 492 org.eclipse.jdt.core.dom.ITypeBinding binding = getNodeTypeBinding(classDecl aration); | |
| 493 if (binding != null) { | |
| 494 binding = binding.getSuperclass(); | |
| 495 while (binding != null) { | |
| 496 for (org.eclipse.jdt.core.dom.IVariableBinding field : binding.getDeclar edFields()) { | |
| 497 hierarchyNames.add(field.getName()); | |
| 498 } | |
| 499 for (org.eclipse.jdt.core.dom.IMethodBinding method : binding.getDeclare dMethods()) { | |
| 500 hierarchyNames.add(method.getName()); | |
| 501 } | |
| 502 binding = binding.getSuperclass(); | |
| 503 } | |
| 504 } | |
| 505 return hierarchyNames; | |
| 506 } | |
| 507 | |
| 508 public boolean isFieldBinding(ASTNode node) { | |
| 509 IBinding binding = getNodeBinding(node); | |
| 510 if (binding instanceof IVariableBinding) { | |
| 511 return ((IVariableBinding) binding).isField(); | |
| 512 } | |
| 513 return false; | |
| 514 } | |
| 515 | |
| 516 public boolean isMethodBinding(ASTNode node) { | |
| 517 IBinding binding = getNodeBinding(node); | |
| 518 return binding instanceof IMethodBinding; | |
| 519 } | |
| 520 | |
| 521 /** | |
| 628 * Remembers that "identifier" is reference to the given Java binding. | 522 * Remembers that "identifier" is reference to the given Java binding. |
| 629 */ | 523 */ |
| 630 public void putReference(SimpleIdentifier identifier, IBinding binding, String bindingSignature) { | 524 public void putReference(SimpleIdentifier identifier, IBinding binding, String bindingSignature) { |
| 631 if (binding != null) { | 525 if (binding != null) { |
| 632 signatureToBinding.put(bindingSignature, binding); | 526 signatureToBinding.put(bindingSignature, binding); |
| 633 identifierToName.put(identifier, identifier.getName()); | 527 identifierToName.put(identifier, identifier.getName()); |
| 634 // remember binding for reference | 528 // remember binding for reference |
| 635 nodeToBinding.put(identifier, binding); | 529 nodeToBinding.put(identifier, binding); |
| 636 // add reference to binding | 530 // add reference to binding |
| 637 List<SimpleIdentifier> identifiers = bindingToIdentifiers.get(binding); | 531 List<SimpleIdentifier> identifiers = bindingToIdentifiers.get(binding); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 713 } | 607 } |
| 714 } | 608 } |
| 715 } | 609 } |
| 716 // run processors | 610 // run processors |
| 717 { | 611 { |
| 718 replaceInnerClassReferences(dartUniverse); | 612 replaceInnerClassReferences(dartUniverse); |
| 719 unwrapVarArgIfAlreadyArray(dartUniverse); | 613 unwrapVarArgIfAlreadyArray(dartUniverse); |
| 720 ensureFieldInitializers(dartUniverse); | 614 ensureFieldInitializers(dartUniverse); |
| 721 dontUseThisInFieldInitializers(dartUniverse); | 615 dontUseThisInFieldInitializers(dartUniverse); |
| 722 ensureUniqueClassMemberNames(dartUniverse); | 616 ensureUniqueClassMemberNames(dartUniverse); |
| 723 ensureNoVariableNameReferenceFromInitializer(dartUniverse); | 617 applyLocalVariableSemanticChanges(dartUniverse); |
| 724 ensureMethodParameterDoesNotHide(dartUniverse); | 618 ensureMethodParameterDoesNotHide(dartUniverse); |
| 725 new ConstructorSemanticProcessor(this).process(dartUniverse); | 619 new ConstructorSemanticProcessor(this).process(dartUniverse); |
| 726 renameConstructors(dartUniverse); | 620 renameConstructors(dartUniverse); |
| 727 insertEnclosingTypeForInstanceCreationArguments(dartUniverse); | 621 insertEnclosingTypeForInstanceCreationArguments(dartUniverse); |
| 728 } | 622 } |
| 729 // done | 623 // done |
| 730 return dartUniverse; | 624 return dartUniverse; |
| 731 } | 625 } |
| 732 | 626 |
| 733 /** | 627 /** |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 894 if (initializer != null) { | 788 if (initializer != null) { |
| 895 variable.setInitializer(initializer); | 789 variable.setInitializer(initializer); |
| 896 } | 790 } |
| 897 } | 791 } |
| 898 } | 792 } |
| 899 return super.visitFieldDeclaration(node); | 793 return super.visitFieldDeclaration(node); |
| 900 } | 794 } |
| 901 }); | 795 }); |
| 902 } | 796 } |
| 903 | 797 |
| 904 /** | |
| 905 * @return the name of member declared in enclosing {@link ClassDeclaration} a nd its super | |
| 906 * classes. | |
| 907 */ | |
| 908 private Set<String> getSuperMembersNames(ASTNode node) { | |
| 909 Set<String> hierarchyNames = Sets.newHashSet(); | |
| 910 ClassDeclaration classDeclaration = node.getAncestor(ClassDeclaration.class) ; | |
| 911 org.eclipse.jdt.core.dom.ITypeBinding binding = getNodeTypeBinding(classDecl aration); | |
| 912 if (binding != null) { | |
| 913 binding = binding.getSuperclass(); | |
| 914 while (binding != null) { | |
| 915 for (org.eclipse.jdt.core.dom.IVariableBinding field : binding.getDeclar edFields()) { | |
| 916 hierarchyNames.add(field.getName()); | |
| 917 } | |
| 918 for (org.eclipse.jdt.core.dom.IMethodBinding method : binding.getDeclare dMethods()) { | |
| 919 hierarchyNames.add(method.getName()); | |
| 920 } | |
| 921 binding = binding.getSuperclass(); | |
| 922 } | |
| 923 } | |
| 924 return hierarchyNames; | |
| 925 } | |
| 926 | |
| 927 private void insertEnclosingTypeForInstanceCreationArguments(CompilationUnit u nit) { | 798 private void insertEnclosingTypeForInstanceCreationArguments(CompilationUnit u nit) { |
| 928 unit.accept(new RecursiveASTVisitor<Void>() { | 799 unit.accept(new RecursiveASTVisitor<Void>() { |
| 929 @Override | 800 @Override |
| 930 public Void visitInstanceCreationExpression(InstanceCreationExpression nod e) { | 801 public Void visitInstanceCreationExpression(InstanceCreationExpression nod e) { |
| 931 IMethodBinding binding = (IMethodBinding) getNodeBinding(node); | 802 IMethodBinding binding = (IMethodBinding) getNodeBinding(node); |
| 932 ConstructorDescription constructorDescription = getConstructorDescriptio n(binding); | 803 ConstructorDescription constructorDescription = getConstructorDescriptio n(binding); |
| 933 if (constructorDescription.insertEnclosingTypeRef) { | 804 if (constructorDescription.insertEnclosingTypeRef) { |
| 934 node.getArgumentList().getArguments().add(0, thisExpression()); | 805 node.getArgumentList().getArguments().add(0, thisExpression()); |
| 935 } | 806 } |
| 936 return super.visitInstanceCreationExpression(node); | 807 return super.visitInstanceCreationExpression(node); |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1120 } | 991 } |
| 1121 } | 992 } |
| 1122 } | 993 } |
| 1123 } | 994 } |
| 1124 } | 995 } |
| 1125 } | 996 } |
| 1126 } | 997 } |
| 1127 }); | 998 }); |
| 1128 } | 999 } |
| 1129 } | 1000 } |
| OLD | NEW |