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

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

Introduction

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

Prototype

IClasspathEntry[] getRawClasspath() throws JavaModelException;

Source Link

Document

Returns the raw classpath for the project, as a list of classpath entries.

Usage

From source file:fede.workspace.eclipse.java.JavaProjectManager.java

License:Apache License

/**
 * Replace project classpath./*from   w  w w .  java2  s  .co  m*/
 * 
 * @param ce
 *            the ce
 * @param project
 *            the project
 * @param progressMonitor
 *            the progress monitor
 * 
 * @return the i classpath entry
 * 
 * @throws JavaModelException
 *             the java model exception
 */
static public IClasspathEntry replaceProjectClasspath(IClasspathEntry ce, IJavaProject project,
        IProgressMonitor progressMonitor) throws JavaModelException {
    if (project == null) {
        return null;
    }

    IClasspathEntry[] classpath = project.getRawClasspath();
    boolean find = false;
    IPath rootPath = ce.getPath();

    int cpLength = classpath.length;
    int newCPIndex = -1;
    IClasspathEntry findentry = null;
    for (int j = 0; j < cpLength; j++) {
        IClasspathEntry entry = classpath[j];
        if (rootPath.equals(entry.getPath())) {
            if (!find) {
                find = true;
                newCPIndex = j;
                classpath[newCPIndex++] = ce;
                findentry = entry;
            }
        } else if (find) {
            classpath[newCPIndex++] = entry;
        }
    }

    if (find) {
        if (newCPIndex == cpLength) {
            project.setRawClasspath(classpath, progressMonitor);
        } else {
            IClasspathEntry[] newClasspath = new IClasspathEntry[newCPIndex];
            System.arraycopy(classpath, 0, newClasspath, 0, newCPIndex);
            project.setRawClasspath(newClasspath, progressMonitor);
        }
    } else {
        IClasspathEntry[] newClasspath = new IClasspathEntry[cpLength + 1];
        System.arraycopy(classpath, 0, newClasspath, 0, cpLength);
        newClasspath[cpLength] = ce;
        project.setRawClasspath(newClasspath, progressMonitor);
    }
    return findentry;
}

From source file:fede.workspace.eclipse.java.JavaProjectManager.java

License:Apache License

/**
 * Removes the project classpath./*from  w ww  . ja  va  2 s.c o m*/
 * 
 * @param rootPath
 *            the root path
 * @param project
 *            the project
 * @param progressMonitor
 *            the progress monitor
 * 
 * @return the i classpath entry
 * 
 * @throws JavaModelException
 *             the java model exception
 */
static public IClasspathEntry removeProjectClasspath(IPath rootPath, IJavaProject project,
        IProgressMonitor progressMonitor) throws JavaModelException {
    if (project == null) {
        return null;
    }

    IClasspathEntry[] classpath = project.getRawClasspath();
    boolean find = false;
    int cpLength = classpath.length;
    int newCPIndex = -1;
    IClasspathEntry findentry = null;
    for (int j = 0; j < cpLength; j++) {
        IClasspathEntry entry = classpath[j];
        if (rootPath.equals(entry.getPath())) {
            if (!find) {
                find = true;
                newCPIndex = j;
                findentry = entry;
            }
        } else if (find) {
            classpath[newCPIndex++] = entry;
        }
    }
    if (find) {
        IClasspathEntry[] newClasspath = new IClasspathEntry[newCPIndex];
        System.arraycopy(classpath, 0, newClasspath, 0, newCPIndex);
        project.setRawClasspath(newClasspath, progressMonitor);
    }
    return findentry;
}

From source file:fede.workspace.eclipse.java.JavaProjectManager.java

License:Apache License

/**
 * Delete java source folder./*from  w  w  w. ja  v a 2  s .c  o  m*/
 * 
 * @param item
 *            the item
 * @param sourceFolder
 *            the source folder
 * @param monitor
 *            the monitor
 * 
 * @throws CoreException
 *             the core exception
 */
