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

The following examples show how to use org.eclipse.jdt.core.IJavaProject#isOnClasspath() . 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: ActionUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isOnBuildPath(IJavaElement element) {
       //fix for bug http://dev.eclipse.org/bugs/show_bug.cgi?id=20051
       if (element.getElementType() == IJavaElement.JAVA_PROJECT)
           return true;
	IJavaProject project= element.getJavaProject();
	try {
		if (!project.isOnClasspath(element))
			return false;
		IProject resourceProject= project.getProject();
		if (resourceProject == null)
			return false;
		IProjectNature nature= resourceProject.getNature(JavaCore.NATURE_ID);
		// We have a Java project
		if (nature != null)
			return true;
	} catch (CoreException e) {
	}
	return false;
}
 
Example 2
Source File: BuildpathIndicatorLabelDecorator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ImageDescriptor getOverlay(Object element) {
	if (element instanceof IResource) {
		IResource resource= (IResource) element;
		IProject project= resource.getProject();
		if (project != null) {
			IJavaProject javaProject= JavaCore.create(project);
			if (javaProject != null) {
				if (javaProject.isOnClasspath(resource)) {
					IJavaElement javaElement= JavaCore.create(resource, javaProject);
					try {
						if (javaElement instanceof IPackageFragmentRoot
								&& ((IPackageFragmentRoot)javaElement).getKind() != IPackageFragmentRoot.K_SOURCE) {
							return JavaPluginImages.DESC_OVR_LIBRARY;
						}
					} catch (JavaModelException e) {
						return null;
					}
				}
			}
		}
	}
	return null;
}
 
Example 3
Source File: ClasspathModifier.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * For a given list of entries, find out what representation they
 * will have in the project and return a list with corresponding
 * elements.
 *
 * @param entries a list of entries to find an appropriate representation
 * for. The list can contain elements of two types:
 * <li><code>IResource</code></li>
 * <li><code>IJavaElement</code></li>
 * @param project the Java project
 * @return a list of elements corresponding to the passed entries.
 */
public static List<?> getCorrespondingElements(List<?> entries, IJavaProject project) {
	List<IAdaptable> result= new ArrayList<IAdaptable>();
	for (int i= 0; i < entries.size(); i++) {
		Object element= entries.get(i);
		IPath path;
		if (element instanceof IResource)
			path= ((IResource) element).getFullPath();
		else
			path= ((IJavaElement) element).getPath();
		IResource resource= getResource(path, project);
		if (resource != null) {
			IJavaElement elem= JavaCore.create(resource);
			if (elem != null && project.isOnClasspath(elem))
				result.add(elem);
			else
				result.add(resource);
		}

	}
	return result;
}
 
Example 4
Source File: Importer.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 6 votes vote down vote up
private void addMembersOfFolderToClasspath(String path, IProgressMonitor monitor, IJavaProject javaProject) throws CoreException, JavaModelException {
	IFolder folder = javaProject.getProject().getFolder(path);
	 if (folder != null && folder.exists())
	 {
		 for (IResource res : folder.members())
		 {
			 if (res.getFileExtension() != null && res.getFileExtension().equals("jar") && res.exists())
			 {
				 // check if this Resource is on the classpath
				 if (!javaProject.isOnClasspath(res)) {
					 if (DEBUG)
						 Activator.log("Adding library [" + res.getFullPath() + "] to classpath for project [" + javaProject.getProject().getName() + "]");
					 FixProjectsUtils.addToClassPath(res, IClasspathEntry.CPE_LIBRARY, javaProject, monitor);
				 }
			 }
		 }
	 }
}
 
Example 5
Source File: JdtToBeBuiltComputer.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isBuiltByUpstream(IPackageFragmentRoot root, IProject project, IProject[] projectsInCorrectBuildOrder) {
	for (IProject p : projectsInCorrectBuildOrder) {
		if (p.equals(project))
			return false;
		if (XtextProjectHelper.hasNature(p) && XtextProjectHelper.hasBuilder(p)) {
			IJavaProject javaProject = JavaCore.create(p);
			if (javaProject.exists()) {
				if (javaProject.isOnClasspath(root)) {
					if (log.isTraceEnabled())
						log.trace("Build of project '"+project.getName()+"' skips indexing classpath entry '"+root.getPath()+"' because it already indexed by "+javaProject.getElementName());
					return true;
				}
			}
		}
	}
	return false;
}
 
