Search in sources :

Example 11 with ClasspathContainerInitializer

use of org.eclipse.jdt.core.ClasspathContainerInitializer in project liferay-ide by liferay.

the class ProjectUtil method createExistingProject.

public static IProject createExistingProject(ProjectRecord record, IRuntime runtime, String sdkLocation, IProgressMonitor monitor) throws CoreException {
    String projectName = record.getProjectName();
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IProject project = workspace.getRoot().getProject(projectName);
    if (record.description == null) {
        // error case
        record.description = workspace.newProjectDescription(projectName);
        IPath locationPath = new Path(record.projectSystemFile.getAbsolutePath());
        if (Platform.getLocation().isPrefixOf(locationPath)) {
            record.description.setLocation(null);
        } else {
            record.description.setLocation(locationPath);
        }
    } else {
        record.description.setName(projectName);
    }
    monitor.beginTask(Msgs.importingProject, 100);
    project.create(record.description, CoreUtil.newSubMonitor(monitor, 30));
    project.open(IResource.FORCE, CoreUtil.newSubMonitor(monitor, 70));
    if (project.getName().endsWith(ISDKConstants.EXT_PLUGIN_PROJECT_SUFFIX)) {
        _fixExtProjectClasspathEntries(project);
    }
    IFacetedProject fProject = ProjectFacetsManager.create(project, true, monitor);
    FacetedProjectWorkingCopy fpwc = new FacetedProjectWorkingCopy(fProject);
    String pluginType = guessPluginType(fpwc);
    SDKPluginFacetUtil.configureProjectAsRuntimeProject(fpwc, runtime, pluginType, sdkLocation, record);
    fpwc.commitChanges(monitor);
    IJavaProject javaProject = JavaCore.create(fProject.getProject());
    CoreUtil.getWorkspace().run(new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor monitor) throws CoreException {
            for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                String segment = entry.getPath().segment(0);
                if ((entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) && segment.equals(PluginClasspathContainerInitializer.ID)) {
                    ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(PluginClasspathContainerInitializer.ID);
                    initializer.initialize(entry.getPath(), javaProject);
                    break;
                }
            }
            monitor.done();
        }
    }, monitor);
    return project;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IFacetedProject(org.eclipse.wst.common.project.facet.core.IFacetedProject) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) FacetedProjectWorkingCopy(org.eclipse.wst.common.project.facet.core.internal.FacetedProjectWorkingCopy) IFacetedProjectWorkingCopy(org.eclipse.wst.common.project.facet.core.IFacetedProjectWorkingCopy) IProject(org.eclipse.core.resources.IProject) PluginClasspathContainerInitializer(com.liferay.ide.project.core.PluginClasspathContainerInitializer) ClasspathContainerInitializer(org.eclipse.jdt.core.ClasspathContainerInitializer) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace)

Example 12 with ClasspathContainerInitializer

use of org.eclipse.jdt.core.ClasspathContainerInitializer in project flux by eclipse.

the class JavaModelUtil method getClasspathEntryToEdit.

/**
 * Helper method that tests if an classpath entry can be found in a
 * container. <code>null</code> is returned if the entry can not be found
 * or if the container does not allows the configuration of source
 * attachments
 * @param jproject The container's parent project
 * @param containerPath The path of the container
 * @param libPath The path of the library to be found
 * @return IClasspathEntry A classpath entry from the container of
 * <code>null</code> if the container can not be modified.
 * @throws JavaModelException thrown if accessing the container failed
 */
public static IClasspathEntry getClasspathEntryToEdit(IJavaProject jproject, IPath containerPath, IPath libPath) throws JavaModelException {
    IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, jproject);
    ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
    if (container != null && initializer != null && initializer.canUpdateClasspathContainer(containerPath, jproject)) {
        return findEntryInContainer(container, libPath);
    }
    // attachment not possible
    return null;
}
Also used : IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) ClasspathContainerInitializer(org.eclipse.jdt.core.ClasspathContainerInitializer)

Example 13 with ClasspathContainerInitializer

use of org.eclipse.jdt.core.ClasspathContainerInitializer in project bndtools by bndtools.

the class BndContainerInitializer method requestClasspathContainerUpdate.

/**
 * Request the BndContainer for the project, if there is one, be updated. This will not create one if there is not
 * already one.
 *
 * @param javaProject The java project of interest. Must not be null.
 * @return true if the classpath container was updated.
 * @throws CoreException
 */
public static boolean requestClasspathContainerUpdate(IJavaProject javaProject) throws CoreException {
    IClasspathContainer oldContainer = getClasspathContainer(javaProject);
    if (oldContainer == null) {
        // project does not have a BndContainer
        return false;
    }
    ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(BndtoolsConstants.BND_CLASSPATH_ID.segment(0));
    if (initializer != null) {
        initializer.requestClasspathContainerUpdate(BndtoolsConstants.BND_CLASSPATH_ID, javaProject, null);
    }
    return getClasspathContainer(javaProject) != oldContainer;
}
Also used : IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) ClasspathContainerInitializer(org.eclipse.jdt.core.ClasspathContainerInitializer)

Example 14 with ClasspathContainerInitializer

use of org.eclipse.jdt.core.ClasspathContainerInitializer in project eclipse.jdt.ls by eclipse.

the class JavaElementLabels method getContainerEntryLabel.

