List of usage examples for org.eclipse.jdt.core IJavaProject getPackageFragmentRoots
IPackageFragmentRoot[] getPackageFragmentRoots() throws JavaModelException;
From source file:org.eclipse.imp.wizards.WizardUtilities.java
License:Open Source License
/** * Returns the string representation of the workbench relative * path to the source location of the given project. * /* ww w . j av a2s . com*/ * Note: This method effectively duplicates one defined in IMPWizard. * That one is intended for use by wizards, where it is commonly needed. * This one is intended for users that are not wizards. * * @param project An IProject that also presumably represents * an IJavaProject * @return The string representation of the path, relative * to the workbench, to the project source location */ public static String getProjectSourceLocation(IProject project) { try { if (project == null) return null; JavaModel jm = JavaModelManager.getJavaModelManager().getJavaModel(); IJavaProject jp = jm.getJavaProject(project); if (jp == null) return null; else { IPackageFragmentRoot[] roots = jp.getPackageFragmentRoots(); for (int i = 0; i < roots.length; i++) { if (roots[i].getCorrespondingResource() instanceof IFolder) { IPath lcnPath = roots[i].getPath(); lcnPath = lcnPath.removeFirstSegments(1); String lcn = lcnPath.toString(); if (lcn.startsWith("/")) lcn = lcn.substring(1); if (!lcn.endsWith("/")) lcn = lcn + "/"; return lcn; } } } } catch (JavaModelException e) { } return null; }
From source file:org.eclipse.incquery.patternlanguage.emf.ui.validation.GenmodelBasedEMFPatternLanguageJavaValidator.java
License:Open Source License
protected String getActualPackageName(PatternModel model) { URI fileURI = model.eResource().getURI(); for (Pair<IStorage, IProject> storage : storage2UriMapper.getStorages(fileURI)) { if (storage.getFirst() instanceof IFile) { IPath fileWorkspacePath = storage.getFirst().getFullPath(); IJavaProject javaProject = JavaCore.create(storage.getSecond()); if (javaProject != null && javaProject.exists() && javaProject.isOpen()) { try { for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) { if (!root.isArchive() && !root.isExternal()) { IResource resource = root.getResource(); if (resource != null) { IPath sourceFolderPath = resource.getFullPath(); if (sourceFolderPath.isPrefixOf(fileWorkspacePath)) { IPath classpathRelativePath = fileWorkspacePath .makeRelativeTo(sourceFolderPath); return classpathRelativePath.removeLastSegments(1).toString().replace("/", "."); }/*from ww w . ja v a 2 s . co m*/ } } } } catch (JavaModelException e) { logger.error("Error resolving package declaration for Pattern Model", e); } } } } return null; }
From source file:org.eclipse.jem.workbench.utility.JemProjectUtilities.java
License:Open Source License
/** * Get all source package fragment roots. * //from w ww . j av a 2 s .c o m * @param javaProj * @return source package fragment roots * @throws JavaModelException * * @since 1.0.0 */ public static List getSourcePackageFragmentRoots(IJavaProject javaProj) throws JavaModelException { List result = new ArrayList(); IPackageFragmentRoot[] roots = javaProj.getPackageFragmentRoots(); for (int i = 0; i < roots.length; i++) { IPackageFragmentRoot root = roots[i]; if (root.getKind() == IPackageFragmentRoot.K_SOURCE) result.add(root); } return result; }
From source file:org.eclipse.jet.taglib.java.JavaActionsUtil.java
License:Open Source License
/** * Find the a container corresponding the the given package in the specified project. * Traverse all project source package roots, looking for an existing instance of a package fragment corresponding to packageName. * If not found, return the first non-existant fragment found. * If the project has no source package roots, then null is returned. * @param jProject the Java project to search * @param packageName the Java package for which a container is sought. * @return the container corresponding to the package, or null. * @throws JavaModelException if the package roots or root kinds cannot be determined. *//*from w w w . ja va 2 s . c o m*/ private static IContainer findOrCreateJavaPackage(IJavaProject jProject, String packageName) throws JavaModelException { IPackageFragment firstNonExistantFragment = null; // Traverse package roots, looking for an existing instance of the package fragment corresponding to packageName. // Otherwise, return the first non-existant fragment final IPackageFragmentRoot[] roots = jProject.getPackageFragmentRoots(); for (int i = 0; i < roots.length; i++) { if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) { IPackageFragment fragment = roots[i].getPackageFragment(packageName); if (fragment.exists()) { return (IContainer) fragment.getResource(); } else if (firstNonExistantFragment == null) { firstNonExistantFragment = fragment; } } } return firstNonExistantFragment != null ? (IContainer) firstNonExistantFragment.getResource() : null; }
From source file:org.eclipse.jpt.common.core.internal.utility.JavaProjectTools.java
License:Open Source License
private static Iterable<IPackageFragmentRoot> getPackageFragmentRoots_(IJavaProject javaProject) throws JavaModelException { return IterableTools.iterable(javaProject.getPackageFragmentRoots()); }
From source file:org.eclipse.jpt.jpa.gen.internal.PackageGenerator.java
License:Open Source License
private IPackageFragmentRoot getDefaultJavaSourceLocation(IJavaProject jproject, String sourceFolder) { IPackageFragmentRoot defaultSrcPath = null; if (jproject != null && jproject.exists()) { try {/*from ww w.j av a 2s . c om*/ IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots(); for (int i = 0; i < roots.length; i++) { if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) { if (defaultSrcPath == null) { defaultSrcPath = roots[i]; } String path = roots[i].getPath().toString(); if (path.equals('/' + sourceFolder)) { return roots[i]; } } } } catch (JavaModelException e) { JptJpaGenPlugin.instance().logError(e); } } return defaultSrcPath; }
From source file:org.eclipse.jpt.jpa.ui.internal.wizards.entity.data.model.EntityDataModelProvider.java
License:Open Source License
protected IContainer getDefaultJavaSourceContainer() { JpaProject jpaProject = getTargetJpaProject(); if (jpaProject == null) { return null; }// w ww .j ava 2 s. co m IJavaProject javaProject = jpaProject.getJavaProject(); try { for (IPackageFragmentRoot pfr : javaProject.getPackageFragmentRoots()) { if (pfr.getKind() == IPackageFragmentRoot.K_SOURCE) { return (IContainer) pfr.getUnderlyingResource(); } } } catch (JavaModelException jme) { // fall through JptJpaUiPlugin.instance().logError(jme); } return null; }
From source file:org.eclipse.jpt.jpa.ui.internal.wizards.entity.data.model.EntityDataModelProvider.java
License:Open Source License
protected IContainer getJavaSourceContainer() { String containerFullPath = getStringProperty(SOURCE_FOLDER); JpaProject jpaProject = getTargetJpaProject(); if (jpaProject == null) { return null; }/*www.j a v a 2 s . c o m*/ IJavaProject javaProject = jpaProject.getJavaProject(); try { for (IPackageFragmentRoot pfr : javaProject.getPackageFragmentRoots()) { if (pfr.getKind() == IPackageFragmentRoot.K_SOURCE) { IContainer container = (IContainer) pfr.getUnderlyingResource(); if (container.getFullPath().equals(new Path(containerFullPath))) { return container; } } } } catch (JavaModelException jme) { // fall through JptJpaUiPlugin.instance().logError(jme); } return null; }
From source file:org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil.java
License:Open Source License
/** * Returns all resolved classpath entries for the specified Java project that * have one of the special WTP classpath component dependency attributes. * // ww w . j a va 2 s . c o m * @param javaProject Java project whose component classpath dependencies are being retrieved. * @param isWebApp True if the target project is associated with a web project. * @param onlyValid If true, only valid dependencies will be returned. If false, the raw entry must be valid but the * resolved can be invalid. * @return Map from IClasspathEntry to IClasspathAttribute for classpath component dependencies. * @throws CoreException Thrown if an error is encountered accessing the unresolved classpath. */ public static Map<IClasspathEntry, IClasspathAttribute> getComponentClasspathDependencies( final IJavaProjectLite javaProjectLite, final boolean isLegacyJ2EE, final boolean onlyValid) throws CoreException { final ClasspathDependencyValidatorData data = new ClasspathDependencyValidatorData( javaProjectLite.getProject()); final boolean isWebApp = JavaEEProjectUtilities.isDynamicWebProject(javaProjectLite.getProject()); // get the raw entries final Map<IClasspathEntry, IClasspathAttribute> referencedRawEntries = getRawComponentClasspathDependencies( javaProjectLite, DependencyAttributeType.CLASSPATH_COMPONENT_DEPENDENCY, isLegacyJ2EE); final Map<IClasspathEntry, IClasspathAttribute> validRawEntries = new HashMap<IClasspathEntry, IClasspathAttribute>(); final Map<IClasspathEntry, IClasspathAttribute> validRawClassPathContainerEntries = new HashMap<IClasspathEntry, IClasspathAttribute>(); // filter out non-valid referenced raw entries final Iterator<IClasspathEntry> i = referencedRawEntries.keySet().iterator(); while (i.hasNext()) { final IClasspathEntry entry = i.next(); final IClasspathAttribute attrib = referencedRawEntries.get(entry); if (isValid(entry, attrib, isWebApp, javaProjectLite.getProject(), data)) { if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { //Put in a separate map the classpath container entries, since they will be handled differently validRawClassPathContainerEntries.put(entry, attrib); } else { validRawEntries.put(entry, attrib); } } } // if we have no valid raw entries, return empty map if (validRawEntries.isEmpty() && validRawClassPathContainerEntries.isEmpty()) { return Collections.emptyMap(); } // XXX Would like to replace the code below with use of a public JDT API that returns // the raw IClasspathEntry for a given resolved IClasspathEntry (see see https://bugs.eclipse.org/bugs/show_bug.cgi?id=183995) // The code must currently leverage IPackageFragmentRoot to determine this // mapping and, because IPackageFragmentRoots do not maintain IClasspathEntry data, a prior // call is needed to getResolvedClasspath() and the resolved IClasspathEntries have to be stored in a Map from IPath-to-IClasspathEntry to // support retrieval using the resolved IPackageFragmentRoot // retrieve the resolved classpath //TODO this call to javaProject needs to be removed. Need to figure out what exactly this is attempting to do. IJavaProject javaProject = JavaCore.create(javaProjectLite.getProject()); //TODO this call to javaProject needs to be removed. Need to figure out what exactly this is attempting to do. final IClasspathEntry[] entries = javaProject.getResolvedClasspath(true); final Map<IPath, IClasspathEntry> pathToResolvedEntry = new HashMap<IPath, IClasspathEntry>(); // store in a map from path to entry for (int j = 0; j < entries.length; j++) { pathToResolvedEntry.put(entries[j].getPath(), entries[j]); } //Gather all resolved entries from the package roots and the classpath containers final Map<IClasspathEntry, IClasspathAttribute> resolvedEntries = new LinkedHashMap<IClasspathEntry, IClasspathAttribute>(); // grab all IPackageFragmentRoots // TODO this ignores project cp entries; can easily add in the raw project cp entries, however, do not have a good way to // map project cp entries resolved from cp containers back to the corresponding raw entry (and thereby determine if the // entry has the publish/export attribute) //TODO this call to javaProject needs to be removed. Need to figure out what exactly this is attempting to do. final IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots(); for (IPackageFragmentRoot root : roots) { final IClasspathEntry rawEntry = root.getRawClasspathEntry(); // is the raw entry valid? IClasspathAttribute attrib = validRawEntries.get(rawEntry); if (attrib == null) { continue; } final IPath pkgFragPath = root.getPath(); final IClasspathEntry resolvedEntry = pathToResolvedEntry.get(pkgFragPath); resolvedEntries.put(resolvedEntry, attrib); } // Add entries coming from classpath containers to the list of resolved entries for (Map.Entry<IClasspathEntry, IClasspathAttribute> entry : validRawClassPathContainerEntries.entrySet()) { IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(entry.getKey().getPath(), javaProject); if (classpathContainer != null) { IClasspathEntry[] classpathContainerEntries = classpathContainer.getClasspathEntries(); if (classpathContainerEntries != null) { for (int j = 0; j < classpathContainerEntries.length; j++) { resolvedEntries.put(classpathContainerEntries[j], entry.getValue()); } } } } //Setup the final result final Map<IClasspathEntry, IClasspathAttribute> referencedEntries = new LinkedHashMap<IClasspathEntry, IClasspathAttribute>(); for (Map.Entry<IClasspathEntry, IClasspathAttribute> mapEntry : resolvedEntries.entrySet()) { final IClasspathEntry resolvedEntry = mapEntry.getKey(); IClasspathAttribute attrib = mapEntry.getValue(); final IClasspathAttribute resolvedAttrib = checkForComponentDependencyAttribute(resolvedEntry, DependencyAttributeType.DEPENDENCY_OR_NONDEPENDENCY, isLegacyJ2EE); // attribute for the resolved entry must either be unspecified or it must be the // dependency attribute for it to be included if (resolvedAttrib == null || resolvedAttrib.getName().equals(CLASSPATH_COMPONENT_DEPENDENCY)) { // filter out resolved entry if it doesn't pass the validation rules if (!onlyValid || isValid(resolvedEntry, resolvedAttrib != null ? resolvedAttrib : attrib, isWebApp, javaProjectLite.getProject(), data)) { if (resolvedAttrib != null) { // if there is an attribute on the sub-entry, use that attrib = resolvedAttrib; } referencedEntries.put(resolvedEntry, attrib); } } } return referencedEntries; }
From source file:org.eclipse.jst.j2ee.internal.common.operations.JavaModelUtil.java
License:Open Source License
/** * Finds a type by its qualified type name (dot separated). * @param jproject The java project to search in * @param fullyQualifiedName The fully qualified name (type name with enclosing type names and package (all separated by dots)) * @return The type found, or null if not existing *//*from w w w . jav a 2 s . c o m*/ public static IType findType(IJavaProject jproject, String fullyQualifiedName) throws JavaModelException { //workaround for bug 22883 IType type = jproject.findType(fullyQualifiedName); if (type != null) return type; IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots(); for (int i = 0; i < roots.length; i++) { IPackageFragmentRoot root = roots[i]; type = findType(root, fullyQualifiedName); if (type != null && type.exists()) return type; } return null; }