public static void deleteJavaSourceFolder(Item item, IFolder sourceFolder, IProgressMonitor monitor)
        throws CoreException {
    IJavaProject javaProject = getJavaProject(item);
    if (javaProject == null) {
        return;
    }

    if (sourceFolder == null) {
        return;
    }

    List<IClasspathEntry> classpath = new ArrayList<IClasspathEntry>(
            Arrays.asList(javaProject.getRawClasspath()));
    classpath.remove(JavaCore.newSourceEntry(sourceFolder.getFullPath()));
    javaProject.setRawClasspath(classpath.toArray(new IClasspathEntry[classpath.size()]), monitor);
}

From source file:fede.workspace.eclipse.java.JavaProjectManager.java

License:Apache License

/**
 * Adds the project classpath.//from   w w w  .  ja  v  a 2s .  c  o  m
 * 
 * @param project
 *            the project
 * @param ce
 *            the ce
 * @param progressMonitor
 *            the progress monitor
 * @param overwrite
 *            true si force le classpath
 * 
 * @throws JavaModelException
 *             the java model exception
 */
static public void addProjectClasspath(IJavaProject project, IClasspathEntry ce,
        IProgressMonitor progressMonitor, boolean overwrite) throws JavaModelException {
    if (project == null) {
        return;
    }

    IClasspathEntry[] classpath = project.getRawClasspath();
    int cpLength = classpath.length;
    for (int i = 0; i < classpath.length; i++) {
        IClasspathEntry entry = classpath[i];
        if (entry.getPath().equals(ce.getPath())) {
            if (overwrite) {
                classpath[i] = ce;
                project.setRawClasspath(classpath, progressMonitor);
            }
            return;
        }
    }

    IClasspathEntry[] newClasspath = new IClasspathEntry[cpLength + 1];
    ;
    System.arraycopy(classpath, 0, newClasspath, 0, cpLength);
    newClasspath[cpLength] = ce;
    project.setRawClasspath(newClasspath, progressMonitor);
}

From source file:fr.imag.adele.cadse.cadseg.menu.ExportBundlePagesAction.java

License:Apache License

