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:de.devboost.emfcustomize.builder.EMFCustomizeBuilder.java

License:Open Source License

private void adjustFactory(IFile iFile, ResourceSet resourceSet) {
    //adjust factory for generation gap pattern
    IJavaProject javaProject = JavaCore.create(iFile.getProject());
    try {//w  w  w  .ja va 2s.co  m
        for (IClasspathEntry cpEntry : javaProject.getRawClasspath()) {
            if (cpEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                //to register newly generated code;
                updateClaspath(new File(
                        iFile.getWorkspace().getRoot().getLocation().toString() + cpEntry.getPath().toString()),
                        "", resourceSet);
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * @param javaElement/*from   w ww  . jav  a  2s  . co  m*/
 * @return absolute path of generated bytecode package for given element
 * @throws JavaModelException
 */
private static String getPackageOutputPath(IJavaElement javaElement) throws JavaModelException {
    String dir = ""; //$NON-NLS-1$
    if (javaElement == null) {
        return dir;
    }

    IJavaProject project = javaElement.getJavaProject();

    if (project == null) {
        return dir;
    }
    // default bytecode location
    IPath path = project.getOutputLocation();

    IResource resource = javaElement.getUnderlyingResource();
    if (resource == null) {
        return dir;
    }
    // resolve multiple output locations here
    if (project.exists() && project.getProject().isOpen()) {
        IClasspathEntry entries[] = project.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry classpathEntry = entries[i];
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath outputPath = classpathEntry.getOutputLocation();
                if (outputPath != null && classpathEntry.getPath().isPrefixOf(resource.getFullPath())) {
                    path = outputPath;
                    break;
                }
            }
        }
    }

    if (path == null) {
        // check the default location if not already included
        IPath def = project.getOutputLocation();
        if (def != null && def.isPrefixOf(resource.getFullPath())) {
            path = def;
        }
    }

    if (path == null) {
        return dir;
    }

    IWorkspace workspace = ResourcesPlugin.getWorkspace();

    if (!project.getPath().equals(path)) {
        IFolder outputFolder = workspace.getRoot().getFolder(path);
        if (outputFolder != null) {
            // linked resources will be resolved here!
            IPath rawPath = outputFolder.getRawLocation();
            if (rawPath != null) {
                path = rawPath;
            }
        }
    } else {
        path = project.getProject().getLocation();
    }

    // here we should resolve path variables,
    // probably existing at first place of path
    IPathVariableManager pathManager = workspace.getPathVariableManager();
    path = pathManager.resolvePath(path);

    if (path == null) {
        return dir;
    }

    if (isPackageRoot(project, resource)) {
        dir = path.toOSString();
    } else {
        String packPath = EclipseUtils.getJavaPackageName(javaElement).replace(Signature.C_DOT,
                PACKAGE_SEPARATOR);
        dir = path.append(packPath).toOSString();
    }
    return dir;
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

private static void getClassURLs(IJavaProject javaProject, List<URL> urls) {
    IProject project = javaProject.getProject();
    IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot();

    IClasspathEntry[] paths = null;//from  w  w  w  . jav  a  2s . c om
    IPath defaultOutputLocation = null;
    try {
        paths = javaProject.getResolvedClasspath(true);
        defaultOutputLocation = javaProject.getOutputLocation();
    } catch (JavaModelException e) {
        // don't show message to user neither log it
        // BytecodeOutlinePlugin.log(e, IStatus.ERROR);
    }
    if (paths != null) {
        IPath projectPath = javaProject.getProject().getLocation();
        for (int i = 0; i < paths.length; ++i) {
            IClasspathEntry cpEntry = paths[i];
            IPath p = null;
            if (cpEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                // filter out source container - there are unused for class
                // search - add bytecode output location instead
                p = cpEntry.getOutputLocation();
                if (p == null) {
                    // default output used:
                    p = defaultOutputLocation;
                }
            } else if (cpEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                String projName = cpEntry.getPath().toPortableString().substring(1);
                IProject proj = workspaceRoot.getProject(projName);
                IJavaProject projj = JavaCore.create(proj);
                getClassURLs(projj, urls);
                continue;
            } else {
                p = cpEntry.getPath();
            }

            if (p == null) {
                continue;
            }
            if (!p.toFile().exists()) {
                // removeFirstSegments: remove project from relative path
                p = projectPath.append(p.removeFirstSegments(1));
                if (!p.toFile().exists()) {
                    continue;
                }
            }
            try {
                urls.add(p.toFile().toURI().toURL());
            } catch (MalformedURLException e) {
                // don't show message to user
                BytecodeOutlinePlugin.log(e, IStatus.ERROR);
            }
        }
    }
}

From source file:de.loskutov.eclipse.jdepend.views.TreeObject.java

License:Open Source License

protected String getPackageOutputPath(IJavaElement jElement) throws JavaModelException {
    String dir = ""; //$NON-NLS-1$
    if (jElement == null) {
        return dir;
    }//from   w  w w. ja v a 2 s  .c om

    IJavaProject project = jElement.getJavaProject();

    if (project == null) {
        return dir;
    }
    IPath path = project.getOutputLocation();

    IResource resource = jElement.getUnderlyingResource();
    if (resource == null) {
        return dir;
    }
    // resolve multiple output locations here
    if (project.exists() && project.getProject().isOpen()) {
        IClasspathEntry entries[] = project.getRawClasspath();

        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry classpathEntry = entries[i];
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath outputPath = classpathEntry.getOutputLocation();
                if (outputPath != null) {
                    // if this source folder contains specified java resource
                    // then take bytecode location from himself
                    if (classpathEntry.getPath().isPrefixOf(resource.getFullPath())) {
                        path = outputPath;
                        break;
                    }
                }
            }
        }
    }

    if (path == null) {
        // check the default location if not already included
        IPath def = project.getOutputLocation();
        if (def != null && def.isPrefixOf(resource.getFullPath())) {
            path = def;
        }
    }

    if (path == null) {
        return dir;
    }

    IWorkspace workspace = ResourcesPlugin.getWorkspace();

    if (!project.getPath().equals(path)) {
        IFolder outputFolder = workspace.getRoot().getFolder(path);
        if (outputFolder != null) {
            // linked resources will be resolved here!
            IPath rawPath = outputFolder.getRawLocation();
            if (rawPath != null) {
                path = rawPath;
            }
        }
    } else {
        path = project.getProject().getLocation();
    }

    if (path == null) {
        return dir;
    }

    // here we should resolve path variables,
    // probably existing at first place of path
    IPathVariableManager pathManager = workspace.getPathVariableManager();
    path = pathManager.resolvePath(path);

    if (path == null) {
        return dir;
    }

    boolean packageRoot = false;
    try {
        packageRoot = isPackageRoot(project, resource);
    } catch (JavaModelException e) {
        // seems to be a bug in 3.3
    }
    if (packageRoot) {
        dir = path.toOSString();
    } else {
        String packPath = getPackageName().replace('.', '/');
        dir = path.append(packPath).toOSString();
    }
    return dir;
}

From source file:de.lynorics.eclipse.jangaroo.m2e.JangarooProjectConversionParticipant.java

License:Open Source License

private void configureBuildSourceDirectories(Model model, IJavaProject javaProject) throws CoreException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    Set<String> sources = new LinkedHashSet<String>();
    Set<String> potentialTestSources = new LinkedHashSet<String>();
    Set<String> potentialResourceDirectories = new LinkedHashSet<String>();
    Set<String> potentialTestResourceDirectories = new LinkedHashSet<String>();
    IPath projectPath = javaProject.getPath();

    for (int i = 0; i < entries.length; i++) {
        if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath path = entries[i].getPath().makeRelativeTo(projectPath);
            if (path.isAbsolute()) {
                //We only support paths relative to the project root, so we skip this one
                continue;
            }/*  w ww.jav  a2s.co m*/
            String portablePath = path.toPortableString();
            boolean isPotentialTestSource = isPotentialTestSource(path);
            boolean isResource = false;
            if (isPotentialTestSource) {
                //          if(DEFAULT_TEST_RESOURCES.equals(portablePath)) {
                //            isResource = potentialTestResourceDirectories.add(portablePath);
                //          } else {
                //            potentialTestSources.add(portablePath);
                //          }
            } else {
                if (DEFAULT_RESOURCES.equals(portablePath)) {
                    isResource = potentialResourceDirectories.add(portablePath);
                } else {
                    sources.add(portablePath);
                }
            }

            if (!isResource) {
                //For source folders not already flagged as resource folder, check if 
                // they contain non-java sources, so we can add them as resources too
                boolean hasNonJangarooResources = false;
                IFolder folder = javaProject.getProject().getFolder(path);
                if (folder.isAccessible()) {
                    NonJangarooResourceVisitor nonJangarooResourceVisitor = new NonJangarooResourceVisitor();
                    try {
                        folder.accept(nonJangarooResourceVisitor);
                    } catch (NonJangarooResourceFoundException ex) {
                        //Expected
                        hasNonJangarooResources = true;
                    } catch (CoreException ex) {
                        //385666 ResourceException is thrown in Helios
                        if (ex.getCause() instanceof NonJangarooResourceFoundException) {
                            hasNonJangarooResources = true;
                        } else {
                            //                log.error("An error occured while analysing {} : {}", folder, ex.getMessage());
                        }
                    }
                }

                if (hasNonJangarooResources) {
                    if (isPotentialTestSource) {
                        potentialTestResourceDirectories.add(portablePath);
                    } else {
                        potentialResourceDirectories.add(portablePath);
                    }
                }
            }
        }
    }

    Build build = getOrCreateBuild(model);

    if (!sources.isEmpty()) {
        if (sources.size() > 1) {
            //We don't know how to handle multiple sources, i.e. how to map to a resource or test source directory
            //That should be dealt by setting the build-helper-plugin config (http://mojo.codehaus.org/build-helper-maven-plugin/usage.html)
            //        log.warn("{} has multiple source entries, this is not supported yet", model.getArtifactId()); //$NON-NLS-1$
        }
        String sourceDirectory = sources.iterator().next();
        if (!DEFAULT_JANGAROO_SOURCE.equals(sourceDirectory)) {
            build.setSourceDirectory(sourceDirectory);
        }

        for (String resourceDirectory : potentialResourceDirectories) {
            if (!DEFAULT_RESOURCES.equals(resourceDirectory) || potentialResourceDirectories.size() > 1) {
                build.addResource(createResource(resourceDirectory));
            }
        }
    }

    if (!potentialTestSources.isEmpty()) {
        if (potentialTestSources.size() > 1) {
            //        log.warn("{} has multiple test source entries, this is not supported yet", model.getArtifactId()); //$NON-NLS-1$
        }
        //      String testSourceDirectory = potentialTestSources.iterator().next();
        //      if(!DEFAULT_JAVA_TEST_SOURCE.equals(testSourceDirectory)) {
        //        build.setTestSourceDirectory(testSourceDirectory);
        //      }
        //      for(String resourceDirectory : potentialTestResourceDirectories) {
        //        if(!DEFAULT_TEST_RESOURCES.equals(resourceDirectory) || potentialTestResourceDirectories.size() > 1) {
        //          build.addTestResource(createResource(resourceDirectory));
        //        }
        //      }
    }

    //Ensure we don't attach a new empty build definition to the model
    if (build.getSourceDirectory() != null || build.getTestSourceDirectory() != null
            || !build.getResources().isEmpty() || !build.getTestResources().isEmpty()) {
        model.setBuild(build);
    }
}

