use of org.eclipse.jdt.internal.core.PackageFragmentRoot 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.internal.core.PackageFragmentRoot in project azure-tools-for-java by Microsoft.
the class AzureDockerUIResources method getCurrentSelectedProject.
public static IProject getCurrentSelectedProject() {
IProject project = null;
ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
ISelection selection = selectionService.getSelection();
if (selection instanceof IStructuredSelection) {
Object element = ((IStructuredSelection) selection).getFirstElement();
if (element instanceof IResource) {
project = ((IResource) element).getProject();
} else if (element instanceof PackageFragmentRoot) {
IJavaProject jProject = ((PackageFragmentRoot) element).getJavaProject();
project = jProject.getProject();
} else if (element instanceof IJavaElement) {
IJavaProject jProject = ((IJavaElement) element).getJavaProject();
project = jProject.getProject();
}
}
if (project == null) {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
if (workspace.getRoot() != null && workspace.getRoot().getProjects().length > 0) {
IProject[] projects = workspace.getRoot().getProjects();
project = projects[projects.length - 1];
} else {
PluginUtil.displayErrorDialog(Display.getDefault().getActiveShell(), "No Active Project", "Must have a project first");
}
}
return project;
}
use of org.eclipse.jdt.internal.core.PackageFragmentRoot in project jbosstools-hibernate by jbosstools.
the class WripperException method getSrcFolder.
private File getSrcFolder() throws JavaModelException {
PackageFragmentRoot packageFragmentRoot = null;
IPackageFragmentRoot[] roots = testProject.getIJavaProject().getAllPackageFragmentRoots();
for (int i = 0; i < roots.length && packageFragmentRoot == null; i++) {
if (roots[i].getClass() == PackageFragmentRoot.class) {
packageFragmentRoot = (PackageFragmentRoot) roots[i];
}
}
assertNotNull(packageFragmentRoot);
return packageFragmentRoot.getResource().getLocation().toFile();
}
use of org.eclipse.jdt.internal.core.PackageFragmentRoot in project jbosstools-hibernate by jbosstools.
the class CompilationUnitCollector method processJavaElements.
/**
* Process object - java element to collect all it's children CompilationUnits
* @param obj
* @param bCollect
*/
public void processJavaElements(Object obj, boolean bCollect) {
if (obj instanceof ICompilationUnit) {
ICompilationUnit cu = (ICompilationUnit) obj;
addCompilationUnit(cu, bCollect);
} else if (obj instanceof File) {
File file = (File) obj;
if (file.getProject() != null) {
IJavaProject javaProject = JavaCore.create(file.getProject());
ICompilationUnit[] cus = Utils.findCompilationUnits(javaProject, file.getFullPath());
if (cus != null) {
for (int i = 0; i < cus.length; i++) {
addCompilationUnit(cus[i], bCollect);
}
}
}
} else if (obj instanceof JavaProject) {
JavaProject javaProject = (JavaProject) obj;
IPackageFragmentRoot[] pfr = null;
try {
pfr = javaProject.getAllPackageFragmentRoots();
} catch (JavaModelException e) {
// just ignore it!
// HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
}
if (pfr != null) {
for (int i = 0; i < pfr.length; i++) {
processJavaElements(pfr[i], bCollect);
}
}
} else if (obj instanceof PackageFragment) {
PackageFragment packageFragment = (PackageFragment) obj;
ICompilationUnit[] cus = null;
try {
cus = packageFragment.getCompilationUnits();
} catch (JavaModelException e) {
// just ignore it!
// HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
}
if (cus != null && cus.length > 0) {
if (bCollect) {
selection2UpdateList.add(obj);
bCollect = false;
}
for (int i = 0; i < cus.length; i++) {
addCompilationUnit(cus[i], bCollect);
}
}
} else if (obj instanceof PackageFragmentRoot) {
JavaElement javaElement = (JavaElement) obj;
JavaElementInfo javaElementInfo = null;
try {
javaElementInfo = (JavaElementInfo) javaElement.getElementInfo();
} catch (JavaModelException e) {
// just ignore it!
// HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
}
if (javaElementInfo != null) {
IJavaElement[] je = javaElementInfo.getChildren();
for (int i = 0; i < je.length; i++) {
processJavaElements(je[i], true);
}
}
} else if (obj instanceof JavaElement) {
JavaElement javaElement = (JavaElement) obj;
ICompilationUnit cu = javaElement.getCompilationUnit();
addCompilationUnit(cu, bCollect);
} else if (obj instanceof SourceType) {
if (bCollect) {
selection2UpdateList.add(obj);
bCollect = false;
}
SourceType sourceType = (SourceType) obj;
processJavaElements(sourceType.getJavaModel(), bCollect);
} else {
// ignore
// System.out.println("1 Blah! " + selection); //$NON-NLS-1$
}
}
use of org.eclipse.jdt.internal.core.PackageFragmentRoot in project che by eclipse.
the class HandleFactory method createOpenable.
/**
* Creates an Openable handle from the given resource path.
* The resource path can be a path to a file in the workbench (e.g. /Proj/com/ibm/jdt/core/HandleFactory.java)
* or a path to a file in a jar file - it then contains the path to the jar file and the path to the file in the jar
* (e.g. c:/jdk1.2.2/jre/lib/rt.jar|java/lang/Object.class or /Proj/rt.jar|java/lang/Object.class)
* NOTE: This assumes that the resource path is the toString() of an IPath,
* in other words, it uses the IPath.SEPARATOR for file path
* and it uses '/' for entries in a zip file.
* If not null, uses the given scope as a hint for getting Java project handles.
*/
public Openable createOpenable(String resourcePath, IJavaSearchScope scope) {
int separatorIndex;
if ((separatorIndex = resourcePath.indexOf(IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR)) > -1) {
// path to a class file inside a jar
// Optimization: cache package fragment root handle and package handles
int rootPathLength;
if (this.lastPkgFragmentRootPath == null || (rootPathLength = this.lastPkgFragmentRootPath.length()) != resourcePath.length() || !resourcePath.regionMatches(0, this.lastPkgFragmentRootPath, 0, rootPathLength)) {
String jarPath = resourcePath.substring(0, separatorIndex);
PackageFragmentRoot root = getJarPkgFragmentRoot(resourcePath, separatorIndex, jarPath, scope);
if (root == null)
// match is outside classpath
return null;
this.lastPkgFragmentRootPath = jarPath;
this.lastPkgFragmentRoot = root;
this.packageHandles = new HashtableOfArrayToObject(5);
}
// create handle
String classFilePath = resourcePath.substring(separatorIndex + 1);
String[] simpleNames = new Path(classFilePath).segments();
String[] pkgName;
int length = simpleNames.length - 1;
if (length > 0) {
pkgName = new String[length];
System.arraycopy(simpleNames, 0, pkgName, 0, length);
} else {
pkgName = CharOperation.NO_STRINGS;
}
IPackageFragment pkgFragment = (IPackageFragment) this.packageHandles.get(pkgName);
if (pkgFragment == null) {
pkgFragment = this.lastPkgFragmentRoot.getPackageFragment(pkgName);
this.packageHandles.put(pkgName, pkgFragment);
}
IClassFile classFile = pkgFragment.getClassFile(simpleNames[length]);
return (Openable) classFile;
} else {
// path to a file in a directory
// Optimization: cache package fragment root handle and package handles
int rootPathLength = -1;
if (this.lastPkgFragmentRootPath == null || !(resourcePath.startsWith(this.lastPkgFragmentRootPath) && !org.eclipse.jdt.internal.compiler.util.Util.isExcluded(resourcePath.toCharArray(), this.lastPkgFragmentRoot.fullInclusionPatternChars(), this.lastPkgFragmentRoot.fullExclusionPatternChars(), false) && (rootPathLength = this.lastPkgFragmentRootPath.length()) > 0 && resourcePath.charAt(rootPathLength) == '/')) {
PackageFragmentRoot root = getPkgFragmentRoot(resourcePath);
if (root == null)
// match is outside classpath
return null;
this.lastPkgFragmentRoot = root;
this.lastPkgFragmentRootPath = this.lastPkgFragmentRoot.internalPath().toString();
this.packageHandles = new HashtableOfArrayToObject(5);
}
// create handle
resourcePath = resourcePath.substring(this.lastPkgFragmentRootPath.length() + 1);
String[] simpleNames = new Path(resourcePath).segments();
String[] pkgName;
int length = simpleNames.length - 1;
if (length > 0) {
pkgName = new String[length];
System.arraycopy(simpleNames, 0, pkgName, 0, length);
} else {
pkgName = CharOperation.NO_STRINGS;
}
IPackageFragment pkgFragment = (IPackageFragment) this.packageHandles.get(pkgName);
if (pkgFragment == null) {
pkgFragment = this.lastPkgFragmentRoot.getPackageFragment(pkgName);
this.packageHandles.put(pkgName, pkgFragment);
}
String simpleName = simpleNames[length];
if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(simpleName)) {
ICompilationUnit unit = pkgFragment.getCompilationUnit(simpleName);
return (Openable) unit;
} else if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(simpleName)) {
IClassFile classFile = pkgFragment.getClassFile(simpleName);
return (Openable) classFile;
}
return null;
}
}
Aggregations