Java Code Examples for org.eclipse.jdt.core.IPackageFragmentRoot#getRawClasspathEntry()

The following examples show how to use org.eclipse.jdt.core.IPackageFragmentRoot#getRawClasspathEntry() . 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: JDTAwareEclipseResourceFileSystemAccess2.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.6
 */
protected void addToSourceFolders(IContainer container) throws JavaModelException {
	IJavaProject jp = JavaCore.create(container.getProject());
	if (jp.exists() && !jp.isOnClasspath(container)) {
		if (getCurrentSource() != null) {
			IPackageFragmentRoot packageFragmentRoot = jp.findPackageFragmentRoot(jp.getPath().append(getCurrentSource()));
			if (packageFragmentRoot != null) {
				IClasspathEntry currentClasspathEntry = packageFragmentRoot.getRawClasspathEntry();
				if (currentClasspathEntry != null) {
					insertClasspathEntry(container, currentClasspathEntry, jp);
					return;
				}
			}
		}
		addClasspathEntry(container, jp);
	}
}
 
Example 2
Source File: JarImportWizardAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void selectionChanged(final IAction action, final ISelection selection) {
	fSelection= null;
	if (selection instanceof IStructuredSelection) {
		final IStructuredSelection structured= (IStructuredSelection) selection;
		if (structured.size() == 1) {
			final Object element= structured.getFirstElement();
			if (element instanceof IPackageFragmentRoot) {
				final IPackageFragmentRoot root= (IPackageFragmentRoot) element;
				try {
					final IClasspathEntry entry= root.getRawClasspathEntry();
					if (JarImportWizard.isValidClassPathEntry(entry) && JarImportWizard.isValidJavaProject(root.getJavaProject())
							&& root.getResolvedClasspathEntry().getReferencingEntry() == null) {
						fSelection= structured;
					}
				} catch (JavaModelException exception) {
					JavaPlugin.log(exception);
				}
			}
		}
	}
	action.setEnabled(fSelection != null);
}
 
Example 3
Source File: JavaSearchScopeFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public boolean isInsideJRE(IJavaElement element) {
	IPackageFragmentRoot root= (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (root != null) {
		try {
			IClasspathEntry entry= root.getRawClasspathEntry();
			if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
				IClasspathContainer container= JavaCore.getClasspathContainer(entry.getPath(), root.getJavaProject());
				return container != null && container.getKind() == IClasspathContainer.K_DEFAULT_SYSTEM;
			}
			return false;
		} catch (JavaModelException e) {
			JavaPlugin.log(e);
		}
	}
	return true; // include JRE in doubt
}
 
Example 4
Source File: JarImportWizard.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void init(final IWorkbench workbench, final IStructuredSelection selection) {
	if (selection != null && selection.size() == 1) {
		final Object element= selection.getFirstElement();
		if (element instanceof IPackageFragmentRoot) {
			final IPackageFragmentRoot root= (IPackageFragmentRoot) element;
			try {
				final IClasspathEntry entry= root.getRawClasspathEntry();
				if (isValidClassPathEntry(entry)
						&& root.getResolvedClasspathEntry().getReferencingEntry() == null)
					fImportData.setPackageFragmentRoot(root);
			} catch (JavaModelException exception) {
				JavaPlugin.log(exception);
			}
		}
	}
}
 
Example 5
Source File: ClasspathModifier.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Find out whether one of the <code>IResource</code>'s parents
 * is excluded.
 *
 * @param resource check the resources parents whether they are
 * excluded or not
 * @param project the Java project
 * @return <code>true</code> if there is an excluded parent,
 * <code>false</code> otherwise
 * @throws JavaModelException
 */
