Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0cbcfe4c8245caafa0104ebdaf84b2e10fee45ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.ui.internal.dialogfield;

import java.util.ArrayList;
import java.util.HashSet;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jst.jsf.common.ui.IFileFolderConstants;

/**
 * @author mengbo
 */
public class JavaSearchScope implements IJavaSearchScope {
	private IProject _project;

	private String _superType;

	private HashSet _allowedTypeSet;

	private IPath[] _enclosingProjectsAndJars;

	private IProject[] _relativeProjects;

	public JavaSearchScope(IProject project, String superType) {
		this._project = project;
		this._superType = superType;
		computeRelativeProjects();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.core.search.IJavaSearchScope#enclosingProjectsAndJars()
	 */
	public IPath[] enclosingProjectsAndJars() {
		if (_enclosingProjectsAndJars == null) {
			ArrayList list = new ArrayList();
			for (int i = 0; i < _relativeProjects.length; i++) {
				try {
					if (_relativeProjects[i].hasNature(JavaCore.NATURE_ID)) {
						IJavaProject javaProject = JavaCore
								.create(_relativeProjects[i]);
						IClasspathEntry[] classpath = javaProject
								.getResolvedClasspath(true);
						for (int j = 0; j < classpath.length; j++) {
							list.add(classpath[j].getPath());
						}
						list.add(javaProject.getPath());
					}
				} catch (CoreException e)// NOPMD
				{
					// skip the project.
				}
			}
			_enclosingProjectsAndJars = (IPath[]) list.toArray(new IPath[(list
					.size())]);
		}
		return _enclosingProjectsAndJars;
	}

	private void computeRelativeProjects() {
		try {
			IProject[] referencedProjects = _project.getReferencedProjects();
			_relativeProjects = new IProject[referencedProjects.length + 1];
			System.arraycopy(referencedProjects, 0, _relativeProjects, 1,
					referencedProjects.length);
			_relativeProjects[0] = _project;
		} catch (CoreException e) {
			_relativeProjects = new IProject[] { _project };
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.core.search.IJavaSearchScope#encloses(java.lang.String)
	 */
	public boolean encloses(String resourcePath) {
		if (_allowedTypeSet == null) {
			try {
				_allowedTypeSet = findAllowedTypes(_superType);
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}
		if (_allowedTypeSet == null) {
			_allowedTypeSet = new HashSet();
		}
		int separatorIndex = resourcePath.indexOf(JAR_FILE_ENTRY_SEPARATOR);
		if (separatorIndex != -1) {
			String className = resourcePath.substring(separatorIndex + 1,
					resourcePath.length() - 6).replace('/', '.');
			if (_allowedTypeSet.contains(className)) {
				return true;
			}
		} else if (_allowedTypeSet.contains(resourcePath)) {
			return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.core.search.IJavaSearchScope#encloses(org.eclipse.jdt.core.IJavaElement)
	 */
	public boolean encloses(IJavaElement element) {
		return encloses(element.getPath().toString());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.core.search.IJavaSearchScope#includesBinaries()
	 */
	public boolean includesBinaries() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.core.search.IJavaSearchScope#includesClasspaths()
	 */
	public boolean includesClasspaths() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.core.search.IJavaSearchScope#setIncludesBinaries(boolean)
	 */
	public void setIncludesBinaries(boolean includesBinaries) {
	    //    do nothing, includeBinaries always true
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jdt.core.search.IJavaSearchScope#setIncludesClasspaths(boolean)
	 */
	public void setIncludesClasspaths(boolean includesClasspaths) {
        // do nothing, includeClasspaths always trues
	}

	private HashSet findAllowedTypes(String superType) throws CoreException {
		HashSet set = new HashSet();

		IProject[] projects = _relativeProjects;

		for (int i = 0; i < projects.length; i++) {
			IType type = null;
			if (projects[i].hasNature(JavaCore.NATURE_ID)) {
				IJavaProject javaProject = JavaCore.create(projects[i]);
				if (superType != null) {
					try {
						type = javaProject.findType(superType);
						if (type != null) {
							ITypeHierarchy typeHierarchy = type
									.newTypeHierarchy(javaProject, null);
							IType[] subtypes = typeHierarchy
									.getAllSubtypes(type);
							for (int j = 0; j < subtypes.length; j++) {
								if (!subtypes[j].isBinary()) {
									set.add(subtypes[j].getPath().toString());
								} else {
									String path = subtypes[j].getPath()
											.toString();
									if (path != null
											&& path
													.endsWith(IFileFolderConstants.DOT
															+ IFileFolderConstants.EXT_JAR)) {
										set.add(subtypes[j]
												.getFullyQualifiedName());
									} else {
										set.add(path);
									}
								}
							}
						}
					} catch (JavaModelException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return set;
	}

	/**
	 * @return Returns the superType.
	 */
	public String getSuperType() {
		return _superType;
	}

	/**
	 * @param superType
	 *            The superType to set.
	 */
	public void setSuperType(String superType) {
		this._superType = superType;
	}
}

Back to the top