Java Code Examples for org.eclipse.jdt.core.IPackageFragmentRoot#K_SOURCE

The following examples show how to use org.eclipse.jdt.core.IPackageFragmentRoot#K_SOURCE . 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: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isInferTypeArgumentsAvailable(final IJavaElement element) throws JavaModelException {
	if (!Checks.isAvailable(element)) {
		return false;
	} else if (element instanceof IJavaProject) {
		IJavaProject project= (IJavaProject) element;
		IClasspathEntry[] classpathEntries= project.getRawClasspath();
		for (int i= 0; i < classpathEntries.length; i++) {
			if (classpathEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE)
				return true;
		}
		return false;
	} else if (element instanceof IPackageFragmentRoot) {
		return ((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE;
	} else if (element instanceof IPackageFragment) {
		return ((IPackageFragment) element).getKind() == IPackageFragmentRoot.K_SOURCE;
	} else if (element instanceof ICompilationUnit) {
		return true;
	} else if (element.getAncestor(IJavaElement.COMPILATION_UNIT) != null) {
		return true;
	} else {
		return false;
	}
}
 
Example 2
Source File: SourceFirstPackageSelectionDialogField.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IPackageFragmentRoot searchSourcePackageFragmentRoot(IJavaElement jElement) {
	IJavaElement parent= jElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (parent == null) {
		return null;
	}

	IPackageFragmentRoot res= (IPackageFragmentRoot)parent;
	try {
		if (res.getKind() == IPackageFragmentRoot.K_SOURCE) {
			return res;
		}
	} catch (JavaModelException e) {
		// nothing to do
	}

	return null;
}
 
Example 3
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static boolean isInferTypeArgumentsAvailable(final IJavaElement element) throws JavaModelException {
	if (!Checks.isAvailable(element)) {
		return false;
	} else if (element instanceof IJavaProject) {
		IJavaProject project = (IJavaProject) element;
		IClasspathEntry[] classpathEntries = project.getRawClasspath();
		for (int i = 0; i < classpathEntries.length; i++) {
			if (classpathEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
				return true;
			}
		}
		return false;
	} else if (element instanceof IPackageFragmentRoot) {
		return ((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE;
	} else if (element instanceof IPackageFragment) {
		return ((IPackageFragment) element).getKind() == IPackageFragmentRoot.K_SOURCE;
	} else if (element instanceof ICompilationUnit) {
		return true;
	} else if (element.getAncestor(IJavaElement.COMPILATION_UNIT) != null) {
		return true;
	} else {
		return false;
	}
}
 
Example 4
Source File: RegionBasedHierarchyBuilder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Adds all of the openables defined within this package fragment to the
 * list.
 */
private void injectAllOpenablesForPackageFragment(
	IPackageFragment packFrag,
	ArrayList openables) {

	try {
		IPackageFragmentRoot root = (IPackageFragmentRoot) packFrag.getParent();
		int kind = root.getKind();
		if (kind != 0) {
			boolean isSourcePackageFragment = (kind == IPackageFragmentRoot.K_SOURCE);
			if (isSourcePackageFragment) {
				ICompilationUnit[] cus = packFrag.getCompilationUnits();
				for (int i = 0, length = cus.length; i < length; i++) {
					openables.add(cus[i]);
				}
			} else {
				IClassFile[] classFiles = packFrag.getClassFiles();
				for (int i = 0, length = classFiles.length; i < length; i++) {
					openables.add(classFiles[i]);
				}
			}
		}
	} catch (JavaModelException e) {
		// ignore
	}
}
 
Example 5
Source File: EditFilterAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected boolean canHandle(IStructuredSelection selection) {
	if (selection.size() != 1)
		return false;

	try {
		Object element= selection.getFirstElement();
		if (element instanceof IJavaProject) {
			return ClasspathModifier.isSourceFolder((IJavaProject)element);
		} else if (element instanceof IPackageFragmentRoot) {
			IPackageFragmentRoot packageFragmentRoot= ((IPackageFragmentRoot) element);
			if (packageFragmentRoot.getKind() != IPackageFragmentRoot.K_SOURCE)
				return false;

			return packageFragmentRoot.getJavaProject() != null;
		}
	} catch (JavaModelException e) {
	}
	return false;
}
 
Example 6
Source File: JavadocOptionsManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean containsCompilationUnits(IPackageFragmentRoot root) throws JavaModelException {
	if (root.getKind() != IPackageFragmentRoot.K_SOURCE) {
		return false;
	}

	IJavaElement[] elements= root.getChildren();
	for (int i= 0; i < elements.length; i++) {
		if (elements[i] instanceof IPackageFragment) {
			IPackageFragment fragment= (IPackageFragment) elements[i];
			if (containsCompilationUnits(fragment)) {
				return true;
			}
		}
	}
	return false;
}
 
Example 7
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 8
Source File: CleanUpAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void collectCompilationUnits(IPackageFragmentRoot root, Collection<IJavaElement> result) throws JavaModelException {
	if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
		IJavaElement[] children= root.getChildren();
		for (int i= 0; i < children.length; i++) {
			collectCompilationUnits((IPackageFragment)children[i], result);
		}
	}
}
 
Example 9
Source File: StandardJavaElementContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Evaluates all children of a given {@link IFolder}. Clients can override this method.
 * @param folder The folder to evaluate the children for.
 * @return The children of the given folder.
 * @exception CoreException if the folder does not exist.
 *
 * @since 3.3
 */
protected Object[] getFolderContent(IFolder folder) throws CoreException {
	IResource[] members= folder.members();
	IJavaProject javaProject= JavaCore.create(folder.getProject());
	if (javaProject == null || !javaProject.exists())
		return members;
	boolean isFolderOnClasspath = javaProject.isOnClasspath(folder);
	List<IResource> nonJavaResources= new ArrayList<IResource>();
	// Can be on classpath but as a member of non-java resource folder
	for (int i= 0; i < members.length; i++) {
		IResource member= members[i];
		// A resource can also be a java element
		// in the case of exclusion and inclusion filters.
		// We therefore exclude Java elements from the list
		// of non-Java resources.
		if (isFolderOnClasspath) {
			if (javaProject.findPackageFragmentRoot(member.getFullPath()) == null) {
				nonJavaResources.add(member);
			}
		} else if (!javaProject.isOnClasspath(member)) {
			nonJavaResources.add(member);
		} else {
			IJavaElement element= JavaCore.create(member, javaProject);
			if (element instanceof IPackageFragmentRoot
					&& javaProject.equals(element.getJavaProject())
					&& ((IPackageFragmentRoot)element).getKind() != IPackageFragmentRoot.K_SOURCE) {
				// don't skip libs and class folders on the classpath of their project
				nonJavaResources.add(member);
			}
		}
	}
	return nonJavaResources.toArray();
}
 
Example 10
Source File: MultiOrganizeImportsHandler.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void collectRelevantFiles(IPackageFragmentRoot root, Multimap<IProject, IFile> result)
		throws JavaModelException {
	if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
		IJavaElement[] children = root.getChildren();
		for (int i = 0; i < children.length; i++) {
			collectRelevantFiles((IPackageFragment) children[i], result);
		}

	}
}
 
Example 11
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 12
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 13
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isWritable(IPackageFragmentRoot packageFragmentRoot) {
	try {
		return packageFragmentRoot.exists() && ! packageFragmentRoot.isArchive() && ! packageFragmentRoot.isReadOnly()
				&& packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE;
	} catch (JavaModelException e) {
		return false;
	}
}
 
Example 14
Source File: PackageFragment.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns a the collection of class files in this - a folder package fragment which has a root
 * that has its kind set to <code>IPackageFragmentRoot.K_Source</code> does not
 * recognize class files.
 *
 * @see IPackageFragment#getClassFiles()
 */
public IClassFile[] getClassFiles() throws JavaModelException {
	if (getKind() == IPackageFragmentRoot.K_SOURCE) {
		return NO_CLASSFILES;
	}

	ArrayList list = getChildrenOfType(CLASS_FILE);
	IClassFile[] array= new IClassFile[list.size()];
	list.toArray(array);
	return array;
}
 
Example 15
Source File: CleanUpAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isEnabled(IStructuredSelection selection) {
	Object[] selected= selection.toArray();
	for (int i= 0; i < selected.length; i++) {
		try {
			if (selected[i] instanceof IJavaElement) {
				IJavaElement elem= (IJavaElement)selected[i];
				if (elem.exists()) {
					switch (elem.getElementType()) {
						case IJavaElement.TYPE:
							return elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT; // for browsing perspective
						case IJavaElement.COMPILATION_UNIT:
							return true;
						case IJavaElement.IMPORT_CONTAINER:
							return true;
						case IJavaElement.PACKAGE_FRAGMENT:
						case IJavaElement.PACKAGE_FRAGMENT_ROOT:
							IPackageFragmentRoot root= (IPackageFragmentRoot)elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
							return (root.getKind() == IPackageFragmentRoot.K_SOURCE);
						case IJavaElement.JAVA_PROJECT:
							// https://bugs.eclipse.org/bugs/show_bug.cgi?id=65638
							return true;
					}
				}
			} else if (selected[i] instanceof LogicalPackage) {
				return true;
			} else if (selected[i] instanceof IWorkingSet) {
				IWorkingSet workingSet= (IWorkingSet) selected[i];
				return IWorkingSetIDs.JAVA.equals(workingSet.getId());
			}
		} catch (JavaModelException e) {
			if (!e.isDoesNotExist()) {
				JavaPlugin.log(e);
			}
		}
	}
	return false;
}
 
Example 16
Source File: CheckProjectHelper.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks that the Java file for the given class name exists in the project.
 *
 * @param project
 *          the project, must not be {@code null}
 * @param className
 *          the class name, must not be {@code null}
 * @return whether the corresponding java file exists in the project
 */
public boolean isJavaFilePresent(final IProject project, final String className) {
  int packageSplitIndex = className.lastIndexOf('.');
  if (packageSplitIndex == -1) {
    throw new IllegalArgumentException(String.format("%s is a simple class name or not a class name at all.", className));
  }
  int fileNameEndIndex = className.indexOf('$');
  if (fileNameEndIndex == -1) {
    fileNameEndIndex = className.length();
  }
  String packageName = className.substring(0, packageSplitIndex);
  String fileName = className.substring(packageSplitIndex + 1, fileNameEndIndex) + ".java";
  try {
    if (project.hasNature(JavaCore.NATURE_ID)) {
      for (IPackageFragmentRoot root : JavaCore.create(project).getAllPackageFragmentRoots()) {
        if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
          for (IJavaElement element : root.getChildren()) {
            if (element instanceof IPackageFragment && packageName.equals(element.getElementName())
                && ((IPackageFragment) element).getCompilationUnit(fileName).exists()) {
              return true;
            }
          }
        }
      }
    }
  } catch (CoreException e) {
    LOGGER.error(String.format("Failed to read project %s.", project.getName()), e);
  }
  return false;
}
 
