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

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

Introduction

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

Prototype

int getEntryKind();

Source Link

Document

Returns the kind of this classpath entry.

Usage

From source file:de.ovgu.featureide.featurehouse.FeatureHouseComposer.java

License:Open Source License

/**
 * Sets the Java build path to the folder at the build folder, named like the current configuration.
 * @param buildPath The name of the current configuration
 *//*from  ww w .  j  a va  2s.co  m*/
private void setJavaBuildPath(String buildPath) {
    try {
        JavaProject javaProject = new JavaProject(featureProject.getProject(), null);
        IClasspathEntry[] classpathEntrys = javaProject.getRawClasspath();

        int i = 0;
        for (IClasspathEntry e : classpathEntrys) {
            if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath path = featureProject.getBuildFolder().getFolder(buildPath).getFullPath();

                /** return if nothing has to be changed **/
                if (e.getPath().equals(path)) {
                    return;
                }

                /** change the actual source entry to the new build path **/
                ClasspathEntry changedEntry = new ClasspathEntry(e.getContentKind(), e.getEntryKind(), path,
                        e.getInclusionPatterns(), e.getExclusionPatterns(), e.getSourceAttachmentPath(),
                        e.getSourceAttachmentRootPath(), null, e.isExported(), e.getAccessRules(),
                        e.combineAccessRules(), e.getExtraAttributes());
                classpathEntrys[i] = changedEntry;
                javaProject.setRawClasspath(classpathEntrys, null);
                return;
            }
            i++;
        }

        /** case: there is no source entry at the class path
         *       add the source entry to the classpath **/
        IFolder folder = featureProject.getBuildFolder().getFolder(buildPath);
        ClasspathEntry sourceEntry = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE,
                IClasspathEntry.CPE_SOURCE, folder.getFullPath(), new IPath[0], new IPath[0], null, null, null,
                false, null, false, new IClasspathAttribute[0]);
        IClasspathEntry[] newEntrys = new IClasspathEntry[classpathEntrys.length + 1];
        System.arraycopy(classpathEntrys, 0, newEntrys, 0, classpathEntrys.length);
        newEntrys[newEntrys.length - 1] = sourceEntry;
        javaProject.setRawClasspath(newEntrys, null);
    } catch (JavaModelException e) {
        FeatureHouseCorePlugin.getDefault().logError(e);
    }
}

From source file:de.ovgu.featureide.migration.impl.DefaultSPLMigrator.java

License:Open Source License

/**
 * @param project/*from www.ja v a  2s .  c  o  m*/
 * @param destinationPath
 * @param classpathToMigrate
 */
private void migrateSourceFiles(IProject project, IPath destinationPath, IClasspathEntry[] classpathToMigrate) {
    IPath relativeDestinationPath = ((IPath) destinationPath.clone()).makeRelativeTo(newProject.getFullPath());
    System.out.println(
            "migrate source destination: " + destinationPath + " relative: " + relativeDestinationPath);
    IFolder destination = newProject.getFolder(relativeDestinationPath);

    for (IClasspathEntry entry : classpathToMigrate) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            try {
                IPath relativeSourcePath = entry.getPath().makeRelativeTo(project.getFullPath());
                IFolder source = project.getFolder(relativeSourcePath);

                SPLMigrationUtils.recursiveCopyFiles(source, destination);

                newProject.refreshLocal(IProject.DEPTH_INFINITE, null);

            } catch (CoreException e) {
                CorePlugin.getDefault().logError(e);
                e.printStackTrace();
            }
        }
    }
}

From source file:de.ovgu.featureide.migration.impl.DefaultSPLMigrator.java

License:Open Source License

/**
 * @param newJavaProject/*from  w  w  w . j a  v a  2 s  .  co m*/
 * @param classpathToMigrate
 * @param newClassPath
 * @throws JavaModelException
 */
private void migrateLibraryAndContainerEntries(JavaProject newJavaProject, IClasspathEntry[] classpathToMigrate,
        List<IClasspathEntry> newClassPath) throws JavaModelException {
    for (IClasspathEntry entry : classpathToMigrate) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                || entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
            if (!newClassPath.contains(entry))
                newClassPath.add(entry);
    }
    newJavaProject.setRawClasspath(newClassPath.toArray(new IClasspathEntry[newClassPath.size()]), null);
}

From source file:de.ovgu.featureide.ui.actions.generator.Generator.java

License:Open Source License

/**
 * Sets the classpath entries for the newly created project
 * @param p The new project/*  w  w w  . jav  a2 s.  co  m*/
 */
