Java Code Examples for org.eclipse.jdt.core.IPackageFragment#getNonJavaResources()

The following examples show how to use org.eclipse.jdt.core.IPackageFragment#getNonJavaResources() . 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: EmptyPackageFilter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Tells whether the given package has unfiltered resources.
 *
 * @param viewer the viewer
 * @param pkg the package
 * @return <code>true</code> if the package has unfiltered resources
 * @throws JavaModelException if this element does not exist or if an exception occurs while
 *             accessing its corresponding resource
 * @since 3.4.1
 */
static boolean hasUnfilteredResources(Viewer viewer, IPackageFragment pkg) throws JavaModelException {
	Object[] resources= pkg.getNonJavaResources();
	int length= resources.length;
	if (length == 0)
		return false;

	if (!(viewer instanceof StructuredViewer))
		return true;

	ViewerFilter[] filters= ((StructuredViewer)viewer).getFilters();
	resourceLoop: for (int i= 0; i < length; i++) {
		for (int j= 0; j < filters.length; j++) {
			if (!filters[j].select(viewer, pkg, resources[i]))
				continue resourceLoop;
		}
		return true;

	}
	return false;
}
 
Example 2
Source File: JavaElementResourceMapping.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static ResourceTraversal[] getPackageFragmentTraversals(IPackageFragment pack) throws CoreException {
	ArrayList<ResourceTraversal> res= new ArrayList<ResourceTraversal>();
	IContainer container= (IContainer)pack.getResource();
	
	if (container != null) {
		res.add(new ResourceTraversal(new IResource[] { container }, IResource.DEPTH_ONE, 0));
		if (pack.exists()) { // folder may not exist any more, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=269167
			Object[] nonJavaResources= pack.getNonJavaResources();
			for (int i= 0; i < nonJavaResources.length; i++) {
				Object resource= nonJavaResources[i];
				if (resource instanceof IFolder) {
					res.add(new ResourceTraversal(new IResource[] { (IResource)resource }, IResource.DEPTH_INFINITE, 0));
				}
			}
		}
	}

	return res.toArray(new ResourceTraversal[res.size()]);
}
 
Example 3
Source File: NLSAccessorConfigurationDialog.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Object[] createFileListInput() {
	try {

		IPackageFragment fPkgFragment= fResourceBundlePackage.getSelected();
		if (fPkgFragment == null)
			return new Object[0];
		List<Object> result= new ArrayList<Object>(1);
		Object[] nonjava= fPkgFragment.getNonJavaResources();
		for (int i= 0; i < nonjava.length; i++) {
			if (isPropertyFile(nonjava[i]))
				result.add(nonjava[i]);
		}
		return result.toArray();

	} catch (JavaModelException e) {
		ExceptionHandler.handle(e, NLSUIMessages.NLSAccessorConfigurationDialog_externalizing, NLSUIMessages .NLSAccessorConfigurationDialog_exception);
		return new Object[0];
	}
}
 
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: RefactoringModifications.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected IResource[] collectResourcesOfInterest(IPackageFragment source) throws CoreException {
	IJavaElement[] children = source.getChildren();
	int childOfInterest = IJavaElement.COMPILATION_UNIT;
	if (source.getKind() == IPackageFragmentRoot.K_BINARY) {
		childOfInterest = IJavaElement.CLASS_FILE;
	}
	ArrayList<IResource> result = new ArrayList<IResource>(children.length);
	for (int i = 0; i < children.length; i++) {
		IJavaElement child = children[i];
		if (child.getElementType() == childOfInterest && child.getResource() != null) {
			result.add(child.getResource());
		}
	}
	// Gather non-java resources
	Object[] nonJavaResources = source.getNonJavaResources();
	for (int i= 0; i < nonJavaResources.length; i++) {
		Object element= nonJavaResources[i];
		if (element instanceof IResource) {
			result.add((IResource) element);
		}
	}
	return result.toArray(new IResource[result.size()]);
}
 
Example 6
Source File: ClientBundleResourceSelectionDialog.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private boolean showPackage(IPackageFragment pckg)
    throws JavaModelException {
  // Ignore packages with no non-Java resources
  Object[] nonJavaResources = pckg.getNonJavaResources();
  if (nonJavaResources.length == 0) {
    return false;
  }

  // Only show packages with visible resources underneath
  for (Object nonJavaResource : nonJavaResources) {
    if (nonJavaResource instanceof IFile) {
      if (showFile((IFile) nonJavaResource)) {
        return true;
      }
    }
  }
  return false;
}
 