@Override
public void doFinish(UIPlatform uiPlatform, Object monitor) throws Exception {
    super.doFinish(uiPlatform, monitor);
    IProgressMonitor pmo = (IProgressMonitor) monitor;
    try {/*ww w.jav a  2s .  co m*/
        EclipsePluginContentManger project = (EclipsePluginContentManger) cadsedef.getContentItem();
        pmo.beginTask("export cadse " + cadsedef.getName(), 1);

        String qname = cadsedef.getQualifiedName();
        String qname_version = "";
        String version = "";
        IJavaProject jp = project.getMainMappingContent(IJavaProject.class);
        IProject eclipseProject = project.getProject();
        IPath eclipseProjectPath = eclipseProject.getFullPath();

        // jp.getProject().build(kind, pmo)
        IPath defaultOutputLocation = jp.getOutputLocation();

        IPath manifestPath = new Path(JarFile.MANIFEST_NAME);
        HashSet<IPath> excludePath = new HashSet<IPath>();
        excludePath.add(eclipseProjectPath.append(manifestPath));
        excludePath.add(eclipseProjectPath.append(".project"));
        excludePath.add(eclipseProjectPath.append(".classpath"));
        excludePath.add(eclipseProjectPath.append("run-cadse-" + cadsedef.getName() + ".launch"));
        excludePath.add(eclipseProjectPath.append(".melusine.ser"));
        excludePath.add(eclipseProjectPath.append(".melusine.xml"));

        Manifest mf = new Manifest(eclipseProject.getFile(manifestPath).getContents());
        File pf = new File(file, qname + ".jar");
        File sourceDir = new File(file, qname + ".Source");

        if (tstamp) {
            Date d = new Date(System.currentTimeMillis());
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
            String timeStamp = "v" + formatter.format(d);

            version = mf.getMainAttributes().getValue(OsgiManifest.BUNDLE_VERSION);
            String[] partVersion = version.split("\\.");
            if (partVersion.length == 4) {
                version = version + "-" + timeStamp;
            } else {
                version = version + "." + timeStamp;
            }
            mf.getMainAttributes().putValue(OsgiManifest.BUNDLE_VERSION, version);
            pf = new File(file, qname + "_" + version + ".jar");
            qname_version = qname + "_" + version;
            sourceDir = new File(file, qname + ".Source_" + version);
        }

        JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(pf), mf);
        HashMap<IFile, IPath> files = new HashMap<IFile, IPath>();

        IWorkspaceRoot eclipseRoot = eclipseProject.getWorkspace().getRoot();
        IContainer classesFolder = (IContainer) eclipseRoot.findMember(defaultOutputLocation);
        addFolder(files, excludePath, classesFolder, Path.EMPTY);

        IClasspathEntry[] classpaths = jp.getRawClasspath();
        for (IClasspathEntry entry : classpaths) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                if (entry.getOutputLocation() != null) {
                    classesFolder = (IContainer) eclipseRoot.findMember(entry.getOutputLocation());
                    addFolder(files, excludePath, classesFolder, Path.EMPTY);
                }
                IPath sourcePath = entry.getPath();
                excludePath.add(sourcePath);
                continue;
            }
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {

            }
        }

        addFolder(files, excludePath, eclipseProject, Path.EMPTY);
        pmo.beginTask("export cadse " + cadsedef.getName(), files.size());

        Set<IFile> keySet = new TreeSet<IFile>(new Comparator<IFile>() {
            public int compare(IFile o1, IFile o2) {
                return o1.getFullPath().toPortableString().compareTo(o2.getFullPath().toPortableString());
            }
        });
        keySet.addAll(files.keySet());
        for (IFile f : keySet) {
            IPath entryPath = files.get(f);
            pmo.worked(1);
            try {
                ZipUtil.addEntryZip(outputStream, f.getContents(), entryPath.toPortableString(),
                        f.getLocalTimeStamp());
            } catch (Throwable e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        outputStream.close();
        if (deleteOld) {
            File[] childrenFiles = file.listFiles();
            if (childrenFiles != null) {
                for (File f : childrenFiles) {
                    if (f.equals(pf)) {
                        continue;
                    }

                    String fileName = f.getName();

                    if (!fileName.startsWith(qname)) {
                        continue;
                    }
                    FileUtil.deleteDir(f);
                }
            }
        }
        if (exportSource && tstamp) {
            sourceDir.mkdir();
            File srcDir = new File(sourceDir, "src");
            srcDir.mkdir();
            File mfDir = new File(sourceDir, "META-INF");
            mfDir.mkdir();
            File modelDir = new File(srcDir, qname_version);
            modelDir.mkdir();
            FileUtil.setFile(new File(sourceDir, "plugin.xml"),
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<?eclipse version=\"3.0\"?>\n"
                            + "<plugin>\n" + "<extension point=\"org.eclipse.pde.core.source\">\n"
                            + "<location path=\"src\"/>\n" + "</extension>\n" + "</plugin>\n");
            String vendor_info = CadseDefinitionManager.getVendorNameAttribute(cadsedef);
            FileUtil.setFile(new File(mfDir, "MANIFEST.MF"),
                    "Manifest-Version: 1.0\n" + "Bundle-ManifestVersion: 2\n" + "Bundle-SymbolicName: " + qname
                            + ".Source;singleton:=true\n" + "Bundle-Version: " + version + "\n"
                            + "Bundle-Localization: plugin\n" + "Bundle-Vendor: " + vendor_info + "\n"
                            + "Bundle-Name: " + qname + "\n");

            excludePath.clear();
            files.clear();
            classesFolder = (IContainer) eclipseRoot.findMember(defaultOutputLocation);
            excludePath.add(classesFolder.getFullPath());
            for (IClasspathEntry entry : classpaths) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    if (entry.getOutputLocation() != null) {
                        classesFolder = (IContainer) eclipseRoot.findMember(entry.getOutputLocation());
                        addFolder(files, excludePath, classesFolder, Path.EMPTY);
                    }
                    IPath sourcePath = entry.getPath();
                    excludePath.add(sourcePath);
                    ZipUtil.zipDirectory(eclipseRoot.findMember(sourcePath).getLocation().toFile(),
                            new File(modelDir, "src.zip"), null);
                    continue;
                }
            }
            addFolder(files, excludePath, eclipseProject, Path.EMPTY);
            keySet = files.keySet();
            for (IFile f : keySet) {
                IPath entryPath = files.get(f);
                try {
                    FileUtil.copy(f.getLocation().toFile(), new File(modelDir, entryPath.toOSString()), false);
                } catch (Throwable e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
    } catch (RuntimeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

    }
}

From source file:fr.imag.adele.cadse.test.tutos.tuto2.Tuto2Part3_tc_execution.java

License:Apache License

@Test
public void test_build_path() throws Exception {

    packageExplorerView.show();//ww  w  .j  a  v  a 2s. c o m
    packageExplorerView.selectNode("ServletAPI", "sources");
    packageExplorerView.selectNode("ServletAPI");
    packageExplorerView.capture("image076");

    /* Gets the IJavaProject */
    GTCadseTree cadseTree = workspaceView.findTree();
    Item servlet_item = cadseTree.getItem(new GTTreePath("ServletAPI"));
    IJavaProject jp = servlet_item.getMainMappingContent(IJavaProject.class);

    /* Creates a new entry */
    IClasspathEntry libEntry = JavaCore.newLibraryEntry(
            new Path(System.getProperty("test.resourcesPath") + File.separator + "servlet-api.jar"), null,
            null);

    /* Add entry to classpath */
    IClasspathEntry[] classpath = jp.getRawClasspath();
    int cpLength = classpath.length;
    IClasspathEntry[] newClasspath = new IClasspathEntry[cpLength + 1];
    System.arraycopy(classpath, 0, newClasspath, 0, cpLength);
    newClasspath[cpLength] = libEntry;
    jp.setRawClasspath(newClasspath, null);

    GTTreePath api = new GTTreePath("ServletAPI", "Referenced Libraries",
            "servlet-api.jar" + " - " + System.getProperty("test.resourcesPath"));
    packageExplorerView.selectNode(api);
    packageExplorerView.capture("image080");

    packageExplorerView.contextMenu(api, "Build Path", "Configure Build Path...").click();
    shell = new GTCadseShell("Properties for ServletAPI");

    bot.tabItem("Order and Export").activate();
    bot.table().getTableItem("servlet-api.jar" + " - " + System.getProperty("test.resourcesPath")).select();
    bot.table().getTableItem("servlet-api.jar" + " - " + System.getProperty("test.resourcesPath")).check();
    shell.capture("image082");
    shell.close();

    checkCompilationErrors(workspaceView, new GTTreePath("HelloApp", "test.HelloServlet"));
    bot.sleep(2000); // Waits until errors disappear.
    packageExplorerView.show();
    packageExplorerView.capture("image084");
}

From source file:fr.inria.diverse.commons.eclipse.pde.wizards.pages.pde.ui.templates.AbstractTemplateSection.java

License:Open Source License

/**
 * Returns the folder with Java files in the target project. The default
 * implementation looks for source folders in the classpath of the target
 * folders and picks the first one encountered. Subclasses may override this
 * behaviour.//from w w w. j a  v  a2  s . co m
 * 
 * @param monitor
 *            progress monitor to use
 * @return source folder that will be used to generate Java files or
 *         <samp>null </samp> if none found.
 */

protected IFolder getSourceFolder(IProgressMonitor monitor) {
    IFolder sourceFolder = null;

    try {
        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] classpath = javaProject.getRawClasspath();
        for (int i = 0; i < classpath.length; i++) {
            IClasspathEntry entry = classpath[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath path = entry.getPath().removeFirstSegments(1);
                if (path.segmentCount() > 0)
                    sourceFolder = project.getFolder(path);
                break;
            }
        }
    } catch (JavaModelException e) {
        PDEPlugin.logException(e);
    }
    return sourceFolder;
}

