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 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 @Override | 202 @Override |
| 203 public Void visitSimpleIdentifier(SimpleIdentifier node) { | 203 public Void visitSimpleIdentifier(SimpleIdentifier node) { |
| 204 if (node.getName().equals(parameterName) | 204 if (node.getName().equals(parameterName) |
| 205 && getNodeBinding(node) != parameterBinding) { | 205 && getNodeBinding(node) != parameterBinding) { |
| 206 hasHiding.set(true); | 206 hasHiding.set(true); |
| 207 } | 207 } |
| 208 return super.visitSimpleIdentifier(node); | 208 return super.visitSimpleIdentifier(node); |
| 209 } | 209 } |
| 210 }); | 210 }); |
| 211 if (hasHiding.get()) { | 211 if (hasHiding.get()) { |
| 212 String newName = generateUniqueName(parameterName); | 212 Set<String> used = getSuperMembersNames(node); |
| 213 String newName = generateUniqueParameterName(used, parameterName); | |
| 213 renameIdentifier(parameter.getIdentifier(), newName); | 214 renameIdentifier(parameter.getIdentifier(), newName); |
| 214 } | 215 } |
| 215 } | 216 } |
| 216 } | 217 } |
| 217 return super.visitMethodDeclaration(node); | 218 return super.visitMethodDeclaration(node); |
| 218 } | 219 } |
| 220 | |
| 221 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
| |
| 222 int index = 2; | |
| 223 while (true) { | |
| 224 String newName = name + index; | |
| 225 if (!used.contains(newName)) { | |
| 226 return newName; | |
| 227 } | |
| 228 index++; | |
| 229 } | |
| 230 } | |
| 219 }); | 231 }); |
| 220 } | 232 } |
| 221 | 233 |
| 222 public void ensureNoVariableNameReferenceFromInitializer(CompilationUnit unit) { | 234 public void ensureNoVariableNameReferenceFromInitializer(CompilationUnit unit) { |
| 223 unit.accept(new RecursiveASTVisitor<Void>() { | 235 unit.accept(new RecursiveASTVisitor<Void>() { |
| 236 private Set<String> hierarchyNames; | |
| 237 private Set<String> methodNames; | |
| 224 private String currentVariableName = null; | 238 private String currentVariableName = null; |
| 225 private boolean hasNameReference = false; | 239 private boolean hasNameReference = false; |
| 226 | 240 |
| 227 @Override | 241 @Override |
| 242 public Void visitClassDeclaration(ClassDeclaration node) { | |
| 243 hierarchyNames = null; | |
| 244 try { | |
| 245 return super.visitClassDeclaration(node); | |
| 246 } finally { | |
| 247 hierarchyNames = null; | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 @Override | |
| 252 public Void visitMethodDeclaration(MethodDeclaration node) { | |
| 253 methodNames = null; | |
| 254 try { | |
| 255 return super.visitMethodDeclaration(node); | |
| 256 } finally { | |
| 257 methodNames = null; | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 @Override | |
| 228 public Void visitSimpleIdentifier(SimpleIdentifier node) { | 262 public Void visitSimpleIdentifier(SimpleIdentifier node) { |
| 229 hasNameReference |= node.getName().equals(currentVariableName); | 263 hasNameReference |= node.getName().equals(currentVariableName); |
| 230 return super.visitSimpleIdentifier(node); | 264 return super.visitSimpleIdentifier(node); |
| 231 } | 265 } |
| 232 | 266 |
| 233 @Override | 267 @Override |
| 234 public Void visitVariableDeclaration(VariableDeclaration node) { | 268 public Void visitVariableDeclaration(VariableDeclaration node) { |
| 235 String oldVariableName = currentVariableName; | 269 String oldVariableName = currentVariableName; |
| 236 try { | 270 try { |
| 237 currentVariableName = node.getName().getName(); | 271 currentVariableName = node.getName().getName(); |
| 238 hasNameReference = false; | 272 hasNameReference = false; |
| 239 Expression initializer = node.getInitializer(); | 273 Expression initializer = node.getInitializer(); |
| 240 if (initializer != null) { | 274 if (initializer != null) { |
| 241 initializer.accept(this); | 275 initializer.accept(this); |
| 242 } | 276 } |
| 243 if (hasNameReference || forbiddenNames.contains(currentVariableName)) { | 277 if (hasNameReference || forbiddenNames.contains(currentVariableName)) { |
| 244 String newName = generateUniqueName(currentVariableName); | 278 ensureHierarchyNames(node); |
| 279 ensureMethodNames(node); | |
| 280 String newName = generateUniqueVariableName(currentVariableName); | |
| 245 renameIdentifier(node.getName(), newName); | 281 renameIdentifier(node.getName(), newName); |
| 246 } | 282 } |
| 247 } finally { | 283 } finally { |
| 248 currentVariableName = oldVariableName; | 284 currentVariableName = oldVariableName; |
| 249 } | 285 } |
| 250 return null; | 286 return null; |
| 251 } | 287 } |
| 288 | |
| 289 private void ensureHierarchyNames(ASTNode node) { | |
| 290 if (hierarchyNames != null) { | |
| 291 return; | |
| 292 } | |
| 293 hierarchyNames = getSuperMembersNames(node); | |
| 294 } | |
| 295 | |
| 296 private void ensureMethodNames(ASTNode node) { | |
| 297 methodNames = Sets.newHashSet(); | |
| 298 MethodDeclaration method = node.getAncestor(MethodDeclaration.class); | |
| 299 if (method != null) { | |
| 300 method.accept(new RecursiveASTVisitor<Void>() { | |
| 301 @Override | |
| 302 public Void visitVariableDeclaration(VariableDeclaration node) { | |
| 303 methodNames.add(node.getName().getName()); | |
| 304 return super.visitVariableDeclaration(node); | |
| 305 } | |
| 306 }); | |
| 307 } | |
| 308 } | |
| 309 | |
| 310 /** | |
| 311 * @return the new name for variable which does not conflict with name of any member in super | |
| 312 * classes - {@link #hierarchyNames}. | |
| 313 */ | |
| 314 private String generateUniqueVariableName(String name) { | |
| 315 int index = 2; | |
| 316 while (true) { | |
| 317 String newName = name + index; | |
| 318 if (!hierarchyNames.contains(newName) && !methodNames.contains(newName ) | |
| 319 && !forbiddenNames.contains(newName)) { | |
| 320 methodNames.add(newName); | |
| 321 return newName; | |
| 322 } | |
| 323 index++; | |
| 324 } | |
| 325 } | |
| 252 }); | 326 }); |
| 253 } | 327 } |
| 254 | 328 |
| 255 public void ensureUniqueClassMemberNames(CompilationUnit unit) { | 329 public void ensureUniqueClassMemberNames(CompilationUnit unit) { |
| 256 unit.accept(new RecursiveASTVisitor<Void>() { | 330 unit.accept(new RecursiveASTVisitor<Void>() { |
| 257 private final Set<ClassMember> untouchableMethods = Sets.newHashSet(); | 331 private final Set<ClassMember> untouchableMethods = Sets.newHashSet(); |
| 258 private final Map<String, ClassMember> usedClassMembers = Maps.newHashMap( ); | 332 private final Map<String, ClassMember> usedClassMembers = Maps.newHashMap( ); |
| 259 | 333 |
| 260 @Override | 334 @Override |
| 261 public Void visitClassDeclaration(ClassDeclaration node) { | 335 public Void visitClassDeclaration(ClassDeclaration node) { |
| (...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 714 variable.setInitializer(initializer); | 788 variable.setInitializer(initializer); |
| 715 } | 789 } |
| 716 } | 790 } |
| 717 } | 791 } |
| 718 return super.visitFieldDeclaration(node); | 792 return super.visitFieldDeclaration(node); |
| 719 } | 793 } |
| 720 }); | 794 }); |
| 721 } | 795 } |
| 722 | 796 |
| 723 /** | 797 /** |
| 724 * @return the globally unique name, based on the given one. | 798 * @return the name of member declared in enclosing {@link ClassDeclaration} a nd its super |
| 799 * classes. | |
| 725 */ | 800 */ |
| 726 private String generateUniqueName(String name) { | 801 private Set<String> getSuperMembersNames(ASTNode node) { |
| 727 if (usedNames.contains(name) || forbiddenNames.contains(name)) { | 802 Set<String> hierarchyNames = Sets.newHashSet(); |
| 728 int index = 2; | 803 ClassDeclaration classDeclaration = node.getAncestor(ClassDeclaration.class) ; |
| 729 while (true) { | 804 org.eclipse.jdt.core.dom.ITypeBinding binding = getNodeTypeBinding(classDecl aration); |
| 730 String newName = name + index; | 805 if (binding != null) { |
| 731 if (!usedNames.contains(newName) && !forbiddenNames.contains(newName)) { | 806 binding = binding.getSuperclass(); |
| 732 usedNames.add(newName); | 807 while (binding != null) { |
| 733 return newName; | 808 for (org.eclipse.jdt.core.dom.IVariableBinding field : binding.getDeclar edFields()) { |
| 809 hierarchyNames.add(field.getName()); | |
| 734 } | 810 } |
| 735 index++; | 811 for (org.eclipse.jdt.core.dom.IMethodBinding method : binding.getDeclare dMethods()) { |
| 812 hierarchyNames.add(method.getName()); | |
| 813 } | |
| 814 binding = binding.getSuperclass(); | |
| 736 } | 815 } |
| 737 } | 816 } |
| 738 return name; | 817 return hierarchyNames; |
| 739 } | 818 } |
| 740 | 819 |
| 741 /** | 820 /** |
| 742 * @return the Java AST of the given Java {@link File} in context of {@link #s ourceFolders}. | 821 * @return the Java AST of the given Java {@link File} in context of {@link #s ourceFolders}. |
| 743 */ | 822 */ |
| 744 private Map<File, CompilationUnit> parseJavaFiles(final List<File> javaFiles) throws Exception { | 823 private Map<File, CompilationUnit> parseJavaFiles(final List<File> javaFiles) throws Exception { |
| 745 String paths[] = new String[javaFiles.size()]; | 824 String paths[] = new String[javaFiles.size()]; |
| 746 final Map<String, File> pathToFile = Maps.newHashMap(); | 825 final Map<String, File> pathToFile = Maps.newHashMap(); |
| 747 for (int i = 0; i < javaFiles.size(); i++) { | 826 for (int i = 0; i < javaFiles.size(); i++) { |
| 748 File javaFile = javaFiles.get(i); | 827 File javaFile = javaFiles.get(i); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 867 List<CompilationUnitMember> fileMembers = fileToMembers.get(javaFile); | 946 List<CompilationUnitMember> fileMembers = fileToMembers.get(javaFile); |
| 868 if (fileMembers == null) { | 947 if (fileMembers == null) { |
| 869 fileMembers = Lists.newArrayList(); | 948 fileMembers = Lists.newArrayList(); |
| 870 fileToMembers.put(javaFile, fileMembers); | 949 fileToMembers.put(javaFile, fileMembers); |
| 871 } | 950 } |
| 872 fileMembers.addAll(dartDeclarations); | 951 fileMembers.addAll(dartDeclarations); |
| 873 } | 952 } |
| 874 } | 953 } |
| 875 } | 954 } |
| 876 | 955 |
| 877 // XXX | |
| 878 private void unwrapVarArgIfAlreadyArray(CompilationUnit unit) { | 956 private void unwrapVarArgIfAlreadyArray(CompilationUnit unit) { |
| 879 unit.accept(new RecursiveASTVisitor<Void>() { | 957 unit.accept(new RecursiveASTVisitor<Void>() { |
| 880 @Override | 958 @Override |
| 881 public Void visitMethodInvocation(MethodInvocation node) { | 959 public Void visitMethodInvocation(MethodInvocation node) { |
| 882 Object binding = nodeToBinding.get(node); | 960 Object binding = nodeToBinding.get(node); |
| 883 if (binding instanceof IMethodBinding) { | 961 if (binding instanceof IMethodBinding) { |
| 884 IMethodBinding methodBinding = (IMethodBinding) binding; | 962 IMethodBinding methodBinding = (IMethodBinding) binding; |
| 885 if (methodBinding.isVarargs()) { | 963 if (methodBinding.isVarargs()) { |
| 886 List<Expression> args = node.getArgumentList().getArguments(); | 964 List<Expression> args = node.getArgumentList().getArguments(); |
| 887 if (!args.isEmpty() && args.get(args.size() - 1) instanceof ListLite ral) { | 965 if (!args.isEmpty() && args.get(args.size() - 1) instanceof ListLite ral) { |
| 888 ListLiteral listLiteral = (ListLiteral) args.get(args.size() - 1); | 966 ListLiteral listLiteral = (ListLiteral) args.get(args.size() - 1); |
| 889 List<Expression> elements = listLiteral.getElements(); | 967 List<Expression> elements = listLiteral.getElements(); |
| 890 if (elements.size() == 1) { | 968 if (elements.size() == 1) { |
| 891 Expression element = elements.get(0); | 969 Expression element = elements.get(0); |
| 892 if (nodeToTypeBinding.get(element) instanceof ITypeBinding) { | 970 if (nodeToTypeBinding.get(element) instanceof ITypeBinding) { |
| 893 ITypeBinding elementTypeBinding = nodeToTypeBinding.get(elemen t); | 971 ITypeBinding elementTypeBinding = nodeToTypeBinding.get(elemen t); |
| 894 if (elementTypeBinding.isArray()) { | 972 if (elementTypeBinding.isArray()) { |
| 895 args.set(args.size() - 1, element); | 973 args.set(args.size() - 1, element); |
| 896 } | 974 } |
| 897 } | 975 } |
| 898 } | 976 } |
| 899 } | 977 } |
| 900 } | 978 } |
| 901 } | 979 } |
| 902 return super.visitMethodInvocation(node); | 980 return super.visitMethodInvocation(node); |
| 903 } | 981 } |
| 904 }); | 982 }); |
| 905 } | 983 } |
| 906 } | 984 } |
| OLD | NEW |