Java Code Examples for org.eclipse.jdt.core.IClasspathEntry#getContentKind()

The following examples show how to use org.eclipse.jdt.core.IClasspathEntry#getContentKind() . 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: JavaProject.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @see IJavaProject
 */
public IPath readOutputLocation() {
	// Read classpath file without creating markers nor logging problems
	IClasspathEntry[][] classpath = readFileEntries(null/*not interested in unknown elements*/);
	if (classpath[0] == JavaProject.INVALID_CLASSPATH)
		return defaultOutputLocation();

	// extract the output location
	IPath outputLocation = null;
	if (classpath[0].length > 0) {
		IClasspathEntry entry = classpath[0][classpath[0].length - 1];
		if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) {
			outputLocation = entry.getPath();
		}
	}
	return outputLocation;
}
 
Example 2
Source File: JavaProject.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @see IJavaProject
 */
public IClasspathEntry[] readRawClasspath() {
	// Read classpath file without creating markers nor logging problems
	IClasspathEntry[][] classpath = readFileEntries(null/*not interested in unknown elements*/);
	if (classpath[0] == JavaProject.INVALID_CLASSPATH)
		return defaultClasspath();

	// discard the output location
	if (classpath[0].length > 0) {
		IClasspathEntry entry = classpath[0][classpath[0].length - 1];
		if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) {
			IClasspathEntry[] copy = new IClasspathEntry[classpath[0].length - 1];
			System.arraycopy(classpath[0], 0, copy, 0, copy.length);
			classpath[0] = copy;
		}
	}
	return classpath[0];
}
 
Example 3
Source File: DuplicatedCode.java    From JDeodorant with MIT License 6 votes vote down vote up
private ICompilationUnit getICompilationUnit(IJavaProject iJavaProject, String fullName) {
	try {
		IClasspathEntry[] classpathEntries = iJavaProject.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().length() > iJavaProject.getProject().getName().length() + 2) {
					String fullPath = path.toString().substring(iJavaProject.getProject().getName().length() + 2) + "/" + fullName;

					ICompilationUnit iCompilationUnit = (ICompilationUnit)JavaCore.create(iJavaProject.getProject().getFile(fullPath));
					if (iCompilationUnit != null && iCompilationUnit.exists())
						return iCompilationUnit;
				}
			}
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 4
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 5
Source File: CrySLBuilder.java    From CogniCrypt with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected IProject[] build(int kind, Map<String, String> args, IProgressMonitor monitor) throws CoreException {
	try {
		CrySLParser csmr = new CrySLParser(getProject());
		final IProject curProject = getProject();
		List<IPath> resourcesPaths = new ArrayList<IPath>();
		List<IPath> outputPaths = new ArrayList<IPath>();
		IJavaProject projectAsJavaProject = JavaCore.create(curProject);

		for (final IClasspathEntry entry : projectAsJavaProject.getResolvedClasspath(true)) {
			if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
				IPath res = entry.getPath();
				if (!projectAsJavaProject.getPath().equals(res)) {
					resourcesPaths.add(res);
					IPath outputLocation = entry.getOutputLocation();
					outputPaths.add(outputLocation != null ? outputLocation : projectAsJavaProject.getOutputLocation());
				}
			}
		}
		for (int i = 0; i < resourcesPaths.size(); i++) {
			CrySLParserUtils.storeRulesToFile(csmr.readRulesWithin(resourcesPaths.get(i).toOSString()),
					ResourcesPlugin.getWorkspace().getRoot().findMember(outputPaths.get(i)).getLocation().toOSString());
		}
	}
	catch (IOException e) {
		Activator.getDefault().logError(e, "Build of CrySL rules failed.");
	}

	return null;
}
 
Example 6
Source File: CrySLBuilder.java    From CogniCrypt with Eclipse Public License 2.0 5 votes vote down vote up
protected void clean(IProgressMonitor monitor) throws CoreException {
	IProject project = getProject();
	for (final IClasspathEntry entry : JavaCore.create(project).getResolvedClasspath(true)) {
		if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE && !(entry.getPath().toPortableString().lastIndexOf(Constants.innerFileSeparator) < 1)) {
			IPath outputLocation = entry.getOutputLocation();
			if (outputLocation != null) {
				Arrays.asList(new File(project.getLocation().toOSString() + Constants.outerFileSeparator + outputLocation.removeFirstSegments(1).toOSString()).listFiles())
						.parallelStream().filter(e -> e.exists()).forEach(e -> e.delete());
			}
		}
	}
}
 
Example 7
Source File: JarImportWizard.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Is the specified class path entry pointing to a valid location for
 * import?
 *
 * @param entry
 *            the class path entry
 * @return <code>true</code> if it is a valid package fragment root,
 *         <code>false</code> otherwise
 */
public static boolean isValidClassPathEntry(final IClasspathEntry entry) {
	Assert.isNotNull(entry);
	final int kind= entry.getEntryKind();
	if (kind == IClasspathEntry.CPE_LIBRARY)
		return entry.getContentKind() == IPackageFragmentRoot.K_BINARY;
	else if (kind == IClasspathEntry.CPE_VARIABLE)
		return true; // be optimistic
	return false;
}
 
Example 8
Source File: ClasspathChange.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int classpathContains(IClasspathEntry[] list, IClasspathEntry entry) {
	IPath[] exclusionPatterns = entry.getExclusionPatterns();
	IPath[] inclusionPatterns = entry.getInclusionPatterns();
	int listLen = list == null ? 0 : list.length;
	nextEntry: for (int i = 0; i < listLen; i++) {
		IClasspathEntry other = list[i];
		if (other.getContentKind() == entry.getContentKind()
			&& other.getEntryKind() == entry.getEntryKind()
			&& other.isExported() == entry.isExported()
			&& other.getPath().equals(entry.getPath())) {
				// check custom outputs
				IPath entryOutput = entry.getOutputLocation();
				IPath otherOutput = other.getOutputLocation();
				if (entryOutput == null) {
					if (otherOutput != null)
						continue;
				} else {
					if (!entryOutput.equals(otherOutput))
						continue;
				}

				// check inclusion patterns
				IPath[] otherIncludes = other.getInclusionPatterns();
				if (inclusionPatterns != otherIncludes) {
				    if (inclusionPatterns == null) continue;
					int includeLength = inclusionPatterns.length;
					if (otherIncludes == null || otherIncludes.length != includeLength)
						continue;
					for (int j = 0; j < includeLength; j++) {
						// compare toStrings instead of IPaths
						// since IPath.equals is specified to ignore trailing separators
						if (!inclusionPatterns[j].toString().equals(otherIncludes[j].toString()))
							continue nextEntry;
					}
				}
				// check exclusion patterns
				IPath[] otherExcludes = other.getExclusionPatterns();
				if (exclusionPatterns != otherExcludes) {
				    if (exclusionPatterns == null) continue;
					int excludeLength = exclusionPatterns.length;
					if (otherExcludes == null || otherExcludes.length != excludeLength)
						continue;
					for (int j = 0; j < excludeLength; j++) {
						// compare toStrings instead of IPaths
						// since IPath.equals is specified to ignore trailing separators
						if (!exclusionPatterns[j].toString().equals(otherExcludes[j].toString()))
							continue nextEntry;
					}
				}
				return i;
		}
	}
	return -1;
}
 
Example 9
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;
}