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;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.HashSet;
015: import java.util.Iterator;
016: import java.util.List;
017: import java.util.Set;
018:
019: import org.eclipse.core.runtime.Assert;
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.core.runtime.IPath;
022:
023: import org.eclipse.core.resources.IProject;
024:
025: import org.eclipse.jdt.core.IClasspathEntry;
026: import org.eclipse.jdt.core.IJavaElement;
027: import org.eclipse.jdt.core.IJavaProject;
028: import org.eclipse.jdt.core.IMember;
029: import org.eclipse.jdt.core.IPackageFragmentRoot;
030: import org.eclipse.jdt.core.JavaCore;
031: import org.eclipse.jdt.core.JavaModelException;
032: import org.eclipse.jdt.core.search.IJavaSearchScope;
033: import org.eclipse.jdt.core.search.SearchEngine;
034:
035: import org.eclipse.jdt.internal.corext.util.JdtFlags;
036:
037: public class RefactoringScopeFactory {
038:
039: /*
040: * Adds to <code> projects </code> IJavaProject objects for all projects directly or indirectly referencing focus. @param projects IJavaProjects will be added to this set
041: */
042: private static void addReferencingProjects(IJavaProject focus,
043: Set projects) throws JavaModelException {
044: IProject[] referencingProjects = focus.getProject()
045: .getReferencingProjects();
046: for (int i = 0; i < referencingProjects.length; i++) {
047: IJavaProject candidate = JavaCore
048: .create(referencingProjects[i]);
049: if (candidate == null || projects.contains(candidate)
050: || !candidate.exists())
051: continue; // break cycle
052: IClasspathEntry entry = getReferencingClassPathEntry(
053: candidate, focus);
054: if (entry != null) {
055: projects.add(candidate);
056: if (entry.isExported())
057: addReferencingProjects(candidate, projects);
058: }
059: }
060: }
061:
062: private static void addRelatedReferencing(IJavaProject focus,
063: Set projects) throws CoreException {
064: IProject[] referencingProjects = focus.getProject()
065: .getReferencingProjects();
066: for (int i = 0; i < referencingProjects.length; i++) {
067: IJavaProject candidate = JavaCore
068: .create(referencingProjects[i]);
069: if (candidate == null || projects.contains(candidate)
070: || !candidate.exists())
071: continue; // break cycle
072: IClasspathEntry entry = getReferencingClassPathEntry(
073: candidate, focus);
074: if (entry != null) {
075: projects.add(candidate);
076: if (entry.isExported()) {
077: addRelatedReferencing(candidate, projects);
078: addRelatedReferenced(candidate, projects);
079: }
080: }
081: }
082: }
083:
084: private static void addRelatedReferenced(IJavaProject focus,
085: Set projects) throws CoreException {
086: IProject[] referencedProjects = focus.getProject()
087: .getReferencedProjects();
088: for (int i = 0; i < referencedProjects.length; i++) {
089: IJavaProject candidate = JavaCore
090: .create(referencedProjects[i]);
091: if (candidate == null || projects.contains(candidate)
092: || !candidate.exists())
093: continue; // break cycle
094: IClasspathEntry entry = getReferencingClassPathEntry(focus,
095: candidate);
096: if (entry != null) {
097: projects.add(candidate);
098: if (entry.isExported()) {
099: addRelatedReferenced(candidate, projects);
100: addRelatedReferencing(candidate, projects);
101: }
102: }
103: }
104: }
105:
106: /**
107: * Creates a new search scope with all compilation units possibly referencing <code>javaElement</code>,
108: * considering the visibility of the element.
109: *
110: * @param javaElement the java element
111: * @return the search scope
112: * @throws JavaModelException if an error occurs
113: */
114: public static IJavaSearchScope create(IJavaElement javaElement)
115: throws JavaModelException {
116: return RefactoringScopeFactory.create(javaElement, true);
117: }
118:
119: /**
120: * Creates a new search scope with all compilation units possibly referencing <code>javaElement</code>.
121: *
122: * @param javaElement the java element
123: * @param considerVisibility consider visibility of javaElement iff <code>true</code>
124: * @return the search scope
125: * @throws JavaModelException if an error occurs
126: */
127: public static IJavaSearchScope create(IJavaElement javaElement,
128: boolean considerVisibility) throws JavaModelException {
129: if (considerVisibility & javaElement instanceof IMember) {
130: IMember member = (IMember) javaElement;
131: if (JdtFlags.isPrivate(member)) {
132: if (member.getCompilationUnit() != null)
133: return SearchEngine
134: .createJavaSearchScope(new IJavaElement[] { member
135: .getCompilationUnit() });
136: else
137: return SearchEngine
138: .createJavaSearchScope(new IJavaElement[] { member });
139: }
140: // Removed code that does some optimizations regarding package visible members. The problem is that
141: // there can be a package fragment with the same name in a different source folder or project. So we
142: // have to treat package visible members like public or protected members.
143: }
144: return create(javaElement.getJavaProject());
145: }
146:
147: private static IJavaSearchScope create(IJavaProject javaProject)
148: throws JavaModelException {
149: return SearchEngine.createJavaSearchScope(
150: getAllScopeElements(javaProject), false);
151: }
152:
153: /**
154: * Creates a new search scope comprising <code>members</code>.
155: *
156: * @param members the members
157: * @return the search scope
158: * @throws JavaModelException if an error occurs
159: */
160: public static IJavaSearchScope create(IMember[] members)
161: throws JavaModelException {
162: Assert.isTrue(members != null && members.length > 0);
163: IMember candidate = members[0];
164: int visibility = getVisibility(candidate);
165: for (int i = 1; i < members.length; i++) {
166: int mv = getVisibility(members[i]);
167: if (mv > visibility) {
168: visibility = mv;
169: candidate = members[i];
170: }
171: }
172: return create(candidate);
173: }
174:
175: /**
176: * Creates a new search scope with all projects possibly referenced
177: * from the given <code>javaElements</code>.
178: *
179: * @param javaElements the java elements
180: * @return the search scope
181: */
182: public static IJavaSearchScope createReferencedScope(
183: IJavaElement[] javaElements) {
184: Set projects = new HashSet();
185: for (int i = 0; i < javaElements.length; i++) {
186: projects.add(javaElements[i].getJavaProject());
187: }
188: IJavaProject[] prj = (IJavaProject[]) projects
189: .toArray(new IJavaProject[projects.size()]);
190: return SearchEngine.createJavaSearchScope(prj, true);
191: }
192:
193: /**
194: * Creates a new search scope with all projects possibly referenced
195: * from the given <code>javaElements</code>.
196: *
197: * @param javaElements the java elements
198: * @param includeMask the include mask
199: * @return the search scope
200: */
201: public static IJavaSearchScope createReferencedScope(
202: IJavaElement[] javaElements, int includeMask) {
203: Set projects = new HashSet();
204: for (int i = 0; i < javaElements.length; i++) {
205: projects.add(javaElements[i].getJavaProject());
206: }
207: IJavaProject[] prj = (IJavaProject[]) projects
208: .toArray(new IJavaProject[projects.size()]);
209: return SearchEngine.createJavaSearchScope(prj, includeMask);
210: }
211:
212: /**
213: * Creates a new search scope containing all projects which reference or are referenced by the specified project.
214: *
215: * @param project the project
216: * @param includeMask the include mask
217: * @return the search scope
218: * @throws CoreException if a referenced project could not be determined
219: */
220: public static IJavaSearchScope createRelatedProjectsScope(
221: IJavaProject project, int includeMask) throws CoreException {
222: IJavaProject[] projects = getRelatedProjects(project);
223: return SearchEngine
224: .createJavaSearchScope(projects, includeMask);
225: }
226:
227: private static IJavaElement[] getAllScopeElements(
228: IJavaProject project) throws JavaModelException {
229: Collection sourceRoots = getAllSourceRootsInProjects(getReferencingProjects(project));
230: return (IPackageFragmentRoot[]) sourceRoots
231: .toArray(new IPackageFragmentRoot[sourceRoots.size()]);
232: }
233:
234: /*
235: * @param projects a collection of IJavaProject @return Collection a collection of IPackageFragmentRoot, one element for each packageFragmentRoot which lies within a project in <code> projects </code> .
236: */
237: private static Collection getAllSourceRootsInProjects(
238: Collection projects) throws JavaModelException {
239: List result = new ArrayList();
240: for (Iterator it = projects.iterator(); it.hasNext();)
241: result.addAll(getSourceRoots((IJavaProject) it.next()));
242: return result;
243: }
244:
245: /*
246: * Finds, if possible, a classpathEntry in one given project such that this classpath entry references another given project. If more than one entry exists for the referenced project and at least one is exported, then an exported entry will be returned.
247: */
248: private static IClasspathEntry getReferencingClassPathEntry(
249: IJavaProject referencingProject,
250: IJavaProject referencedProject) throws JavaModelException {
251: IClasspathEntry result = null;
252: IPath path = referencedProject.getProject().getFullPath();
253: IClasspathEntry[] classpath = referencingProject
254: .getResolvedClasspath(true);
255: for (int i = 0; i < classpath.length; i++) {
256: IClasspathEntry entry = classpath[i];
257: if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT
258: && path.equals(entry.getPath())) {
259: if (entry.isExported())
260: return entry;
261: // Consider it as a candidate. May be there is another entry that is
262: // exported.
263: result = entry;
264: }
265: }
266: return result;
267: }
268:
269: private static IJavaProject[] getRelatedProjects(IJavaProject focus)
270: throws CoreException {
271: final Set projects = new HashSet();
272:
273: addRelatedReferencing(focus, projects);
274: addRelatedReferenced(focus, projects);
275:
276: projects.add(focus);
277: return (IJavaProject[]) projects
278: .toArray(new IJavaProject[projects.size()]);
279: }
280:
281: private static Collection getReferencingProjects(IJavaProject focus)
282: throws JavaModelException {
283: Set projects = new HashSet();
284:
285: addReferencingProjects(focus, projects);
286: projects.add(focus);
287: return projects;
288: }
289:
290: private static List getSourceRoots(IJavaProject javaProject)
291: throws JavaModelException {
292: List elements = new ArrayList();
293: IPackageFragmentRoot[] roots = javaProject
294: .getPackageFragmentRoots();
295: // Add all package fragment roots except archives
296: for (int i = 0; i < roots.length; i++) {
297: IPackageFragmentRoot root = roots[i];
298: if (!root.isArchive())
299: elements.add(root);
300: }
301: return elements;
302: }
303:
304: private static int getVisibility(IMember member)
305: throws JavaModelException {
306: if (JdtFlags.isPrivate(member))
307: return 0;
308: if (JdtFlags.isPackageVisible(member))
309: return 1;
310: if (JdtFlags.isProtected(member))
311: return 2;
312: return 4;
313: }
314:
315: private RefactoringScopeFactory() {
316: // no instances
317: }
318: }
|