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

The following examples show how to use org.eclipse.jdt.core.IJavaProject#getAllPackageFragmentRoots() . 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: JavaProjectsStateHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected List<String> getPackageFragmentRootHandles(IJavaProject project) {
	List<String> result = Lists.newArrayList();
	List<String> binaryAndNonLocalFragments = Lists.newArrayList();
	try {
		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;
}
 
Example 2
Source File: XbaseResourceForEditorInputFactoryTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testValidationIsDisabled_03() throws Exception {
	IProject project = AbstractXbaseUITestCase.createPluginProject("my.plugin.project");
	IJavaProject jp = JavaCore.create(project);
	boolean wasTested = false;
	for (IPackageFragmentRoot pfr : jp.getAllPackageFragmentRoots()) {
		if (pfr.isArchive()) {
			for (Object o : pfr.getNonJavaResources()) {
				if (o instanceof IStorage) {
					assertTrue(isValidationDisabled((IStorage) o));
					wasTested = true;
				}
			}
		}
	}
	assertTrue(wasTested);
}
 
Example 3
Source File: ClientBundleResourceSelectionDialog.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private boolean showProject(IJavaProject project) throws JavaModelException {
  // Always show this project
  if (javaProject.equals(project)) {
    return true;
  }

  // Only show dependent projects
  if (!javaProject.isOnClasspath(project)) {
    return false;
  }

  // Only show projects with visible source roots underneath
  for (IPackageFragmentRoot srcRoot : project.getAllPackageFragmentRoots()) {
    if (showPackageFragmentRoot(srcRoot)) {
      return true;
    }
  }
  return false;
}
 
Example 4
Source File: NLSHintHelper.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static IPackageFragment getResourceBundlePackage(IJavaProject javaProject, String packageName, String resourceName) throws JavaModelException {
	IPackageFragmentRoot[] allRoots= javaProject.getAllPackageFragmentRoots();
	for (int i= 0; i < allRoots.length; i++) {
		IPackageFragmentRoot root= allRoots[i];
		if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
			IPackageFragment packageFragment= root.getPackageFragment(packageName);
			if (packageFragment.exists()) {
				Object[] resources= packageFragment.isDefaultPackage() ? root.getNonJavaResources() : packageFragment.getNonJavaResources();
				for (int j= 0; j < resources.length; j++) {
					Object object= resources[j];
					if (object instanceof IFile) {
						IFile file= (IFile) object;
						if (file.getName().equals(resourceName)) {
							return packageFragment;
						}
					}
				}
			}
		}
	}
	return null;
}
 
Example 5
Source File: PackageSelectionDialogButtonField.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public PackageSelectionDialogButtonField(String label, String button, PackageBrowseAdapter adapter, IJavaProject root) {
	super(adapter);
	setContentAssistProcessor(new JavaPackageCompletionProcessor(new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_ROOT)));

	IPackageFragmentRoot[] roots;
	try {
		roots= root.getAllPackageFragmentRoots();
		((JavaPackageCompletionProcessor)getContentAssistProcessor()).setPackageFragmentRoot(roots[0]);
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
		// if exception no content assist .. but thats no problem
	}
	setLabelText(label);
	setButtonLabel(button);
	adapter.setReceiver(this);
}
 
Example 6
Source File: JavaModelSearch.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns a list of package fragments for the given package name.
 *
 * @param javaProject the java project whose classpath will be searched
 * @param packageName the package name to find corresponding fragments for
 * @return a possibly empty list of package fragments
 * @throws JavaModelException
 */
public static List<IPackageFragment> getPackageFragments(
    IJavaProject javaProject, String packageName) throws JavaModelException {
  List<IPackageFragment> packageFragments = new ArrayList<IPackageFragment>();

  for (IPackageFragmentRoot packageFragmentRootroot : javaProject.getAllPackageFragmentRoots()) {
    IPackageFragment packageFragment = packageFragmentRootroot.getPackageFragment(packageName);
    if (packageFragment.exists()) {
      packageFragments.add(packageFragment);
    }
  }

  return packageFragments;
}
 
Example 7
Source File: ClasspathResourceUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Determines if a resource is available on a project's classpath. This method searches both source paths and JAR/ZIP
 * files.
 */
public static boolean isResourceOnClasspath(IJavaProject javaProject, IPath resourcePath) throws JavaModelException {
  String pckg = JavaUtilities.getPackageNameFromPath(resourcePath.removeLastSegments(1));
  String fileName = resourcePath.lastSegment();

  for (IPackageFragment pckgFragment : JavaModelSearch.getPackageFragments(javaProject, pckg)) {
    if (ClasspathResourceUtilities.isFileOnPackageFragment(fileName, pckgFragment)) {
      return true;
    }
  }

  // If the resource doesn't follow normal java naming convention for resources, such as /resources/folder-name/file-name.js
  for (IPackageFragmentRoot root : javaProject.getAllPackageFragmentRoots()) {
    IPackageFragment folderFragment = root.getPackageFragment(pckg);
    IResource folder = folderFragment.getResource();
    if (folder == null || !folder.exists() || !(folder instanceof IContainer)) {
      continue;
    }
    IResource resource = ((IContainer) folder).findMember(fileName);
    if (resource != null && resource.exists()) {
      return true;
    }
  }

  // Not there
  return false;
}
 