Example 7
Source File: RefactoringModifications.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
protected IResource[] collectResourcesOfInterest(IPackageFragment source) throws CoreException {
	IJavaElement[] children = source.getChildren();
	int childOfInterest = IJavaElement.COMPILATION_UNIT;
	if (source.getKind() == IPackageFragmentRoot.K_BINARY) {
		childOfInterest = IJavaElement.CLASS_FILE;
	}
	ArrayList<IResource> result = new ArrayList<>(children.length);
	for (int i = 0; i < children.length; i++) {
		IJavaElement child = children[i];
		if (child.getElementType() == childOfInterest && child.getResource() != null) {
			result.add(child.getResource());
		}
	}
	// Gather non-java resources
	Object[] nonJavaResources = source.getNonJavaResources();
	for (int i= 0; i < nonJavaResources.length; i++) {
		Object element= nonJavaResources[i];
		if (element instanceof IResource) {
			result.add((IResource) element);
		}
	}
	return result.toArray(new IResource[result.size()]);
}
 
Example 8
Source File: JavaElementResourceMapping.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static ResourceTraversal[] getPackageFragmentTraversals(IPackageFragment pack) throws CoreException {
	ArrayList<ResourceTraversal> res= new ArrayList<>();
	IContainer container= (IContainer)pack.getResource();

	if (container != null) {
		res.add(new ResourceTraversal(new IResource[] { container }, IResource.DEPTH_ONE, 0));
		if (pack.exists()) { // folder may not exist any more, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=269167
			Object[] nonJavaResources= pack.getNonJavaResources();
			for (int i= 0; i < nonJavaResources.length; i++) {
				Object resource= nonJavaResources[i];
				if (resource instanceof IFolder) {
					res.add(new ResourceTraversal(new IResource[] { (IResource)resource }, IResource.DEPTH_INFINITE, 0));
				}
			}
		}
	}

	return res.toArray(new ResourceTraversal[res.size()]);
}
 
Example 9
Source File: PackageFragmentRootWalker.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected T traverse(IPackageFragment pack, boolean stopOnFirstResult, TraversalState state) throws JavaModelException {
	T result = null;
	state.push(pack);
	IJavaElement[] children = pack.getChildren();
	for (IJavaElement iJavaElement : children) {
		if (iJavaElement instanceof IPackageFragment) {
			result = traverse((IPackageFragment) iJavaElement, stopOnFirstResult, state);
			if (stopOnFirstResult && result!=null)
				return result;
		}
	}
	Object[] resources = pack.getNonJavaResources();
	for (Object object : resources) {
		if (object instanceof IJarEntryResource) {
			result = traverse((IJarEntryResource) object, stopOnFirstResult, state);
			if (stopOnFirstResult && result!=null)
				return result;
		}
	}
	state.pop();
	return result;
}
 
Example 10
Source File: ModuleUtils.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Find modules in package
 *
 * @param pckg
 * @param modules
 * @param includeJars
 * @throws JavaModelException
 */
protected static void testNonJavaResourcesForModules(IPackageFragment pckg, Map<String, IModule> modules,
    boolean includeJars) throws JavaModelException {
  for (Object resource : pckg.getNonJavaResources()) {
    IModule module = create(resource, includeJars);
    if (module != null) {
      String moduleName = module.getQualifiedName();
      if (!modules.containsKey(moduleName)) {
        modules.put(moduleName, module);
      }
    }
  }
}
 
Example 11
Source File: RenamePackageProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isPackageNameOkInRoot(String newName, IPackageFragmentRoot root) throws CoreException {
	IPackageFragment pack= root.getPackageFragment(newName);
	if (! pack.exists())
		return true;
	else if (pack.containsJavaResources())
		return false;
	else if (pack.getNonJavaResources().length != 0)
		return false;
	else
		return true;
}
 
Example 12
Source File: RenamePackageProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isPackageNameOkInRoot(String newName, IPackageFragmentRoot root) throws CoreException {
	IPackageFragment pack= root.getPackageFragment(newName);
	if (! pack.exists()) {
		return true;
	} else if (pack.containsJavaResources()) {
		return false;
	} else if (pack.getNonJavaResources().length != 0) {
		return false;
	} else {
		return true;
	}
}
 