public static boolean parentExcluded(IResource resource, IJavaProject project) throws JavaModelException {
	if (resource.getFullPath().equals(project.getPath()))
		return false;
	IPackageFragmentRoot root= getFragmentRoot(resource, project, null);
	if (root == null) {
		return true;
	}
	IPath path= resource.getFullPath().removeFirstSegments(root.getPath().segmentCount());
	IClasspathEntry entry= root.getRawClasspathEntry();
	if (entry == null)
		return true; // there is no build path entry, this is equal to the fact that the parent is excluded
	while (path.segmentCount() > 0) {
		if (contains(path, entry.getExclusionPatterns(), null))
			return true;
		path= path.removeLastSegments(1);
	}
	return false;
}
 
Example 6
Source File: JavadocContentAccess2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static String getSourceAttachmentEncoding(IPackageFragmentRoot root) throws JavaModelException {
	String encoding= ResourcesPlugin.getEncoding();
	IClasspathEntry entry= root.getRawClasspathEntry();

	if (entry != null) {
		int kind= entry.getEntryKind();
		if (kind == IClasspathEntry.CPE_LIBRARY || kind == IClasspathEntry.CPE_VARIABLE) {
			IClasspathAttribute[] extraAttributes= entry.getExtraAttributes();
			for (int i= 0; i < extraAttributes.length; i++) {
				IClasspathAttribute attrib= extraAttributes[i];
				if (IClasspathAttribute.SOURCE_ATTACHMENT_ENCODING.equals(attrib.getName())) {
					return attrib.getValue();
				}
			}
		}
	}

	return encoding;
}
 
Example 7
Source File: BinaryRefactoringHistoryWizard.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Configures the classpath of the project before refactoring.
 *
 * @param project
 *            the java project
 * @param root
 *            the package fragment root to refactor
 * @param folder
 *            the temporary source folder
 * @param monitor
 *            the progress monitor to use
 * @throws IllegalStateException
 *             if the plugin state location does not exist
 * @throws CoreException
 *             if an error occurs while configuring the class path
 */
