Java Code Examples for org.eclipse.jdt.internal.corext.util.JavaModelUtil#getPackageFragmentRoot()
The following examples show how to use
org.eclipse.jdt.internal.corext.util.JavaModelUtil#getPackageFragmentRoot() .
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: SourceJarLocations.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
public static File getSourceJarPath(IJavaElement element) throws JavaModelException { IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(element); if (root == null) { return null; } IClasspathEntry entry = root.getResolvedClasspathEntry(); IPath sourceAttachment = entry.getSourceAttachmentPath(); if (sourceAttachment == null) { return null; //No source jar could be found } return sourceAttachment.toFile(); }
Example 2
Source File: ReorgCorrectionsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static boolean canModifyAccessRules(IBinding binding) { IJavaElement element= binding.getJavaElement(); if (element == null) return false; IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element); if (root == null) return false; try { IClasspathEntry classpathEntry= root.getRawClasspathEntry(); if (classpathEntry == null) return false; if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) return true; if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { ClasspathContainerInitializer classpathContainerInitializer= JavaCore.getClasspathContainerInitializer(classpathEntry.getPath().segment(0)); IStatus status= classpathContainerInitializer.getAccessRulesStatus(classpathEntry.getPath(), root.getJavaProject()); return status.isOK(); } } catch (JavaModelException e) { return false; } return false; }
Example 3
Source File: MainMethodSearchEngine.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
@Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object enclosingElement= match.getElement(); if (enclosingElement instanceof IMethod) { // defensive code try { IMethod curr= (IMethod) enclosingElement; if (curr.isMainMethod()) { if (!considerExternalJars()) { IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(curr); if (root == null || root.isArchive()) { return; } } if (!considerBinaries() && curr.isBinary()) { return; } fResult.add(curr.getDeclaringType()); } } catch (JavaModelException e) { JavaPlugin.log(e.getStatus()); } } }
Example 4
Source File: JavaDocLocations.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public static URL getJavadocBaseLocation(IJavaElement element) throws JavaModelException { if (element.getElementType() == IJavaElement.JAVA_PROJECT) { return getProjectJavadocLocation((IJavaProject) element); } IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(element); if (root == null) { return null; } if (root.getKind() == IPackageFragmentRoot.K_BINARY) { IClasspathEntry entry = root.getResolvedClasspathEntry(); URL javadocLocation = getLibraryJavadocLocation(entry); if (javadocLocation != null) { return getLibraryJavadocLocation(entry); } entry = root.getRawClasspathEntry(); switch (entry.getEntryKind()) { case IClasspathEntry.CPE_LIBRARY: case IClasspathEntry.CPE_VARIABLE: return getLibraryJavadocLocation(entry); default: return null; } } else { return getProjectJavadocLocation(root.getJavaProject()); } }
Example 5
Source File: ReorgCorrectionsSubProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public static void getWrongPackageDeclNameProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) throws CoreException { ICompilationUnit cu= context.getCompilationUnit(); // correct package declaration int relevance= cu.getPackageDeclarations().length == 0 ? IProposalRelevance.MISSING_PACKAGE_DECLARATION : IProposalRelevance.CORRECT_PACKAGE_DECLARATION; // bug 38357 proposals.add(new CorrectPackageDeclarationProposal(cu, problem, relevance)); // move to package IPackageDeclaration[] packDecls= cu.getPackageDeclarations(); String newPackName= packDecls.length > 0 ? packDecls[0].getElementName() : ""; //$NON-NLS-1$ IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(cu); IPackageFragment newPack= root.getPackageFragment(newPackName); ICompilationUnit newCU= newPack.getCompilationUnit(cu.getElementName()); boolean isLinked= cu.getResource().isLinked(); if (!newCU.exists() && !isLinked) { String label; if (newPack.isDefaultPackage()) { label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_default_description, BasicElementLabels.getFileName(cu)); } else { String packageLabel= JavaElementLabels.getElementLabel(newPack, JavaElementLabels.ALL_DEFAULT); label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_description, new Object[] { BasicElementLabels.getFileName(cu), packageLabel }); } proposals.add(new ChangeCorrectionProposal(label, CodeActionKind.QuickFix, new MoveCompilationUnitChange(cu, newPack), IProposalRelevance.MOVE_CU_TO_PACKAGE)); } }
Example 6
Source File: JavaDocLocations.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public static URL getJavadocBaseLocation(IJavaElement element) throws JavaModelException { if (element.getElementType() == IJavaElement.JAVA_PROJECT) { return getProjectJavadocLocation((IJavaProject) element); } IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element); if (root == null) { return null; } if (root.getKind() == IPackageFragmentRoot.K_BINARY) { IClasspathEntry entry= root.getResolvedClasspathEntry(); URL javadocLocation= getLibraryJavadocLocation(entry); if (javadocLocation != null) { return getLibraryJavadocLocation(entry); } entry= root.getRawClasspathEntry(); switch (entry.getEntryKind()) { case IClasspathEntry.CPE_LIBRARY: case IClasspathEntry.CPE_VARIABLE: return getLibraryJavadocLocation(entry); default: return null; } } else { return getProjectJavadocLocation(root.getJavaProject()); } }
Example 7
Source File: JavaElementComparator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private IPackageFragmentRoot getPackageFragmentRoot(Object element) { if (element instanceof PackageFragmentRootContainer) { // return first package fragment root from the container PackageFragmentRootContainer cp= (PackageFragmentRootContainer)element; Object[] roots= cp.getPackageFragmentRoots(); if (roots.length > 0) return (IPackageFragmentRoot)roots[0]; // non resolvable - return null return null; } return JavaModelUtil.getPackageFragmentRoot((IJavaElement)element); }
Example 8
Source File: NewContainerWizardPage.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Initializes the source folder field with a valid package fragment root. * The package fragment root is computed from the given Java element. * * @param elem the Java element used to compute the initial package * fragment root used as the source folder */ protected void initContainerPage(IJavaElement elem) { IPackageFragmentRoot initRoot= null; if (elem != null) { initRoot= JavaModelUtil.getPackageFragmentRoot(elem); try { if (initRoot == null || initRoot.getKind() != IPackageFragmentRoot.K_SOURCE) { IJavaProject jproject= elem.getJavaProject(); if (jproject != null) { initRoot= null; if (jproject.exists()) { IPackageFragmentRoot[] roots= jproject.getPackageFragmentRoots(); for (int i= 0; i < roots.length; i++) { if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) { initRoot= roots[i]; break; } } } if (initRoot == null) { initRoot= jproject.getPackageFragmentRoot(jproject.getResource()); } } } } catch (JavaModelException e) { JavaPlugin.log(e); } } setPackageFragmentRoot(initRoot, true); }
Example 9
Source File: ReorgCorrectionsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public static void getWrongPackageDeclNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException { ICompilationUnit cu= context.getCompilationUnit(); boolean isLinked= cu.getResource().isLinked(); // correct package declaration int relevance= cu.getPackageDeclarations().length == 0 ? IProposalRelevance.MISSING_PACKAGE_DECLARATION : IProposalRelevance.CORRECT_PACKAGE_DECLARATION; // bug 38357 proposals.add(new CorrectPackageDeclarationProposal(cu, problem, relevance)); // move to package IPackageDeclaration[] packDecls= cu.getPackageDeclarations(); String newPackName= packDecls.length > 0 ? packDecls[0].getElementName() : ""; //$NON-NLS-1$ IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(cu); IPackageFragment newPack= root.getPackageFragment(newPackName); ICompilationUnit newCU= newPack.getCompilationUnit(cu.getElementName()); if (!newCU.exists() && !isLinked) { String label; if (newPack.isDefaultPackage()) { label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_default_description, BasicElementLabels.getFileName(cu)); } else { String packageLabel= JavaElementLabels.getElementLabel(newPack, JavaElementLabels.ALL_DEFAULT); label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_description, new Object[] { BasicElementLabels.getFileName(cu), packageLabel }); } CompositeChange composite= new CompositeChange(label); composite.add(new CreatePackageChange(newPack)); composite.add(new MoveCompilationUnitChange(cu, newPack)); proposals.add(new ChangeCorrectionProposal(label, composite, IProposalRelevance.MOVE_CU_TO_PACKAGE, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_MOVE))); } }
Example 10
Source File: JavaElementLabelComposer.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
/** * Appends the label for a Java element with the flags as defined by this class. * * @param element the element to render * @param flags the rendering flags. */ public void appendElementLabel(IJavaElement element, long flags) { int type= element.getElementType(); IPackageFragmentRoot root= null; if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT && type != IJavaElement.PACKAGE_FRAGMENT_ROOT) { root= JavaModelUtil.getPackageFragmentRoot(element); } if (root != null && getFlag(flags, JavaElementLabels.PREPEND_ROOT_PATH)) { appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED); fBuilder.append(JavaElementLabels.CONCAT_STRING); } switch (type) { case IJavaElement.METHOD: appendMethodLabel((IMethod) element, flags); break; case IJavaElement.FIELD: appendFieldLabel((IField) element, flags); break; case IJavaElement.LOCAL_VARIABLE: appendLocalVariableLabel((ILocalVariable) element, flags); break; case IJavaElement.TYPE_PARAMETER: appendTypeParameterLabel((ITypeParameter) element, flags); break; case IJavaElement.INITIALIZER: appendInitializerLabel((IInitializer) element, flags); break; case IJavaElement.TYPE: appendTypeLabel((IType) element, flags); break; case IJavaElement.CLASS_FILE: appendClassFileLabel((IClassFile) element, flags); break; case IJavaElement.COMPILATION_UNIT: appendCompilationUnitLabel((ICompilationUnit) element, flags); break; case IJavaElement.PACKAGE_FRAGMENT: appendPackageFragmentLabel((IPackageFragment) element, flags); break; case IJavaElement.PACKAGE_FRAGMENT_ROOT: appendPackageFragmentRootLabel((IPackageFragmentRoot) element, flags); break; case IJavaElement.IMPORT_CONTAINER: case IJavaElement.IMPORT_DECLARATION: case IJavaElement.PACKAGE_DECLARATION: appendDeclarationLabel(element, flags); break; case IJavaElement.JAVA_PROJECT: case IJavaElement.JAVA_MODEL: fBuilder.append(element.getElementName()); break; default: fBuilder.append(element.getElementName()); } if (root != null && getFlag(flags, JavaElementLabels.APPEND_ROOT_PATH)) { fBuilder.append(JavaElementLabels.CONCAT_STRING); appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED); } }
Example 11
Source File: JavadocContentAccess2.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private boolean handleDocRoot(TagElement node) { if (!TagElement.TAG_DOCROOT.equals(node.getTagName())) { return false; } try { String url = null; if (fElement instanceof IMember && ((IMember) fElement).isBinary()) { URL javadocBaseLocation = JavaDocLocations.getJavadocBaseLocation(fElement); if (javadocBaseLocation != null) { url = javadocBaseLocation.toExternalForm(); } } else { IPackageFragmentRoot srcRoot = JavaModelUtil.getPackageFragmentRoot(fElement); if (srcRoot != null) { IResource resource = srcRoot.getResource(); if (resource != null) { /* * Too bad: Browser widget knows nothing about EFS and custom URL handlers, * so IResource#getLocationURI() does not work in all cases. * We only support the local file system for now. * A solution could be https://bugs.eclipse.org/bugs/show_bug.cgi?id=149022 . */ IPath location = resource.getLocation(); if (location != null) { url = location.toFile().toURI().toASCIIString(); } } } } if (url != null) { if (url.endsWith("/")) { //$NON-NLS-1$ url = url.substring(0, url.length() - 1); } fBuf.append(url); return true; } } catch (JavaModelException e) { } return false; }
Example 12
Source File: NewTypeWizardPage.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Hook method that gets called when the enclosing type name has changed. The method * validates the enclosing type and returns the status of the validation. It also updates the * enclosing type model. * <p> * Subclasses may extend this method to perform their own validation. * </p> * * @return the status of the validation */ protected IStatus enclosingTypeChanged() { StatusInfo status= new StatusInfo(); fCurrEnclosingType= null; IPackageFragmentRoot root= getPackageFragmentRoot(); fEnclosingTypeDialogField.enableButton(root != null); if (root == null) { status.setError(""); //$NON-NLS-1$ return status; } String enclName= getEnclosingTypeText(); if (enclName.length() == 0) { status.setError(NewWizardMessages.NewTypeWizardPage_error_EnclosingTypeEnterName); return status; } try { IType type= findType(root.getJavaProject(), enclName); if (type == null) { status.setError(NewWizardMessages.NewTypeWizardPage_error_EnclosingTypeNotExists); return status; } if (type.getCompilationUnit() == null) { status.setError(NewWizardMessages.NewTypeWizardPage_error_EnclosingNotInCU); return status; } if (!JavaModelUtil.isEditable(type.getCompilationUnit())) { status.setError(NewWizardMessages.NewTypeWizardPage_error_EnclosingNotEditable); return status; } fCurrEnclosingType= type; IPackageFragmentRoot enclosingRoot= JavaModelUtil.getPackageFragmentRoot(type); if (!enclosingRoot.equals(root)) { status.setWarning(NewWizardMessages.NewTypeWizardPage_warning_EnclosingNotInSourceFolder); } return status; } catch (JavaModelException e) { status.setError(NewWizardMessages.NewTypeWizardPage_error_EnclosingTypeNotExists); JavaPlugin.log(e); return status; } }
Example 13
Source File: JarPackageWizard.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static boolean isInArchiveOrExternal(IJavaElement element) { IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element); return root != null && (root.isArchive() || root.isExternal()); }
Example 14
Source File: JavadocContentAccess2.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private boolean handleDocRoot(TagElement node) { if (!TagElement.TAG_DOCROOT.equals(node.getTagName())) return false; try { String url= null; if (fElement instanceof IMember && ((IMember) fElement).isBinary()) { URL javadocBaseLocation= JavaUI.getJavadocBaseLocation(fElement); if (javadocBaseLocation != null) { url= javadocBaseLocation.toExternalForm(); } } else { IPackageFragmentRoot srcRoot= JavaModelUtil.getPackageFragmentRoot(fElement); if (srcRoot != null) { IResource resource= srcRoot.getResource(); if (resource != null) { /* * Too bad: Browser widget knows nothing about EFS and custom URL handlers, * so IResource#getLocationURI() does not work in all cases. * We only support the local file system for now. * A solution could be https://bugs.eclipse.org/bugs/show_bug.cgi?id=149022 . */ IPath location= resource.getLocation(); if (location != null) { url= location.toFile().toURI().toASCIIString(); } } } } if (url != null) { if (url.endsWith("/")) { //$NON-NLS-1$ url= url.substring(0, url.length() -1); } fBuf.append(url); return true; } } catch (JavaModelException e) { } return false; }
Example 15
Source File: JavaElementLabelComposer.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Appends the label for a Java element with the flags as defined by this class. * * @param element the element to render * @param flags the rendering flags. */ public void appendElementLabel(IJavaElement element, long flags) { int type= element.getElementType(); IPackageFragmentRoot root= null; if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT && type != IJavaElement.PACKAGE_FRAGMENT_ROOT) root= JavaModelUtil.getPackageFragmentRoot(element); if (root != null && getFlag(flags, JavaElementLabels.PREPEND_ROOT_PATH)) { appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED); fBuffer.append(JavaElementLabels.CONCAT_STRING); } switch (type) { case IJavaElement.METHOD: appendMethodLabel((IMethod) element, flags); break; case IJavaElement.FIELD: appendFieldLabel((IField) element, flags); break; case IJavaElement.LOCAL_VARIABLE: appendLocalVariableLabel((ILocalVariable) element, flags); break; case IJavaElement.TYPE_PARAMETER: appendTypeParameterLabel((ITypeParameter) element, flags); break; case IJavaElement.INITIALIZER: appendInitializerLabel((IInitializer) element, flags); break; case IJavaElement.TYPE: appendTypeLabel((IType) element, flags); break; case IJavaElement.CLASS_FILE: appendClassFileLabel((IClassFile) element, flags); break; case IJavaElement.COMPILATION_UNIT: appendCompilationUnitLabel((ICompilationUnit) element, flags); break; case IJavaElement.PACKAGE_FRAGMENT: appendPackageFragmentLabel((IPackageFragment) element, flags); break; case IJavaElement.PACKAGE_FRAGMENT_ROOT: appendPackageFragmentRootLabel((IPackageFragmentRoot) element, flags); break; case IJavaElement.IMPORT_CONTAINER: case IJavaElement.IMPORT_DECLARATION: case IJavaElement.PACKAGE_DECLARATION: appendDeclarationLabel(element, flags); break; case IJavaElement.JAVA_PROJECT: case IJavaElement.JAVA_MODEL: fBuffer.append(element.getElementName()); break; default: fBuffer.append(element.getElementName()); } if (root != null && getFlag(flags, JavaElementLabels.APPEND_ROOT_PATH)) { int offset= fBuffer.length(); fBuffer.append(JavaElementLabels.CONCAT_STRING); appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED); if (getFlag(flags, JavaElementLabels.COLORIZE)) { fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE); } } }
Example 16
Source File: JavadocHelpContext.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
public JavadocHelpContext(IContext context, Object[] elements) throws JavaModelException { Assert.isNotNull(elements); if (context instanceof IContext2) fTitle= ((IContext2)context).getTitle(); List<IHelpResource> helpResources= new ArrayList<IHelpResource>(); String javadocSummary= null; for (int i= 0; i < elements.length; i++) { if (elements[i] instanceof IJavaElement) { IJavaElement element= (IJavaElement) elements[i]; // if element isn't on the build path skip it if (!ActionUtil.isOnBuildPath(element)) continue; // Create Javadoc summary if (BUG_85721_FIXED) { if (javadocSummary == null) { javadocSummary= retrieveText(element); if (javadocSummary != null) { String elementLabel= JavaElementLabels.getTextLabel(element, JavaElementLabels.ALL_DEFAULT); // FIXME: needs to be NLSed once the code becomes active javadocSummary= "<b>Javadoc for " + elementLabel + ":</b><br>" + javadocSummary; //$NON-NLS-1$//$NON-NLS-2$ } } else { javadocSummary= ""; // no Javadoc summary for multiple selection //$NON-NLS-1$ } } URL url= JavaUI.getJavadocLocation(element, true); if (url == null || doesNotExist(url)) { IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element); if (root != null) { url= JavaUI.getJavadocBaseLocation(element); if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { element= element.getJavaProject(); } else { element= root; } url= JavaUI.getJavadocLocation(element, false); } } if (url != null) { IHelpResource javaResource= new JavaUIHelpResource(element, getURLString(url)); helpResources.add(javaResource); } } } // Add static help topics if (context != null) { IHelpResource[] resources= context.getRelatedTopics(); if (resources != null) { for (int j= 0; j < resources.length; j++) { helpResources.add(resources[j]); } } } fHelpResources= helpResources.toArray(new IHelpResource[helpResources.size()]); if (context != null) fText= context.getText(); if (BUG_85721_FIXED) { if (javadocSummary != null && javadocSummary.length() > 0) { if (fText != null) fText= context.getText() + "<br><br>" + javadocSummary; //$NON-NLS-1$ else fText= javadocSummary; } } if (fText == null) fText= ""; //$NON-NLS-1$ }