List of usage examples for org.eclipse.jdt.core IJavaProject findElement
IJavaElement findElement(IPath path) throws JavaModelException;
IJavaElement
corresponding to the given classpath-relative path, or null
if no such IJavaElement
is found. From source file:org.eclipse.che.jdt.util.JavaModelUtil.java
License:Open Source License
/** * Finds a type container by container name. The returned element will be of type * <code>IType</code> or a <code>IPackageFragment</code>. <code>null</code> is returned if the * type container could not be found.// w w w . j a va 2 s. c o m * * @param jproject * The Java project defining the context to search * @param typeContainerName * A dot separated name of the type container * @return returns the container * @throws JavaModelException * thrown when the project can not be accessed * @see #getTypeContainerName(IType) */ public static IJavaElement findTypeContainer(IJavaProject jproject, String typeContainerName) throws JavaModelException { // try to find it as type IJavaElement result = jproject.findType(typeContainerName); if (result == null) { // find it as package IPath path = new Path(typeContainerName.replace('.', '/')); result = jproject.findElement(path); if (!(result instanceof IPackageFragment)) { result = null; } } return result; }
From source file:org.eclipse.che.plugin.java.testing.AbstractJavaTestRunner.java
License:Open Source License
private ICompilationUnit findCompilationUnitByPath(IJavaProject javaProject, String filePath) { try {/* w w w . j av a 2 s . c om*/ IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(false); IPath packageRootPath = null; for (IClasspathEntry classpathEntry : resolvedClasspath) { if (filePath.startsWith(classpathEntry.getPath().toOSString())) { packageRootPath = classpathEntry.getPath(); break; } } if (packageRootPath == null) { throw getRuntimeException(filePath); } String packagePath = packageRootPath.toOSString(); if (!packagePath.endsWith("/")) { packagePath += '/'; } String pathToClass = filePath.substring(packagePath.length()); IJavaElement element = javaProject.findElement(new Path(pathToClass)); if (element != null && element.getElementType() == IJavaElement.COMPILATION_UNIT) { return (ICompilationUnit) element; } else { throw getRuntimeException(filePath); } } catch (JavaModelException e) { throw new RuntimeException("Can't find Compilation Unit.", e); } }
From source file:org.eclipse.jst.j2ee.internal.web.deployables.WebDeployableArtifactUtil.java
License:Open Source License
public static String getClassNameForType(IResource resource, String superType) { if (resource == null) return null; try {/*from w w w . jav a2 s. c om*/ IProject project = resource.getProject(); IPath path = resource.getFullPath(); if (!project.hasNature(JavaCore.NATURE_ID) || path == null) return null; IJavaProject javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID); if (!javaProject.isOpen()) javaProject.open(new NullProgressMonitor()); // output location may not be on classpath IPath outputPath = javaProject.getOutputLocation(); if (outputPath != null && "class".equals(path.getFileExtension()) && outputPath.isPrefixOf(path)) { //$NON-NLS-1$ int count = outputPath.segmentCount(); path = path.removeFirstSegments(count); } // remove initial part of classpath IClasspathEntry[] classPathEntry = javaProject.getResolvedClasspath(true); if (classPathEntry != null) { int size = classPathEntry.length; for (int i = 0; i < size; i++) { IPath classPath = classPathEntry[i].getPath(); if (classPath.isPrefixOf(path)) { int count = classPath.segmentCount(); path = path.removeFirstSegments(count); i += size; } } } // get java element IJavaElement javaElement = javaProject.findElement(path); IType[] types = getTypes(javaElement); if (types != null) { int size2 = types.length; for (int i = 0; i < size2; i++) { if (hasSuperclass(types[i], superType)) return types[i].getFullyQualifiedName(); } } return null; } catch (Exception e) { return null; } }
From source file:org.eclipse.jst.jsf.common.ui.internal.dialogfield.JavaClassWizard.java
License:Open Source License
public boolean performFinish() { if (_mainPage.getPackageText() != null && _mainPage.getPackageText().length() > 0) { StringBuffer buffer = new StringBuffer(_mainPage.getPackageText()); buffer.append(".");//$NON-NLS-1$ buffer.append(_mainPage.getTypeName()); _className = buffer.toString();// www. j ava 2 s . c o m } else { _className = _mainPage.getTypeName(); } _classArgs = _mainPage.getClassArgs(); IRunnableWithProgress op = new WorkspaceModifyOperation() { protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { _mainPage.createType(monitor); IResource resource = _mainPage.getModifiedResource(); if (resource != null && _autoOpenResource) { selectAndReveal(resource); if (_project.hasNature(JavaCore.NATURE_ID)) { IJavaProject jProject = JavaCore.create(_project); IJavaElement jElement = jProject .findElement(resource.getProjectRelativePath().removeFirstSegments(1)); if (jElement != null) { JavaUI.openInEditor(jElement); } } else if (resource instanceof IFile) { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IDE.openEditor(page, (IFile) resource, true); } } } }; try { getContainer().run(false, true, op); } catch (InvocationTargetException e) { e.printStackTrace(); // PDEPlugin.logException(e); } catch (InterruptedException e) { e.printStackTrace();// PDEPlugin.logException(e); } return true; }
From source file:org.eclipse.jst.jsf.common.ui.internal.dialogfield.JavaUIHelper.java
License:Open Source License
/** * @param project/*from w w w .j av a 2 s . com*/ * @param className */ static void doOpenClass(IProject project, String className) { String path = className.replace('.', '/') + ".java"; //$NON-NLS-1$ try { if (project.hasNature(JavaCore.NATURE_ID)) { IJavaProject javaProject = JavaCore.create(project); IJavaElement result = javaProject.findElement(new Path(path)); JavaUI.openInEditor(result); } else { IResource resource = project.findMember(new Path(path)); if (resource instanceof IFile) { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IDE.openEditor(page, (IFile) resource, true); } } } catch (PartInitException e) { e.printStackTrace();// PDEPlugin.logException(e); } catch (JavaModelException e) { e.printStackTrace();// Display.getCurrent().beep(); } catch (CoreException e) { e.printStackTrace();// PDEPlugin.logException(e); } }
From source file:org.eclipse.jst.jsf.common.ui.internal.dialogfield.JavaUIHelper.java
License:Open Source License
/** * @param project//w w w.j a v a2 s .c o m * @param className * @return true if the class exists in project */ static boolean doesClassExist(IProject project, String className) { String path = className.replace('.', '/') + ".java"; //$NON-NLS-1$ try { if (project.hasNature(JavaCore.NATURE_ID)) { IJavaProject javaProject = JavaCore.create(project); IJavaElement result = javaProject.findElement(new Path(path)); return result != null; } IResource resource = project.findMember(new Path(path)); return resource != null; } catch (JavaModelException e) { return false; } catch (CoreException e) { return false; } }
From source file:org.eclipse.jst.jsf.common.ui.internal.utils.PathUtil.java
License:Open Source License
/** * @param javaProject// www. j a v a 2s .c o m * @param parent * @return the IPath for a a classpath object (?) */ public static IPath getPathOnClasspath(IJavaProject javaProject, Object parent) { IPath result = null; if (javaProject == null || parent == null) { return new Path(""); //$NON-NLS-1$ } IClasspathEntry[] entries = javaProject.readRawClasspath(); IPath classPath = null; if (parent instanceof IResource) { if (((javaProject != null) && !javaProject.isOnClasspath((IResource) parent))) { return new Path(""); //$NON-NLS-1$ } if (parent instanceof IFile) { IPath elementPath = ((IFile) parent).getFullPath(); if (((IFile) parent).getFileExtension().equalsIgnoreCase(IFileFolderConstants.EXT_PROPERTIES)) { int machings = 0; try { for (int i = 0; i < entries.length; i++) { // Determine whether on this classentry's path int n = entries[i].getPath().matchingFirstSegments(elementPath); if (n > machings) { // Get package name machings = n; classPath = elementPath.removeFirstSegments(machings).removeLastSegments(1); } } // Not on the classpath? if (classPath == null) { return null; } else if (classPath.segmentCount() > 0) { IJavaElement element = javaProject.findElement(classPath); if (element != null) { IPath path = element.getPath(); if (path != null) { IPath path1 = path.removeFirstSegments(machings); String fileName = ((IFile) parent).getName(); if (fileName != null) { result = path1.append(fileName); } } } } else { result = ((IFile) parent).getFullPath().removeFirstSegments(machings); } } catch (Exception e) { return null; } } } } else if (parent instanceof IJarEntryResource) { IPath elementPath = ((IJarEntryResource) parent).getFullPath(); if (elementPath.getFileExtension().equalsIgnoreCase(IFileFolderConstants.EXT_PROPERTIES)) { result = elementPath; } } if (result != null) { return result; } return new Path(""); //$NON-NLS-1$ }
From source file:org.eclipse.jst.pagedesigner.utils.JavaUtil.java
License:Open Source License
/** * /*from w w w.ja v a 2s . com*/ * @param javaProject * @param parent * @return the path in javaProject or new Path("") if not found on a class path * @author mengbo */ public static IPath getPathOnClasspath(IJavaProject javaProject, Object parent) { IPath result = null; if (javaProject == null || parent == null) { return new Path(""); //$NON-NLS-1$ } IClasspathEntry[] entries = javaProject.readRawClasspath(); IPath classPath = null; if (parent instanceof IResource) { if (((javaProject != null) && !javaProject.isOnClasspath((IResource) parent))) { return new Path(""); //$NON-NLS-1$ } if (parent instanceof IFile) { IPath elementPath = ((IFile) parent).getFullPath(); if (((IFile) parent).getFileExtension().equalsIgnoreCase(IFileFolderConstants.EXT_PROPERTIES)) { int machings = 0; try { for (int i = 0; i < entries.length; i++) { // Determine whether on this classentry's path machings = entries[i].getPath().matchingFirstSegments(elementPath); if (machings > 0) { // Get package name classPath = elementPath.removeFirstSegments(machings).removeLastSegments(1); break; } } // Not on the classpath? if (classPath == null) { return null; } else if (classPath.segmentCount() > 0) result = javaProject.findElement(classPath).getPath().removeFirstSegments(machings) .append(((IFile) parent).getName()); else result = ((IFile) parent).getFullPath().removeFirstSegments(machings); } catch (Exception e) { // Error.DesignerPropertyTool.NatureQuerying = Error in // project java nature querying PDPlugin.getLogger(JavaUtil.class).error("Error.DesignerPropertyTool.NatureQuerying", e); //$NON-NLS-1$ return null; } } } } else if (parent instanceof IJarEntryResource) { IPath elementPath = ((IJarEntryResource) parent).getFullPath(); if (elementPath.getFileExtension().equalsIgnoreCase(IFileFolderConstants.EXT_PROPERTIES)) { result = elementPath; } } if (result != null) { return result; } return new Path(""); //$NON-NLS-1$ }
From source file:org.eclipse.pde.api.tools.util.tests.ApiBaselineManagerTests.java
License:Open Source License
/** * Tests that removing a source file from an API aware project causes the * workspace description to be updated/*from w ww . j a va 2s . c o m*/ */ public void testWPUpdateSourceRemoved() throws Exception { IJavaProject project = getTestingProject(); assertNotNull("The testing project must exist", project); //$NON-NLS-1$ IPackageFragmentRoot root = project.findPackageFragmentRoot( new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute()); assertNotNull("the 'src' package fragment root must exist", root); //$NON-NLS-1$ assertTestSource(root, TESTING_PACKAGE, "TestClass1"); //$NON-NLS-1$ IJavaElement element = project.findElement(new Path("a/b/c/TestClass1.java")); //$NON-NLS-1$ assertNotNull("the class a.b.c.TestClass1 must exist in the project", element); //$NON-NLS-1$ element.getResource().delete(true, new NullProgressMonitor()); IApiDescription desc = getTestProjectApiDescription(); assertNotNull("the testing project api description must exist", desc); //$NON-NLS-1$ IApiAnnotations annot = desc.resolveAnnotations(Factory.typeDescriptor("a.b.c.TestClass1")); //$NON-NLS-1$ assertNull("the annotations for a.b.c.TestClass1 should no longer be present", annot); //$NON-NLS-1$ }
From source file:org.eclipse.pde.api.tools.util.tests.ApiBaselineManagerTests.java
License:Open Source License
/** * Tests that making Javadoc changes to the source file TestClass2 cause the * workspace baseline to be updated.//from w w w . j a va 2s . co m * * This test adds a @noinstantiate tag to the source file TestClass2 */ public void testWPUpdateSourceTypeChanged() throws Exception { IJavaProject project = getTestingProject(); assertNotNull("The testing project must exist", project); //$NON-NLS-1$ IPackageFragmentRoot root = project.findPackageFragmentRoot( new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute()); assertNotNull("the 'src' package fragment root must exist", root); //$NON-NLS-1$ NullProgressMonitor monitor = new NullProgressMonitor(); IPackageFragment fragment = root.getPackageFragment("a.b.c"); //$NON-NLS-1$ FileUtils.importFileFromDirectory(SRC_LOC.append("TestClass2.java").toFile(), fragment.getPath(), monitor); //$NON-NLS-1$ ICompilationUnit element = (ICompilationUnit) project.findElement(new Path("a/b/c/TestClass2.java")); //$NON-NLS-1$ assertNotNull("TestClass2 must exist in the test project", element); //$NON-NLS-1$ updateTagInSource(element, "TestClass2", null, "@noinstantiate", false); //$NON-NLS-1$ //$NON-NLS-2$ IApiDescription desc = getTestProjectApiDescription(); assertNotNull("the testing project api description must exist", desc); //$NON-NLS-1$ IApiAnnotations annot = desc.resolveAnnotations(Factory.typeDescriptor("a.b.c.TestClass2")); //$NON-NLS-1$ assertNotNull("the annotations for a.b.c.TestClass2 cannot be null", annot); //$NON-NLS-1$ assertTrue("there must be a noinstantiate setting for TestClass2", //$NON-NLS-1$ (annot.getRestrictions() & RestrictionModifiers.NO_INSTANTIATE) != 0); assertTrue("there must be a noextend setting for TestClass2", //$NON-NLS-1$ (annot.getRestrictions() & RestrictionModifiers.NO_EXTEND) != 0); }