List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getKind
int getKind() throws JavaModelException;
From source file:org.jboss.mapper.eclipse.internal.util.JavaUtil.java
License:Open Source License
/** * Returns the first resource folder in the project. If the project is a * maven project, the first resource folder configured will be used. * * @param project the Java project// w ww . j a va 2 s . c o m * * @return the resource root; may be null. */ public static IResource getFirstResourceRoot(IJavaProject project) { if (project == null) { return null; } try { IResource sourceRoot = null; for (IPackageFragmentRoot frag : project.getPackageFragmentRoots()) { if (frag.getKind() == IPackageFragmentRoot.K_SOURCE) { sourceRoot = frag.getUnderlyingResource(); break; } } return sourceRoot; } catch (JavaModelException e) { return null; } }
From source file:org.jboss.mapper.eclipse.internal.util.JavaUtil.java
License:Open Source License
/** * @param project//from ww w .ja va 2 s .c o m * @return the root package for the supplied project */ public static IPackageFragmentRoot getFirstJavaSourceRoot(IJavaProject project) { if (project == null) { return null; } try { IPackageFragmentRoot sourceRoot = null; for (IPackageFragmentRoot frag : project.getPackageFragmentRoots()) { if (frag.getKind() == IPackageFragmentRoot.K_SOURCE) { sourceRoot = frag; break; } } return sourceRoot; } catch (JavaModelException e) { return null; } }
From source file:org.jboss.tools.arquillian.ui.internal.detectors.ArquillianResourceHyperlinkDetector.java
License:Open Source License
private IFile getFile(String resource, ITypeRoot javaElement) { IJavaProject project = javaElement.getJavaProject(); try {//from ww w .ja va2 s .c om IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots(); for (IPackageFragmentRoot root : roots) { if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { IPath path = root.getPath(); path = path.append(resource); path = path.removeFirstSegments(1); IFile file = project.getProject().getFile(path); if (file != null && file.exists()) { return file; } } } } catch (JavaModelException e) { ArquillianUIActivator.log(e); } return null; }
From source file:org.jboss.tools.as.sourcelookup.ui.actions.AttachSourcesActionDelegate.java
License:Open Source License
private void attachSource(IPackageFragmentRoot fragment, IPath newSourcePath) { try {/*from w ww . ja v a 2 s. co m*/ if (fragment == null || fragment.getKind() != IPackageFragmentRoot.K_BINARY) { return; } IPath containerPath = null; IJavaProject jproject = fragment.getJavaProject(); IClasspathEntry entry = fragment.getRawClasspathEntry(); if (entry == null) { entry = JavaCore.newLibraryEntry(fragment.getPath(), null, null); } else { if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { containerPath = entry.getPath(); ClasspathContainerInitializer initializer = JavaCore .getClasspathContainerInitializer(containerPath.segment(0)); IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, jproject); if (initializer == null || container == null) { return; } IStatus status = initializer.getSourceAttachmentStatus(containerPath, jproject); if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) { return; } if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) { return; } entry = JavaModelUtil.findEntryInContainer(container, fragment.getPath()); if (entry == null) { return; } } } IClasspathEntry entry1; CPListElement elem = CPListElement.createFromExisting(entry, null); elem.setAttribute(CPListElement.SOURCEATTACHMENT, newSourcePath); entry1 = elem.getClasspathEntry(); if (entry1.equals(entry)) { return; } IClasspathEntry newEntry = entry1; String[] changedAttributes = { CPListElement.SOURCEATTACHMENT }; BuildPathSupport.modifyClasspathEntry(null, newEntry, changedAttributes, jproject, containerPath, newEntry.getReferencingEntry() != null, new NullProgressMonitor()); } catch (CoreException e) { // ignore } }
From source file:org.jboss.tools.maven.sourcelookup.ui.internal.util.SourceLookupUtil.java
License:Open Source License
public static void attachSource(final IPackageFragmentRoot fragment, final IPath newSourcePath, boolean displayDialog) { try {/*from ww w.java 2s .com*/ if (fragment == null || fragment.getKind() != IPackageFragmentRoot.K_BINARY) { return; } String value = SourceLookupActivator.getDefault().getAutoAddSourceAttachment(); if (SourceLookupActivator.AUTO_ADD_JBOSS_SOURCE_ATTACHMENT_NEVER.equals(value)) { return; } if (displayDialog && SourceLookupActivator.AUTO_ADD_JBOSS_SOURCE_ATTACHMENT_PROMPT.equals(value)) { final boolean[] attach = new boolean[1]; Display.getDefault().syncExec(new Runnable() { @Override public void run() { attach[0] = promptToAddSourceAttachment(fragment.getElementName(), newSourcePath.toString()); } }); if (!attach[0]) { return; } ; } IPath containerPath = null; IJavaProject jproject = fragment.getJavaProject(); IClasspathEntry entry = fragment.getRawClasspathEntry(); if (entry == null) { entry = JavaCore.newLibraryEntry(fragment.getPath(), null, null); } else { if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { containerPath = entry.getPath(); ClasspathContainerInitializer initializer = JavaCore .getClasspathContainerInitializer(containerPath.segment(0)); IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, jproject); if (initializer == null || container == null) { return; } IStatus status = initializer.getSourceAttachmentStatus(containerPath, jproject); if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) { return; } if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) { return; } entry = JavaModelUtil.findEntryInContainer(container, fragment.getPath()); if (entry == null) { return; } } } IClasspathEntry entry1; CPListElement elem = CPListElement.createFromExisting(entry, null); elem.setAttribute(CPListElement.SOURCEATTACHMENT, newSourcePath); entry1 = elem.getClasspathEntry(); if (entry1.equals(entry)) { return; } IClasspathEntry newEntry = entry1; String[] changedAttributes = { CPListElement.SOURCEATTACHMENT }; BuildPathSupport.modifyClasspathEntry(null, newEntry, changedAttributes, jproject, containerPath, newEntry.getReferencingEntry() != null, new NullProgressMonitor()); } catch (CoreException e) { // ignore } }
From source file:org.jboss.tools.pde.sourcelookup.core.internal.utils.ClasspathUtils.java
License:Open Source License
public static boolean isBinaryFragment(IPackageFragmentRoot pfr) { try {//from www. j a v a 2 s. co m return pfr != null && pfr.getKind() == IPackageFragmentRoot.K_BINARY; } catch (JavaModelException e) { return false; } }
From source file:org.jboss.tools.pde.sourcelookup.core.internal.utils.ClasspathUtils.java
License:Open Source License
public static void attachSource(final IPackageFragmentRoot fragment, final IPath newSourcePath, IProgressMonitor monitor) {//from ww w.j a va 2 s . c om try { if (fragment == null || fragment.getKind() != IPackageFragmentRoot.K_BINARY) { return; } if (!Objects.equals(fragment.getSourceAttachmentPath(), newSourcePath)) { // would be so cool if it refreshed the UI automatically fragment.attachSource(newSourcePath, null, monitor); // close the root so that source attachment cache is flushed. Else UI // won't update fragment.close(); // we have to manually fire a delta to notify the UI about the source // attachment. JavaModelManager manager = JavaModelManager.getJavaModelManager(); JavaElementDelta attachedSourceDelta = new JavaElementDelta(fragment.getJavaModel()); attachedSourceDelta.sourceAttached(fragment); manager.getDeltaProcessor().fire(attachedSourceDelta, ElementChangedEvent.POST_CHANGE); } } catch (CoreException e) { // ignore } }
From source file:org.jboss.tools.pde.sourcelookup.ui.internal.actions.DownloadSourcesActionDelegate.java
License:Open Source License
private boolean isBinaryProject(IPackageFragmentRoot fragment) { try {//from w w w.j a v a 2 s .co m return fragment.getKind() == IPackageFragmentRoot.K_BINARY; } catch (JavaModelException e) { return false; } }
From source file:org.jboss.tools.windup.model.domain.ConfigurationResourceUtil.java
License:Open Source License
public static IPackageFragment[] computePackages(ConfigurationElement configuration) { List<IPackageFragment> packages = Lists.newArrayList(); List<IPackageFragment> currentFragments = Lists.newArrayList(getCurrentPackages(configuration)); for (IProject project : getCurrentProjects(configuration)) { JavaProject javaProject = (JavaProject) JavaCore.create(project); try {/*from w w w . j a v a 2s . co m*/ for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) { if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { for (Object obj : root.getChildren()) { if (obj instanceof IPackageFragment) { IPackageFragment fragment = (IPackageFragment) obj; if (fragment.hasChildren() && !currentFragments.contains(fragment)) { packages.add(fragment); } } } } } } catch (JavaModelException e) { Activator.log(e); } } return packages.stream().toArray(IPackageFragment[]::new); }
From source file:org.jboss.tools.ws.creation.core.utils.JBossWSCreationUtils.java
License:Open Source License
public static ICompilationUnit findUnitByFileName(IJavaElement javaElem, String filePath) throws Exception { ICompilationUnit unit = null;/*from w w w . jav a2 s . com*/ if (!javaElem.getOpenable().isOpen()) { javaElem.getOpenable().open(null); } IJavaElement[] elems = null; if (javaElem instanceof IParent) { IParent parent = (IParent) javaElem; elems = parent.getChildren(); } if (elems == null) { return null; } for (IJavaElement elem : elems) { if (elem.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) { IPackageFragmentRoot root = (IPackageFragmentRoot) elem; if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { unit = findUnitByFileName(elem, filePath); if (unit != null) { return unit; } } } else if ((elem.getElementType() == IJavaElement.PACKAGE_FRAGMENT) || (elem.getElementType() == IJavaElement.JAVA_PROJECT)) { unit = findUnitByFileName(elem, filePath); if (unit != null) { return unit; } } else if (elem.getElementType() == IJavaElement.COMPILATION_UNIT) { ICompilationUnit compUnit = (ICompilationUnit) elem; if (compUnit.getPath().toString().equals(filePath)) { compUnit.open(null); return compUnit; } } } return null; }