use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class IndexSelector method canSeeFocus.
private static int canSeeFocus(IJavaElement focus, JavaProject javaProject, char[][][] focusQualifiedNames) {
try {
if (focus == null)
return PROJECT_CAN_NOT_SEE_FOCUS;
if (focus.equals(javaProject))
return PROJECT_CAN_SEE_FOCUS;
if (focus instanceof JarPackageFragmentRoot) {
// focus is part of a jar
IPath focusPath = focus.getPath();
IClasspathEntry[] entries = javaProject.getExpandedClasspath();
for (int i = 0, length = entries.length; i < length; i++) {
IClasspathEntry entry = entries[i];
if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && entry.getPath().equals(focusPath))
return PROJECT_CAN_SEE_FOCUS;
}
return PROJECT_CAN_NOT_SEE_FOCUS;
}
// look for dependent projects
IPath focusPath = ((JavaProject) focus).getProject().getFullPath();
IClasspathEntry[] entries = javaProject.getExpandedClasspath();
for (int i = 0, length = entries.length; i < length; i++) {
IClasspathEntry entry = entries[i];
if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT && entry.getPath().equals(focusPath)) {
if (focusQualifiedNames != null) {
// builder state is usable, hence use it to try to reduce project which can see the focus...
State projectState = (State) JavaModelManager.getJavaModelManager().getLastBuiltState(javaProject.getProject(), null);
if (projectState != null) {
Object[] values = projectState.getReferences().valueTable;
int vLength = values.length;
for (int j = 0; j < vLength; j++) {
if (values[j] == null)
continue;
ReferenceCollection references = (ReferenceCollection) values[j];
if (references.includes(focusQualifiedNames, null, null)) {
return PROJECT_CAN_SEE_FOCUS;
}
}
return PROJECT_SOURCE_CAN_NOT_SEE_FOCUS;
}
}
return PROJECT_CAN_SEE_FOCUS;
}
}
return PROJECT_CAN_NOT_SEE_FOCUS;
} catch (JavaModelException e) {
return PROJECT_CAN_NOT_SEE_FOCUS;
}
}
use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class IndexSelector method canSeeFocus.
/**
* Returns whether elements of the given project or jar can see the given focus (an IJavaProject or
* a JarPackageFragmentRot) either because the focus is part of the project or the jar, or because it is
* accessible throught the project's classpath
*/
public static int canSeeFocus(SearchPattern pattern, IPath projectOrJarPath) {
try {
IJavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
IJavaProject project = getJavaProject(projectOrJarPath, model);
IJavaElement[] focuses = getFocusedElementsAndTypes(pattern, project, null);
if (focuses.length == 0)
return PROJECT_CAN_NOT_SEE_FOCUS;
if (project != null) {
return canSeeFocus(focuses, (JavaProject) project, null);
}
// projectOrJarPath is a jar
// it can see the focus only if it is on the classpath of a project that can see the focus
int result = PROJECT_CAN_NOT_SEE_FOCUS;
IJavaProject[] allProjects = model.getJavaProjects();
for (int i = 0, length = allProjects.length; i < length; i++) {
JavaProject otherProject = (JavaProject) allProjects[i];
IClasspathEntry entry = otherProject.getClasspathEntryFor(projectOrJarPath);
if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
int canSeeFocus = canSeeFocus(focuses, otherProject, null);
if (canSeeFocus == PROJECT_CAN_SEE_FOCUS)
return PROJECT_CAN_SEE_FOCUS;
if (canSeeFocus == PROJECT_SOURCE_CAN_NOT_SEE_FOCUS)
result = PROJECT_SOURCE_CAN_NOT_SEE_FOCUS;
}
}
return result;
} catch (JavaModelException e) {
return PROJECT_CAN_NOT_SEE_FOCUS;
}
}
use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class IndexSelector method initializeIndexLocations.
/*
* Compute the list of paths which are keying index files.
*/
private void initializeIndexLocations() {
IPath[] projectsAndJars = this.searchScope.enclosingProjectsAndJars();
IndexManager manager = JavaModelManager.getIndexManager();
// use a linked set to preserve the order during search: see bug 348507
LinkedHashSet locations = new LinkedHashSet();
IJavaElement focus = MatchLocator.projectOrJarFocus(this.pattern);
if (focus == null) {
for (int i = 0; i < projectsAndJars.length; i++) {
IPath path = projectsAndJars[i];
Object target = JavaModel.getTarget(path, false);
if (// case of an external folder
target instanceof IFolder)
path = ((IFolder) target).getFullPath();
locations.add(manager.computeIndexLocation(path));
}
} else {
try {
// See whether the state builder might be used to reduce the number of index locations
// find the projects from projectsAndJars that see the focus then walk those projects looking for the jars from projectsAndJars
int length = projectsAndJars.length;
JavaProject[] projectsCanSeeFocus = new JavaProject[length];
SimpleSet visitedProjects = new SimpleSet(length);
int projectIndex = 0;
SimpleSet externalLibsToCheck = new SimpleSet(length);
ObjectVector superTypes = new ObjectVector();
IJavaElement[] focuses = getFocusedElementsAndTypes(this.pattern, focus, superTypes);
char[][][] focusQualifiedNames = null;
boolean isAutoBuilding = ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding();
if (isAutoBuilding && focus instanceof IJavaProject) {
focusQualifiedNames = getQualifiedNames(superTypes);
}
IJavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
for (int i = 0; i < length; i++) {
IPath path = projectsAndJars[i];
JavaProject project = (JavaProject) getJavaProject(path, model);
if (project != null) {
visitedProjects.add(project);
/*We are adding all modules to the locations for searching in each one.
Now the location contains not only current module and Jars on which depends, but also all modules from the workspace.*/
locations.add(manager.computeIndexLocation(path));
/*int canSeeFocus = canSeeFocus(focuses, project, focusQualifiedNames);
if (canSeeFocus == PROJECT_CAN_SEE_FOCUS) {
locations.add(manager.computeIndexLocation(path));
}
if (canSeeFocus != PROJECT_CAN_NOT_SEE_FOCUS) {
projectsCanSeeFocus[projectIndex++] = project;
}*/
} else {
externalLibsToCheck.add(path);
}
}
for (int i = 0; i < projectIndex && externalLibsToCheck.elementSize > 0; i++) {
IClasspathEntry[] entries = projectsCanSeeFocus[i].getResolvedClasspath();
for (int j = entries.length; --j >= 0; ) {
IClasspathEntry entry = entries[j];
if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
IPath path = entry.getPath();
if (externalLibsToCheck.remove(path) != null) {
Object target = JavaModel.getTarget(path, false);
if (// case of an external folder
target instanceof IFolder)
path = ((IFolder) target).getFullPath();
locations.add(manager.computeIndexLocation(path));
}
}
}
}
// jar files can be included in the search scope without including one of the projects that references them, so scan all projects that have not been visited
if (externalLibsToCheck.elementSize > 0) {
IJavaProject[] allProjects = model.getJavaProjects();
for (int i = 0, l = allProjects.length; i < l && externalLibsToCheck.elementSize > 0; i++) {
JavaProject project = (JavaProject) allProjects[i];
if (!visitedProjects.includes(project)) {
IClasspathEntry[] entries = project.getResolvedClasspath();
for (int j = entries.length; --j >= 0; ) {
IClasspathEntry entry = entries[j];
if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
IPath path = entry.getPath();
if (externalLibsToCheck.remove(path) != null) {
Object target = JavaModel.getTarget(path, false);
if (// case of an external folder
target instanceof IFolder)
path = ((IFolder) target).getFullPath();
locations.add(manager.computeIndexLocation(path));
}
}
}
}
}
}
} catch (JavaModelException e) {
// ignored
}
}
// Ensure no nulls
locations.remove(null);
this.indexLocations = (IndexLocation[]) locations.toArray(new IndexLocation[locations.size()]);
}
use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class JavaModelManager method create.
/**
* Returns the package fragment or package fragment root corresponding to the given folder,
* its parent or great parent being the given project.
* or <code>null</code> if unable to associate the given folder with a Java element.
* <p>
* Note that a package fragment root is returned rather than a default package.
* <p>
* Creating a Java element has the side effect of creating and opening all of the
* element's parents if they are not yet open.
*/
public static IJavaElement create(IFolder folder, IJavaProject project) {
if (folder == null) {
return null;
}
IJavaElement element;
if (project == null) {
project = JavaCore.create(folder.getProject());
element = determineIfOnClasspath(folder, project);
if (element == null) {
// walk all projects and find one that have the given folder on its classpath
IJavaProject[] projects;
try {
projects = org.eclipse.jdt.internal.core.JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects();
} catch (JavaModelException e) {
return null;
}
for (int i = 0, length = projects.length; i < length; i++) {
project = projects[i];
element = determineIfOnClasspath(folder, project);
if (element != null)
break;
}
}
} else {
element = determineIfOnClasspath(folder, project);
}
return element;
}
use of org.eclipse.jdt.core.JavaModelException in project che by eclipse.
the class JavaModelManager method determineIfOnClasspath.
/**
* Returns the package fragment root represented by the resource, or
* the package fragment the given resource is located in, or <code>null</code>
* if the given resource is not on the classpath of the given project.
*/
public static IJavaElement determineIfOnClasspath(IResource resource, IJavaProject project) {
IPath resourcePath = resource.getFullPath();
boolean isExternal = ExternalFoldersManager.isExternalFolderPath(resourcePath);
if (isExternal)
resourcePath = resource.getLocation();
try {
JavaProjectElementInfo projectInfo = (JavaProjectElementInfo) org.eclipse.jdt.internal.core.JavaModelManager.getJavaModelManager().getInfo(project);
JavaProjectElementInfo.ProjectCache projectCache = projectInfo == null ? null : projectInfo.projectCache;
HashtableOfArrayToObject allPkgFragmentsCache = projectCache == null ? null : projectCache.allPkgFragmentsCache;
boolean isJavaLike = Util.isJavaLikeFileName(resourcePath.lastSegment());
IClasspathEntry[] entries = // JAVA file can only live inside SRC folder (on the raw path)
isJavaLike ? // JAVA file can only live inside SRC folder (on the raw path)
project.getRawClasspath() : ((JavaProject) project).getResolvedClasspath();
int length = entries.length;
if (length > 0) {
String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
for (int i = 0; i < length; i++) {
IClasspathEntry entry = entries[i];
if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT)
continue;
IPath rootPath = entry.getPath();
if (rootPath.equals(resourcePath)) {
if (isJavaLike)
return null;
return project.getPackageFragmentRoot(resource);
} else if (rootPath.isPrefixOf(resourcePath)) {
// allow creation of package fragment if it contains a .java file that is included
if (!Util.isExcluded(resourcePath, ((ClasspathEntry) entry).fullInclusionPatternChars(), ((ClasspathEntry) entry).fullExclusionPatternChars(), true)) {
// given we have a resource child of the root, it cannot be a JAR pkg root
PackageFragmentRoot root = isExternal ? new ExternalPackageFragmentRoot(rootPath, (JavaProject) project) : (PackageFragmentRoot) ((JavaProject) project).getFolderPackageFragmentRoot(rootPath);
if (root == null)
return null;
IPath pkgPath = resourcePath.removeFirstSegments(rootPath.segmentCount());
if (resource.getType() == IResource.FILE) {
// if the resource is a file, then remove the last segment which
// is the file name in the package
pkgPath = pkgPath.removeLastSegments(1);
}
String[] pkgName = pkgPath.segments();
// (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=133141)
if (allPkgFragmentsCache != null && allPkgFragmentsCache.containsKey(pkgName))
return root.getPackageFragment(pkgName);
if (pkgName.length != 0 && JavaConventions.validatePackageName(Util.packageName(pkgPath, sourceLevel, complianceLevel), sourceLevel, complianceLevel).getSeverity() == IStatus.ERROR) {
return null;
}
return root.getPackageFragment(pkgName);
}
}
}
}
} catch (JavaModelException npe) {
return null;
}
return null;
}
Aggregations