Example usage for org.eclipse.jdt.core IJavaProject setRawClasspath

List of usage examples for org.eclipse.jdt.core IJavaProject setRawClasspath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject setRawClasspath.

Prototype

void setRawClasspath(IClasspathEntry[] entries, IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Sets the classpath of this project using a list of classpath entries.

Usage

From source file:com.liferay.ide.kaleo.core.WorkflowSupportManager.java

License:Open Source License

private void computeClasspath(IJavaProject project, IProgressMonitor monitor) {
    int numEntries = 2;
    IPath runtimeContainerPath = null;/*from  ww w .ja  v a  2  s.co  m*/

    try {
        String id = this.currentServer.getRuntime().getId();
        runtimeContainerPath = new Path(
                "org.eclipse.jst.server.core.container/com.liferay.ide.eclipse.server.tomcat.runtimeClasspathProvider/"
                        + id);
        numEntries++;
    } catch (Throwable t) {
        // do nothing
    }

    IClasspathEntry[] classpath = new IClasspathEntry[numEntries];
    classpath[0] = JavaCore.newContainerEntry(JavaRuntime.newDefaultJREContainerPath());
    classpath[1] = JavaCore.newSourceEntry(project.getProject().getFolder("src").getFullPath());

    if (runtimeContainerPath != null) {
        classpath[2] = JavaCore.newContainerEntry(runtimeContainerPath);
    }

    try {
        project.setRawClasspath(classpath, monitor);
    } catch (JavaModelException e) {
    }
}

From source file:com.liferay.ide.layouttpl.core.facet.LayoutTplPluginFacetInstall.java

License:Open Source License

protected void removeUnneededClasspathEntries() {
    IFacetedProjectWorkingCopy facetedProject = getFacetedProject();
    IJavaProject javaProject = JavaCore.create(facetedProject.getProject());

    try {/*  w  w  w .  j  a  va  2  s .  co m*/
        IClasspathEntry[] existingClasspath = javaProject.getRawClasspath();
        List<IClasspathEntry> newClasspath = new ArrayList<IClasspathEntry>();

        for (IClasspathEntry entry : existingClasspath) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                continue;
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                String path = entry.getPath().toPortableString();

                if (path.contains("org.eclipse.jdt.launching.JRE_CONTAINER") || //$NON-NLS-1$
                        path.contains("org.eclipse.jst.j2ee.internal.web.container") || //$NON-NLS-1$
                        path.contains("org.eclipse.jst.j2ee.internal.module.container")) //$NON-NLS-1$
                {
                    continue;
                }
            }

            newClasspath.add(entry);
        }

        javaProject.setRawClasspath(newClasspath.toArray(new IClasspathEntry[0]), null);

        IResource sourceFolder = javaProject.getProject()
                .findMember(IPluginFacetConstants.PORTLET_PLUGIN_SDK_SOURCE_FOLDER);

        if (sourceFolder.exists()) {
            sourceFolder.delete(true, null);
        }
    } catch (Exception e) {
        // no need to report errors
    }
}

From source file:com.liferay.ide.project.core.library.PluginLibraryInstallOperation.java

License:Open Source License

@Override
public void execute(LibraryProviderOperationConfig config, IProgressMonitor monitor) throws CoreException {
    IFacetedProjectBase facetedProject = config.getFacetedProject();

    IProject project = facetedProject.getProject();

    IJavaProject javaProject = JavaCore.create(project);

    IPath containerPath = getClasspathContainerPath();

    // IDE-413 check to make sure that the containerPath doesn't already existing.

    IClasspathEntry[] entries = javaProject.getRawClasspath();

    for (IClasspathEntry entry : entries) {
        if (entry.getPath().equals(containerPath)) {
            return;
        }//  w ww . j a v a2  s.  co m
    }

    IAccessRule[] accessRules = new IAccessRule[] {};

    IClasspathAttribute[] attributes = new IClasspathAttribute[] { JavaCore.newClasspathAttribute(
            IClasspathDependencyConstants.CLASSPATH_COMPONENT_NON_DEPENDENCY, StringPool.EMPTY) };

    IClasspathEntry newEntry = JavaCore.newContainerEntry(containerPath, accessRules, attributes, false);

    IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];

    System.arraycopy(entries, 0, newEntries, 0, entries.length);

    newEntries[entries.length] = newEntry;

    javaProject.setRawClasspath(newEntries, monitor);
}

