1 11 package org.eclipse.jdt.internal.corext.refactoring.structure; 12 13 import java.util.Collection ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Set ; 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 39 public final class ImportRewriteUtil { 40 41 50 public static void addImports(final CompilationUnitRewrite rewrite, final ASTNode node, final Map typeImports, final Map staticImports, final boolean declarations) { 51 addImports(rewrite, node, typeImports, staticImports, null, declarations); 52 } 53 54 64 public static void addImports(final CompilationUnitRewrite rewrite, final ASTNode node, final Map typeImports, final Map staticImports, final Collection excludeBindings, final boolean declarations) { 65 Assert.isNotNull(rewrite); 66 Assert.isNotNull(node); 67 Assert.isNotNull(typeImports); 68 Assert.isNotNull(staticImports); 69 final Set types= new HashSet (); 70 final Set members= new HashSet (); 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 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 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 126 public static void collectImports(final IJavaProject project, final ASTNode node, final Collection typeBindings, final Collection staticBindings, final boolean declarations) { 127 collectImports(project, node, typeBindings, staticBindings, null, declarations); 128 } 129 130 140 public static void collectImports(final IJavaProject project, final ASTNode node, final Collection typeBindings, final Collection staticBindings, final Collection excludeBindings, final boolean declarations) { 141 Assert.isNotNull(project); 142 Assert.isNotNull(node); 143 Assert.isNotNull(typeBindings); 144 Assert.isNotNull(staticBindings); 145 final Set types= new HashSet (); 146 final Set members= new HashSet (); 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 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 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 } 179 } 180 | Popular Tags |