Java Code Examples for org.eclipse.jdt.core.IJavaProject#getPackageFragmentRoots()

The following examples show how to use org.eclipse.jdt.core.IJavaProject#getPackageFragmentRoots() . 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: JarEntryEditorInputFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IPackageFragmentRoot fuzzyResolveRoot(IPackageFragmentRoot restoredRoot) throws JavaModelException {
	IJavaProject project= restoredRoot.getJavaProject();
	String rootName= restoredRoot.getElementName();
	int versionSepIndex= rootName.indexOf('_');
	if (versionSepIndex > 0) {
		// try stripping plug-in version number
		String prefix= rootName.substring(0, versionSepIndex);
		IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
		for (int i= 0; i < roots.length; i++) {
			if (roots[i].getElementName().startsWith(prefix)) {
				return roots[i];
			}
		}
	}
	return null;
}
 
Example 2
Source File: JavaUtils.java    From developer-studio with Apache License 2.0 6 votes vote down vote up
public static IPackageFragmentRoot[] getSourceFoldersForProject(IProject project) throws JavaModelException{
IJavaProject p = JavaCore.create(project);
IPackageFragmentRoot[] packageFragmentRoots = p.getPackageFragmentRoots();
ArrayList<IPackageFragmentRoot> jarClassPaths = new ArrayList<IPackageFragmentRoot>();
for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
	if(isWebApp && packageFragmentRoot.isArchive()){
       if (packageFragmentRoot.getRawClasspathEntry().getEntryKind()==IClasspathEntry.CPE_SOURCE){
       	jarClassPaths.add(packageFragmentRoot);
       }
 }else if(!isWebApp){
	 if (packageFragmentRoot.getRawClasspathEntry().getEntryKind()==IClasspathEntry.CPE_SOURCE){
        	jarClassPaths.add(packageFragmentRoot);
        }
 }
      }
return jarClassPaths.toArray(new IPackageFragmentRoot[]{});
  }
 
Example 3
Source File: JavaBrowsingContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected Object[] getPackageFragmentRoots(IJavaProject project) throws JavaModelException {
	if (!project.getProject().isOpen())
		return NO_CHILDREN;

	IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
	List<IJavaElement> list= new ArrayList<IJavaElement>(roots.length);
	// filter out package fragments that correspond to projects and
	// replace them with the package fragments directly
	for (int i= 0; i < roots.length; i++) {
		IPackageFragmentRoot root= roots[i];
		if (!root.isExternal()) {
			IJavaElement[] children= root.getChildren();
			for (int k= 0; k < children.length; k++)
				list.add(children[k]);
		}
		else if (hasChildren(root)) {
			list.add(root);
		}
	}
	return concatenate(list.toArray(), project.getNonJavaResources());
}
 
