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

The following examples show how to use org.eclipse.jdt.core.IPackageFragment#getKind() . 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: 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 2
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 3
Source File: JavaBrowsingContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Object[] getPackageContents(IPackageFragment fragment) throws JavaModelException {
	ISourceReference[] sourceRefs;
	if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
		sourceRefs= fragment.getCompilationUnits();
	}
	else {
		IClassFile[] classFiles= fragment.getClassFiles();
		List<IClassFile> topLevelClassFile= new ArrayList<IClassFile>();
		for (int i= 0; i < classFiles.length; i++) {
			IType type= classFiles[i].getType();
			if (type != null && type.getDeclaringType() == null && !type.isAnonymous() && !type.isLocal())
				topLevelClassFile.add(classFiles[i]);
		}
		sourceRefs= topLevelClassFile.toArray(new ISourceReference[topLevelClassFile.size()]);
	}

	Object[] result= new Object[0];
	for (int i= 0; i < sourceRefs.length; i++)
		result= concatenate(result, removeImportAndPackageDeclarations(getChildren(sourceRefs[i])));
	return concatenate(result, fragment.getNonJavaResources());
}
 
Example 4
Source File: JavaWorkingSetPageContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean hasChildren(Object element) {

	if (element instanceof IProject && !((IProject)element).isAccessible())
		return false;

	if (element instanceof IPackageFragment) {
		IPackageFragment pkg= (IPackageFragment)element;
		try {
			if (pkg.getKind() == IPackageFragmentRoot.K_BINARY) {
				// Don't show IJarEntryResource
				return pkg.getChildren().length > 0;
			}
		} catch (JavaModelException ex) {
			// use super behavior
		}
	}
	return super.hasChildren(element);
}
 
Example 5
Source File: ProblemMarkerBuilder.java    From CogniCrypt with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * getUnitForParser Method to get the CompilationUnits from the PackageFragments and start the parsing process from there
 *
 * @param mypackage IPackageFragment from startMarking
 * @throws JavaModelException
 */
private void getUnitForParser(final IPackageFragment mypackage) throws JavaModelException {
	if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
		for (final ICompilationUnit unit : mypackage.getCompilationUnits()) {
			setupParser(unit);
		}
	}
}
 
Example 6
Source File: UserJavaProject.java    From CogniCrypt with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * List all methods located in the source folder of a javaProject
 *
 * @param javaProject
 * @return list of methods
 * @throws CoreException
 */
public List<IMethod> listOfAllMethods() throws CoreException {
	final IJavaProject javaProject = toJavaProject(this.project);
	if (javaProject != null) {
		final List<IMethod> methodsList = new ArrayList<IMethod>();
		final IPackageFragment[] packages = javaProject.getPackageFragments();
		for (final IPackageFragment pack : packages) {
			// look at the package from the source folder
			if (pack.getKind() == IPackageFragmentRoot.K_SOURCE) {

				for (final ICompilationUnit unit : pack.getCompilationUnits()) {

					final IType[] allTypes = unit.getAllTypes();
					for (final IType type : allTypes) {
						final IMethod[] methods = type.getMethods();

						for (final IMethod method : methods) {
							methodsList.add(method);
						}
					}
				}
			}
		}
		return methodsList;
	}
	return null;
}
 
Example 7
Source File: PackageReferenceLocator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isDeclaringPackageFragment(IPackageFragment packageFragment, ReferenceBinding typeBinding) {
	char[] fileName = typeBinding.getFileName();
	if (fileName != null) {
		// retrieve the actual file name from the full path (sources are generally only containing it already)
		fileName = CharOperation.replaceOnCopy(fileName, '/', '\\'); // ensure to not do any side effect on file name (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=136016)
		fileName = CharOperation.lastSegment(fileName, '\\');

		try {
			switch (packageFragment.getKind()) {
				case IPackageFragmentRoot.K_SOURCE :
					if (!org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(fileName) || !packageFragment.getCompilationUnit(new String(fileName)).exists()) {
						return false; // unit doesn't live in selected package
					}
					break;
				case IPackageFragmentRoot.K_BINARY :
//					if (Util.isJavaFileName(fileName)) { // binary with attached source
//						int length = fileName.length;
//						System.arraycopy(fileName, 0, fileName = new char[length], 0, length - 4); // copy all but extension
//						System.arraycopy(SuffixConstants.SUFFIX_class, 0, fileName, length - 4, 4);
//					}
					if (!Util.isClassFileName(fileName) || !packageFragment.getClassFile(new String(fileName)).exists()) {
						return false; // classfile doesn't live in selected package
					}
					break;
			}
		} catch(JavaModelException e) {
			// unable to determine kind; tolerate this match
		}
	}
	return true; // by default, do not eliminate
}
 
