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:org.eclipse.eatop.eaadapter.ea2ecore.postprocessings.CreateReleaseZipFile.java

License:Open Source License

private void addToJavaClasspath(IProject project, IClasspathEntry entry)
        throws CoreException, JavaModelException {

    IJavaProject jPrj = JavaCore.create(project);

    IClasspathEntry[] clsPathEntries = jPrj.getRawClasspath();
    IClasspathEntry[] newClsPathEntries = new IClasspathEntry[clsPathEntries.length + 1];

    for (int i = 0; i < clsPathEntries.length; i++) {
        if (clsPathEntries[i].getPath().equals(entry.getPath())) {
            return;
        }/*from w  ww  . ja  va2 s. c om*/
        newClsPathEntries[i] = clsPathEntries[i];
    }

    newClsPathEntries[clsPathEntries.length] = entry;

    jPrj.setRawClasspath(newClsPathEntries, monitor);
}

From source file:org.eclipse.eatop.eaadapter.ea2ecore.postprocessings.CreateReleaseZipFile.java

License:Open Source License

private void removeFromJavaClasspath(IProject project, IPath path) throws CoreException, JavaModelException {

    IJavaProject jPrj = JavaCore.create(project);

    IClasspathEntry[] clsPathEntries = jPrj.getRawClasspath();
    List<IClasspathEntry> newClsPathEntries = new ArrayList<IClasspathEntry>();

    for (IClasspathEntry clsPathEntry : clsPathEntries) {
        if (!clsPathEntry.getPath().equals(path)) {
            newClsPathEntries.add(clsPathEntry);
        }//from w  w w.  j a v  a 2s .  c om
    }

    jPrj.setRawClasspath(newClsPathEntries.toArray(new IClasspathEntry[newClsPathEntries.size()]), monitor);
}

From source file:org.eclipse.edt.ide.core.utils.EclipseUtilities.java

License:Open Source License

/**
 * Adds the outputFolder as a Java source folder if the project is a Java project.
 * //from  www . j ava 2  s.co m
 * @param project       The project containing the folder (used when outputFolder is a relative path)
 * @param outputFolder  The path of the folder. It may be project-relative, or workspace-relative. If workspace-relative
 *                      it should start with 'F/' for a folder or 'P/' for a project.
 * @param forceClasspathRefresh A classpath needs to be refreshed if an entry already exists for the output folder, but the folder has yet to be
 *                         created.  This can occur when a project is exported without a generation directory.
 * @throws CoreException
 */
public static void addToJavaBuildPathIfNecessary(IProject project, String outputFolder,
        boolean forceClasspathRefresh) throws CoreException {
    if (project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject.exists()) {
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            IPath outputFolderPath = new Path(convertFromInternalPath(outputFolder));
            boolean needToAdd = true;

            IPath fullPath = outputFolderPath.isAbsolute() ? outputFolderPath
                    : outputFolderPath.segmentCount() == 0 ? project.getFullPath()
                            : project.getFolder(outputFolderPath).getFullPath();

            for (int i = 0; i < entries.length; i++) {
                if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath nextPath = entries[i].getPath();
                    // JDT throws an error if you have a source folder within a source folder. We could add exclusions to support this, but
                    // for now we just won't add the folder.
                    if (nextPath.isPrefixOf(fullPath) || fullPath.isPrefixOf(nextPath)) {
                        needToAdd = false;
                        break;
                    }
                }
            }

            if (needToAdd) {
                IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
                System.arraycopy(entries, 0, newEntries, 0, entries.length);
                newEntries[newEntries.length - 1] = JavaCore.newSourceEntry(fullPath);
                javaProject.setRawClasspath(newEntries, null);
            }

            if (!needToAdd && forceClasspathRefresh) {
                javaProject.setRawClasspath(javaProject.readRawClasspath(), javaProject.readOutputLocation(),
                        null);
            }
        }
    }
}

From source file:org.eclipse.edt.ide.core.utils.EclipseUtilities.java

License:Open Source License

/**
 * Adds the runtime containers to the project if necessary. This does nothing if the project is
 * not a Java project./*from w w  w  .  ja va2s  . co m*/
 * 
 * @param project   The Java project.
 * @param generator  The generator provider.
 * @param ctx  The generation context.
 */