Example 13
Source File: SARLElementImageProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the image description of the package fragment.
 *
 * @param fragment the element.
 * @return the descriptor.
 */
@SuppressWarnings("checkstyle:all")
private ImageDescriptor getPackageFragmentIcon(IPackageFragment fragment) {
	boolean containsJavaElements = false;
	try {
		containsJavaElements = fragment.hasChildren();
	} catch (JavaModelException e) {
		// assuming no children;
	}
	try {
		if (!containsJavaElements) {
			final Object[] resources = fragment.getNonJavaResources();
			if (resources.length > 0) {
				for (final Object child : resources) {
					if (isSarlResource(child)) {
						return JavaPluginImages.DESC_OBJS_PACKAGE;
					}
				}
				return JavaPluginImages.DESC_OBJS_EMPTY_PACKAGE_RESOURCES;
			}
		}
	} catch (JavaModelException exception) {
		//
	}
	if (!containsJavaElements) {
		return JavaPluginImages.DESC_OBJS_EMPTY_PACKAGE;
	}
	return JavaPluginImages.DESC_OBJS_PACKAGE;
}
 
Example 14
Source File: StandardJavaElementContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tests if the given element is a empty package fragment.
 *
 * @param element the element to test
 * @return returns <code>true</code> if the package fragment is empty
 * @throws JavaModelException thrown if accessing the element failed
 */
protected boolean isPackageFragmentEmpty(IJavaElement element) throws JavaModelException {
	if (element instanceof IPackageFragment) {
		IPackageFragment fragment= (IPackageFragment)element;
		if (fragment.exists() && !(fragment.hasChildren() || fragment.getNonJavaResources().length > 0) && fragment.hasSubpackages())
			return true;
	}
	return false;
}
 
Example 15
Source File: ProblemsLabelDecorator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int getPackageErrorTicksFromMarkers(IPackageFragment pack) throws CoreException {
	// Packages are special: They must not consider markers on subpackages.
	
	IResource res= pack.getResource();
	if (res == null || !res.isAccessible()) {
		return 0;
	}
	
	// markers on package itself (e.g. missing @NonNullByDefault)
	int severity= res.findMaxProblemSeverity(IMarker.PROBLEM, true, IResource.DEPTH_ZERO);
	if (severity == IMarker.SEVERITY_ERROR)
		return ERRORTICK_ERROR;
	
	// markers on CUs
	for (ICompilationUnit cu : pack.getCompilationUnits()) {
		severity= Math.max(severity, cu.getResource().findMaxProblemSeverity(IMarker.PROBLEM, true, IResource.DEPTH_ZERO));
		if (severity == IMarker.SEVERITY_ERROR)
			return ERRORTICK_ERROR;
	}
	
	// markers on files and folders
	for (Object object : pack.getNonJavaResources()) {
		if (object instanceof IResource) {
			IResource resource= (IResource) object;
			severity= Math.max(severity, resource.findMaxProblemSeverity(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE));
			if (severity == IMarker.SEVERITY_ERROR)
				return ERRORTICK_ERROR;
		}
	}
	
	// SEVERITY_ERROR already handled above
	if (severity == IMarker.SEVERITY_WARNING) {
		return ERRORTICK_WARNING;
	}
	return 0;
}
 
Example 16
Source File: JavaElementImageProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private ImageDescriptor getPackageFragmentIcon(IJavaElement element) throws JavaModelException {
	IPackageFragment fragment= (IPackageFragment)element;
	boolean containsJavaElements= false;
	try {
		containsJavaElements= fragment.hasChildren();
	} catch(JavaModelException e) {
		// assuming no children;
	}
	if(!containsJavaElements && (fragment.getNonJavaResources().length > 0))
		return JavaPluginImages.DESC_OBJS_EMPTY_PACKAGE_RESOURCES;
	else if (!containsJavaElements)
		return JavaPluginImages.DESC_OBJS_EMPTY_PACKAGE;
	return JavaPluginImages.DESC_OBJS_PACKAGE;
}
 