Example 8
Source File: JarManifestWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates a selection dialog that lists all packages under the given package
 * fragment root.
 * The caller is responsible for opening the dialog with <code>Window.open</code>,
 * and subsequently extracting the selected packages (of type
 * <code>IPackageFragment</code>) via <code>SelectionDialog.getResult</code>.
 *
 * @param packageFragments the package fragments
 * @return a new selection dialog
 */
protected SelectionDialog createPackageDialog(Set<IJavaElement> packageFragments) {
	List<IPackageFragment> packages= new ArrayList<IPackageFragment>(packageFragments.size());
	for (Iterator<IJavaElement> iter= packageFragments.iterator(); iter.hasNext();) {
		IPackageFragment fragment= (IPackageFragment)iter.next();
		boolean containsJavaElements= false;
		int kind;
		try {
			kind= fragment.getKind();
			containsJavaElements= fragment.getChildren().length > 0;
		} catch (JavaModelException ex) {
			ExceptionHandler.handle(ex, getContainer().getShell(), JarPackagerMessages.JarManifestWizardPage_error_jarPackageWizardError_title, Messages.format(JarPackagerMessages.JarManifestWizardPage_error_jarPackageWizardError_message, JavaElementLabels.getElementLabel(fragment, JavaElementLabels.ALL_DEFAULT)));
			continue;
		}
		if (kind != IPackageFragmentRoot.K_BINARY && containsJavaElements)
			packages.add(fragment);
	}
	StandardJavaElementContentProvider cp= new StandardJavaElementContentProvider() {
		@Override
		public boolean hasChildren(Object element) {
			// prevent the + from being shown in front of packages
			return !(element instanceof IPackageFragment) && super.hasChildren(element);
		}
	};
	final DecoratingLabelProvider provider= new DecoratingLabelProvider(new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT), new ProblemsLabelDecorator(null));
	ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getContainer().getShell(), provider, cp);
	dialog.setDoubleClickSelects(false);
	dialog.setComparator(new JavaElementComparator());
	dialog.setInput(JavaCore.create(JavaPlugin.getWorkspace().getRoot()));
	dialog.addFilter(new EmptyInnerPackageFilter());
	dialog.addFilter(new LibraryFilter());
	dialog.addFilter(new SealPackagesFilter(packages));
	dialog.setValidator(new ISelectionStatusValidator() {
		public IStatus validate(Object[] selection) {
			StatusInfo res= new StatusInfo();
			for (int i= 0; i < selection.length; i++) {
				if (!(selection[i] instanceof IPackageFragment)) {
					res.setError(JarPackagerMessages.JarManifestWizardPage_error_mustContainPackages);
					return res;
				}
			}
			res.setOK();
			return res;
		}
	});
	return dialog;
}
 
Example 9
Source File: StandardJavaElementContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Evaluates all children of a given {@link IPackageFragment}. Clients can override this method.
 * @param fragment The fragment to evaluate the children for.
 * @return The children of the given package fragment.
 * @exception JavaModelException if the package fragment does not exist or if an
 *      exception occurs while accessing its corresponding resource
 *
 * @since 3.3
 */
protected Object[] getPackageContent(IPackageFragment fragment) throws JavaModelException {
	if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
		return concatenate(fragment.getCompilationUnits(), fragment.getNonJavaResources());
	}
	return concatenate(fragment.getClassFiles(), fragment.getNonJavaResources());
}