public static void addRuntimesToProject(IProject project, IGenerator generator, EglContext ctx) {
    EDTRuntimeContainer[] baseRuntimes = generator instanceof AbstractGenerator
            ? ((AbstractGenerator) generator).resolveBaseRuntimeContainers()
            : null;

    EDTRuntimeContainer[] containersToAdd;
    Set<String> requiredContainers = ctx.getRequiredRuntimeContainers();
    if (requiredContainers.size() == 0) {
        if (baseRuntimes == null || baseRuntimes.length == 0) {
            return;
        }
        containersToAdd = baseRuntimes;
    } else {
        Set<EDTRuntimeContainer> containers = new HashSet<EDTRuntimeContainer>(10);
        if (baseRuntimes != null && baseRuntimes.length > 0) {
            containers.addAll(Arrays.asList(baseRuntimes));
        }
        for (EDTRuntimeContainer container : generator.getRuntimeContainers()) {
            if (requiredContainers.contains(container.getId())) {
                containers.add(container);
            }
        }
        containersToAdd = containers.toArray(new EDTRuntimeContainer[containers.size()]);
    }

    if (containersToAdd == null || containersToAdd.length == 0) {
        return;
    }

    try {
        if (project.hasNature(JavaCore.NATURE_ID)) {
            IJavaProject javaProject = JavaCore.create(project);
            IClasspathEntry[] classpath = javaProject.getRawClasspath();

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

            for (int i = 0; i < containersToAdd.length; i++) {
                IPath path = containersToAdd[i].getPath();
                boolean found = false;
                for (int j = 0; j < classpath.length; j++) {
                    if (classpath[j].getEntryKind() == IClasspathEntry.CPE_CONTAINER
                            && classpath[j].getPath().equals(path)) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    additions.add(JavaCore.newContainerEntry(path));
                }
            }

            if (additions.size() > 0) {
                IClasspathEntry[] newEntries = new IClasspathEntry[classpath.length + additions.size()];
                System.arraycopy(classpath, 0, newEntries, 0, classpath.length);
                for (int i = 0; i < additions.size(); i++) {
                    newEntries[classpath.length + i] = additions.get(i);
                }
                javaProject.setRawClasspath(newEntries, null);
            }
        }
    } catch (CoreException e) {
        EDTCoreIDEPlugin.log(e);
    }
}

From source file:org.eclipse.edt.ide.core.utils.EclipseUtilities.java

License:Open Source License

/**
 * Get the source folder name. For new Java projects, check the source
 * folder name in the Java Build Path preferences. For all types of Java
 * projects, use the first source folder from the classpath.
 * /*from   w ww . ja v  a  2s .c o  m*/
 * @param myProject
 *            The project where the folder for Java source resides.
 * 
 * @return String The Java source folder name.
 */
public static String getJavaSourceFolderName(IProject project) {
    String folderName = null;

    try {
        if (project.hasNature(JavaCore.NATURE_ID)) {
            // Use the first folder from the project's classpath. 
            IJavaProject javaProject = JavaCore.create(project);
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    // Get the folder from the entry's path.  The project name
                    // needs to be removed from the path before this will work.
                    IPath path = entry.getPath().removeFirstSegments(1);
                    return path.toString();
                }
            }
        }
    } catch (Exception e) {
    }
    return folderName;
}

From source file:org.eclipse.edt.ide.deployment.rui.operation.CopyJavaRuntimeResourcesOperation.java

License:Open Source License

/**
 * Finds all the classpath entries on the Java build path, including referenced projects, that are of type {@link IClasspathEntry#CPE_CONTAINER}.
 * @throws CoreException//from ww w.  j a  v  a2s .  co m
 */
private void getClasspathContainers(IJavaProject project, Set<IJavaProject> seen, Set<IClasspathEntry> entries)
        throws CoreException {
    if (seen.contains(project)) {
        return;
    }

    seen.add(project);

    for (IClasspathEntry entry : project.getRawClasspath()) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_CONTAINER:
            entries.add(entry);
            break;

        case IClasspathEntry.CPE_PROJECT:
            IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(entry.getPath().lastSegment());
            if (p.isAccessible() && p.hasNature(JavaCore.NATURE_ID)) {
                getClasspathContainers(JavaCore.create(p), seen, entries);
            }
            break;
        }
    }
}

