001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.structure;
011:
012: import java.util.Collection;
013: import java.util.HashSet;
014: import java.util.Iterator;
015: import java.util.Map;
016: import java.util.Set;
017:
018: import org.eclipse.core.runtime.Assert;
019:
020: import org.eclipse.jdt.core.IJavaProject;
021: import org.eclipse.jdt.core.dom.ASTNode;
022: import org.eclipse.jdt.core.dom.Block;
023: import org.eclipse.jdt.core.dom.IBinding;
024: import org.eclipse.jdt.core.dom.IMethodBinding;
025: import org.eclipse.jdt.core.dom.ITypeBinding;
026: import org.eclipse.jdt.core.dom.IVariableBinding;
027: import org.eclipse.jdt.core.dom.MethodDeclaration;
028: import org.eclipse.jdt.core.dom.Name;
029: import org.eclipse.jdt.core.dom.SimpleName;
030: import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
031:
032: import org.eclipse.jdt.internal.corext.codemanipulation.ImportReferencesCollector;
033:
034: /**
035: * Utility methods to manage static and non-static imports of a compilation unit.
036: *
037: * @since 3.1
038: */
039: public final class ImportRewriteUtil {
040:
041: /**
042: * Adds the necessary imports for an AST node to the specified compilation unit.
043: *
044: * @param rewrite the compilation unit rewrite whose compilation unit's imports should be updated
045: * @param node the AST node specifying the element for which imports should be added
046: * @param typeImports the map of name nodes to strings (element type: Map <Name, String>).
047: * @param staticImports the map of name nodes to strings (element type: Map <Name, String>).
048: * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise
049: */
050: public static void addImports(final CompilationUnitRewrite rewrite,
051: final ASTNode node, final Map typeImports,
052: final Map staticImports, final boolean declarations) {
053: addImports(rewrite, node, typeImports, staticImports, null,
054: declarations);
055: }
056:
057: /**
058: * Adds the necessary imports for an AST node to the specified compilation unit.
059: *
060: * @param rewrite the compilation unit rewrite whose compilation unit's imports should be updated
061: * @param node the AST node specifying the element for which imports should be added
062: * @param typeImports the map of name nodes to strings (element type: Map <Name, String>).
063: * @param staticImports the map of name nodes to strings (element type: Map <Name, String>).
064: * @param excludeBindings the set of bindings to exclude (element type: Set <IBinding>).
065: * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise
066: */
067: public static void addImports(final CompilationUnitRewrite rewrite,
068: final ASTNode node, final Map typeImports,
069: final Map staticImports, final Collection excludeBindings,
070: final boolean declarations) {
071: Assert.isNotNull(rewrite);
072: Assert.isNotNull(node);
073: Assert.isNotNull(typeImports);
074: Assert.isNotNull(staticImports);
075: final Set types = new HashSet();
076: final Set members = new HashSet();
077: final ImportReferencesCollector collector = new ImportReferencesCollector(
078: rewrite.getCu().getJavaProject(), null, types, members) {
079:
080: public final boolean visit(final Block block) {
081: Assert.isNotNull(block);
082: if (declarations
083: && block.getParent() instanceof MethodDeclaration)
084: return false;
085: return super .visit(block);
086: }
087: };
088: node.accept(collector);
089: final ImportRewrite rewriter = rewrite.getImportRewrite();
090: final ImportRemover remover = rewrite.getImportRemover();
091: Name name = null;
092: IBinding binding = null;
093: for (final Iterator iterator = types.iterator(); iterator
094: .hasNext();) {
095: name = (Name) iterator.next();
096: binding = name.resolveBinding();
097: if (binding instanceof ITypeBinding) {
098: final ITypeBinding type = (ITypeBinding) binding;
099: if (excludeBindings == null
100: || !excludeBindings.contains(type)) {
101: typeImports.put(name, rewriter.addImport(type));
102: remover.registerAddedImport(((SimpleName) name)
103: .getIdentifier());
104: }
105: }
106: }
107: for (final Iterator iterator = members.iterator(); iterator
108: .hasNext();) {
109: name = (Name) iterator.next();
110: binding = name.resolveBinding();
111: if (binding instanceof IVariableBinding) {
112: final IVariableBinding variable = (IVariableBinding) binding;
113: final ITypeBinding declaring = variable
114: .getDeclaringClass();
115: if (declaring != null
116: && (excludeBindings == null || !excludeBindings
117: .contains(variable))) {
118: staticImports.put(name, rewriter
119: .addStaticImport(variable));
120: remover.registerAddedStaticImport(declaring
121: .getQualifiedName(), variable.getName(),
122: true);
123: }
124: } else if (binding instanceof IMethodBinding) {
125: final IMethodBinding method = (IMethodBinding) binding;
126: final ITypeBinding declaring = method
127: .getDeclaringClass();
128: if (declaring != null
129: && (excludeBindings == null || !excludeBindings
130: .contains(method))) {
131: staticImports.put(name, rewriter
132: .addStaticImport(method));
133: remover.registerAddedStaticImport(declaring
134: .getQualifiedName(), method.getName(),
135: false);
136: }
137: }
138: }
139: }
140:
141: /**
142: * Collects the necessary imports for an element represented by the specified AST node.
143: *
144: * @param project the java project containing the element
145: * @param node the AST node specifying the element for which imports should be collected
146: * @param typeBindings the set of type bindings (element type: Set <ITypeBinding>).
147: * @param staticBindings the set of bindings (element type: Set <IBinding>).
148: * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise
149: */
150: public static void collectImports(final IJavaProject project,
151: final ASTNode node, final Collection typeBindings,
152: final Collection staticBindings, final boolean declarations) {
153: collectImports(project, node, typeBindings, staticBindings,
154: null, declarations);
155: }
156:
157: /**
158: * Collects the necessary imports for an element represented by the specified AST node.
159: *
160: * @param project the java project containing the element
161: * @param node the AST node specifying the element for which imports should be collected
162: * @param typeBindings the set of type bindings (element type: Set <ITypeBinding>).
163: * @param staticBindings the set of bindings (element type: Set <IBinding>).
164: * @param excludeBindings the set of bindings to exclude (element type: Set <IBinding>).
165: * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise
166: */
167: public static void collectImports(final IJavaProject project,
168: final ASTNode node, final Collection typeBindings,
169: final Collection staticBindings,
170: final Collection excludeBindings, final boolean declarations) {
171: Assert.isNotNull(project);
172: Assert.isNotNull(node);
173: Assert.isNotNull(typeBindings);
174: Assert.isNotNull(staticBindings);
175: final Set types = new HashSet();
176: final Set members = new HashSet();
177: final ImportReferencesCollector collector = new ImportReferencesCollector(
178: project, null, types, members) {
179:
180: public final boolean visit(final Block block) {
181: Assert.isNotNull(block);
182: if (declarations
183: && block.getParent() instanceof MethodDeclaration)
184: return false;
185: return super .visit(block);
186: }
187: };
188: node.accept(collector);
189: Name name = null;
190: IBinding binding = null;
191: for (final Iterator iterator = types.iterator(); iterator
192: .hasNext();) {
193: name = (Name) iterator.next();
194: binding = name.resolveBinding();
195: if (binding instanceof ITypeBinding) {
196: final ITypeBinding type = (ITypeBinding) binding;
197: if (excludeBindings == null
198: || !excludeBindings.contains(type))
199: typeBindings.add(type);
200: }
201: }
202: for (final Iterator iterator = members.iterator(); iterator
203: .hasNext();) {
204: name = (Name) iterator.next();
205: binding = name.resolveBinding();
206: if (binding != null
207: && (excludeBindings == null || !excludeBindings
208: .contains(binding)))
209: staticBindings.add(binding);
210: }
211: }
212:
213: private ImportRewriteUtil() {
214: // Not for instantiation
215: }
216: }
|