use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class JavaSearchScope method packageFragmentRoot.
/**
* @see AbstractJavaSearchScope#packageFragmentRoot(String, int, String)
*/
public IPackageFragmentRoot packageFragmentRoot(String resourcePathString, int jarSeparatorIndex, String jarPath) {
int index = -1;
boolean isJarFile = jarSeparatorIndex != -1;
if (isJarFile) {
// internal or external jar (case 3, 4, or 5)
String relativePath = resourcePathString.substring(jarSeparatorIndex + 1);
index = indexOf(jarPath, relativePath);
} else {
// resource in workspace (case 1 or 2)
index = indexOf(resourcePathString);
}
if (index >= 0) {
int idx = this.projectIndexes[index];
String projectPath = idx == -1 ? null : (String) this.projectPaths.get(idx);
if (projectPath != null) {
IJavaProject project = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject(projectPath));
if (isJarFile) {
IResource resource = JavaModel.getWorkspaceTarget(new Path(jarPath));
if (resource != null)
return project.getPackageFragmentRoot(resource);
return project.getPackageFragmentRoot(jarPath);
}
Object target = JavaModel.getWorkspaceTarget(new Path(this.containerPaths[index] + '/' + this.relativePaths[index]));
if (target != null) {
if (target instanceof IProject) {
return project.getPackageFragmentRoot((IProject) target);
}
IJavaElement element = JavaModelManager.create((IResource) target, project);
return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
}
}
}
return null;
}
use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class JavaSearchScope method encloses.
/* (non-Javadoc)
* @see IJavaSearchScope#encloses(IJavaElement)
*/
public boolean encloses(IJavaElement element) {
if (this.elements != null) {
for (int i = 0, length = this.elements.size(); i < length; i++) {
IJavaElement scopeElement = (IJavaElement) this.elements.get(i);
IJavaElement searchedElement = element;
while (searchedElement != null) {
if (searchedElement.equals(scopeElement))
return true;
searchedElement = searchedElement.getParent();
}
}
return false;
}
IPackageFragmentRoot root = (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
if (root != null && root.isArchive()) {
// external or internal jar
IPath rootPath = root.getPath();
String rootPathToString = rootPath.getDevice() == null ? rootPath.toString() : rootPath.toOSString();
IPath relativePath = getPath(element, true);
return indexOf(rootPathToString, relativePath.toString()) >= 0;
}
// resource in workspace
String fullResourcePathString = getPath(element, false).toString();
return indexOf(fullResourcePathString) >= 0;
}
use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class JavaSearchScope method add.
/**
* Add an element to the java search scope.
* @param element The element we want to add to current java search scope
* @throws JavaModelException May happen if some Java Model info are not available
*/
public void add(IJavaElement element) throws JavaModelException {
IPath containerPath = null;
String containerPathToString = null;
PackageFragmentRoot root = null;
int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES;
switch(element.getElementType()) {
case IJavaElement.JAVA_MODEL:
// a workspace sope should be used
break;
case IJavaElement.JAVA_PROJECT:
add((JavaProject) element, null, includeMask, new HashSet(2), new HashSet(2), null);
break;
case IJavaElement.PACKAGE_FRAGMENT_ROOT:
root = (PackageFragmentRoot) element;
IPath rootPath = root.internalPath();
containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : rootPath;
containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
IResource rootResource = root.resource();
String projectPath = root.getJavaProject().getPath().toString();
if (rootResource != null && rootResource.isAccessible()) {
String relativePath = Util.relativePath(rootResource.getFullPath(), containerPath.segmentCount());
add(projectPath, relativePath, containerPathToString, false, /*not a package*/
null);
} else {
//$NON-NLS-1$
add(projectPath, "", containerPathToString, false, /*not a package*/
null);
}
break;
case IJavaElement.PACKAGE_FRAGMENT:
root = (PackageFragmentRoot) element.getParent();
projectPath = root.getJavaProject().getPath().toString();
if (root.isArchive()) {
String relativePath = Util.concatWith(((PackageFragment) element).names, '/');
containerPath = root.getPath();
containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
add(projectPath, relativePath, containerPathToString, true, /*package*/
null);
} else {
IResource resource = ((JavaElement) element).resource();
if (resource != null) {
if (resource.isAccessible()) {
containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.internalPath();
} else {
// for working copies, get resource container full path
containerPath = resource.getParent().getFullPath();
}
containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
String relativePath = Util.relativePath(resource.getFullPath(), containerPath.segmentCount());
add(projectPath, relativePath, containerPathToString, true, /*package*/
null);
}
}
break;
default:
// remember sub-cu (or sub-class file) java elements
if (element instanceof IMember) {
if (this.elements == null) {
this.elements = new ArrayList();
}
this.elements.add(element);
}
root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
projectPath = root.getJavaProject().getPath().toString();
String relativePath;
if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
containerPath = root.getParent().getPath();
relativePath = Util.relativePath(getPath(element, false), root.getJavaProject().getPath().segmentCount());
} else {
containerPath = root.internalPath();
relativePath = getPath(element, true).toString();
}
containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
add(projectPath, relativePath, containerPathToString, false, /*not a package*/
null);
}
if (root != null)
addEnclosingProjectOrJar(root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.getPath());
}
use of org.eclipse.jdt.core.IJavaElement 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.IJavaElement in project che by eclipse.
the class JavaModelOperation method run.
/**
* Runs this operation and registers any deltas created.
*
* @throws CoreException
* if the operation fails
* @see IWorkspaceRunnable
*/
public void run(IProgressMonitor monitor) throws CoreException {
JavaModelManager manager = JavaModelManager.getJavaModelManager();
DeltaProcessor deltaProcessor = manager.getDeltaProcessor();
int previousDeltaCount = deltaProcessor.javaModelDeltas.size();
try {
this.progressMonitor = monitor;
pushOperation(this);
try {
if (canModifyRoots()) {
// computes the root infos before executing the operation
// noop if aready initialized
manager.deltaState.initializeRoots(false);
}
executeOperation();
} finally {
if (isTopLevelOperation()) {
runPostActions();
}
}
} finally {
try {
// reacquire delta processor as it can have been reset during executeOperation()
deltaProcessor = manager.getDeltaProcessor();
// update JavaModel using deltas that were recorded during this operation
for (int i = previousDeltaCount, size = deltaProcessor.javaModelDeltas.size(); i < size; i++) {
deltaProcessor.updateJavaModel((IJavaElementDelta) deltaProcessor.javaModelDeltas.get(i));
}
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=83646
for (int i = 0, length = this.resultElements.length; i < length; i++) {
IJavaElement element = this.resultElements[i];
Openable openable = (Openable) element.getOpenable();
if (!(openable instanceof CompilationUnit) || !((CompilationUnit) openable).isWorkingCopy()) {
// a working copy must remain a child of its parent even after a move
((JavaElement) openable.getParent()).close();
}
switch(element.getElementType()) {
case IJavaElement.PACKAGE_FRAGMENT_ROOT:
case IJavaElement.PACKAGE_FRAGMENT:
deltaProcessor.projectCachesToReset.add(element.getJavaProject());
break;
}
}
deltaProcessor.resetProjectCaches();
// - but the operation has not modified any resource
if (isTopLevelOperation()) {
if ((deltaProcessor.javaModelDeltas.size() > previousDeltaCount || !deltaProcessor.reconcileDeltas.isEmpty()) && !hasModifiedResource()) {
deltaProcessor.fire(null, DeltaProcessor.DEFAULT_CHANGE_EVENT);
}
// else deltas are fired while processing the resource delta
}
} finally {
popOperation();
}
}
}
Aggregations