KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > codemanipulation > ContextSensitiveImportRewriteContext


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.codemanipulation;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
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 /**
36  * This <code>ImportRewriteContext</code> is aware of all the types visible in
37  * <code>compilationUnit</code> at <code>position</code>.
38  */

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 JavaDoc qualifier, String JavaDoc 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 JavaDoc list= fCompilationUnit.types();
89         for (Iterator JavaDoc 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 JavaDoc[] addedImports= fImportRewrite.getAddedImports();
108         String JavaDoc qualifiedName= JavaModelUtil.concatenateName(qualifier, name);
109         for (int i= 0; i < addedImports.length; i++) {
110             String JavaDoc 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")) { //$NON-NLS-1$
120
//No explicit import statement required
121
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 JavaDoc 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 JavaDoc name, String JavaDoc importt) {
145         int index= importt.lastIndexOf('.');
146         String JavaDoc 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 JavaDoc qualifier, String JavaDoc 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 JavaDoc name) {
175         return binding.getName().equals(name);
176     }
177
178     private boolean isSameType(ITypeBinding binding, String JavaDoc qualifier, String JavaDoc name) {
179         String JavaDoc qualifiedName= JavaModelUtil.concatenateName(qualifier, name);
180         return binding.getQualifiedName().equals(qualifiedName);
181     }
182     
183     private boolean isConflictingType(ITypeBinding binding, String JavaDoc qualifier, String JavaDoc 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 JavaDoc imports= new ArrayList JavaDoc();
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