Example 8
Source File: NLSHintHelper.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IStorage getResourceBundle(IJavaProject javaProject, String packageName, String resourceName) throws JavaModelException {
	IPackageFragmentRoot[] allRoots= javaProject.getAllPackageFragmentRoots();
	for (int i= 0; i < allRoots.length; i++) {
		IPackageFragmentRoot root= allRoots[i];
		if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
			IStorage storage= getResourceBundle(root, packageName, resourceName);
			if (storage != null)
				return storage;
		}
	}
	return null;
}
 
Example 9
Source File: JavaUI.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a selection dialog that lists all packages of the given Java project.
 * The caller is responsible for opening the dialog with <code>Window.open</code>,
 * and subsequently extracting the selected package (of type
 * <code>IPackageFragment</code>) via <code>SelectionDialog.getResult</code>.
 *
 * @param parent the parent shell of the dialog to be created
 * @param project the Java project
 * @param style flags defining the style of the dialog; the valid flags are:
 *   <code>IJavaElementSearchConstants.CONSIDER_BINARIES</code>, indicating that
 *   packages from binary package fragment roots should be included in addition
 *   to those from source package fragment roots;
 *   <code>IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS</code>, indicating that
 *   packages from required projects should be included as well.
 * @param filter the initial pattern to filter the set of packages. For example "com" shows
 * all packages starting with "com". The meta character '?' representing any character and
 * '*' representing any string are supported. Clients can pass an empty string if no filtering
 * is required.
 * @return a new selection dialog
 * @exception JavaModelException if the selection dialog could not be opened
 *
 * @since 2.0
 */
public static SelectionDialog createPackageDialog(Shell parent, IJavaProject project, int style, String filter) throws JavaModelException {
	Assert.isTrue((style | IJavaElementSearchConstants.CONSIDER_BINARIES | IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS) ==
		(IJavaElementSearchConstants.CONSIDER_BINARIES | IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS));

	IPackageFragmentRoot[] roots= null;
	if ((style & IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS) != 0) {
	    roots= project.getAllPackageFragmentRoots();
	} else {
		roots= project.getPackageFragmentRoots();
	}

	List<IPackageFragmentRoot> consideredRoots= null;
	if ((style & IJavaElementSearchConstants.CONSIDER_BINARIES) != 0) {
		consideredRoots= Arrays.asList(roots);
	} else {
		consideredRoots= new ArrayList<IPackageFragmentRoot>(roots.length);
		for (int i= 0; i < roots.length; i++) {
			IPackageFragmentRoot root= roots[i];
			if (root.getKind() != IPackageFragmentRoot.K_BINARY)
				consideredRoots.add(root);

		}
	}

	IJavaSearchScope searchScope= SearchEngine.createJavaSearchScope(consideredRoots.toArray(new IJavaElement[consideredRoots.size()]));
	BusyIndicatorRunnableContext context= new BusyIndicatorRunnableContext();
	if (style == 0 || style == IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS) {
		return createPackageDialog(parent, context, searchScope, false, true, filter);
	} else {
		return createPackageDialog(parent, context, searchScope, false, false, filter);
	}
}
 
Example 10
Source File: JavaClassFilter.java    From java-debug with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Substitute the variables in the exclusion filter list.
 *
 * <p>
 * For example, a sample input could be:
 * [
 *  "$JDK",
 *  "$Libraries",
 *  "junit.*",
 *  "java.lang.ClassLoader"
 * ].
 *
 * Variable "$JDK" means skipping the classes from the JDK, and variable "$Libraries"
 * means skipping the classes from the application libraries.
 *
 * <p>
 * This function will resolve the packages belonging to the variable group first, and
 * then use a greedy algorithm to generate a list of wildcards to cover these packages.
 */
public static String[] resolveClassFilters(List<Object> unresolvedFilters) {
    if (unresolvedFilters == null || unresolvedFilters.isEmpty()) {
        return new String[0];
    }

    int variableScope = BLOCK_NONE;
    Set<String> hardcodePatterns = new LinkedHashSet<>();
    for (Object filter : unresolvedFilters) {
        if (Objects.equals("$JDK", filter)) {
            variableScope = variableScope | BLOCK_JDK;
        } else if (Objects.equals("$Libraries", filter)) {
            variableScope = variableScope | BLOCK_LIB;
        } else if (filter instanceof String) {
            String value = (String) filter;
            if (StringUtils.isNotBlank(value)) {
                hardcodePatterns.add(value.trim());
            }
        }
    }

    if (variableScope == BLOCK_NONE) {
        return hardcodePatterns.toArray(new String[0]);
    }

    Set<String> blackList = new LinkedHashSet<>();
    Set<String> whiteList = new LinkedHashSet<>();
    IJavaProject[] javaProjects = ProjectUtils.getJavaProjects();
    for (IJavaProject javaProject : javaProjects) {
        try {
            IPackageFragmentRoot[] roots = javaProject.getAllPackageFragmentRoots();
            for (IPackageFragmentRoot root : roots) {
                if (isOnBlackList(root, variableScope)) {
                    collectPackages(root, blackList);
                } else {
                    collectPackages(root, whiteList);
                }
            }
        } catch (JavaModelException e) {
            logger.log(Level.SEVERE, String.format("Failed to get the classpath entry for the PackageFragmentRoot: %s", e.toString()), e);
        }
    }

    return convertToExclusionPatterns(blackList, whiteList, hardcodePatterns);
}