Example 4
Source File: TypeCheckingEvolution.java    From JDeodorant with MIT License 6 votes vote down vote up
private List<TypeCheckElimination> generateTypeCheckEliminationsWithinJavaProject(IJavaProject javaProject, TypeCheckElimination elimination) {
	List<TypeCheckElimination> typeCheckEliminations = new ArrayList<TypeCheckElimination>();
	try {
		IPackageFragmentRoot[] iPackageFragmentRoots = javaProject.getPackageFragmentRoots();
		for(IPackageFragmentRoot iPackageFragmentRoot : iPackageFragmentRoots) {
			IJavaElement[] children = iPackageFragmentRoot.getChildren();
			for(IJavaElement child : children) {
				if(child.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
					IPackageFragment iPackageFragment = (IPackageFragment)child;
					ICompilationUnit[] iCompilationUnits = iPackageFragment.getCompilationUnits();
					for(ICompilationUnit iCompilationUnit : iCompilationUnits) {
						ASTParser parser = ASTParser.newParser(ASTReader.JLS);
				        parser.setKind(ASTParser.K_COMPILATION_UNIT);
				        parser.setSource(iCompilationUnit);
				        parser.setResolveBindings(true); // we need bindings later on
				        CompilationUnit compilationUnit = (CompilationUnit)parser.createAST(null);
						typeCheckEliminations.addAll(generateTypeCheckEliminationsWithinCompilationUnit(compilationUnit, elimination));
					}
				}
			}
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
	return typeCheckEliminations;
}
 
Example 5
Source File: LibrariesWorkbookPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IPackageFragmentRoot getSelectedPackageFragmentRoot() {
	final List<Object> elements= fLibrariesList.getSelectedElements();
	if (elements.size() == 1) {
		final Object object= elements.get(0);
		if (object instanceof CPListElement) {
			final CPListElement element= (CPListElement) object;
			final IClasspathEntry entry= element.getClasspathEntry();
			if (JarImportWizard.isValidClassPathEntry(entry)) {
				final IJavaProject project= element.getJavaProject();
				if (project != null) {
					try {
						final IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
						for (int index= 0; index < roots.length; index++) {
							if (entry.equals(roots[index].getRawClasspathEntry()))
								return roots[index];
						}
					} catch (JavaModelException exception) {
						JavaPlugin.log(exception);
					}
				}
			}
		}
	}
	return null;
}
 
Example 6
Source File: JavadocProjectContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Object[] getPackageFragmentRoots(IJavaProject project) throws JavaModelException {
	ArrayList<Object> result= new ArrayList<Object>();

	IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
	for (int i= 0; i < roots.length; i++) {
		IPackageFragmentRoot root= roots[i];
		if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
			if (root.getPath().equals(root.getJavaProject().getPath())) {
				Object[] packageFragments= getPackageFragments(root);
				for (int k= 0; k < packageFragments.length; k++) {
					result.add(packageFragments[k]);
				}
			} else {
				result.add(root);
			}
		}
	}
	return result.toArray();
}
 
Example 7
Source File: FatJarPackageWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static IPackageFragmentRoot[] findRootsInProject(IPath entry, IJavaProject project) {
	ArrayList<IPackageFragmentRoot> result= new ArrayList<IPackageFragmentRoot>();

	try {
		IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
		for (int i= 0; i < roots.length; i++) {
			IPackageFragmentRoot packageFragmentRoot= roots[i];
			if (isRootAt(packageFragmentRoot, entry))
				result.add(packageFragmentRoot);
		}
	} catch (Exception e) {
		JavaPlugin.log(e);
	}

	return result.toArray(new IPackageFragmentRoot[result.size()]);
}
 
Example 8
Source File: ElementTypeTeller.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Checks a package if it belong to an existing model. Searches for a
 * package-info.java compilation unit in the package or one of the ancestor
 * packages and checks if it has the {@link Model} annotation.
 */
public static boolean isModelPackage(IPackageFragment pack) {
	try {
		IJavaProject javaProject = pack.getJavaProject();
		String packageName = pack.getElementName();
		for (IPackageFragmentRoot pfRoot : javaProject.getPackageFragmentRoots()) {
			if (!pfRoot.isExternal()) {
				if (isModelPackage(pfRoot, packageName)) {
					return true;
				}
			}
		}
	} catch (JavaModelException e) {
		// TODO: use PluginLogWrapper
		e.printStackTrace();
	}
	return false;
}
 
Example 9
Source File: JavaProjectsStateHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected IPackageFragmentRoot getJavaElement(final IFile file) {
	IJavaProject jp = JavaCore.create(file.getProject());
	if (!jp.exists())
		return null;
	IPackageFragmentRoot[] roots;
	try {
		roots = jp.getPackageFragmentRoots();
		for (IPackageFragmentRoot root : roots) {
			if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
				IResource resource2 = root.getUnderlyingResource();
				if (resource2.contains(file))
					return root;
			}
		}
	} catch (JavaModelException e) {
		if (!e.isDoesNotExist())
			log.error(e.getMessage(), e);
	}
	return null;
}
 
Example 10
Source File: Storage2UriMapperJavaImpl.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.4
 */
private void updateCache(IJavaProject project) {
	Set<PackageFragmentRootData> datas = newHashSet();
	try {
		if (project.exists() && project.getProject().isAccessible()) {
			for(IPackageFragmentRoot root: project.getPackageFragmentRoots()) {
				boolean isCachable = root.isArchive() || root.isExternal();
				if(isCachable) 
					datas.add(getCachedData(root));
			}
		}
	} catch (JavaModelException e) {
		if (!e.isDoesNotExist())
			log.error("Error getting package fragments roots of " + project.getElementName(), e);
	} finally {
		clearCache(project, datas);
	}
}
 