Example 6
Source File: Importer.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Some addons don't have their classpath correctly configured in Eclipse and won't compile
 * 
 * @param monitor
 * @param javaProject
 * @throws JavaModelException
 */
private void fixAddons(IProgressMonitor monitor, IJavaProject javaProject) throws JavaModelException {
	
	final String projectName = javaProject.getProject().getName();
	
	if (projectName.equals("orderselfserviceaddon") || projectName.equals("notificationaddon")
			|| projectName.equals("customerinterestsaddon") || projectName.equals("consignmenttrackingaddon")) {
		
		IProject acceleratorstorefrontcommonsProject = ResourcesPlugin.getWorkspace().getRoot()
				.getProject("acceleratorstorefrontcommons");
		if (acceleratorstorefrontcommonsProject != null && acceleratorstorefrontcommonsProject.exists()) {
			if (!javaProject.isOnClasspath(acceleratorstorefrontcommonsProject)) {
				FixProjectsUtils.addToClassPath(acceleratorstorefrontcommonsProject, IClasspathEntry.CPE_PROJECT,
						javaProject, monitor);
			}
		}
	}
	
	if (projectName.equals("stocknotificationaddon")) {
		IProject notificationfacadesProject = ResourcesPlugin.getWorkspace().getRoot()
				.getProject("notificationfacades");
		if (notificationfacadesProject != null && notificationfacadesProject.exists()) {
			if (!javaProject.isOnClasspath(notificationfacadesProject)) {
				FixProjectsUtils.addToClassPath(notificationfacadesProject, IClasspathEntry.CPE_PROJECT,
						javaProject, monitor);
			}
		}
	}
}
 
Example 7
Source File: ResetAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean canHandle(IStructuredSelection elements) {
	try {
        for (Iterator<?> iterator= elements.iterator(); iterator.hasNext();) {
            Object element= iterator.next();
            if (element instanceof IJavaProject) {
            	IJavaProject project= (IJavaProject)element;
            	if (!project.isOnClasspath(project))
            		return false;

            	IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(project.getPath(), project, IClasspathEntry.CPE_SOURCE);
                if (entry.getInclusionPatterns().length == 0 && entry.getExclusionPatterns().length == 0)
                    return false;

        		return true;
            } else if (element instanceof IPackageFragmentRoot) {
            	if (ClasspathModifier.filtersSet((IPackageFragmentRoot)element))
            		return true;
            } else if (element instanceof CPListElementAttribute) {
            	if (!ClasspathModifier.isDefaultOutputFolder((CPListElementAttribute)element))
            		return true;
            } else {
            	return false;
            }
        }
       } catch (JavaModelException e) {
        return false;
       }
	return false;
}
 
Example 8
Source File: FixProjectsUtils.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 5 votes vote down vote up
private static void removeSourceDirectoryIfNotExsistingForDir(IProgressMonitor monitor, IProject project, IJavaProject javaProject, String dir) throws JavaModelException {
	
	if (!project.getFolder(dir).exists())
	{
		if (javaProject.isOnClasspath(project.getFolder(dir)))
		{
			removeFromClassPath(project.getFolder(dir), IClasspathEntry.CPE_SOURCE, javaProject, monitor);
		}				
	}		
}
 
Example 9
Source File: ExtensionModuleTrimmer.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 5 votes vote down vote up
private static void removeSourceFolder(IProgressMonitor monitor, IProject project, String folderName,
		String classpathEntry) throws CoreException, JavaModelException {
	IFolder webFolder = project.getFolder(folderName);
	if (webFolder != null) {
		webFolder.delete(true, false, monitor);
	}
	// Project is missing required source folder: 'web/src'
	IJavaProject javaProject = JavaCore.create(project);
	if (javaProject.isOnClasspath(project.getFolder(classpathEntry))) {
		FixProjectsUtils.removeFromClassPath(project.getFolder(classpathEntry), IClasspathEntry.CPE_SOURCE,
				javaProject, monitor);
	}
}
 