From source file:org.eclipse.edt.ide.deployment.services.operation.ConfigureRuntimePropertiesOperation.java

License:Open Source License

private void genProperties(DeploymentContext context, Set<Part> services, boolean setGlobalProperty,
        DeploymentResultMessageRequestor requestor) {
    IFile file = null;//from www  .ja va 2  s  .  co m
    try {
        if (context.getTargetProject().hasNature(JavaCore.NATURE_ID)) {
            IJavaProject javaProject = JavaCore.create(context.getTargetProject());
            IPath srcFolder = null;
            for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    srcFolder = entry.getPath();
                    break;
                }
            }

            if (srcFolder != null) {
                StringBuilder contents = new StringBuilder(100);
                Properties props = new Properties();
                file = ResourcesPlugin.getWorkspace().getRoot().getFile(srcFolder.append(RUNUNIT_PROPERTIES));
                if (file.exists()) {
                    // Read in the previous entries.
                    BufferedInputStream bis = null;
                    try {
                        bis = new BufferedInputStream(file.getContents(true));
                        props.load(bis);

                        // Also load the previous file contents so we can append to it.
                        contents.append(Util.getFileContents(file));
                        if (contents.charAt(contents.length() - 1) != '\n') {
                            contents.append('\n');
                        }
                    } finally {
                        if (bis != null) {
                            try {
                                bis.close();
                            } catch (IOException ioe) {
                            }
                        }
                    }

                }

                String ddName = context.getDeploymentDesc().getEGLDDFileName().toLowerCase();
                boolean changed = false;

                if (setGlobalProperty && appendPropertyIfNecessary(Constants.APPLICATION_PROPERTY_FILE_NAME_KEY,
                        ddName, props, contents)) {
                    changed = true;
                }

                for (Part part : services) {
                    String generatedName;
                    String id = part.getCaseSensitiveName();
                    String pkg = part.getCaseSensitivePackageName();

                    if (pkg == null || pkg.length() == 0) {
                        generatedName = JavaAliaser.getAlias(id);
                    } else {
                        generatedName = JavaAliaser.packageNameAlias(pkg) + '.' + JavaAliaser.getAlias(id);
                    }

                    String key = Constants.APPLICATION_PROPERTY_FILE_NAME_KEY + '.' + generatedName;
                    if (appendPropertyIfNecessary(key, ddName, props, contents)) {
                        changed = true;
                    }
                }

                if (changed) {
                    ByteArrayInputStream bais = new ByteArrayInputStream(contents.toString().getBytes());
                    if (file.exists()) {
                        file.setContents(bais, true, false, null);
                    } else {
                        file.create(bais, true, null);
                    }

                    requestor.addMessage(DeploymentUtilities.createEGLDeploymentInformationalMessage(
                            EGLMessage.EGL_DEPLOYMENT_DEPLOYED_RT_PROPERTY_FILE, null,
                            new String[] { file.getProjectRelativePath().toPortableString() }));
                }
            }
        }
    } catch (Exception e) {
        requestor.addMessage(DeploymentUtilities.createEGLDeploymentErrorMessage(
                EGLMessage.EGL_DEPLOYMENT_FAILED_DEPLOY_RT_PROPERTY_FILE, null,
                new String[] { file == null ? RUNUNIT_PROPERTIES
                        : file.getProjectRelativePath().toPortableString() }));
        requestor.addMessage(
                DeploymentUtilities.createEGLDeploymentErrorMessage(EGLMessage.EGL_DEPLOYMENT_EXCEPTION, null,
                        new String[] { DeploymentUtilities.createExceptionMessage(e) }));
    }
}

From source file:org.eclipse.edt.ide.eunit.internal.actions.GenTestDriverAction.java

License:Open Source License

