Java Code Examples for org.eclipse.jdt.core.IJavaProject#findElement()
The following examples show how to use
org.eclipse.jdt.core.IJavaProject#findElement() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Binding2JavaModel.java From lapse-plus with GNU General Public License v3.0 | 6 votes |
/** * Converts the given <code>ITypeBinding</code> into a <code>IType</code> * using the classpath defined by the given Java project. Returns <code>null</code> * if the conversion isn't possible. */ public static IType find(ITypeBinding type, IJavaProject scope) throws JavaModelException { if (type.isPrimitive()) return null; String[] typeElements= getNameComponents(type); IJavaElement element= scope.findElement(getPathToCompilationUnit(type.getPackage(), typeElements[0])); IType candidate= null; if (element instanceof ICompilationUnit) { candidate= ((ICompilationUnit)element).getType(typeElements[0]); } else if (element instanceof IClassFile) { candidate= ((IClassFile)element).getType(); } else if (element == null){ if (type.isMember()) candidate= findType(scope, getFullyQualifiedImportName(type.getDeclaringClass())); else candidate= findType(scope, getFullyQualifiedImportName(type)); } if (candidate == null || typeElements.length == 1) return candidate; return findTypeInType(typeElements, candidate); }
Example 2
Source File: JavaHotCodeReplaceProvider.java From java-debug with Eclipse Public License 1.0 | 6 votes |
/** * Returns the source file associated with the given type, or <code>null</code> * if no source file could be found. * * @param project * the java project containing the classfile * @param qualifiedName * fully qualified name of the type, slash delimited * @param sourceAttribute * debug source attribute, or <code>null</code> if none */ private IResource getSourceFile(IJavaProject project, String qualifiedName, String sourceAttribute) { String name = null; IJavaElement element = null; try { if (sourceAttribute == null) { element = findElement(qualifiedName, project); } else { int i = qualifiedName.lastIndexOf('/'); if (i > 0) { name = qualifiedName.substring(0, i + 1); name = name + sourceAttribute; } else { name = sourceAttribute; } element = project.findElement(new Path(name)); } if (element instanceof ICompilationUnit) { ICompilationUnit cu = (ICompilationUnit) element; return cu.getCorrespondingResource(); } } catch (CoreException e) { logger.log(Level.INFO, "Failed to get source file with exception" + e.getMessage(), e); } return null; }
Example 3
Source File: JavaHotCodeReplaceProvider.java From java-debug with Eclipse Public License 1.0 | 6 votes |
/** * Returns the class file or compilation unit containing the given fully * qualified name in the specified project. All registered java like file * extensions are considered. * * @param qualifiedTypeName * fully qualified type name * @param project * project to search in * @return class file or compilation unit or <code>null</code> * @throws CoreException * if an exception occurs */ public static IJavaElement findElement(String qualifiedTypeName, IJavaProject project) throws CoreException { String path = qualifiedTypeName; final String[] javaLikeExtensions = JavaCore.getJavaLikeExtensions(); int pos = path.indexOf('$'); if (pos != -1) { path = path.substring(0, pos); } path = path.replace('.', IPath.SEPARATOR); path += "."; //$NON-NLS-1$ for (String ext : javaLikeExtensions) { IJavaElement element = project.findElement(new Path(path + ext)); if (element != null) { return element; } } return null; }
Example 4
Source File: CompletionHandlerTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Test public void testCompletion_javadoc() throws Exception { IJavaProject javaProject = JavaCore.create(project); ICompilationUnit unit = (ICompilationUnit) javaProject.findElement(new Path("org/sample/TestJavadoc.java")); unit.becomeWorkingCopy(null); String joinOnCompletion = System.getProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION); try { System.setProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION, "true"); int[] loc = findCompletionLocation(unit, "inner."); CompletionParams position = JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1])); String source = unit.getSource(); changeDocument(unit, source, 3); Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, new NullProgressMonitor()); changeDocument(unit, source, 4); CompletionList list = server.completion(position).join().getRight(); CompletionItem resolved = server.resolveCompletionItem(list.getItems().get(0)).join(); assertEquals("Test ", resolved.getDocumentation().getLeft()); } finally { unit.discardWorkingCopy(); if (joinOnCompletion == null) { System.clearProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION); } else { System.setProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION, joinOnCompletion); } } }
Example 5
Source File: CompletionHandlerTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Test public void testCompletion_nojavadoc() throws Exception { IJavaProject javaProject = JavaCore.create(project); ClientPreferences mockCapabilies = Mockito.mock(ClientPreferences.class); Mockito.when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies); Mockito.when(mockCapabilies.isSupportsCompletionDocumentationMarkdown()).thenReturn(true); ICompilationUnit unit = (ICompilationUnit) javaProject.findElement(new Path("org/sample/Foo5.java")); unit.becomeWorkingCopy(null); try { int[] loc = findCompletionLocation(unit, "nam"); CompletionParams position = JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1])); CompletionList list = server.completion(position).join().getRight(); CompletionItem resolved = server.resolveCompletionItem(list.getItems().get(0)).join(); assertNull(resolved.getDocumentation()); } catch (Exception e) { fail("Unexpected exception " + e); } finally { unit.discardWorkingCopy(); } }
Example 6
Source File: CompletionHandlerTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Test public void testCompletion_javadocMarkdown() throws Exception { IJavaProject javaProject = JavaCore.create(project); ClientPreferences mockCapabilies = Mockito.mock(ClientPreferences.class); Mockito.when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies); Mockito.when(mockCapabilies.isSupportsCompletionDocumentationMarkdown()).thenReturn(true); ICompilationUnit unit = (ICompilationUnit) javaProject.findElement(new Path("org/sample/TestJavadoc.java")); unit.becomeWorkingCopy(null); String joinOnCompletion = System.getProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION); try { System.setProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION, "true"); int[] loc = findCompletionLocation(unit, "inner."); CompletionParams position = JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1])); String source = unit.getSource(); changeDocument(unit, source, 3); Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, new NullProgressMonitor()); changeDocument(unit, source, 4); CompletionList list = server.completion(position).join().getRight(); CompletionItem resolved = server.resolveCompletionItem(list.getItems().get(0)).join(); MarkupContent markup = resolved.getDocumentation().getRight(); assertNotNull(markup); assertEquals(MarkupKind.MARKDOWN, markup.getKind()); assertEquals("Test", markup.getValue()); } finally { unit.discardWorkingCopy(); if (joinOnCompletion == null) { System.clearProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION); } else { System.setProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION, joinOnCompletion); } } }
Example 7
Source File: JavaModelUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * 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. * * @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; }
Example 8
Source File: NLSAccessorConfigurationDialog.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private void validatePropertyPackage() { IPackageFragmentRoot root= fResourceBundlePackage.getSelectedFragmentRoot(); if ((root == null) || !root.exists()) { setInvalid(IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_property_package_root_invalid); return; } IPackageFragment fragment= fResourceBundlePackage.getSelected(); if ((fragment == null) || !fragment.exists()) { setInvalid(IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_property_package_invalid); return; } String pkgName= fragment.getElementName(); IStatus status= JavaConventionsUtil.validatePackageName(pkgName, root); if ((pkgName.length() > 0) && (status.getSeverity() == IStatus.ERROR)) { setInvalid(IDX_BUNDLE_PACKAGE, status.getMessage()); return; } IPath pkgPath= new Path(pkgName.replace('.', IPath.SEPARATOR)).makeRelative(); IJavaProject project= fRefactoring.getCu().getJavaProject(); try { IJavaElement element= project.findElement(pkgPath); if (element == null || !element.exists()) { setInvalid(IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_must_exist); return; } IPackageFragment fPkgFragment= (IPackageFragment) element; if (!PackageBrowseAdapter.canAddPackage(fPkgFragment)) { setInvalid(IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_incorrect_package); return; } if (!PackageBrowseAdapter.canAddPackageRoot((IPackageFragmentRoot) fPkgFragment.getParent())) { setInvalid(IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_incorrect_package); return; } } catch (JavaModelException e) { setInvalid(IDX_BUNDLE_PACKAGE, e.getStatus().getMessage()); return; } setValid(IDX_BUNDLE_PACKAGE); }