// TODO remove redundant calculations for each configuration
// TODO copy settings
private void setClassPath(IProject p) {
    JavaProject baseProject = new JavaProject(builder.featureProject.getProject(), null);
    JavaProject newProject = new JavaProject(p, null);
    try {
        IClasspathEntry[] entries = baseProject.getRawClasspath().clone();
        for (int i = 0; i < entries.length; i++) {
            // set source entry to "src"
            IClasspathEntry e = entries[i];
            if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                entries[i] = new ClasspathEntry(e.getContentKind(), e.getEntryKind(), new Path("src"),
                        e.getInclusionPatterns(), e.getExclusionPatterns(), e.getSourceAttachmentPath(),
                        e.getSourceAttachmentRootPath(), null, e.isExported(), e.getAccessRules(),
                        e.combineAccessRules(), e.getExtraAttributes());
            } else if (e.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                // set the library entries and copy the libraries 
                // which are direct at the old projects folder  
                IPath path = e.getPath().removeFirstSegments(1);
                IProject project = builder.featureProject.getProject();
                IFile file = project.getFile(path);
                if (!file.exists()) {
                    path = e.getPath();
                    file = project.getFile(path);
                    if (!file.exists()) {
                        continue;
                    }
                }
                createLibFolder(p.getFile(path).getParent());
                file.copy(p.getFile(e.getPath().removeFirstSegments(1)).getFullPath(), true, null);
                entries[i] = new ClasspathEntry(e.getContentKind(), e.getEntryKind(),
                        e.getPath().removeFirstSegments(1), e.getInclusionPatterns(), e.getExclusionPatterns(),
                        e.getSourceAttachmentPath(), e.getSourceAttachmentRootPath(), null, e.isExported(),
                        e.getAccessRules(), e.combineAccessRules(), e.getExtraAttributes());
            }
        }
        newProject.setRawClasspath(entries, null);
    } catch (JavaModelException e) {
        UIPlugin.getDefault().logError(e);
    } catch (CoreException e) {
        UIPlugin.getDefault().logError(e);
    }
}

From source file:de.ovgu.featureide.ui.wizards.ConversionPage.java

License:Open Source License

/**
 * Set the build path to the java projects build path
 *///from  ww w . j  a va  2 s  .  c  om
private void setBuildPath() {
    try {
        if (p.hasNature(JAVA_NATURE)) {
            JavaProject javaProject = new JavaProject(p, null);
            for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    String path = entry.getPath().toOSString();
                    if (path.contains("\\"))
                        path = path.substring(path.indexOf('\\') + 1);
                    if (path.contains("\\"))
                        path = path.substring(path.indexOf('\\') + 1);

                    buildPath.setText(path);
                    buildPath.setEnabled(false);
                    buildLabel.setToolTipText(MESSAGE);
                    return;
                }
            }
        }
    } catch (CoreException e) {
        UIPlugin.getDefault().logError(e);
    }
}

From source file:de.ovgu.featureide.ui.wizards.FeatureCProjectPage.java

License:Open Source License

/**
 * Set the build path to the java projects build path
 *///  ww w  .  ja v  a  2s  .  c  o m
private void setBuildPath() {
    try {
        if (project.hasNature(JAVA_NATURE)) {
            JavaProject javaProject = new JavaProject(project, null);
            for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    String path = entry.getPath().toOSString();
                    String fileSeparator = System.getProperty("file.separator");

                    if (path.contains(fileSeparator))
                        path = path.substring(path.indexOf(fileSeparator) + 1);
                    if (path.contains(fileSeparator))
                        path = path.substring(path.indexOf(fileSeparator) + 1);

                    buildPath.setText(path);
                    buildPath.setEnabled(false);
                    buildLabel.setToolTipText(MESSAGE);
                    return;
                }
            }
        }
    } catch (CoreException e) {
        UIPlugin.getDefault().logError(e);
    }
}

From source file:de.tobject.findbugs.builder.FindBugsWorker.java

License:Open Source License

/**
 * @return map of all source folders to output folders, for current java
 *         project, where both are represented by absolute IPath objects
 *
 * @throws CoreException// ww w .jav a  2s  .c o m
 */
private Map<IPath, IPath> createOutputLocations() throws CoreException {

    Map<IPath, IPath> srcToOutputMap = new HashMap<IPath, IPath>();

    // get the default location => relative to wsp
    IPath defaultOutputLocation = ResourceUtils.relativeToAbsolute(javaProject.getOutputLocation());
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    // path to the project without project name itself
    IClasspathEntry entries[] = javaProject.getResolvedClasspath(true);
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry classpathEntry = entries[i];
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath outputLocation = ResourceUtils.getOutputLocation(classpathEntry, defaultOutputLocation);
            if (outputLocation == null) {
                continue;
            }
            IResource cpeResource = root.findMember(classpathEntry.getPath());
            // patch from 2891041: do not analyze derived "source" folders
            // because they probably contain auto-generated classes
            if (cpeResource != null && cpeResource.isDerived()) {
                continue;
            }
            // TODO not clear if it is absolute in workspace or in global FS
            IPath srcLocation = ResourceUtils.relativeToAbsolute(classpathEntry.getPath());
            if (srcLocation != null) {
                srcToOutputMap.put(srcLocation, outputLocation);
            }
        }
    }

    return srcToOutputMap;
}