Example 17
Source File: DeveloperProject.java    From CogniCrypt with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return Path to Source Folder of Project.
 * @throws CoreException See {@link org.eclipse.core.resources.IProject#hasNature(String) hasNature()}
 */
public String getSourcePath() throws CoreException {
	if (this.project.isOpen() && this.project.hasNature(Constants.JavaNatureID)) {
		for (final IClasspathEntry entry : JavaCore.create(this.project).getResolvedClasspath(true)) {
			if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
				return entry.getPath().removeFirstSegments(1).toOSString();
			}
		}
	}
	return null;
}
 
Example 18
Source File: JavaSearchNameEnvironment.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void computeClasspathLocations(IWorkspaceRoot workspaceRoot, JavaProject javaProject) {

	IPackageFragmentRoot[] roots = null;
	try {
		roots = javaProject.getAllPackageFragmentRoots();
	} catch (JavaModelException e) {
		// project doesn't exist
		this.locations = new ClasspathLocation[0];
		return;
	}
	int length = roots.length;
	ClasspathLocation[] cpLocations = new ClasspathLocation[length];
	int index = 0;
	JavaModelManager manager = JavaModelManager.getJavaModelManager();
	for (int i = 0; i < length; i++) {
		PackageFragmentRoot root = (PackageFragmentRoot) roots[i];
		IPath path = root.getPath();
		try {
			if (root.isArchive()) {
				ZipFile zipFile = manager.getZipFile(path);
				cpLocations[index++] = new ClasspathJar(zipFile, ((ClasspathEntry) root.getRawClasspathEntry()).getAccessRuleSet());
			} else {
				Object target = JavaModel.getTarget(path, true);
				if (target == null) {
					// target doesn't exist any longer
					// just resize cpLocations
					System.arraycopy(cpLocations, 0, cpLocations = new ClasspathLocation[cpLocations.length-1], 0, index);
				} else if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
					cpLocations[index++] = new ClasspathSourceDirectory((IContainer)target, root.fullExclusionPatternChars(), root.fullInclusionPatternChars());
				} else {
					cpLocations[index++] = ClasspathLocation.forBinaryFolder((IContainer) target, false, ((ClasspathEntry) root.getRawClasspathEntry()).getAccessRuleSet());
				}
			}
		} catch (CoreException e1) {
			// problem opening zip file or getting root kind
			// consider root corrupt and ignore
			// just resize cpLocations
			System.arraycopy(cpLocations, 0, cpLocations = new ClasspathLocation[cpLocations.length-1], 0, index);
		}
	}
	this.locations = cpLocations;
}
 
