1 11 package org.eclipse.jdt.internal.corext.codemanipulation; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.jdt.core.ICompilationUnit; 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.IJavaProject; 20 import org.eclipse.jdt.core.IPackageFragment; 21 import org.eclipse.jdt.core.IType; 22 import org.eclipse.jdt.core.JavaModelException; 23 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 24 import org.eclipse.jdt.core.dom.CompilationUnit; 25 import org.eclipse.jdt.core.dom.IBinding; 26 import org.eclipse.jdt.core.dom.ITypeBinding; 27 import org.eclipse.jdt.core.dom.Modifier; 28 import org.eclipse.jdt.core.dom.Name; 29 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 30 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext; 31 32 import org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer; 33 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 34 35 39 public class ContextSensitiveImportRewriteContext extends ImportRewriteContext { 40 41 private final CompilationUnit fCompilationUnit; 42 private final int fPosition; 43 private IBinding[] fDeclarationsInScope; 44 private Name[] fImportedNames; 45 private final ImportRewrite fImportRewrite; 46 47 public ContextSensitiveImportRewriteContext(CompilationUnit compilationUnit, int position, ImportRewrite importRewrite) { 48 fCompilationUnit= compilationUnit; 49 fPosition= position; 50 fImportRewrite= importRewrite; 51 fDeclarationsInScope= null; 52 fImportedNames= null; 53 } 54 55 public int findInContext(String qualifier, String name, int kind) { 56 int defaultResult= fImportRewrite.getDefaultImportRewriteContext().findInContext(qualifier, name, kind); 57 if (defaultResult != ImportRewriteContext.RES_NAME_UNKNOWN) 58 return defaultResult; 59 60 IBinding[] declarationsInScope= getDeclarationsInScope(); 61 for (int i= 0; i < declarationsInScope.length; i++) { 62 if (declarationsInScope[i] instanceof ITypeBinding) { 63 ITypeBinding typeBinding= (ITypeBinding)declarationsInScope[i]; 64 if (isSameType(typeBinding, qualifier, name)) { 65 return RES_NAME_FOUND; 66 } else if (isConflicting(typeBinding, name)) { 67 return RES_NAME_CONFLICT; 68 } 69 } else if (declarationsInScope[i] != null) { 70 if (isConflicting(declarationsInScope[i], name)) { 71 return RES_NAME_CONFLICT; 72 } 73 } 74 } 75 76 77 Name[] names= getImportedNames(); 78 for (int i= 0; i < names.length; i++) { 79 IBinding binding= names[i].resolveBinding(); 80 if (binding instanceof ITypeBinding) { 81 ITypeBinding typeBinding= (ITypeBinding)binding; 82 if (isConflictingType(typeBinding, qualifier, name)) { 83 return RES_NAME_CONFLICT; 84 } 85 } 86 } 87 88 List list= fCompilationUnit.types(); 89 for (Iterator iter= list.iterator(); iter.hasNext();) { 90 AbstractTypeDeclaration type= (AbstractTypeDeclaration)iter.next(); 91 ITypeBinding binding= type.resolveBinding(); 92 if (binding != null) { 93 if (isSameType(binding, qualifier, name)) { 94 return RES_NAME_FOUND; 95 } else { 96 ITypeBinding decl= containingDeclaration(binding, qualifier, name); 97 while (decl != null && !decl.equals(binding)) { 98 int modifiers= decl.getModifiers(); 99 if (Modifier.isPrivate(modifiers)) 100 return RES_NAME_CONFLICT; 101 decl= decl.getDeclaringClass(); 102 } 103 } 104 } 105 } 106 107 String [] addedImports= fImportRewrite.getAddedImports(); 108 String qualifiedName= JavaModelUtil.concatenateName(qualifier, name); 109 for (int i= 0; i < addedImports.length; i++) { 110 String addedImport= addedImports[i]; 111 if (qualifiedName.equals(addedImport)) { 112 return RES_NAME_FOUND; 113 } else { 114 if (isConflicting(name, addedImport)) 115 return RES_NAME_CONFLICT; 116 } 117 } 118 119 if (qualifier.equals("java.lang")) { IJavaElement parent= fCompilationUnit.getJavaElement().getParent(); 122 if (parent instanceof IPackageFragment) { 123 IPackageFragment packageFragment= (IPackageFragment)parent; 124 try { 125 ICompilationUnit[] compilationUnits= packageFragment.getCompilationUnits(); 126 for (int i= 0; i < compilationUnits.length; i++) { 127 ICompilationUnit cu= compilationUnits[i]; 128 IType[] allTypes= cu.getAllTypes(); 129 for (int j= 0; j < allTypes.length; j++) { 130 IType type= allTypes[j]; 131 String packageTypeName= type.getFullyQualifiedName(); 132 if (isConflicting(name, packageTypeName)) 133 return RES_NAME_CONFLICT; 134 } 135 } 136 } catch (JavaModelException e) { 137 } 138 } 139 } 140 141 return RES_NAME_UNKNOWN; 142 } 143 144 private boolean isConflicting(String name, String importt) { 145 int index= importt.lastIndexOf('.'); 146 String importedName; 147 if (index == -1) { 148 importedName= importt; 149 } else { 150 importedName= importt.substring(index + 1, importt.length()); 151 } 152 if (importedName.equals(name)) { 153 return true; 154 } 155 return false; 156 } 157 158 private ITypeBinding containingDeclaration(ITypeBinding binding, String qualifier, String name) { 159 ITypeBinding[] declaredTypes= binding.getDeclaredTypes(); 160 for (int i= 0; i < declaredTypes.length; i++) { 161 ITypeBinding childBinding= declaredTypes[i]; 162 if (isSameType(childBinding, qualifier, name)) { 163 return childBinding; 164 } else { 165 ITypeBinding result= containingDeclaration(childBinding, qualifier, name); 166 if (result != null) { 167 return result; 168 } 169 } 170 } 171 return null; 172 } 173 174 private boolean isConflicting(IBinding binding, String name) { 175 return binding.getName().equals(name); 176 } 177 178 private boolean isSameType(ITypeBinding binding, String qualifier, String name) { 179 String qualifiedName= JavaModelUtil.concatenateName(qualifier, name); 180 return binding.getQualifiedName().equals(qualifiedName); 181 } 182 183 private boolean isConflictingType(ITypeBinding binding, String qualifier, String name) { 184 binding= binding.getTypeDeclaration(); 185 return !isSameType(binding, qualifier, name) && isConflicting(binding, name); 186 } 187 188 private IBinding[] getDeclarationsInScope() { 189 if (fDeclarationsInScope == null) { 190 ScopeAnalyzer analyzer= new ScopeAnalyzer(fCompilationUnit); 191 fDeclarationsInScope= analyzer.getDeclarationsInScope(fPosition, ScopeAnalyzer.METHODS | ScopeAnalyzer.TYPES | ScopeAnalyzer.VARIABLES); 192 } 193 return fDeclarationsInScope; 194 } 195 196 private Name[] getImportedNames() { 197 if (fImportedNames == null) { 198 IJavaProject project= null; 199 IJavaElement javaElement= fCompilationUnit.getJavaElement(); 200 if (javaElement != null) 201 project= javaElement.getJavaProject(); 202 203 List imports= new ArrayList (); 204 ImportReferencesCollector.collect(fCompilationUnit, project, null, imports, null); 205 fImportedNames= (Name[])imports.toArray(new Name[imports.size()]); 206 } 207 return fImportedNames; 208 } 209 } 210 | Popular Tags |