Example 17
Source File: JavaEditorBreadcrumb.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Object[] getPackageContent(IPackageFragment pack) {
	ArrayList<Object> result= new ArrayList<Object>();
	try {
		ICompilationUnit[] units= pack.getCompilationUnits();
		for (int i= 0; i < units.length; i++) {
			if (JavaModelUtil.isPackageInfo(units[i]))
				result.add(units[i]);
			IType[] types= units[i].getTypes();
			for (int j= 0; j < types.length; j++) {
				if (isValidType(types[j]))
					result.add(types[j]);
			}
		}

		IClassFile[] classFiles= pack.getClassFiles();
		for (int i= 0; i < classFiles.length; i++) {
			if (isValidType(classFiles[i].getType()))
				result.add(classFiles[i].getType());
		}

		Object[] nonJavaResources= pack.getNonJavaResources();
		for (int i= 0; i < nonJavaResources.length; i++) {
			result.add(nonJavaResources[i]);
		}
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
	}

	return result.toArray();
}
 
Example 18
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IEditorPart openDeclaration(IJavaElement element) throws PartInitException, JavaModelException {
	if (!(element instanceof IPackageFragment)) {
		return JavaUI.openInEditor(element);
	}
	
	IPackageFragment packageFragment= (IPackageFragment) element;
	ITypeRoot typeRoot;
	IPackageFragmentRoot root= (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
		typeRoot= packageFragment.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
	} else {
		typeRoot= packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
	}

	// open the package-info file in editor if one exists
	if (typeRoot.exists())
		return JavaUI.openInEditor(typeRoot);

	// open the package.html file in editor if one exists
	Object[] nonJavaResources= packageFragment.getNonJavaResources();
	for (Object nonJavaResource : nonJavaResources) {
		if (nonJavaResource instanceof IFile) {
			IFile file= (IFile) nonJavaResource;
			if (file.exists() && JavaModelUtil.PACKAGE_HTML.equals(file.getName())) {
				return EditorUtility.openInEditor(file, true);
			}
		}
	}

	// select the package in the Package Explorer if there is no associated package Javadoc file
	PackageExplorerPart view= (PackageExplorerPart) JavaPlugin.getActivePage().showView(JavaUI.ID_PACKAGES);
	view.tryToReveal(packageFragment);
	return null;
}
 
Example 19
Source File: ClasspathResourceUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns the given file or JAR entry if it is on the given package fragment. The file can be a Java file, class
 * file, or a non-Java resource.
 * <p>
 * This method returns null for .java files or .class files inside JARs.
 *
 * @param fileName
 *          the file name
 * @param pckgFragment
 *          the package fragment to search
 * @return the file as an IResource or IJarEntryResource, or null
 * @throws JavaModelException
 */
public static IStorage resolveFileOnPackageFragment(String fileName, IPackageFragment pckgFragment)
    throws JavaModelException {

  boolean isJavaFile = JavaCore.isJavaLikeFileName(fileName);
  boolean isClassFile = ResourceUtils.endsWith(fileName, ".class");

  // Check the non-Java resources first
  Object[] nonJavaResources = pckgFragment.getNonJavaResources();
  for (Object nonJavaResource : nonJavaResources) {
    if (nonJavaResource instanceof IFile) {
      IFile file = (IFile) nonJavaResource;
      String resFileName = file.getName();

      if (ResourceUtils.areFilenamesEqual(resFileName, fileName)) {
        // Java source files that have been excluded from the build path
        // show up as non-Java resources, but we'll ignore them since
        // they're not available on the classpath.
        if (!JavaCore.isJavaLikeFileName(resFileName)) {
          return file;
        } else {
          return null;
        }
      }
    }

    // JAR resources are not IResource's, so we need to handle them
    // differently
    if (nonJavaResource instanceof IJarEntryResource) {
      IJarEntryResource jarEntry = (IJarEntryResource) nonJavaResource;
      if (jarEntry.isFile() && ResourceUtils.areFilenamesEqual(jarEntry.getName(), fileName)) {
        return jarEntry;
      }
    }
  }

  // If we're looking for a .java or .class file, we can use the regular
  // Java Model methods.

  if (isJavaFile) {
    ICompilationUnit cu = pckgFragment.getCompilationUnit(fileName);
    if (cu.exists()) {
      return (IFile) cu.getCorrespondingResource();
    }
  }

  if (isClassFile) {
    IClassFile cf = pckgFragment.getClassFile(fileName);
    if (cf.exists()) {
      return (IFile) cf.getCorrespondingResource();
    }
  }

  return null;
}
 
Example 20
Source File: PackageExplorerContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean isEmpty(IPackageFragment fragment) throws JavaModelException {
	return !fragment.containsJavaResources() && fragment.getNonJavaResources().length == 0;
}