Example 19
Source File: JavaModelUtility.java    From JDeodorant with MIT License 4 votes vote down vote up
public static Set<String> getAllSourceDirectories(IJavaProject jProject)
		throws JavaModelException {
	/*
	 * We sort the src paths by their character lengths in a non-increasing
	 * order, because we are going to see whether a Java file's path starts
	 * with a specific source path For example, if the Java file's path is
	 * "src/main/org/blah/blah", the "src/main" is considered the source
	 * path not "src/"
	 */
	Set<String> allSrcDirectories = new TreeSet<String>(new Comparator<String>() {
		public int compare(String o1, String o2) {
			if (o1.equals(o2))
				return 0;

			if (o1.length() == o2.length())
				return 1;

			return -Integer.compare(o1.length(), o2.length());
		}
	});

	IClasspathEntry[] classpathEntries = jProject
			.getResolvedClasspath(true);

	for (int i = 0; i < classpathEntries.length; i++) {
		IClasspathEntry entry = classpathEntries[i];
		if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
			IPath path = entry.getPath();
			if (path.toString().equals(
					"/" + jProject.getProject().getName()))
				allSrcDirectories.add(path.toString());
			else if (path.toString().length() > jProject.getProject()
					.getName().length() + 2) {
				String srcDirectory = path.toString().substring(
						jProject.getProject().getName().length() + 2);
				allSrcDirectories.add(srcDirectory);
			}
		}
	}
	return allSrcDirectories;
}
 
Example 20
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());
}