From source file:com.liferay.ide.project.core.modules.templates.AbstractLiferayComponentTemplate.java

License:Open Source License

protected void createResorcesFolder(IProject project) throws CoreException {
    IFolder resourceFolder = liferayProject.getSourceFolder("resources");

    if (resourceFolder == null || !resourceFolder.exists()) {
        IJavaProject javaProject = JavaCore.create(project);

        List<IClasspathEntry> existingRawClasspath = Arrays.asList(javaProject.getRawClasspath());
        List<IClasspathEntry> newRawClasspath = new ArrayList<IClasspathEntry>();

        IClasspathAttribute[] attributes = new IClasspathAttribute[] {
                JavaCore.newClasspathAttribute("FROM_GRADLE_MODEL", "true") }; //$NON-NLS-1$ //$NON-NLS-2$

        IClasspathEntry resourcesEntry = JavaCore.newSourceEntry(
                project.getFullPath().append("src/main/resources"), new IPath[0], new IPath[0], null,
                attributes);// ww  w  .ja v a2 s.c o m

        newRawClasspath.add(resourcesEntry);

        for (IClasspathEntry entry : existingRawClasspath) {
            newRawClasspath.add(entry);
        }

        javaProject.setRawClasspath(newRawClasspath.toArray(new IClasspathEntry[0]), new NullProgressMonitor());

        project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
    }
}

From source file:com.liferay.ide.project.core.util.ProjectUtil.java

License:Open Source License

public static IProject createExistingProject(final ProjectRecord record, final IPath sdkLocation,
        IProgressMonitor monitor) throws CoreException {
    String projectName = record.getProjectName();

    final 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 it is under the root use the default location
        if (Platform.getLocation().isPrefixOf(locationPath)) {
            record.description.setLocation(null);
        } else {//  w w w. ja  v  a 2s.  c o  m
            record.description.setLocation(locationPath);
        }
    } else {
        record.description.setName(projectName);
    }

    project.create(record.description, new SubProgressMonitor(monitor, 30));

    project.open(IResource.FORCE, new SubProgressMonitor(monitor, 70));

    // need to check to see if we an ext project with source folders with incorrect parent attributes
    if (project.getName().endsWith(ISDKConstants.EXT_PLUGIN_PROJECT_SUFFIX)) {
        fixExtProjectClasspathEntries(project);
    }

    IFacetedProject fProject = ProjectFacetsManager.create(project, true, monitor);

    FacetedProjectWorkingCopy fpwc = new FacetedProjectWorkingCopy(fProject);

    final String pluginType = guessPluginType(fpwc);

    SDKPluginFacetUtil.configureProjectAsSDKProject(fpwc, pluginType, sdkLocation.toPortableString(), record);

    fpwc.commitChanges(monitor);

    final IJavaProject javaProject = JavaCore.create(fProject.getProject());

    ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
        @Override
        public void run(IProgressMonitor monitor) throws CoreException {
            List<IClasspathEntry> rawClasspaths = new ArrayList<IClasspathEntry>();

            IPath containerPath = null;

            for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                        && entry.getPath().segment(0).equals(SDKClasspathContainer.ID)) {
                    containerPath = entry.getPath();
                    break;
                }

                if (!isLiferayRuntimePluginClassPath(entry)) {
                    rawClasspaths.add(entry);
                }
            }

            if (containerPath != null) {
                JavaCore.getClasspathContainerInitializer(SDKClasspathContainer.ID).initialize(containerPath,
                        javaProject);
            } else {
                javaProject.setRawClasspath(rawClasspaths.toArray(new IClasspathEntry[rawClasspaths.size()]),
                        new NullProgressMonitor());

                javaProject.setRawClasspath(rawClasspaths.toArray(new IClasspathEntry[rawClasspaths.size()]),
                        new NullProgressMonitor());

                IAccessRule[] accessRules = new IAccessRule[] {};

                IClasspathAttribute[] attributes = new IClasspathAttribute[] { JavaCore.newClasspathAttribute(
                        IClasspathDependencyConstants.CLASSPATH_COMPONENT_NON_DEPENDENCY, StringPool.EMPTY) };

                IPath cpePath = new Path(SDKClasspathContainer.ID);
                ;

                IClasspathEntry newEntry = JavaCore.newContainerEntry(cpePath, accessRules, attributes, false);

                IClasspathEntry[] entries = javaProject.getRawClasspath();

                for (IClasspathEntry entry : entries) {
                    if (entry.getPath().equals(cpePath)) {
                        return;
                    }
                }

                IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];

                System.arraycopy(entries, 0, newEntries, 0, entries.length);

                newEntries[entries.length] = newEntry;

                javaProject.setRawClasspath(newEntries, monitor);
            }
            monitor.done();

            final SDK sdk = SDKUtil.createSDKFromLocation(sdkLocation);

            SDKUtil.openAsProject(sdk);
        }
    }, monitor);
    return project;
}