From source file:de.ovgu.featureide.ahead.actions.FeatureHouseToAHEADConversion.java

License:Open Source License

/**
 * Sets the java build path to the build folder of the given feature project.
 * @param featureProject/* w  w w  .j a v a 2 s  .com*/
 */
private void setJavaBuildPath(IFeatureProject featureProject) {
    try {
        JavaProject javaProject = new JavaProject(featureProject.getProject(), null);
        IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
        for (int i = 0; i < classpathEntries.length; i++) {
            /** change the actual source entry **/
            if (classpathEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                classpathEntries[i] = setSourceEntry(classpathEntries[i]);
                javaProject.setRawClasspath(classpathEntries, null);
                return;
            }
        }

        /** case: no source entry **/
        IClasspathEntry[] newEntries = new IClasspathEntry[classpathEntries.length + 1];
        System.arraycopy(classpathEntries, 0, newEntries, 0, classpathEntries.length);
        newEntries[newEntries.length - 1] = getSourceEntry();
        javaProject.setRawClasspath(classpathEntries, null);
    } catch (JavaModelException e) {
        AheadCorePlugin.getDefault().logError(e);
    }
}

From source file:de.ovgu.featureide.ahead.actions.FeatureHouseToAHEADConversion.java

License:Open Source License

/**
 * @return A default source entry/*www.  j a  v a 2 s .  c  o  m*/
 */