/**
 * Returns the label of a classpath container.
 * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}.
 *
 * @param containerPath the path of the container
 * @param project the project the container is resolved in
 * @return the label of the classpath container
 * @throws JavaModelException when resolving of the container failed
 */
public static String getContainerEntryLabel(IPath containerPath, IJavaProject project) throws JavaModelException {
    IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, project);
    if (container != null) {
        return org.eclipse.jdt.internal.core.manipulation.util.Strings.markLTR(container.getDescription());
    }
    ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
    if (initializer != null) {
        return org.eclipse.jdt.internal.core.manipulation.util.Strings.markLTR(initializer.getDescription(containerPath, project));
    }
    return BasicElementLabels.getPathLabel(containerPath, false);
}
Also used : IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) ClasspathContainerInitializer(org.eclipse.jdt.core.ClasspathContainerInitializer)

Example 15 with ClasspathContainerInitializer

use of org.eclipse.jdt.core.ClasspathContainerInitializer in project eclipse.jdt.ls by eclipse.

the class SourceAttachmentCommand method getClasspathEntry.

private static ClasspathEntryWrapper getClasspathEntry(IPackageFragmentRoot root) throws CoreException {
    IClasspathEntry entry = null;
    IPath containerPath = null;
    boolean canEditEncoding = true;
    try {
        entry = JavaModelUtil.getClasspathEntry(root);
    } catch (JavaModelException e) {
        JavaLanguageServerPlugin.logException("getting ClasspathEntry from the PackageFragmentRoot ", e);
        throw constructCoreException(Messages.format("Cannot find the ClasspathEntry for the JAR '{0}' of this class file", BasicElementLabels.getPathLabel(root.getPath(), true)), e);
    }
    try {
        if (root.getKind() != IPackageFragmentRoot.K_BINARY) {
            throw constructCoreException(Messages.format("The JAR '{0}' of this class file contains non binary files which does not support the attachment.", BasicElementLabels.getPathLabel(root.getPath(), true)));
        }
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            IJavaProject javaProject = root.getJavaProject();
            containerPath = entry.getPath();
            ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
            IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, javaProject);
            if (initializer == null || container == null) {
                throw constructCoreException(Messages.format("The JAR of this class file belongs to container '{0}' can not be configured.", BasicElementLabels.getPathLabel(containerPath, false)));
            }
            String containerName = container.getDescription();
            IStatus status = initializer.getSourceAttachmentStatus(containerPath, javaProject);
            if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
                throw constructCoreException(Messages.format("The JAR of this class file belongs to container '{0}' which does not support the attachment of sources to its entries.", containerName));
            }
            if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
                throw constructCoreException(Messages.format("The JAR of this class file belongs to container '{0}' which does not allow modifications to source attachments on its entries.", containerName));
            }
            IStatus attributeStatus = initializer.getAttributeStatus(containerPath, javaProject, IClasspathAttribute.SOURCE_ATTACHMENT_ENCODING);
            canEditEncoding = !(attributeStatus.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED || attributeStatus.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY);
            entry = JavaModelUtil.findEntryInContainer(container, root.getPath());
            if (entry == null) {
                throw constructCoreException(Messages.format("Cannot find the ClasspathEntry from container '{0}'.", containerName));
            }
        }
    } catch (JavaModelException e) {
        JavaLanguageServerPlugin.logException("Exception getting the ClasspathEntry for the packageFragmentRoot ", e);
        throw constructCoreException(Messages.format("Cannot find the ClasspathEntry for the JAR '{0}' of this class file", BasicElementLabels.getPathLabel(root.getPath(), true)), e);
    }
    return new ClasspathEntryWrapper(entry, containerPath, canEditEncoding);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IStatus(org.eclipse.core.runtime.IStatus) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) ClasspathContainerInitializer(org.eclipse.jdt.core.ClasspathContainerInitializer)

Aggregations

ClasspathContainerInitializer (org.eclipse.jdt.core.ClasspathContainerInitializer)16 IClasspathContainer (org.eclipse.jdt.core.IClasspathContainer)10 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)9 IJavaProject (org.eclipse.jdt.core.IJavaProject)8 IPath (org.eclipse.core.runtime.IPath)6 IProject (org.eclipse.core.resources.IProject)5 CoreException (org.eclipse.core.runtime.CoreException)4 JavaModelException (org.eclipse.jdt.core.JavaModelException)4 PluginClasspathContainerInitializer (com.liferay.ide.project.core.PluginClasspathContainerInitializer)2 IFolder (org.eclipse.core.resources.IFolder)2 IWorkspace (org.eclipse.core.resources.IWorkspace)2 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IStatus (org.eclipse.core.runtime.IStatus)2 Path (org.eclipse.core.runtime.Path)2 FlexibleProjectContainer (org.eclipse.jst.common.jdt.internal.classpath.FlexibleProjectContainer)2 IFacetedProject (org.eclipse.wst.common.project.facet.core.IFacetedProject)2 IFacetedProjectWorkingCopy (org.eclipse.wst.common.project.facet.core.IFacetedProjectWorkingCopy)2 FacetedProjectWorkingCopy (org.eclipse.wst.common.project.facet.core.internal.FacetedProjectWorkingCopy)2 SDK (com.liferay.ide.sdk.core.SDK)1