From source file:com.liferay.ide.project.core.util.ProjectUtil.java

License:Open Source License

private static void fixExtProjectClasspathEntries(IProject project) {
    try {//from   w  w  w .  ja  va 2 s .  c o m
        boolean fixedAttr = false;

        IJavaProject javaProject = JavaCore.create(project);

        List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>();

        IClasspathEntry[] entries = javaProject.getRawClasspath();

        for (IClasspathEntry entry : entries) {
            IClasspathEntry newEntry = null;

            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                List<IClasspathAttribute> newAttrs = new ArrayList<IClasspathAttribute>();

                IClasspathAttribute[] attrs = entry.getExtraAttributes();

                if (!CoreUtil.isNullOrEmpty(attrs)) {
                    for (IClasspathAttribute attr : attrs) {
                        IClasspathAttribute newAttr = null;

                        if ("owner.project.facets".equals(attr.getName()) && //$NON-NLS-1$
                                "liferay.plugin".equals(attr.getValue())) //$NON-NLS-1$
                        {
                            newAttr = JavaCore.newClasspathAttribute(attr.getName(), "liferay.ext"); //$NON-NLS-1$
                            fixedAttr = true;
                        } else {
                            newAttr = attr;
                        }

                        newAttrs.add(newAttr);
                    }

                    newEntry = JavaCore.newSourceEntry(entry.getPath(), entry.getInclusionPatterns(),
                            entry.getExclusionPatterns(), entry.getOutputLocation(),
                            newAttrs.toArray(new IClasspathAttribute[0]));
                }
            }

            if (newEntry == null) {
                newEntry = entry;
            }

            newEntries.add(newEntry);
        }

        if (fixedAttr) {
            IProgressMonitor monitor = new NullProgressMonitor();

            javaProject.setRawClasspath(newEntries.toArray(new IClasspathEntry[0]), monitor);

            try {
                javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
            } catch (Exception e) {
                ProjectCore.logError(e);
            }
        }

        fixExtProjectSrcFolderLinks(project);
    } catch (Exception ex) {
        ProjectCore.logError("Exception trying to fix Ext project classpath entries.", ex); //$NON-NLS-1$
    }
}

From source file:com.liferay.ide.sdk.ui.SDKProjectRenameParticipant.java

License:Open Source License