From source file:de.tobject.findbugs.builder.PDEClassPathGenerator.java

License:Open Source License

@SuppressWarnings("restriction")
private static Set<String> createJavaClasspath(IJavaProject javaProject, Set<IProject> projectOnCp) {
    LinkedHashSet<String> classPath = new LinkedHashSet<String>();
    try {// w  ww  . j a  v  a 2s . c o m
        // doesn't return jre libraries
        String[] defaultClassPath = JavaRuntime.computeDefaultRuntimeClassPath(javaProject);
        for (String classpathEntry : defaultClassPath) {
            IPath path = new Path(classpathEntry);
            if (isValidPath(path)) {
                classPath.add(path.toOSString());
            }
        }
        // add CPE_CONTAINER classpathes
        IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
        for (IClasspathEntry entry : rawClasspath) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(entry.getPath(),
                        javaProject);
                if (classpathContainer != null) {
                    if (classpathContainer instanceof JREContainer) {
                        IClasspathEntry[] classpathEntries = classpathContainer.getClasspathEntries();
                        for (IClasspathEntry iClasspathEntry : classpathEntries) {
                            IPath path = iClasspathEntry.getPath();
                            // smallest possible fix for #1228 Eclipse plugin always uses host VM to resolve JDK classes
                            if (isValidPath(path) && "rt.jar".equals(path.lastSegment())) {
                                classPath.add(path.toOSString());
                                break;
                            }
                        }
                    } else {
                        IClasspathEntry[] classpathEntries = classpathContainer.getClasspathEntries();
                        for (IClasspathEntry classpathEntry : classpathEntries) {
                            IPath path = classpathEntry.getPath();
                            // shortcut for real files
                            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                                    && isValidPath(path)) {
                                classPath.add(path.toOSString());
                            } else {
                                resolveInWorkspace(classpathEntry, classPath, projectOnCp);
                            }
                        }
                    }
                }
            }
        }
    } catch (CoreException e) {
        FindbugsPlugin.getDefault().logException(e,
                "Could not compute aux. classpath for project " + javaProject);
    }
    return classPath;
}

From source file:de.tobject.findbugs.builder.PDEClassPathGenerator.java

License:Open Source License

private static void resolveInWorkspace(IClasspathEntry classpathEntry, Set<String> classPath,
        Set<IProject> projectOnCp) {
    int entryKind = classpathEntry.getEntryKind();
    switch (entryKind) {
    case IClasspathEntry.CPE_PROJECT:
        Set<String> cp = resolveProjectClassPath(classpathEntry.getPath(), projectOnCp);
        classPath.addAll(cp);//from  w w w  .  ja  va2  s . c o m
        break;
    case IClasspathEntry.CPE_VARIABLE:
        classpathEntry = JavaCore.getResolvedClasspathEntry(classpathEntry);
        if (classpathEntry == null) {
            return;
        }
        //$FALL-THROUGH$
    case IClasspathEntry.CPE_LIBRARY:
        String lib = resolveLibrary(classpathEntry.getPath());
        if (lib != null) {
            classPath.add(lib);
        }
        break;
    case IClasspathEntry.CPE_SOURCE:
        // ???
        break;
    default:
        break;
    }
}

From source file:de.tobject.findbugs.builder.PDEClassPathGenerator.java

License:Open Source License

private static void appendBundleToClasspath(BundleDescription bd, List<String> pdeClassPath,
        IPath defaultOutputLocation) {//from  ww w.  j a  va  2  s.  co m
    IPluginModelBase model = PluginRegistry.findModel(bd);
    if (model == null) {
        return;
    }
    ArrayList<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
    ClasspathUtilCore.addLibraries(model, classpathEntries);

    for (IClasspathEntry cpe : classpathEntries) {
        IPath location;
        if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            location = ResourceUtils.getOutputLocation(cpe, defaultOutputLocation);
        } else {
            location = cpe.getPath();
        }
        if (location == null) {
            continue;
        }
        String locationStr = location.toOSString();
        if (pdeClassPath.contains(locationStr)) {
            continue;
        }
        // extra cleanup for some directories on classpath
        String bundleLocation = bd.getLocation();
        if (bundleLocation != null && !"jar".equals(location.getFileExtension())
                && new File(bundleLocation).isDirectory()) {
            if (bd.getSymbolicName().equals(location.lastSegment())) {
                // ignore badly resolved plugin directories inside workspace
                // ("." as classpath is resolved as plugin root directory)
                // which is, if under workspace, NOT a part of the classpath
                continue;
            }
        }
        if (!location.isAbsolute()) {
            location = ResourceUtils.relativeToAbsolute(location);
        }
        if (!isValidPath(location)) {
            continue;
        }
        locationStr = location.toOSString();
        if (!pdeClassPath.contains(locationStr)) {
            pdeClassPath.add(locationStr);
        }
    }
}