Example 11
Source File: PackageFragmentRootWalkerTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testTraversePackageFragmentRoot() throws Exception {
	IJavaProject project = createJavaProject("foo");
	String jarName = "JarWalkerTest.jar";
	IFile file = project.getProject().getFile(jarName);
	file.create(getClass().getResourceAsStream(jarName), true, new NullProgressMonitor());
	addJarToClasspath(project, file);
	
	final Set<IPath> pathes = new HashSet<IPath>();
	PackageFragmentRootWalker<Void> walker = new PackageFragmentRootWalker<Void>() {
		@Override
		protected Void handle(IJarEntryResource jarEntry, TraversalState state) {
			pathes.add(jarEntry.getFullPath());
			return null;
		}
	};
	for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
		if (root.getElementName().equals(jarName))
			walker.traverse(root,false);
	}
	assertEquals(3,pathes.size());
}
 
Example 12
Source File: OrganizeImportsCommand.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Organize imports underlying a directory
 *
 * @param folderUri
 *            Selected folder URI
 * @param proj
 *            the folder associated project
 * @return
 * @throws CoreException
 */
public WorkspaceEdit organizeImportsInDirectory(String folderUri, IProject proj) throws CoreException {
	WorkspaceEdit rootEdit = new WorkspaceEdit();
	IPackageFragment fragment = null;
	if (JDTUtils.toURI(folderUri) != null) {
		fragment = JDTUtils.resolvePackage(folderUri);
	}
	// Select an individual package
	if (fragment != null) {
		organizeImportsInPackageFragment(fragment, rootEdit);
	} else if (proj != null) {
		// Search the packages under the selected folder:
		IJavaProject javaProject = JavaCore.create(proj);
		IPath rootPath = ResourceUtils.filePathFromURI(folderUri);
		IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
		HashSet<IJavaElement> result = new HashSet<>();
		for (IPackageFragmentRoot root : roots) {
			if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
				String packageRoot = root.getResource().getLocation().toString();
				if (packageRoot.toLowerCase().indexOf(rootPath.toString().toLowerCase()) >= 0) {
					collectCompilationUnits(javaProject, result, null);
				}
			}
		}
		for (IJavaElement elem : result) {
			if (elem.getElementType() == IJavaElement.COMPILATION_UNIT) {
				organizeImportsInCompilationUnit((ICompilationUnit) elem, rootEdit);
			}
		}
	}
	return rootEdit;
}
 
Example 13
Source File: RefactoringScopeFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IPackageFragmentRoot[] getAllScopeElements(IJavaProject project, boolean onlySourceRoots) throws JavaModelException {
	Collection<IJavaProject> referencingProjects= getReferencingProjects(project);
	List<IPackageFragmentRoot> result= new ArrayList<IPackageFragmentRoot>();
	for (Iterator<IJavaProject> it= referencingProjects.iterator(); it.hasNext();) {
		IJavaProject javaProject= it.next();
		IPackageFragmentRoot[] roots= javaProject.getPackageFragmentRoots();
		// Add all package fragment roots except archives
		for (int i= 0; i < roots.length; i++) {
			IPackageFragmentRoot root= roots[i];
			if (!onlySourceRoots || root.getKind() == IPackageFragmentRoot.K_SOURCE)
				result.add(root);
		}
	}
	return result.toArray(new IPackageFragmentRoot[result.size()]);
}
 
