Example usage for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE

List of usage examples for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE.

Prototype

int CPE_SOURCE

To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE.

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a folder containing package fragments with source code to be compiled.

Usage

From source file:org.eclipse.edt.debug.core.java.filters.ClasspathEntryFilter.java

License:Open Source License

protected void processEntries(IClasspathEntry[] entries, IJavaProject project, Map<String, Object> classMap)
        throws CoreException {
    for (IClasspathEntry entry : entries) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            processLibraryEntry(entry, classMap);
            break;

        case IClasspathEntry.CPE_CONTAINER:
            processContainerEntry(entry, project, classMap);
            break;

        case IClasspathEntry.CPE_SOURCE:
            processSourceEntry(entry, classMap);
            break;

        case IClasspathEntry.CPE_PROJECT:
            IProject depProject = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(entry.getPath().lastSegment());
            if (depProject.isAccessible() && depProject.hasNature(JavaCore.NATURE_ID)) {
                IJavaProject jp = JavaCore.create(depProject);
                processEntries(jp.getResolvedClasspath(true), jp, classMap);
            }//ww  w . j a  v a 2  s. co m
            break;

        default:
            EDTDebugCorePlugin.log(new Status(IStatus.WARNING, EDTDebugCorePlugin.PLUGIN_ID,
                    NLS.bind(EDTDebugCoreMessages.TypeFilterClasspathEntryNotSupported,
                            new Object[] { entry.getEntryKind(), getId() })));
            break;
        }
    }
}

From source file:org.eclipse.edt.debug.core.java.filters.WorkspaceProjectClassFilter.java

License:Open Source License

@Override
protected IClasspathEntry[] getCommonClasspathEntries() {
    String[] projects = getProjectNames();
    if (projects != null && projects.length > 0) {
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        List<IClasspathEntry> list = new ArrayList<IClasspathEntry>();
        for (String project : projects) {
            IProject proj = root.getProject(project);
            try {
                if (proj.isAccessible() && proj.hasNature(JavaCore.NATURE_ID)) {
                    for (IClasspathEntry entry : JavaCore.create(proj).getResolvedClasspath(true)) {
                        switch (entry.getEntryKind()) {
                        case IClasspathEntry.CPE_LIBRARY:
                            if (includeLibraries()) {
                                list.add(entry);
                            }//from  ww w  .j a  v  a 2s .c  o m
                            break;

                        case IClasspathEntry.CPE_PROJECT:
                            if (includeReferencedProjects()) {
                                list.add(entry);
                            }
                            break;

                        case IClasspathEntry.CPE_CONTAINER:
                            if (includeContainers()) {
                                list.add(entry);
                            }
                            break;

                        case IClasspathEntry.CPE_SOURCE:
                            if (includeSource()) {
                                list.add(entry);
                            }
                            break;
                        }
                    }
                }
            } catch (CoreException ce) {
                EDTDebugCorePlugin.log(ce);
            }
        }
        return list.toArray(new IClasspathEntry[list.size()]);
    }
    return null;
}

From source file:org.eclipse.edt.debug.internal.core.java.SMAPBuilder.java

License:Open Source License

private void buildAll() throws CoreException {
    // Performance: only visit the bin directories.
    IJavaProject project = JavaCore.create(getProject());
    IClasspathEntry[] cp = project.getResolvedClasspath(true);

    Set<IPath> binDirectories = new HashSet<IPath>(cp.length);
    for (IClasspathEntry entry : cp) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath outputLocation = entry.getOutputLocation();
            if (outputLocation != null) {
                binDirectories.add(outputLocation);
            }/*from  w  w  w.ja  v  a  2  s.  co m*/
        }
    }

    IPath outputLocation = project.getOutputLocation();
    if (outputLocation != null) {
        binDirectories.add(outputLocation);
    }

    ResourceVisitor visitor = new ResourceVisitor();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    for (IPath path : binDirectories) {
        IResource resource = root.findMember(path);
        if (resource != null) {
            resource.accept(visitor);
        }
    }
}

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.
 * /* w w w  .j a  v  a  2 s .c  om*/
 * @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

/**
 * 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 w  w  . j  ava2  s  .  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.services.generators.ServiceUriMappingGenerator.java

License:Open Source License

private static String createClasspath(IJavaProject javaProject)
        throws JavaModelException, MalformedURLException, URISyntaxException {
    StringBuffer classpath = new StringBuffer();
    String path;/*from ww  w  .  java 2 s.  c o m*/
    IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(true);
    for (int i = 0; i < resolvedClasspath.length; i++) {
        IClasspathEntry entry = resolvedClasspath[i];
        if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            path = entry.getPath().toPortableString();
        } else {
            if (entry.getOutputLocation() != null) {
                path = javaProject.getProject().getLocation().removeLastSegments(1)
                        .append(entry.getOutputLocation()).toPortableString();
            } else {
                path = javaProject.getProject().getLocation().removeLastSegments(1)
                        .append(javaProject.getOutputLocation()).toPortableString();
            }
        }
        classpath.append(path);
        classpath.append(";");
    }
    return classpath.toString();
}

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;/* www .j a va  2 s. c  o 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 getCreateRununitPropertyOperation(final IWorkspaceRoot wsRoot,
        final IProject driverProject) {
    WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
        @Override/*from  ww w.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.deployment.ui.DeploymentUtilities.java

License:Open Source License

public static void getJavaSourceFolders(IProject sourceProject, List<IResource> resources)
        throws CoreException, JavaModelException {

    //for the source project get all of it's Java source folders
    //and include java folders in this project classpath
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IJavaProject javaProject = JavaCore.create(sourceProject);
    if (PlatformUI.isWorkbenchRunning() && javaProject.exists() && javaProject.isOpen()
            || !PlatformUI.isWorkbenchRunning() && javaProject.exists()) {
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        if (entries != null && entries.length > 0) {
            for (int idx = entries.length - 1; idx >= 0; idx--) {
                if (entries[idx].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IResource element = root.findMember(entries[idx].getPath());

                    if (element != null && element.exists()) {
                        resources.add(element);
                    }/*from  w  ww.  j  ava2s.co  m*/
                }
            }
        }
    }
}

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

License:Open Source License

private IResource getSrcFolder(IEGLProject project) {
    try {//w  w  w . j  a va 2 s  .c  o m
        if (EGLProject.hasEGLNature(project.getProject())) {
            IEGLPathEntry[] entries = project.getRawEGLPath();
            for (IEGLPathEntry classpathEntry : entries) {
                if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    return project.getProject().getFolder(classpathEntry.getPath().removeFirstSegments(1));
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}