@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    final String newName = this.getArguments().getNewName();

    return new Change() {
        @Override/*from   w  w w .j  av  a2  s. co m*/
        public String getName() {
            return "Update Ivy classpath entry"; //$NON-NLS-1$
        }

        @Override
        public void initializeValidationData(IProgressMonitor pm) {
            System.out.println();
        }

        @Override
        public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException {
            return new RefactoringStatus();
        }

        @Override
        public Change perform(IProgressMonitor pm) throws CoreException {
            final RenameJavaProjectProcessor rjpp = (RenameJavaProjectProcessor) getProcessor();
            final IJavaProject newJavaProject = (IJavaProject) rjpp.getNewElement();
            final IvyClasspathContainerConfiguration conf = new IvyClasspathContainerConfiguration(
                    newJavaProject, ISDKConstants.IVY_XML_FILE, true);

            IClasspathEntry oldEntry = null;

            for (IClasspathEntry cpEntry : newJavaProject.getRawClasspath()) {
                if (cpEntry.getPath().segment(0).equals(IvyClasspathContainer.CONTAINER_ID)) {
                    oldEntry = cpEntry;
                    break;
                }
            }

            IvyClasspathContainerConfAdapter.load(conf, oldEntry.getPath(), oldEntry.getExtraAttributes());

            final String oldIvySettingsPath = conf.getIvySettingsSetup().getRawIvySettingsPath();
            final String oldIvyUserDir = conf.getIvySettingsSetup().getRawIvyUserDir();

            conf.setProject(newJavaProject);
            conf.getIvySettingsSetup().setIvySettingsPath(oldIvySettingsPath.replaceAll(oldName, newName));
            conf.getIvySettingsSetup().setIvyUserDir(oldIvyUserDir.replaceAll(oldName, newName));

            List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>();

            for (IClasspathEntry cpEntry : newJavaProject.getRawClasspath()) {
                if (!cpEntry.getPath().segment(0).equals(IvyClasspathContainer.CONTAINER_ID)) {
                    newEntries.add(cpEntry);
                }
            }

            IPath newIvyPath = IvyClasspathContainerConfAdapter.getPath(conf);

            newJavaProject.setRawClasspath(newEntries.toArray(new IClasspathEntry[0]), pm);

            IClasspathEntry ivyEntry = JavaCore.newContainerEntry(newIvyPath, null,
                    oldEntry.getExtraAttributes(), false);

            IvyClasspathContainer ivycp = new IvyClasspathContainer(newJavaProject, newIvyPath,
                    new IClasspathEntry[0], new IClasspathAttribute[0]);
            JavaCore.setClasspathContainer(newIvyPath, new IJavaProject[] { newJavaProject },
                    new IClasspathContainer[] { ivycp }, pm);

            IClasspathEntry[] entries = newJavaProject.getRawClasspath();

            newEntries.add(ivyEntry);
            entries = (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]);
            newJavaProject.setRawClasspath(entries, newJavaProject.getOutputLocation(), pm);

            JavaCore.getClasspathContainerInitializer(IvyClasspathContainer.CONTAINER_ID)
                    .requestClasspathContainerUpdate(newIvyPath, newJavaProject, ivycp);

            ivycp.launchResolve(false, pm);

            return null;
        }

        @Override
        public Object getModifiedElement() {
            return null;
        }
    };
}

From source file:com.liferay.ide.theme.core.facet.ThemePluginFacetInstall.java

License:Open Source License

