]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/structure/ImportRewriteUtil.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / structure / ImportRewriteUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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;
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.IBinding;
24 import org.eclipse.jdt.core.dom.IMethodBinding;
25 import org.eclipse.jdt.core.dom.ITypeBinding;
26 import org.eclipse.jdt.core.dom.IVariableBinding;
27 import org.eclipse.jdt.core.dom.Name;
28 import org.eclipse.jdt.core.dom.SimpleName;
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.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 context the import rewrite context, or <code>null</code> if none available
46          * @param node the AST node specifying the element for which imports should be added
47          * @param typeImports the map of name nodes to strings (element type: Map <Name, String>).
48          * @param staticImports the map of name nodes to strings (element type: Map <Name, String>).
49          * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise
50          */
51         public static void addImports(final CompilationUnitRewrite rewrite, ImportRewriteContext context, final ASTNode node, final Map<Name, String> typeImports, final Map<Name, String> staticImports, final boolean declarations) {
52                 addImports(rewrite, context, node, typeImports, staticImports, null, declarations);
53         }
54
55         /**
56          * Adds the necessary imports for an AST node to the specified compilation unit.
57          *
58          * @param rewrite the compilation unit rewrite whose compilation unit's imports should be updated
59          * @param context the import rewrite context, or <code>null</code> if none available
60          * @param node the AST node specifying the element for which imports should be added
61          * @param typeImports the map of name nodes to strings (element type: Map <Name, String>).
62          * @param staticImports the map of name nodes to strings (element type: Map <Name, String>).
63          * @param excludeBindings the set of bindings to exclude (element type: Set <IBinding>).
64          * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise
65          */
66         public static void addImports(final CompilationUnitRewrite rewrite, ImportRewriteContext context, final ASTNode node, final Map<Name, String> typeImports, final Map<Name, String> staticImports, final Collection<IBinding> excludeBindings, final boolean declarations) {
67                 Assert.isNotNull(rewrite);
68                 Assert.isNotNull(node);
69                 Assert.isNotNull(typeImports);
70                 Assert.isNotNull(staticImports);
71                 final Set<SimpleName> types= new HashSet<SimpleName>();
72                 final Set<SimpleName> members= new HashSet<SimpleName>();
73
74                 ImportReferencesCollector.collect(node, rewrite.getCu().getJavaProject(), null, declarations, types, members);
75
76                 final ImportRewrite rewriter= rewrite.getImportRewrite();
77                 final ImportRemover remover= rewrite.getImportRemover();
78                 Name name= null;
79                 IBinding binding= null;
80                 for (final Iterator<SimpleName> iterator= types.iterator(); iterator.hasNext();) {
81                         name= iterator.next();
82                         binding= name.resolveBinding();
83                         if (binding instanceof ITypeBinding) {
84                                 final ITypeBinding type= (ITypeBinding) binding;
85                                 if (excludeBindings == null || !excludeBindings.contains(type)) {
86                                         typeImports.put(name, rewriter.addImport(type, context));
87                                         remover.registerAddedImport(((SimpleName)name).getIdentifier());
88                                 }
89                         }
90                 }
91                 for (final Iterator<SimpleName> iterator= members.iterator(); iterator.hasNext();) {
92                         name= iterator.next();
93                         binding= name.resolveBinding();
94                         if (binding instanceof IVariableBinding) {
95                                 final IVariableBinding variable= (IVariableBinding) binding;
96                                 final ITypeBinding declaring= variable.getDeclaringClass();
97                                 if (declaring != null && (excludeBindings == null || !excludeBindings.contains(variable))) {
98                                         staticImports.put(name, rewriter.addStaticImport(variable, context));
99                                         remover.registerAddedStaticImport(declaring.getQualifiedName(), variable.getName(), true);
100                                 }
101                         } else if (binding instanceof IMethodBinding) {
102                                 final IMethodBinding method= (IMethodBinding) binding;
103                                 final ITypeBinding declaring= method.getDeclaringClass();
104                                 if (declaring != null && (excludeBindings == null || !excludeBindings.contains(method))) {
105                                         staticImports.put(name, rewriter.addStaticImport(method, context));
106                                         remover.registerAddedStaticImport(declaring.getQualifiedName(), method.getName(), false);
107                                 }
108                         }
109                 }
110         }
111
112         /**
113          * Collects the necessary imports for an element represented by the specified AST node.
114          *
115          * @param project the java project containing the element
116          * @param node the AST node specifying the element for which imports should be collected
117          * @param typeBindings the set of type bindings (element type: Set <ITypeBinding>).
118          * @param staticBindings the set of bindings (element type: Set <IBinding>).
119          * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise
120          */
121         public static void collectImports(final IJavaProject project, final ASTNode node, final Collection<ITypeBinding> typeBindings, final Collection<IBinding> staticBindings, final boolean declarations) {
122                 collectImports(project, node, typeBindings, staticBindings, null, declarations);
123         }
124
125         /**
126          * Collects the necessary imports for an element represented by the specified AST node.
127          *
128          * @param project the java project containing the element
129          * @param node the AST node specifying the element for which imports should be collected
130          * @param typeBindings the set of type bindings (element type: Set <ITypeBinding>).
131          * @param staticBindings the set of bindings (element type: Set <IBinding>).
132          * @param excludeBindings the set of bindings to exclude (element type: Set <IBinding>).
133          * @param declarations <code>true</code> if method declarations are treated as abstract, <code>false</code> otherwise
134          */
135         public static void collectImports(final IJavaProject project, final ASTNode node, final Collection<ITypeBinding> typeBindings, final Collection<IBinding> staticBindings, final Collection<IBinding> excludeBindings, final boolean declarations) {
136                 Assert.isNotNull(project);
137                 Assert.isNotNull(node);
138                 Assert.isNotNull(typeBindings);
139                 Assert.isNotNull(staticBindings);
140                 final Set<SimpleName> types= new HashSet<SimpleName>();
141                 final Set<SimpleName> members= new HashSet<SimpleName>();
142
143                 ImportReferencesCollector.collect(node, project, null, declarations, types, members);
144
145                 Name name= null;
146                 IBinding binding= null;
147                 for (final Iterator<SimpleName> iterator= types.iterator(); iterator.hasNext();) {
148                         name= iterator.next();
149                         binding= name.resolveBinding();
150                         if (binding instanceof ITypeBinding) {
151                                 final ITypeBinding type= (ITypeBinding) binding;
152                                 if (excludeBindings == null || !excludeBindings.contains(type))
153                                         typeBindings.add(type);
154                         }
155                 }
156                 for (final Iterator<SimpleName> iterator= members.iterator(); iterator.hasNext();) {
157                         name= iterator.next();
158                         binding= name.resolveBinding();
159                         if (binding != null && (excludeBindings == null || !excludeBindings.contains(binding)))
160                                 staticBindings.add(binding);
161                 }
162         }
163
164         private ImportRewriteUtil() {
165                 // Not for instantiation
166         }
167 }