Source Code Cross Referenced for ImportRewriteUtil.java in  » IDE-Eclipse » jdt » org » eclipse » jdt » internal » corext » refactoring » structure » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » jdt » org.eclipse.jdt.internal.corext.refactoring.structure 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.__j_av___a_2_s___.c__om__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.