Example 10
Source File: ClientBundleResourceChangeListener.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isPossibleClientBundleResourceDelta(
    IResourceDelta delta) {
  IResource resource = delta.getResource();

  if (resource.isDerived()) {
    return false;
  }

  if (resource.getType() != IResource.FILE) {
    return false;
  }

  // We only care about additions/deletions. If the contents of a
  // resource file change, it doesn't affect our validation.
  if (delta.getKind() != IResourceDelta.ADDED
      && delta.getKind() != IResourceDelta.REMOVED) {
    return false;
  }

  // Ignore Java source changes, since we're only interested in resource files
  if ("java".equals(resource.getFileExtension())) {
    return false;
  }

  if (!GWTNature.isGWTProject(resource.getProject())) {
    return false;
  }

  IJavaProject javaProject = JavaCore.create(resource.getProject());
  if (!javaProject.isOnClasspath(resource)) {
    return false;
  }

  // All checks passed; it looks like a change we care about
  return true;
}
 
Example 11
Source File: ClientBundleResource.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private IStatus validateFile(IJavaProject javaProject) {
  if (file == null || !file.exists()) {
    return errorStatus("The file ''{0}'' does not exist", file.getName());
  }

  if (!javaProject.isOnClasspath(file)) {
    return errorStatus("The file ''{0}'' is not on the project''s classpath", file.getName());
  }

  return StatusUtilities.OK_STATUS;
}
 
Example 12
Source File: DestinationContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Object[] getResources(IContainer container) {
	try {
		IResource[] members= container.members();
		IJavaProject javaProject= JavaCore.create(container.getProject());
		if (javaProject == null || !javaProject.exists())
			return members;
		boolean isFolderOnClasspath = javaProject.isOnClasspath(container);
		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);
			}
		}
		return nonJavaResources.toArray();
	} catch(CoreException e) {
		return NO_CHILDREN;
	}
}
 
Example 13
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 14
Source File: JavaDebugDelegateCommandHandler.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isOnClasspath(List<Object> arguments) throws DebugException {
    if (arguments.size() < 1) {
        throw new DebugException("No file uri is specified.");
    }

    String uri = (String) arguments.get(0);
    final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
    if (unit == null || unit.getResource() == null || !unit.getResource().exists()) {
        throw new DebugException("The compilation unit " + uri + " doesn't exist.");
    }

    IJavaProject javaProject = unit.getJavaProject();
    return javaProject == null || javaProject.isOnClasspath(unit);
}
 
Example 15
Source File: ClassFileDocumentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Recursively check whether the class file has been deleted.
 * 
 * @param input the package fragment root
 * @param delta the Java element delta
 * @return <code>true</code> if delta processing can be stopped
 */
protected boolean check(IPackageFragmentRoot input, IJavaElementDelta delta) {
	IJavaElement element= delta.getElement();

	if ((delta.getKind() & IJavaElementDelta.REMOVED) != 0 || (delta.getFlags() & IJavaElementDelta.F_CLOSED) != 0) {
		// http://dev.eclipse.org/bugs/show_bug.cgi?id=19023
		if (element.equals(input.getJavaProject()) || element.equals(input)) {
			handleDeleted(fInput);
			return true;
		}
	}

	if (((delta.getFlags() & IJavaElementDelta.F_ARCHIVE_CONTENT_CHANGED) != 0) && input.equals(element)) {
		handleDeleted(fInput);
		return true;
	}

	if (((delta.getFlags() & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) != 0) && input.equals(element)) {
		handleDeleted(fInput);
		return true;
	}

	IJavaElementDelta[] subdeltas= delta.getAffectedChildren();
	for (int i= 0; i < subdeltas.length; i++) {
		if (check(input, subdeltas[i]))
			return true;
	}

	if ((delta.getFlags() & IJavaElementDelta.F_SOURCEDETACHED) != 0 ||
		(delta.getFlags() & IJavaElementDelta.F_SOURCEATTACHED) != 0)
	{
		IClassFile file= fInput != null ? fInput.getClassFile() : null;
		IJavaProject project= input != null ? input.getJavaProject() : null;

		boolean isOnClasspath= false;
		if (file != null && project != null)
			isOnClasspath= project.isOnClasspath(file);

		if (isOnClasspath) {
			fireInputChanged(fInput);
			return false;
		} else {
			handleDeleted(fInput);
			return true;
		}
	}

	return false;
}
 
Example 16
Source File: ClasspathModifier.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Validate the new entry in the context of the existing entries. Furthermore,
 * check if exclusion filters need to be applied and do so if necessary.
 *
 * If validation was successful, add the new entry to the list of existing entries.
 *
 * @param entry the entry to be validated and added to the list of existing entries.
 * @param existingEntries a list of existing entries representing the build path
 * @param project the Java project
 * @throws CoreException in case that validation fails
 */