protected void removeUnneededClasspathEntries() {
    IFacetedProjectWorkingCopy facetedProject = getFacetedProject();
    IJavaProject javaProject = JavaCore.create(facetedProject.getProject());

    try {// ww  w.  j ava  2  s  .  c om
        IClasspathEntry[] existingClasspath = javaProject.getRawClasspath();
        List<IClasspathEntry> newClasspath = new ArrayList<IClasspathEntry>();

        for (IClasspathEntry entry : existingClasspath) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                continue;
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                String path = entry.getPath().toPortableString();
                if (path.contains("org.eclipse.jdt.launching.JRE_CONTAINER") || //$NON-NLS-1$
                        path.contains("org.eclipse.jst.j2ee.internal.web.container") || //$NON-NLS-1$
                        path.contains("org.eclipse.jst.j2ee.internal.module.container")) //$NON-NLS-1$
                {
                    continue;
                }
            }

            newClasspath.add(entry);
        }

        javaProject.setRawClasspath(newClasspath.toArray(new IClasspathEntry[0]), null);

        IResource sourceFolder = javaProject.getProject()
                .findMember(IPluginFacetConstants.PORTLET_PLUGIN_SDK_SOURCE_FOLDER);

        if (sourceFolder.exists()) {
            sourceFolder.delete(true, null);
        }
    } catch (Exception e) {

    }
}

From source file:com.mds.apg.wizards.PhonegapProjectPopulate.java

License:Open Source License

/**
 * Update the classpath with thanks to Larry Isaacs in  
 * http://dev.eclipse.org/newslists/news.eclipse.webtools/msg10002.html
 * /*w  w  w . j  a v a 2 s  .  c om*/
 * With ADT r17 classpath is no longer needed. The Android tools include anything in libs
 * 
 * @throws CoreException 
 * 
 * @throws URISyntaxException
 */

static private void updateClasspath(IProgressMonitor monitor, IProject androidProject, String jarFile,
        Path srcLoc) throws CoreException {

    IJavaProject javaProject = (IJavaProject) androidProject.getNature(JavaCore.NATURE_ID);

    IClasspathEntry[] classpathList = javaProject.readRawClasspath();
    IClasspathEntry[] newClasspaths = new IClasspathEntry[classpathList.length + 1];
    System.arraycopy(classpathList, 0, newClasspaths, 0, classpathList.length);

    // Create the new Classpath entry

    IClasspathEntry newPath = JavaCore.newLibraryEntry(new Path(jarFile), srcLoc, null);
    newClasspaths[classpathList.length] = newPath;

    // write it back out 
    javaProject.setRawClasspath(newClasspaths, monitor);
}

From source file:com.microsoft.applicationinsights.ui.config.AIProjConfigWizardDialog.java

License:Open Source License

private void configureAzureSDK(IJavaProject proj) {
    try {/*  www .j  a  v  a2 s. co  m*/
        IClasspathEntry[] classpath = proj.getRawClasspath();

        for (IClasspathEntry iClasspathEntry : classpath) {
            final IPath containerPath = iClasspathEntry.getPath();
            if (containerPath.toString().contains(Messages.azureSDKcontainerID)) {
                return;
            }
        }

        List<IClasspathEntry> list = new ArrayList<IClasspathEntry>(java.util.Arrays.asList(classpath));
        IClasspathAttribute[] attr = new IClasspathAttribute[1];
        attr[0] = JavaCore.newClasspathAttribute(Messages.jstDep, "/WEB-INF/lib");
        IClasspathEntry jarEntry = JavaCore.newContainerEntry(
                new Path(Messages.azureSDKcontainerID).append(getLatestSDKVersion()), null, attr, false);
        list.add(jarEntry);
        IClasspathEntry[] newClasspath = (IClasspathEntry[]) list.toArray(new IClasspathEntry[0]);
        proj.setRawClasspath(newClasspath, null);
        // Azure SDK configured - application insights configured for the first time for specific project
        Bundle bundle = Activator.getDefault().getBundle();
        if (bundle != null) {
            PluginUtil.showBusy(true, getShell());
            AppInsightsCustomEvent.create("Application Insights", bundle.getVersion().toString());
            PluginUtil.showBusy(false, getShell());
        }
    } catch (Exception e) {
        Activator.getDefault().log(e.getMessage(), e);
    }
}