Example 14
Source File: ASTReader.java    From JDeodorant with MIT License 5 votes vote down vote up
public ASTReader(IJavaProject iJavaProject, IProgressMonitor monitor) throws CompilationErrorDetectedException {
	List<IMarker> markers = buildProject(iJavaProject, monitor);
	if(!markers.isEmpty()) {
		throw new CompilationErrorDetectedException(markers);
	}
	if(monitor != null)
		monitor.beginTask("Parsing selected Java Project", getNumberOfCompilationUnits(iJavaProject));
	systemObject = new SystemObject();
	examinedProject = iJavaProject;
	try {
		IPackageFragmentRoot[] iPackageFragmentRoots = iJavaProject.getPackageFragmentRoots();
		for(IPackageFragmentRoot iPackageFragmentRoot : iPackageFragmentRoots) {
			IJavaElement[] children = iPackageFragmentRoot.getChildren();
			for(IJavaElement child : children) {
				if(child.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
					IPackageFragment iPackageFragment = (IPackageFragment)child;
					ICompilationUnit[] iCompilationUnits = iPackageFragment.getCompilationUnits();
					for(ICompilationUnit iCompilationUnit : iCompilationUnits) {
						if(monitor != null && monitor.isCanceled())
			    			throw new OperationCanceledException();
						systemObject.addClasses(parseAST(iCompilationUnit));
						if(monitor != null)
							monitor.worked(1);
					}
				}
			}
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
	if(monitor != null)
		monitor.done();
}
 
Example 15
Source File: JavadocTreeWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IPath[] getSourcePath(IJavaProject[] projects) {
	HashSet<IPath> res= new HashSet<IPath>();
	//loops through all projects and gets a list if of their source paths
	for (int k= 0; k < projects.length; k++) {
		IJavaProject iJavaProject= projects[k];

		try {
			IPackageFragmentRoot[] roots= iJavaProject.getPackageFragmentRoots();
			for (int i= 0; i < roots.length; i++) {
				IPackageFragmentRoot curr= roots[i];
				if (curr.getKind() == IPackageFragmentRoot.K_SOURCE) {
					IResource resource= curr.getResource();
					if (resource != null) {
						// Using get location is OK here. If the source folder
						// isn't local we can't create Javadoc for it.
						IPath p= resource.getLocation();
						if (p != null) {
							res.add(p);
						}
					}
				}
			}
		} catch (JavaModelException e) {
			JavaPlugin.log(e);
		}
	}
	return res.toArray(new IPath[res.size()]);
}
 
Example 16
Source File: NewContainerWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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 17
Source File: JavaProjectHelper.java    From testability-explorer with Apache License 2.0 5 votes vote down vote up
public List<IPackageFragmentRoot> getAllJavaPackageFragmentRoots(IJavaProject javaProject)
    throws JavaModelException, CoreException {
  List<IPackageFragmentRoot> allJavaPackageFragmentRoot = new ArrayList<IPackageFragmentRoot>();
  IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
  for (IPackageFragmentRoot root : roots) {
    if (!root.isArchive() && !root.getElementName().equals("")) {
      allJavaPackageFragmentRoot.add(root);
    }
  }
  return allJavaPackageFragmentRoot;
}
 
Example 18
Source File: BuilderUtils.java    From JReFrameworker with MIT License 5 votes vote down vote up
/**
 * Returns a collection of K_SOURCE Compilation units in the project's package fragments
 * Reference: https://www.eclipse.org/forums/index.php/t/68072/
 * @param javaproject
 * @return
 */
public static final ICompilationUnit[] getSourceCompilationUnits(IJavaProject jProject) {
	ArrayList<ICompilationUnit> sourceCompilationUnits = new ArrayList<ICompilationUnit>();
	try {
		IPackageFragmentRoot[] roots = jProject.getPackageFragmentRoots();
		for (int i = 0; i < roots.length; i++) {
			IPackageFragmentRoot root = roots[i];
			if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
				IJavaElement[] javaElements = root.getChildren();
				for (int j = 0; j < javaElements.length; j++) {
					IJavaElement javaElement = javaElements[j];
					if (javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
						IPackageFragment pf = (IPackageFragment) javaElement;
						ICompilationUnit[] compilationUnits = pf.getCompilationUnits();
						for (int k = 0; k < compilationUnits.length; k++) {
							ICompilationUnit unit = compilationUnits[k];
							if (unit.isStructureKnown()) {
								sourceCompilationUnits.add(unit);
							}
						}
					}
				}
			}
		}
	} catch (CoreException e) {
		e.printStackTrace();
	}
	ICompilationUnit[] sourceCompilationUnitsArray = new ICompilationUnit[sourceCompilationUnits.size()];
	sourceCompilationUnits.toArray(sourceCompilationUnitsArray);
	return sourceCompilationUnitsArray;
}
 
Example 19
Source File: ReorgUtils.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IPackageFragmentRoot getCorrespondingPackageFragmentRoot(IJavaProject p) throws JavaModelException {
	IPackageFragmentRoot[] roots= p.getPackageFragmentRoots();
	for (int i= 0; i < roots.length; i++) {
		if (isPackageFragmentRootCorrespondingToProject(roots[i]))
			return roots[i];
	}
	return null;
}
 
Example 20
Source File: Binding2JavaModel.java    From lapse-plus with GNU General Public License v3.0 5 votes vote down vote up
/** 
 * Finds a type by its qualified type name (dot separated).
 * @param jproject The java project to search in
 * @param str 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
 */ 
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;
}