private static void validateAndAddEntry(CPListElement entry, List<CPListElement> existingEntries, IJavaProject project) throws CoreException {
	IPath path= entry.getPath();
	IPath projPath= project.getProject().getFullPath();
	IWorkspaceRoot workspaceRoot= ResourcesPlugin.getWorkspace().getRoot();
	IStatus validate= workspaceRoot.getWorkspace().validatePath(path.toString(), IResource.FOLDER);
	StatusInfo rootStatus= new StatusInfo();
	rootStatus.setOK();
	boolean isExternal= isExternalArchiveOrLibrary(entry);
	if (!isExternal && validate.matches(IStatus.ERROR) && !project.getPath().equals(path)) {
		rootStatus.setError(Messages.format(NewWizardMessages.NewSourceFolderWizardPage_error_InvalidRootName, validate.getMessage()));
		throw new CoreException(rootStatus);
	} else {
		if (!isExternal && !project.getPath().equals(path)) {
			IResource res= workspaceRoot.findMember(path);
			if (res != null) {
				if (res.getType() != IResource.FOLDER && res.getType() != IResource.FILE) {
					rootStatus.setError(NewWizardMessages.NewSourceFolderWizardPage_error_NotAFolder);
					throw new CoreException(rootStatus);
				}
			} else {
				URI projLocation= project.getProject().getLocationURI();
				if (projLocation != null) {
					IFileStore store= EFS.getStore(projLocation).getFileStore(path);
					if (store.fetchInfo().exists()) {
						rootStatus.setError(NewWizardMessages.NewSourceFolderWizardPage_error_AlreadyExistingDifferentCase);
						throw new CoreException(rootStatus);
					}
				}
			}
		}

		for (int i= 0; i < existingEntries.size(); i++) {
			CPListElement curr= existingEntries.get(i);
			if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
				if (path.equals(curr.getPath()) && !project.getPath().equals(path)) {
					rootStatus.setError(NewWizardMessages.NewSourceFolderWizardPage_error_AlreadyExisting);
					throw new CoreException(rootStatus);
				}
			}
		}

		if (!isExternal && !entry.getPath().equals(project.getPath()))
			exclude(entry.getPath(), existingEntries, new ArrayList<CPListElement>(), project, null);

		IPath outputLocation= project.getOutputLocation();
		insertAtEndOfCategory(entry, existingEntries);

		IClasspathEntry[] entries= convert(existingEntries);

		IJavaModelStatus status= JavaConventions.validateClasspath(project, entries, outputLocation);
		if (!status.isOK()) {
			if (outputLocation.equals(projPath)) {
				IStatus status2= JavaConventions.validateClasspath(project, entries, outputLocation);
				if (status2.isOK()) {
					if (project.isOnClasspath(project)) {
						rootStatus.setInfo(Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_ReplaceSFandOL, BasicElementLabels.getPathLabel(outputLocation, false)));
					} else {
						rootStatus.setInfo(Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_ReplaceOL, BasicElementLabels.getPathLabel(outputLocation, false)));
					}
					return;
				}
			}
			rootStatus.setError(status.getMessage());
			throw new CoreException(rootStatus);
		}

		if (isSourceFolder(project) || project.getPath().equals(path)) {
			rootStatus.setWarning(NewWizardMessages.NewSourceFolderWizardPage_warning_ReplaceSF);
			return;
		}

		rootStatus.setOK();
		return;
	}
}
 
Example 17
Source File: JavaElementContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean isOnClassPath(ICompilationUnit element) {
	IJavaProject project= element.getJavaProject();
	if (project == null || !project.exists())
		return false;
	return project.isOnClasspath(element);
}
 
Example 18
Source File: JavaBrowsingContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean isOnClassPath(ICompilationUnit element) throws JavaModelException {
	IJavaProject project= element.getJavaProject();
	if (project == null || !project.exists())
		return false;
	return project.isOnClasspath(element);
}
 
Example 19
Source File: PackageExplorerPart.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean isOnClassPath(IFile file) {
	IJavaProject jproject= JavaCore.create(file.getProject());
	return jproject.isOnClasspath(file);
}
 
Example 20
Source File: PackageExplorerContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean isOnClassPath(ICompilationUnit element) {
	IJavaProject project= element.getJavaProject();
	if (project == null || !project.exists())
		return false;
	return project.isOnClasspath(element);
}