KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > structure > ImportRewriteUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.refactoring.structure;
12
13 import java.util.Collection JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.dom.ASTNode;
23 import org.eclipse.jdt.core.dom.Block;
24 import org.eclipse.jdt.core.dom.IBinding;
25 import org.eclipse.jdt.core.dom.IMethodBinding;
26 import org.eclipse.jdt.core.dom.ITypeBinding;
27 import org.eclipse.jdt.core.dom.IVariableBinding;
28 import org.eclipse.jdt.core.dom.MethodDeclaration;
29 import org.eclipse.jdt.core.dom.Name;
30 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
31
32 import org.eclipse.jdt.internal.corext.codemanipulation.ImportReferencesCollector;
33
34 /**
35  * Utility methods to manage static and non-static imports of a compilation unit.
36  *
37  * @since 3.1
38  */

39 public final class ImportRewriteUtil {
40
41     /**
42      * Adds the necessary imports for an AST node to the specified compilation unit.
43      *
44      * @param rewrite the compilation unit rewrite whose compilation unit's imports should be updated
45      * @param node the AST node specifying the element for which imports should be added
46      * @param typeImports the map of name nodes to strings (element type: Map <Name, String>).
47      * @param staticImports the map of name nodes to strings (element type: Map <Name, String>).
48      * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise
49      */

50     public static void addImports(final CompilationUnitRewrite rewrite, final ASTNode node, final Map JavaDoc typeImports, final Map JavaDoc staticImports, final boolean declarations) {
51         addImports(rewrite, node, typeImports, staticImports, null, declarations);
52     }
53
54     /**
55      * Adds the necessary imports for an AST node to the specified compilation unit.
56      *
57      * @param rewrite the compilation unit rewrite whose compilation unit's imports should be updated
58      * @param node the AST node specifying the element for which imports should be added
59      * @param typeImports the map of name nodes to strings (element type: Map <Name, String>).
60      * @param staticImports the map of name nodes to strings (element type: Map <Name, String>).
61      * @param excludeBindings the set of bindings to exclude (element type: Set <IBinding>).
62      * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise
63      */

64     public static void addImports(final CompilationUnitRewrite rewrite, final ASTNode node, final Map JavaDoc typeImports, final Map JavaDoc staticImports, final Collection JavaDoc excludeBindings, final boolean declarations) {
65         Assert.isNotNull(rewrite);
66         Assert.isNotNull(node);
67         Assert.isNotNull(typeImports);
68         Assert.isNotNull(staticImports);
69         final Set JavaDoc types= new HashSet JavaDoc();
70         final Set JavaDoc members= new HashSet JavaDoc();
71         final ImportReferencesCollector collector= new ImportReferencesCollector(rewrite.getCu().getJavaProject(), null, types, members) {
72
73             public final boolean visit(final Block block) {
74                 Assert.isNotNull(block);
75                 if (declarations && block.getParent() instanceof MethodDeclaration)
76                     return false;
77                 return super.visit(block);
78             }
79         };
80         node.accept(collector);
81         final ImportRewrite rewriter= rewrite.getImportRewrite();
82         final ImportRemover remover= rewrite.getImportRemover();
83         Name name= null;
84         IBinding binding= null;
85         for (final Iterator JavaDoc iterator= types.iterator(); iterator.hasNext();) {
86             name= (Name) iterator.next();
87             binding= name.resolveBinding();
88             if (binding instanceof ITypeBinding) {
89                 final ITypeBinding type= (ITypeBinding) binding;
90                 if (excludeBindings == null || !excludeBindings.contains(type)) {
91                     typeImports.put(name, rewriter.addImport(type));
92                     remover.registerAddedImport(type.getQualifiedName());
93                 }
94             }
95         }
96         for (final Iterator JavaDoc iterator= members.iterator(); iterator.hasNext();) {
97             name= (Name) iterator.next();
98             binding= name.resolveBinding();
99             if (binding instanceof IVariableBinding) {
100                 final IVariableBinding variable= (IVariableBinding) binding;
101                 final ITypeBinding declaring= variable.getDeclaringClass();
102                 if (declaring != null && (excludeBindings == null || !excludeBindings.contains(variable))) {
103                     staticImports.put(name, rewriter.addStaticImport(variable));
104                     remover.registerAddedStaticImport(declaring.getQualifiedName(), variable.getName(), true);
105                 }
106             } else if (binding instanceof IMethodBinding) {
107                 final IMethodBinding method= (IMethodBinding) binding;
108                 final ITypeBinding declaring= method.getDeclaringClass();
109                 if (declaring != null && (excludeBindings == null || !excludeBindings.contains(method))) {
110                     staticImports.put(name, rewriter.addStaticImport(method));
111                     remover.registerAddedStaticImport(declaring.getQualifiedName(), method.getName(), false);
112                 }
113             }
114         }
115     }
116
117     /**
118      * Collects the necessary imports for an element represented by the specified AST node.
119      *
120      * @param project the java project containing the element
121      * @param node the AST node specifying the element for which imports should be collected
122      * @param typeBindings the set of type bindings (element type: Set <ITypeBinding>).
123      * @param staticBindings the set of bindings (element type: Set <IBinding>).
124      * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise
125      */

126     public static void collectImports(final IJavaProject project, final ASTNode node, final Collection JavaDoc typeBindings, final Collection JavaDoc staticBindings, final boolean declarations) {
127         collectImports(project, node, typeBindings, staticBindings, null, declarations);
128     }
129
130     /**
131      * Collects the necessary imports for an element represented by the specified AST node.
132      *
133      * @param project the java project containing the element
134      * @param node the AST node specifying the element for which imports should be collected
135      * @param typeBindings the set of type bindings (element type: Set <ITypeBinding>).
136      * @param staticBindings the set of bindings (element type: Set <IBinding>).
137      * @param excludeBindings the set of bindings to exclude (element type: Set <IBinding>).
138      * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise
139      */

140     public static void collectImports(final IJavaProject project, final ASTNode node, final Collection JavaDoc typeBindings, final Collection JavaDoc staticBindings, final Collection JavaDoc excludeBindings, final boolean declarations) {
141         Assert.isNotNull(project);
142         Assert.isNotNull(node);
143         Assert.isNotNull(typeBindings);
144         Assert.isNotNull(staticBindings);
145         final Set JavaDoc types= new HashSet JavaDoc();
146         final Set JavaDoc members= new HashSet JavaDoc();
147         final ImportReferencesCollector collector= new ImportReferencesCollector(project, null, types, members) {
148
149             public final boolean visit(final Block block) {
150                 Assert.isNotNull(block);
151                 if (declarations && block.getParent() instanceof MethodDeclaration)
152                     return false;
153                 return super.visit(block);
154             }
155         };
156         node.accept(collector);
157         Name name= null;
158         IBinding binding= null;
159         for (final Iterator JavaDoc iterator= types.iterator(); iterator.hasNext();) {
160             name= (Name) iterator.next();
161             binding= name.resolveBinding();
162             if (binding instanceof ITypeBinding) {
163                 final ITypeBinding type= (ITypeBinding) binding;
164                 if (excludeBindings == null || !excludeBindings.contains(type))
165                     typeBindings.add(type);
166             }
167         }
168         for (final Iterator JavaDoc iterator= members.iterator(); iterator.hasNext();) {
169             name= (Name) iterator.next();
170             binding= name.resolveBinding();
171             if (binding != null && (excludeBindings == null || !excludeBindings.contains(binding)))
172                 staticBindings.add(binding);
173         }
174     }
175
176     private ImportRewriteUtil() {
177         // Not for instantiation
178
}
179 }
180
Popular Tags