public IClasspathEntry getSourceEntry() {
    return new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, IClasspathEntry.CPE_SOURCE,
            featureProject.getBuildFolder().getFullPath(), new IPath[0], new IPath[0], null, null, null, false,
            null, false, new IClasspathAttribute[0]);
}

From source file:de.ovgu.featureide.aspectj.AspectJComposer.java

License:Open Source License

/**
 * Set the unselected aspects to be excluded from build
 * @param project//from  ww w  .  ja v a 2  s  .c  o m
 */
private void setBuildpaths(IProject project) {
    String buildPath = null;
    if (featureProject == null || featureProject.getBuildPath() == null) {
        buildPath = IFeatureProject.DEFAULT_SOURCE_PATH;
    } else {
        buildPath = featureProject.getBuildPath();
    }

    try {
        JavaProject javaProject = new JavaProject(project, null);
        IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
        /** check if entries already exist **/
        for (int i = 0; i < oldEntries.length; i++) {
            if (oldEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                /** correct the source entry **/
                oldEntries[i] = setSourceEntry(oldEntries[i]);
                javaProject.setRawClasspath(oldEntries, null);
                return;
            }
        }

        /** add the new entry **/
        IClasspathEntry[] entries = new IClasspathEntry[oldEntries.length + 1];
        System.arraycopy(oldEntries, 0, entries, 0, oldEntries.length);
        entries[oldEntries.length] = setSourceEntry(getSourceEntry(buildPath));
        javaProject.setRawClasspath(entries, null);
    } catch (JavaModelException e) {
        CorePlugin.getDefault().logError(e);
    }
}

