List of usage examples for org.eclipse.jdt.core IJavaProject getAllPackageFragmentRoots
IPackageFragmentRoot[] getAllPackageFragmentRoots() throws JavaModelException;
From source file:org.eclipse.xtext.ui.containers.JavaProjectsStateHelper.java
License:Open Source License
protected List<String> getPackageFragmentRootHandles(IJavaProject project) { List<String> result = Lists.newArrayList(); List<String> binaryAndNonLocalFragments = Lists.newArrayList(); try {//w ww . ja va2 s .c o m IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots(); result = Lists.newArrayListWithCapacity(roots.length); for (IPackageFragmentRoot root : roots) { if (root != null && !JavaRuntime.newDefaultJREContainerPath() .isPrefixOf(root.getRawClasspathEntry().getPath())) { if (root.getKind() == IPackageFragmentRoot.K_SOURCE && project.equals(root.getJavaProject())) { // treat local sources with higher priority // see Java behavior in SameClassNamesTest result.add(root.getHandleIdentifier()); } else { binaryAndNonLocalFragments.add(root.getHandleIdentifier()); } } } } catch (JavaModelException e) { if (!e.isDoesNotExist()) { log.error("Cannot find rootHandles in project " + project.getProject().getName(), e); } } result.addAll(binaryAndNonLocalFragments); return result; }
From source file:org.eclipse.xtext.ui.resource.JavaProjectResourceSetInitializer.java
License:Open Source License
protected Map<URI, URI> computePlatformURIMap(IJavaProject javaProject) { HashMap<URI, URI> hashMap = newHashMap(); try {//from w ww . j av a 2s .co m hashMap.putAll(EcorePlugin.computePlatformURIMap(false)); } catch (Exception e) { LOG.error(e.getMessage(), e); } if (!javaProject.exists()) return hashMap; try { IPackageFragmentRoot[] roots = javaProject.getAllPackageFragmentRoots(); for (IPackageFragmentRoot root : roots) { Pair<URI, URI> uriMapping = storage2UriMapper.getURIMapping(root); if (uriMapping != null) { // we could just install the prefix mapping, i.e. platform:resource/my.project/ -> file:/my/path/to/my.project/ // but then we wouldn't be able to load resources when using hosted bundles, because the target path points to the bin folder. // so instead we install qualified file mappings, which also makes normalization faster (i.e. just a lookup in a map instead of testing prefix URIs) // Map<URI, IStorage> mapping = storage2UriMapper.getAllEntries(root); for (URI key : mapping.keySet()) { IStorage storage = mapping.get(key); URI physicalURI = null; if (storage instanceof IFile) { physicalURI = URI.createPlatformResourceURI(storage.getFullPath().toString(), true); } else { physicalURI = key.replacePrefix(uriMapping.getFirst(), uriMapping.getSecond()); } hashMap.put(key, physicalURI); if (key.isPlatformResource()) { URI pluginURI = URI.createPlatformPluginURI(key.toPlatformString(false), false); hashMap.put(pluginURI, physicalURI); } } } } } catch (JavaModelException e) { LOG.error(e.getMessage(), e); } catch (NoClassDefFoundError e) { // guard against broken Eclipse installations / bogus project configuration LOG.error(e.getMessage(), e); } return hashMap; }
From source file:org.eclipse.xtext.ui.resource.XtextResourceSetProvider.java
License:Open Source License
protected Map<URI, URI> computePlatformURIMap(IJavaProject javaProject) { HashMap<URI, URI> hashMap = newHashMap(); try {//from w ww . j a va 2s . c om hashMap.putAll(EcorePlugin.computePlatformURIMap(false)); } catch (Exception e) { LOG.error(e.getMessage(), e); } if (!javaProject.exists()) return hashMap; try { IPackageFragmentRoot[] roots = javaProject.getAllPackageFragmentRoots(); for (IPackageFragmentRoot root : roots) { Pair<URI, URI> uriMapping = storage2UriMapper.getURIMapping(root); if (uriMapping != null) { // we could just install the prefix mapping, i.e. platform:resource/my.project/ -> file:/my/path/to/my.project/ // but then we wouldn't be able to load resources when using hosted bundles, because the target path points to the bin folder. // so instead we install qualified file mappings, which also makes normalization faster (i.e. just a lookup in a map instead of testing prefix URIs) // Map<URI, IStorage> mapping = storage2UriMapper.getAllEntries(root); for (URI key : mapping.keySet()) { IStorage storage = mapping.get(key); URI physicalURI = null; if (storage instanceof IFile) { physicalURI = URI.createPlatformResourceURI(storage.getFullPath().toString(), true); } else { physicalURI = key.replacePrefix(uriMapping.getFirst(), uriMapping.getSecond()); } hashMap.put(key, physicalURI); if (key.isPlatformResource()) { URI pluginURI = URI.createPlatformPluginURI(key.toPlatformString(false), false); hashMap.put(pluginURI, physicalURI); } } } } final IProject project = javaProject.getProject(); for (IProject iProject : project.getWorkspace().getRoot().getProjects()) { if (iProject.isAccessible()) { IPath location = iProject.getLocation(); if (location != null) { // append a trailing slash so that URI.isPrefix is true. hashMap.put(URI.createPlatformResourceURI(iProject.getName() + "/", true), URI.createFileURI(location.toFile().getPath() + "/")); } } } } catch (JavaModelException e) { LOG.error(e.getMessage(), e); } return hashMap; }
From source file:org.eclipse.xtext.ui.util.JdtClasspathUriResolver.java
License:Open Source License
protected URI findResourceInWorkspace(IJavaProject javaProject, URI classpathUri) throws CoreException { if (javaProject.exists()) { String packagePath = classpathUri.trimSegments(1).path(); String fullPath = classpathUri.path(); String fileName = classpathUri.lastSegment(); IPath filePath = new Path(fileName); String packageName = isEmpty(packagePath) ? "" : packagePath.substring(1).replace('/', '.'); for (IPackageFragmentRoot packageFragmentRoot : javaProject.getAllPackageFragmentRoots()) { IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(packageName); if (packageFragment.exists()) { IResource packageFragmentResource = packageFragment.getResource(); if (packageFragmentResource instanceof IContainer) { IFile file = ((IContainer) packageFragmentResource).getFile(filePath); if (file.exists()) return createPlatformResourceURI(file); } else { // jar file or external class folder if (packageFragmentRoot.isArchive()) { // jar file Object[] nonJavaResources = getNonJavaResources(packageFragmentRoot, packageFragment); for (Object nonJavaResource : nonJavaResources) { IJarEntryResource jarEntryResource = (IJarEntryResource) nonJavaResource; if (fullPath.equals(jarEntryResource.getFullPath().toString())) { IResource packageFragmentRootResource = packageFragmentRoot.getResource(); if (packageFragmentRootResource != null) { // we have a resource - use nested platform/resource URI packageFragmentRootURI = createPlatformResourceURI( packageFragmentRootResource); URI result = createArchiveURI(packageFragmentRootURI, fullPath); return result; } else { // no resource - use file uri IPath packageFragmentRootPath = packageFragmentRoot.getPath(); URI packageFragmentRootURI = URI .createFileURI(packageFragmentRootPath.toString()); URI result = createArchiveURI(packageFragmentRootURI, fullPath); return result; }// w ww . ja v a 2s . co m } } } else if (packageFragmentRoot.isExternal()) { // external class folder Object[] nonJavaResources = getNonJavaResources(packageFragmentRoot, packageFragment); for (Object nonJavaResource : nonJavaResources) { IJarEntryResource jarEntryResource = (IJarEntryResource) nonJavaResource; if (fileName.equals(jarEntryResource.getName())) { IResource packageFragmentRootResource = ((ExternalPackageFragmentRoot) packageFragmentRoot) .resource(); IPath absolutePath = packageFragmentRootResource.getFullPath(); absolutePath = absolutePath.append(fullPath); return createPlatformResourceURI(absolutePath); } } } } } } // not found in a source folder - look for a resource relative to project root // of this project or one of its dependencies URI result = findResourceInProjectRoot(javaProject, classpathUri.path(), Sets.<String>newHashSet()); if (result != null) { return result; } } return classpathUri; }
From source file:org.entirej.ide.ui.utils.JavaAccessUtils.java
License:Apache License
public static IPackageFragmentRoot[] getRoots(IJavaProject project, boolean ignoreJRE) { ArrayList<IPackageFragmentRoot> result = new ArrayList<IPackageFragmentRoot>(); try {/* w w w . j a va 2 s . c om*/ IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots(); for (int i = 0; i < roots.length; i++) { if (!ignoreJRE || !isJRELibrary(roots[i])) { result.add(roots[i]); } } } catch (JavaModelException e) { } return result.toArray(new IPackageFragmentRoot[result.size()]); }
From source file:org.fastcode.util.SourceUtil.java
License:Open Source License
/** * * @param project//from w w w.j av a 2 s . c om * @param paramType * @return * @throws Exception */ public static Object getElementFromProject(final IJavaProject project, final String paramType, final String filter) throws Exception { IType type = project.findType(paramType); if (type != null && type.exists()) { return type; } IFile file = project.getProject().getFile(paramType); if (file != null && file.exists()) { return file; } final IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots(); int nClasses = 0; for (final IPackageFragmentRoot root : roots) { if (root.getElementName() != null && filter != null && root.getElementName().endsWith(filter)) { continue; } final IJavaElement[] elements = root.getChildren(); for (int i = 0; i < elements.length; i++) { final IJavaElement element = elements[i]; final IPackageFragment fragment = (IPackageFragment) element.getAdapter(IPackageFragment.class); if (fragment == null) { continue; } // TODO redo this logic later with JavaCore.create(file) String path = fragment.getPath().toString(); if (path.length() > root.getPath().toString().length()) { path = path.substring(root.getPath().toString().length() + 1); } final String newpath = path.replaceAll(FORWARD_SLASH, "\\."); if (paramType.startsWith(newpath)) { String tmpName = paramType.replaceFirst(newpath, path); tmpName = tmpName.replaceFirst("\\.", FORWARD_SLASH); tmpName = root.getPath().toString() + FORWARD_SLASH + tmpName; tmpName = tmpName.substring(project.getProject().getName().length() + 1); final IResource resource = project.getProject().findMember(tmpName); // JavaCore.create(file); if (resource != null && resource.exists()) { return resource; } } final IJavaElement fes[] = fragment.getChildren(); for (int j = 0; j < fes.length; j++) { final String className = fes[j].getElementName(); final int elementType = fes[j].getElementType(); if (elementType == TYPE) { type = (IType) fes[j]; } else if (elementType == COMPILATION_UNIT) { type = ((ICompilationUnit) fes[j]).findPrimaryType(); } else if (elementType == CLASS_FILE) { type = ((IClassFile) fes[j]).findPrimaryType(); } else if (elementType == IResource.FILE) { file = (IFile) fes[j]; } if (type != null && type.getFullyQualifiedName().equals(paramType)) { return type; } else if (file != null && paramType.equals(fragment.getElementName() + DOT + fes[j].getElementName())) { return file; } nClasses++; } } } final String projectName = project.getElementName(); return null; }
From source file:org.fastcode.util.SourceUtil.java
License:Open Source License
/** * * @param project// w w w . j a v a 2 s .c o m * @return * @throws Exception */ public static IPackageFragmentRoot[] getPackageRootsFromProject(final IJavaProject project) throws Exception { final IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots(); for (final IPackageFragmentRoot root : roots) { /* * System.out.println("Package name " + root.getElementName()); * System.out.println("Package type " + root.getElementType()); * System.out.println("Package path " + root.getPath()); */ } return roots; }
From source file:org.fastcode.util.SourceUtil.java
License:Open Source License
/** * * @param packageName/*from w w w . jav a2 s . c o m*/ * @return * @throws JavaModelException */ public static IPackageFragment getPackageFragmentFromWorkspace(final String packageName) throws JavaModelException { final IProject[] javaProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); for (final IProject project : javaProjects) { final IJavaProject javaProj = JavaCore.create(project); if (javaProj == null || !javaProj.exists() || !javaProj.isOpen()) { continue; } for (final IPackageFragmentRoot packageFragmentRoot : javaProj.getAllPackageFragmentRoots()) { final IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(packageName); if (packageFragment != null && packageFragment.exists()) { return packageFragment; } } } return null; }
From source file:org.fastcode.util.SourceUtil.java
License:Open Source License
/** * * @param srcPath/* ww w .j a va 2 s . c o m*/ * @param checkFullPath * * @return * @throws JavaModelException */ public static IPackageFragmentRoot getJavaElementFromSourcePathFromProject(final String srcPath, final IJavaProject javaProject, final boolean checkFullPath) throws JavaModelException { for (final IPackageFragmentRoot packageFragmentRoot : javaProject.getAllPackageFragmentRoots()) { final String rootPath = packageFragmentRoot.getPath().toString(); if (srcPath.equals( checkFullPath ? rootPath : rootPath.substring(javaProject.getElementName().length() + 1))) { return packageFragmentRoot; } } return null; }
From source file:org.fastcode.util.SourceUtil.java
License:Open Source License
/** * * @param project//w w w . j ava 2s .c o m * @return */ public static String[][] getSourcePathsForProject(final String project) { final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); final List<String> paths = new ArrayList<String>(); try { for (final IProject prjct : projects) { if (project == null || prjct.getName().equals(project)) { final IJavaProject javaProject = JavaCore.create(prjct); if (javaProject == null || !javaProject.exists()) { continue; } if (isProjectBinary(javaProject)) { continue; } for (final IPackageFragmentRoot packageFragmentRoot : javaProject .getAllPackageFragmentRoots()) { if (packageFragmentRoot == null || !packageFragmentRoot.exists() || packageFragmentRoot.isArchive() || packageFragmentRoot.isExternal()) { continue; } if (!packageFragmentRoot.getParent().equals(javaProject)) { // discard // roots // which // come // from // another // project. continue; } final String fullSrcPath = packageFragmentRoot.getPath().toString(); final String srcPath = fullSrcPath .replaceFirst(FORWARD_SLASH + javaProject.getElementName(), EMPTY_STR); if (!paths.contains(srcPath)) { paths.add(srcPath); } } } } } catch (final Exception ex) { ex.printStackTrace(); } return getStringArrayFromList(paths, true); }