| 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 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 currentVariableName = oldVariableName; | 244 currentVariableName = oldVariableName; |
| 245 } | 245 } |
| 246 return null; | 246 return null; |
| 247 } | 247 } |
| 248 }); | 248 }); |
| 249 } | 249 } |
| 250 | 250 |
| 251 public void ensureUniqueClassMemberNames(CompilationUnit unit) { | 251 public void ensureUniqueClassMemberNames(CompilationUnit unit) { |
| 252 unit.accept(new RecursiveASTVisitor<Void>() { | 252 unit.accept(new RecursiveASTVisitor<Void>() { |
| 253 private final Set<ClassMember> untouchableMethods = Sets.newHashSet(); | 253 private final Set<ClassMember> untouchableMethods = Sets.newHashSet(); |
| 254 private final Set<String> usedClassMemberNames = Sets.newHashSet(); | 254 private final Map<String, ClassMember> usedClassMembers = Maps.newHashMap(
); |
| 255 | 255 |
| 256 @Override | 256 @Override |
| 257 public Void visitClassDeclaration(ClassDeclaration node) { | 257 public Void visitClassDeclaration(ClassDeclaration node) { |
| 258 usedClassMemberNames.clear(); | 258 usedClassMembers.clear(); |
| 259 // fill "static" methods from super classes | 259 // fill "static" methods from super classes |
| 260 { | 260 { |
| 261 org.eclipse.jdt.core.dom.ITypeBinding binding = getNodeTypeBinding(nod
e); | 261 org.eclipse.jdt.core.dom.ITypeBinding binding = getNodeTypeBinding(nod
e); |
| 262 if (binding != null) { | 262 if (binding != null) { |
| 263 binding = binding.getSuperclass(); | 263 binding = binding.getSuperclass(); |
| 264 while (binding != null) { | 264 while (binding != null) { |
| 265 for (org.eclipse.jdt.core.dom.IMethodBinding method : binding.getD
eclaredMethods()) { | 265 for (org.eclipse.jdt.core.dom.IMethodBinding method : binding.getD
eclaredMethods()) { |
| 266 if (org.eclipse.jdt.core.dom.Modifier.isStatic(method.getModifie
rs())) { | 266 if (org.eclipse.jdt.core.dom.Modifier.isStatic(method.getModifie
rs())) { |
| 267 usedClassMemberNames.add(method.getName()); | 267 usedClassMembers.put(method.getName(), null); |
| 268 } | 268 } |
| 269 } | 269 } |
| 270 binding = binding.getSuperclass(); | 270 binding = binding.getSuperclass(); |
| 271 } | 271 } |
| 272 } | 272 } |
| 273 } | 273 } |
| 274 // fill "untouchable" methods | 274 // fill "untouchable" methods |
| 275 for (ClassMember member : node.getMembers()) { | 275 for (ClassMember member : node.getMembers()) { |
| 276 if (member instanceof MethodDeclaration) { | 276 if (member instanceof MethodDeclaration) { |
| 277 MethodDeclaration methodDeclaration = (MethodDeclaration) member; | 277 MethodDeclaration methodDeclaration = (MethodDeclaration) member; |
| 278 Object binding = nodeToBinding.get(member); | 278 Object binding = nodeToBinding.get(member); |
| 279 if (JavaUtils.isMethodDeclaredInClass(binding, "java.lang.Object"))
{ | 279 if (JavaUtils.isMethodDeclaredInClass(binding, "java.lang.Object"))
{ |
| 280 untouchableMethods.add(member); | 280 untouchableMethods.add(member); |
| 281 usedClassMemberNames.add(methodDeclaration.getName().getName()); | 281 usedClassMembers.put(methodDeclaration.getName().getName(), null); |
| 282 } | 282 } |
| 283 } | 283 } |
| 284 } | 284 } |
| 285 // ensure unique method names (and prefer to keep method name over field
name) | 285 // ensure unique method names (and prefer to keep method name over field
name) |
| 286 for (ClassMember member : node.getMembers()) { | 286 for (ClassMember member : node.getMembers()) { |
| 287 if (member instanceof MethodDeclaration) { | 287 if (member instanceof MethodDeclaration) { |
| 288 MethodDeclaration methodDeclaration = (MethodDeclaration) member; | 288 MethodDeclaration method = (MethodDeclaration) member; |
| 289 if (untouchableMethods.contains(methodDeclaration)) { | 289 // untouchable |
| 290 if (untouchableMethods.contains(method)) { |
| 290 continue; | 291 continue; |
| 291 } | 292 } |
| 292 ensureUniqueName(methodDeclaration.getName()); | 293 // getter/setter can share name |
| 294 { |
| 295 ClassMember otherMember = usedClassMembers.get(method.getName().ge
tName()); |
| 296 if (otherMember instanceof MethodDeclaration) { |
| 297 MethodDeclaration otherMethod = (MethodDeclaration) otherMember; |
| 298 if (method.isGetter() && otherMethod.isSetter() || method.isSett
er() |
| 299 && otherMethod.isGetter()) { |
| 300 continue; |
| 301 } |
| 302 } |
| 303 } |
| 304 // ensure unique name |
| 305 ensureUniqueName(method.getName(), method); |
| 293 } | 306 } |
| 294 } | 307 } |
| 295 // ensure unique field names (if name is already used be method) | 308 // ensure unique field names (if name is already used be method) |
| 296 for (ClassMember member : node.getMembers()) { | 309 for (ClassMember member : node.getMembers()) { |
| 297 if (member instanceof FieldDeclaration) { | 310 if (member instanceof FieldDeclaration) { |
| 298 FieldDeclaration fieldDeclaration = (FieldDeclaration) member; | 311 FieldDeclaration fieldDeclaration = (FieldDeclaration) member; |
| 299 for (VariableDeclaration field : fieldDeclaration.getFields().getVar
iables()) { | 312 for (VariableDeclaration field : fieldDeclaration.getFields().getVar
iables()) { |
| 300 ensureUniqueName(field.getName()); | 313 ensureUniqueName(field.getName(), fieldDeclaration); |
| 301 } | 314 } |
| 302 } | 315 } |
| 303 } | 316 } |
| 304 // no recursion | 317 // no recursion |
| 305 return null; | 318 return null; |
| 306 } | 319 } |
| 307 | 320 |
| 308 private void ensureUniqueName(Identifier declarationName) { | 321 private void ensureUniqueName(Identifier declarationName, ClassMember memb
er) { |
| 309 if (declarationName instanceof SimpleIdentifier) { | 322 if (declarationName instanceof SimpleIdentifier) { |
| 310 SimpleIdentifier declarationIdentifier = (SimpleIdentifier) declaratio
nName; | 323 SimpleIdentifier declarationIdentifier = (SimpleIdentifier) declaratio
nName; |
| 311 String name = declarationIdentifier.getName(); | 324 String name = declarationIdentifier.getName(); |
| 312 if (forbiddenNames.contains(name) || usedClassMemberNames.contains(nam
e)) { | 325 if (!isUniqueClassMemberName(name)) { |
| 313 String newName = generateUniqueName(name); | 326 String newName = generateUniqueName(name); |
| 314 // rename binding | 327 // rename binding |
| 315 if (!newName.equals(name)) { | 328 if (!newName.equals(name)) { |
| 316 renameIdentifier(declarationIdentifier, newName); | 329 renameIdentifier(declarationIdentifier, newName); |
| 317 name = newName; | 330 name = newName; |
| 318 } | 331 } |
| 319 } | 332 } |
| 320 // remember that name is used | 333 // remember that name is used |
| 321 usedClassMemberNames.add(name); | 334 usedClassMembers.put(name, member); |
| 322 } | 335 } |
| 323 } | 336 } |
| 337 |
| 338 private String generateUniqueName(String name) { |
| 339 if (!isGloballyUniqueClassMemberName(name)) { |
| 340 int index = 2; |
| 341 while (true) { |
| 342 String newName = name + index; |
| 343 if (isGloballyUniqueClassMemberName(newName)) { |
| 344 usedNames.add(newName); |
| 345 return newName; |
| 346 } |
| 347 index++; |
| 348 } |
| 349 } |
| 350 return name; |
| 351 } |
| 352 |
| 353 private boolean isGloballyUniqueClassMemberName(String name) { |
| 354 return isUniqueClassMemberName(name) && !usedNames.contains(name); |
| 355 } |
| 356 |
| 357 private boolean isUniqueClassMemberName(String name) { |
| 358 return !forbiddenNames.contains(name) && !usedClassMembers.containsKey(n
ame); |
| 359 } |
| 324 }); | 360 }); |
| 325 } | 361 } |
| 326 | 362 |
| 327 /** | 363 /** |
| 328 * @return the artificial {@link ClassDeclaration}created for Java creation of
anonymous class | 364 * @return the artificial {@link ClassDeclaration}created for Java creation of
anonymous class |
| 329 * declaration. | 365 * declaration. |
| 330 */ | 366 */ |
| 331 public ClassDeclaration getAnonymousDeclaration(InstanceCreationExpression cre
ation) { | 367 public ClassDeclaration getAnonymousDeclaration(InstanceCreationExpression cre
ation) { |
| 332 return anonymousDeclarations.get(creation); | 368 return anonymousDeclarations.get(creation); |
| 333 } | 369 } |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 865 } | 901 } |
| 866 } | 902 } |
| 867 } | 903 } |
| 868 } | 904 } |
| 869 } | 905 } |
| 870 return super.visitMethodInvocation(node); | 906 return super.visitMethodInvocation(node); |
| 871 } | 907 } |
| 872 }); | 908 }); |
| 873 } | 909 } |
| 874 } | 910 } |
| OLD | NEW |