private static void configureClasspath(final IJavaProject project, final IPackageFragmentRoot root, final IFolder folder, final IProgressMonitor monitor) throws IllegalStateException, CoreException {
	try {
		monitor.beginTask(JarImportMessages.JarImportWizard_prepare_import, 200);
		final IClasspathEntry entry= root.getRawClasspathEntry();
		final IClasspathEntry[] entries= project.getRawClasspath();
		final List<IClasspathEntry> list= new ArrayList<IClasspathEntry>();
		list.addAll(Arrays.asList(entries));
		final IFileStore store= EFS.getLocalFileSystem().getStore(JavaPlugin.getDefault().getStateLocation().append(STUB_FOLDER).append(project.getElementName()));
		if (store.fetchInfo(EFS.NONE, new SubProgressMonitor(monitor, 25, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)).exists())
			store.delete(EFS.NONE, new SubProgressMonitor(monitor, 25, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
		store.mkdir(EFS.NONE, new SubProgressMonitor(monitor, 25, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
		folder.createLink(store.toURI(), IResource.NONE, new SubProgressMonitor(monitor, 25, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
		addExclusionPatterns(list, folder.getFullPath());
		for (int index= 0; index < entries.length; index++) {
			if (entries[index].equals(entry))
				list.add(index, JavaCore.newSourceEntry(folder.getFullPath()));
		}
		project.setRawClasspath(list.toArray(new IClasspathEntry[list.size()]), false, new SubProgressMonitor(monitor, 100, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
	} finally {
		monitor.done();
	}
}
 
Example 8
Source File: ReorgCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean canModifyAccessRules(IBinding binding) {
	IJavaElement element= binding.getJavaElement();
	if (element == null)
		return false;

	IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element);
	if (root == null)
		return false;

	try {
		IClasspathEntry classpathEntry= root.getRawClasspathEntry();
		if (classpathEntry == null)
			return false;
		if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
			return true;
		if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
			ClasspathContainerInitializer classpathContainerInitializer= JavaCore.getClasspathContainerInitializer(classpathEntry.getPath().segment(0));
			IStatus status= classpathContainerInitializer.getAccessRulesStatus(classpathEntry.getPath(), root.getJavaProject());
			return status.isOK();
		}
	} catch (JavaModelException e) {
		return false;
	}
	return false;
}
 
Example 9
Source File: JavadocContentAccess2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static String getSourceAttachmentEncoding(IPackageFragmentRoot root) throws JavaModelException {
	String encoding = ResourcesPlugin.getEncoding();
	IClasspathEntry entry = root.getRawClasspathEntry();

	if (entry != null) {
		int kind = entry.getEntryKind();
		if (kind == IClasspathEntry.CPE_LIBRARY || kind == IClasspathEntry.CPE_VARIABLE) {
			IClasspathAttribute[] extraAttributes = entry.getExtraAttributes();
			for (int i = 0; i < extraAttributes.length; i++) {
				IClasspathAttribute attrib = extraAttributes[i];
				if (IClasspathAttribute.SOURCE_ATTACHMENT_ENCODING.equals(attrib.getName())) {
					return attrib.getValue();
				}
			}
		}
	}

	return encoding;
}
 
Example 10
Source File: JarImportWizard.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected boolean deconfigureClasspath(final IClasspathEntry[] entries, final IProgressMonitor monitor) throws CoreException {
	final boolean rename= fImportData.isRenameJarFile();
	if (rename && !fCancelled) {
		final IPackageFragmentRoot root= getPackageFragmentRoot();
		if (root != null) {
			final IClasspathEntry entry= root.getRawClasspathEntry();
			for (int index= 0; index < entries.length; index++) {
				if (entries[index].equals(entry)) {
					final IPath path= getTargetPath(entries[index]);
					if (path != null)
						entries[index]= JavaCore.newLibraryEntry(path, entries[index].getSourceAttachmentPath(), entries[index].getSourceAttachmentRootPath(), entries[index].getAccessRules(), entries[index].getExtraAttributes(), entries[index].isExported());
				}
			}
		}
	}
	if (!fCancelled)
		replaceJarFile(new SubProgressMonitor(monitor, 100, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
	return rename;
}
 
Example 11
Source File: ReorgCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void apply(IDocument document) {
	Map<Object, Object> data= null;
	if (fReferencedType != null) {
		IJavaElement elem= fReferencedType.getJavaElement();
		if (elem != null) {
			IPackageFragmentRoot root= (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
			if (root != null) {
				try {
					IClasspathEntry entry= root.getRawClasspathEntry();
					if (entry != null) {
						data= new HashMap<Object, Object>(1);
						data.put(BuildPathsPropertyPage.DATA_REVEAL_ENTRY, entry);
						if (entry.getEntryKind() != IClasspathEntry.CPE_CONTAINER) {
							data.put(BuildPathsPropertyPage.DATA_REVEAL_ATTRIBUTE_KEY, CPListElement.ACCESSRULES);
						}
					}
				} catch (JavaModelException e) {
					// ignore
				}
			}
		}
	}
	PreferencesUtil.createPropertyDialogOn(JavaPlugin.getActiveWorkbenchShell(), fProject, BuildPathsPropertyPage.PROP_ID, null, data).open();
}
 
Example 12
Source File: JavaElementUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param root the package fragment root
 * @return array of projects that have the specified root on their classpath
 * @throws JavaModelException if getting the raw classpath or all Java projects fails
 */
public static IJavaProject[] getReferencingProjects(IPackageFragmentRoot root) throws JavaModelException {
	IClasspathEntry cpe= root.getRawClasspathEntry();
	if (cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
		cpe= root.getResolvedClasspathEntry();
	IJavaProject[] allJavaProjects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
	List<IJavaProject> result= new ArrayList<IJavaProject>(allJavaProjects.length);
	for (int i= 0; i < allJavaProjects.length; i++) {
		IJavaProject project= allJavaProjects[i];
		IPackageFragmentRoot[] roots= project.findPackageFragmentRoots(cpe);
		if (roots.length > 0)
			result.add(project);
	}
	return result.toArray(new IJavaProject[result.size()]);
}
 
Example 13
Source File: ClasspathUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Return the raw classpath entry on the project's classpath that contributes
 * the given type to the project.
 *
 * @param javaProject The java project
 * @param fullyQualifiedName The fully-qualified type name
 * @return The raw classpath entry that contributes the type to the project,
 *         or <code>null</code> if no such classpath entry can be found.
 * @throws JavaModelException
 */
public static IClasspathEntry findRawClasspathEntryFor(
    IJavaProject javaProject, String fullyQualifiedName)
    throws JavaModelException {
  IType type = javaProject.findType(fullyQualifiedName);
  if (type != null) {
    IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);

    JavaProject jProject = (JavaProject) javaProject;

    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    for (IClasspathEntry rawClasspathEntry : rawClasspath) {
      IClasspathEntry[] resolvedClasspath = jProject.resolveClasspath(new IClasspathEntry[] {rawClasspathEntry});

      // was this - which is no longer, internal api refactor
      //IPackageFragmentRoot[] computePackageFragmentRoots = jProject.computePackageFragmentRoots(resolvedClasspath, true, null);

      // now this - from IPackage
      List<IPackageFragmentRoot> fragmentRoots = new ArrayList<IPackageFragmentRoot>();
      for (IClasspathEntry classPathEntry : resolvedClasspath) {
        IPackageFragmentRoot[] foundRoots = javaProject.findPackageFragmentRoots(classPathEntry);
        fragmentRoots.addAll(Arrays.asList(foundRoots));
      }

      IPackageFragmentRoot[] computePackageFragmentRoots = new IPackageFragmentRoot[fragmentRoots.size()];
      fragmentRoots.toArray(computePackageFragmentRoots);

      if (Arrays.asList(computePackageFragmentRoots).contains(
          packageFragmentRoot)) {
        return rawClasspathEntry;
      }
    }

    return packageFragmentRoot.getRawClasspathEntry();
  }

  return null;
}
 
Example 14
Source File: JavaDocLocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static URL getJavadocBaseLocation(IJavaElement element) throws JavaModelException {
	if (element.getElementType() == IJavaElement.JAVA_PROJECT) {
		return getProjectJavadocLocation((IJavaProject) element);
	}

	IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element);
	if (root == null) {
		return null;
	}

	if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
		IClasspathEntry entry= root.getResolvedClasspathEntry();
		URL javadocLocation= getLibraryJavadocLocation(entry);
		if (javadocLocation != null) {
			return getLibraryJavadocLocation(entry);
		}
		entry= root.getRawClasspathEntry();
		switch (entry.getEntryKind()) {
			case IClasspathEntry.CPE_LIBRARY:
			case IClasspathEntry.CPE_VARIABLE:
				return getLibraryJavadocLocation(entry);
			default:
				return null;
		}
	} else {
		return getProjectJavadocLocation(root.getJavaProject());
	}
}
 
Example 15
Source File: ClasspathModifier.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Find out whether the <code>IResource</code> excluded or not.
 *
 * @param resource the resource to be checked
 * @param project the Java project
 * @return <code>true</code> if the resource is excluded, <code>
 * false</code> otherwise
 * @throws JavaModelException
 */
public static boolean isExcluded(IResource resource, IJavaProject project) throws JavaModelException {
	IPackageFragmentRoot root= getFragmentRoot(resource, project, null);
	if (root == null)
		return false;
	String fragmentName= getName(resource.getFullPath(), root.getPath());
	fragmentName= completeName(fragmentName);
	IClasspathEntry entry= root.getRawClasspathEntry();
	return entry != null && contains(new Path(fragmentName), entry.getExclusionPatterns(), null);
}
 
Example 16
Source File: ClasspathModifier.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Check whether the input parameter of type <code>
 * IPackageFragmentRoot</code> has either it's inclusion or
 * exclusion filter or both set (that means they are
 * not empty).
 *
 * @param root the fragment root to be inspected
 * @return <code>true</code> inclusion or exclusion filter set,
 * <code>false</code> otherwise.
 * @throws JavaModelException
 */
public static boolean filtersSet(IPackageFragmentRoot root) throws JavaModelException {
	if (root == null)
		return false;
	IClasspathEntry entry= root.getRawClasspathEntry();
	IPath[] inclusions= entry.getInclusionPatterns();
	IPath[] exclusions= entry.getExclusionPatterns();
	if (inclusions != null && inclusions.length > 0)
		return true;
	if (exclusions != null && exclusions.length > 0)
		return true;
	return false;
}
 
Example 17
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean appendVariableLabel(IPackageFragmentRoot root, long flags) {
	try {
		IClasspathEntry rawEntry= root.getRawClasspathEntry();
		if (rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
			IClasspathEntry entry= JavaModelUtil.getClasspathEntry(root);
			if (entry.getReferencingEntry() != null) {
				return false; // not the variable entry itself, but a referenced entry
			}
			IPath path= rawEntry.getPath().makeRelative();

			if (getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED)) {
				int segements= path.segmentCount();
				if (segements > 0) {
					fBuilder.append(path.segment(segements - 1));
					if (segements > 1) {
						fBuilder.append(JavaElementLabels.CONCAT_STRING);
						fBuilder.append(path.removeLastSegments(1).toOSString());
					}
				} else {
					fBuilder.append(path.toString());
				}
			} else {
				fBuilder.append(path.toString());
			}
			fBuilder.append(JavaElementLabels.CONCAT_STRING);
			if (root.isExternal()) {
				fBuilder.append(root.getPath().toOSString());
			} else {
				fBuilder.append(root.getPath().makeRelative().toString());
			}

			return true;
		}
	} catch (JavaModelException e) {
		// problems with class path, ignore (bug 202792)
		return false;
	}
	return false;
}
 
Example 18
Source File: RemoveFromClasspathAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean canRemove(Object element){
	if (! (element instanceof IPackageFragmentRoot))
		return false;
	IPackageFragmentRoot root= (IPackageFragmentRoot)element;
	try {
		IClasspathEntry cpe= root.getRawClasspathEntry();
		if (cpe == null || cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER)
			return false; // don't want to remove the container if only a child is selected
		return true;
	} catch (JavaModelException e) {
		if (JavaModelUtil.isExceptionToBeLogged(e))
			JavaPlugin.log(e);
	}
	return false;
}
 
Example 19
Source File: JavaClassFilter.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isJDKPackageFragmentRoot(IPackageFragmentRoot root) throws JavaModelException {
    if (root.getRawClasspathEntry() != null) {
        IPath path = root.getRawClasspathEntry().getPath();
        return path != null && path.segmentCount() > 0
            && Objects.equals(JavaRuntime.JRE_CONTAINER, path.segment(0));
    }

    return false;
}
 
Example 20
Source File: JarPackageWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected boolean validateSourceGroup() {
	if (!(fExportClassFilesCheckbox.getSelection() || fExportOutputFoldersCheckbox.getSelection() || fExportJavaFilesCheckbox.getSelection())) {
		setErrorMessage(JarPackagerMessages.JarPackageWizardPage_error_noExportTypeChecked);
		return false;
	}

	if (getSelectedResources().size() == 0) {
		if (getErrorMessage() != null)
			setErrorMessage(null);
		return false;
	}
	if (fExportClassFilesCheckbox.getSelection() || fExportOutputFoldersCheckbox.getSelection())
		return true;

	// Source file only export - check if there are source files
	Iterator<Object> iter= getSelectedResourcesIterator();
	while (iter.hasNext()) {
		Object element= iter.next();
		if (element instanceof IClassFile) {
			IPackageFragmentRoot root= (IPackageFragmentRoot)((IClassFile)element).getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
			if (root == null)
				continue;
			IClasspathEntry cpEntry;
			try {
				cpEntry= root.getRawClasspathEntry();
			} catch (JavaModelException e) {
				continue;
			}
			if (cpEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
				return true;
			}
		} else {
			return true;
		}
	}

	if (getErrorMessage() != null)
		setErrorMessage(null);
	return false;
}