From source file:de.ovgu.featureide.aspectj.AspectJComposer.java

License:Open Source License

private void addClasspathFile(IProject project, String buildPath) {
    if (buildPath == null) {
        if (featureProject == null || featureProject.getBuildPath() == null) {
            buildPath = IFeatureProject.DEFAULT_SOURCE_PATH;
        } else {//  ww  w . j a  va 2  s  .  c  o  m
            buildPath = featureProject.getBuildPath();
        }
    }

    try {
        JavaProject javaProject = new JavaProject(project, null);
        IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
        boolean sourceAdded = false;
        boolean containerAdded = false;
        boolean ajContainerAdded = false;
        /** check if entries already exist **/
        for (int i = 0; i < oldEntries.length; i++) {
            if (!sourceAdded && oldEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                /** correct the source entry **/
                oldEntries[i] = getSourceEntry(buildPath);
                sourceAdded = true;
            } else if ((!containerAdded || !ajContainerAdded)
                    && oldEntries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                /** check the container entries **/
                if (oldEntries[i].getPath().equals(JRE_CONTAINER)) {
                    containerAdded = true;
                }
                if (oldEntries[i].getPath().equals(ASPECTJRT_CONTAINER)) {
                    ajContainerAdded = true;
                }

            }
        }
        /** case: no new entries **/
        if (sourceAdded && containerAdded && ajContainerAdded) {
            javaProject.setRawClasspath(oldEntries, null);
            return;
        }

        /** add the new entries **/
        IClasspathEntry[] entries = new IClasspathEntry[(sourceAdded ? 0 : 1) + (containerAdded ? 0 : 1)
                + (containerAdded ? 0 : 1) + oldEntries.length];
        System.arraycopy(oldEntries, 0, entries, 0, oldEntries.length);

        if (!sourceAdded) {
            entries[oldEntries.length] = getSourceEntry(buildPath);
        }
        if (!containerAdded) {
            int position = (sourceAdded ? 0 : 1) + oldEntries.length;
            entries[position] = getContainerEntry();
        }
        if (!ajContainerAdded) {
            int position = (sourceAdded ? 0 : 1) + (containerAdded ? 0 : 1) + oldEntries.length;
            entries[position] = getAJContainerEntry();
        }
        javaProject.setRawClasspath(entries, null);
    } catch (JavaModelException e) {
        CorePlugin.getDefault().logError(e);
    }
}

From source file:de.ovgu.featureide.core.builder.ComposerExtensionClass.java

License:Open Source License

private void addClasspathFile(IProject project, String buildPath) {
    try {/*from   ww  w  .  j  a v a  2 s. co m*/
        JavaProject javaProject = new JavaProject(project, null);
        IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
        boolean sourceAdded = false;
        boolean containerAdded = false;
        /** check if entries already exist **/
        for (int i = 0; i < oldEntries.length; i++) {
            if (!sourceAdded && oldEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                /** correct the source entry **/
                // XXX the source entry should be equivalent to the build path, 
                // but e.g. at FeatureHouse the real build path is src/config -> Builder problems
                // -> is it necessary to correct the path?
                if (oldEntries[i].getPath().toString().equals("/" + project.getName())) {
                    /** necessary after creating a new FeatureIDE project **/
                    oldEntries[i] = setSourceEntry(buildPath, oldEntries[i]);
                }
                /** find source entry **/
                sourceAdded = true;
            } else if (!containerAdded && oldEntries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                /** check the container entries **/
                if (oldEntries[i].getPath().equals(JRE_CONTAINER)) {
                    containerAdded = true;
                }
            }
        }
        /** case: no new entries **/
        if (sourceAdded && containerAdded) {
            javaProject.setRawClasspath(oldEntries, null);
            return;
        }

        /** add the new entries **/
        IClasspathEntry[] entries = new IClasspathEntry[(sourceAdded ? 0 : 1) + (containerAdded ? 0 : 1)
                + oldEntries.length];
        System.arraycopy(oldEntries, 0, entries, 0, oldEntries.length);

        if (!sourceAdded) {
            entries[oldEntries.length] = getSourceEntry(buildPath);
        }
        if (!containerAdded) {
            int position = (sourceAdded ? 0 : 1) + oldEntries.length;
            entries[position] = getContainerEntry();
        }

        javaProject.setRawClasspath(entries, null);
    } catch (JavaModelException e) {
        CorePlugin.getDefault().logError(e);
    }
}