From source file:fr.obeo.ariadne.ide.connector.java.internal.explorer.JavaExplorer.java

License:Open Source License

/**
 * Launches the exploration of the given Java project.
 * //from  w  w  w  .j av a  2  s  .c  o m
 * @param project
 *            The Java project to explore
 * @param monitor
 *            The progress monitor
 * @return The Component representing the Java project.
 */
public Component doExplore(IProject project, IProgressMonitor monitor) {
    Component ariadneComponent = this.getOrCreateComponent(project);
    IJavaProject iJavaProject = JavaCore.create(project);
    try {
        IClasspathEntry[] rawClasspath = iJavaProject.getRawClasspath();
        for (IClasspathEntry iClasspathEntry : rawClasspath) {
            // We have the source folders of the project.
            IPath inputFolderPath = iClasspathEntry.getPath();
            IPath outputFolderPath = iClasspathEntry.getOutputLocation();

            if (outputFolderPath == null) {
                outputFolderPath = iJavaProject.getOutputLocation();
            }

            // Create the classpath entry
            ClasspathEntry classpathEntry = CodeFactory.eINSTANCE.createClasspathEntry();
            classpathEntry.setInputFolder(inputFolderPath.toString());
            classpathEntry.setOutputFolder(outputFolderPath.toString());
            ariadneComponent.getClasspathEntries().add(classpathEntry);

            int entryKind = iClasspathEntry.getEntryKind();
            if (IClasspathEntry.CPE_SOURCE == entryKind) {
                // Explore its content located in its input folder
                this.exploreClasspathEntry(iJavaProject, iClasspathEntry, classpathEntry, monitor);
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

    return ariadneComponent;
}

From source file:gov.redhawk.ide.codegen.java.AbstractJavaCodeGenerator.java

License:Open Source License

@Override
public IStatus cleanupSourceFolders(final IProject project, final IProgressMonitor monitor) {
    final IJavaProject jp = JavaCore.create(project);
    final HashSet<IClasspathEntry> paths = new HashSet<IClasspathEntry>();
    try {// w w  w. jav  a 2 s  .co m
        for (final IClasspathEntry path : jp.getRawClasspath()) {
            IPath p = path.getPath();
            if (path.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                if (p.segment(0).equals(project.getFullPath().segment(0))) {
                    p = p.removeFirstSegments(1);
                    if (project.getFolder(p).exists()) {
                        paths.add(path);
                    }
                }
            } else {
                paths.add(path);
            }
        }
        jp.setRawClasspath(paths.toArray(new IClasspathEntry[paths.size()]), monitor);
    } catch (final JavaModelException e) {
        return new Status(IStatus.WARNING, JavaGeneratorPlugin.PLUGIN_ID,
                "Unable to adjust the list of source code folders for the project");
    }
    return new Status(IStatus.OK, JavaGeneratorPlugin.PLUGIN_ID, "Cleaned up source folders");
}

From source file:gov.redhawk.ide.codegen.java.JavaGeneratorUtils.java

License:Open Source License

public static void addRedhawkJavaClassPaths(final IJavaProject jproject, final IProgressMonitor monitor)
        throws CoreException {
    final SubMonitor progress = SubMonitor.convert(monitor, 1);
    final Set<IClasspathEntry> entries = new LinkedHashSet<IClasspathEntry>(
            Arrays.asList(jproject.getRawClasspath()));

    IClasspathEntry e;//w ww  .j a  va2 s  . c om
    e = JavaRuntime.getDefaultJREContainerEntry();
    if (!entries.contains(e)) {
        entries.add(e);
    }

    entries.add(JavaCore.newContainerEntry(ScaCore.OSSIE_LIB_CONTAINER_PATH));
    entries.add(JavaCore.newContainerEntry(ScaCore.SOFT_PKG_REF_CONTAINER_PATH));

    jproject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), progress.newChild(1));
}