protected WorkspaceModifyOperation getSetJavaBuildPathOperation(final IProject javaDriverProject,
        final IProject dependentProj) {
    WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
        @Override//from  ww  w  . j av a  2s.  c o m
        protected void execute(IProgressMonitor monitor)
                throws CoreException, InvocationTargetException, InterruptedException {
            if (javaDriverProject.hasNature(JavaCore.NATURE_ID)) {
                monitor.subTask("Set java build path depends on project " + dependentProj.getName());
                IJavaProject javaProject = JavaCore.create(javaDriverProject);

                IClasspathEntry[] classpath = javaProject.getRawClasspath();

                boolean javaProjBuildPathAlreadySet = false;
                //check to see if the java build path already set for the same dependent project
                for (int p = 0; p < classpath.length && !javaProjBuildPathAlreadySet; p++) {
                    if (classpath[p].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                        IPath dependentProjPath = classpath[p].getPath();
                        if (dependentProj.getFullPath().equals(dependentProjPath))
                            javaProjBuildPathAlreadySet = true;
                    }
                }

                //if not set, set it
                if (!javaProjBuildPathAlreadySet) {
                    List<IClasspathEntry> additions = new ArrayList<IClasspathEntry>();

                    IClasspathEntry newClsPathEntry = JavaCore.newProjectEntry(dependentProj.getFullPath());
                    additions.add(newClsPathEntry);

                    if (additions.size() > 0) {
                        IClasspathEntry[] newEntries = new IClasspathEntry[classpath.length + additions.size()];
                        System.arraycopy(classpath, 0, newEntries, 0, classpath.length);
                        for (int i = 0; i < additions.size(); i++) {
                            newEntries[classpath.length + i] = additions.get(i);
                        }
                        javaProject.setRawClasspath(newEntries, null);
                    }
                }
                monitor.worked(1);
            }
        }
    };

    return op;
}

From source file:org.eclipse.edt.ide.eunit.internal.actions.GenTestDriverAction.java

License:Open Source License

protected WorkspaceModifyOperation getCreateRununitPropertyOperation(final IWorkspaceRoot wsRoot,
        final IProject driverProject) {
    WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
        @Override//from  w ww  .j  a  v  a2 s.c  o  m
        protected void execute(IProgressMonitor monitor)
                throws CoreException, InvocationTargetException, InterruptedException {
            if (driverProject.hasNature(JavaCore.NATURE_ID)) {
                monitor.subTask("Creating rununit property " + RESULTROOT_KEY);
                IJavaProject javaProject = JavaCore.create(driverProject);
                IClasspathEntry[] classpath = javaProject.getRawClasspath();

                IClasspathEntry sourceClsPath = null;
                for (int i = 0; (i < classpath.length) && (sourceClsPath == null); i++) {
                    if (classpath[i].getEntryKind() == IClasspathEntry.CPE_SOURCE)
                        sourceClsPath = classpath[i];
                }
                if (sourceClsPath != null) {
                    IPath propertyFilePath = sourceClsPath.getPath().append(RUNUNIT_PROPERTIES);
                    IFile propertyFile = wsRoot.getFile(propertyFilePath);
                    String propertyOSPath = propertyFile.getLocation().toOSString();

                    try {
                        Properties props = new Properties();
                        if (propertyFile.exists()) {
                            FileReader inReader = new FileReader(propertyOSPath);
                            props.load(inReader);
                            inReader.close();
                        }
                        PrintWriter outWriter = new PrintWriter(propertyOSPath);
                        String resultRootFolder = driverProject.getFolder(RESULTROOT_DIR_APPEND).getLocation()
                                .toOSString();
                        props.put(RESULTROOT_KEY, resultRootFolder);
                        props.store(outWriter, "");
                        outWriter.flush();
                        outWriter.close();

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

                }
                monitor.worked(1);
            }
        }
    };
    return op;
}

From source file:org.eclipse.edt.ide.ui.internal.eglarpackager.EglarUtility.java

License:Open Source License

public static Set<IPath> getOutputLocation(IProject project) {
    Set<IPath> paths = new HashSet<IPath>();

    try {//from w w  w  .ja  va 2  s  .c o  m
        IJavaProject javaProject = null;
        if (JavaEEProjectUtilities.isDynamicWebProject(project)
                || project.hasNature("org.eclipse.jdt.core.javanature")) {
            javaProject = JavaCore.create(project);
            paths.add(javaProject.getOutputLocation());
        }
        if (javaProject != null) {
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            for (int i = 0; i < entries.length; i++) {
                if (entries[i].getOutputLocation() != null) {
                    paths.add(entries[i].getOutputLocation());
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return paths;
}