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

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

Introduction

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

Prototype

IPath getPath();

Source Link

Document

Returns the path of this classpath entry.

Usage

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JavaUtils.java

License:Open Source License

/**
 * Creates a JAR file containing all the resources contained in the source folders of a Java project.
 * @param jp the Java project/*from  w  w  w.j a  va  2s.c o m*/
 * @param monitor the progress monitor
 * @return the JAR file, which was saved in the temporary folder (and should be deleted after use)
 * @throws InvocationTargetException if the creation failed
 * @throws InterruptedException if the creation was interrupted
 */
public static File createDefaultJar(IJavaProject jp, IProgressMonitor monitor)
        throws InvocationTargetException, InterruptedException {

    // Create a default JAR for the implementation
    JarPackageData jarDescription = new JarPackageData();
    jarDescription.setIncludeDirectoryEntries(true);
    jarDescription.setExportClassFiles(true);
    jarDescription.setOverwrite(true);
    jarDescription.setIncludeDirectoryEntries(true);

    jarDescription.setExportJavaFiles(false);
    jarDescription.setCompress(true);
    jarDescription.setExportErrors(true);
    jarDescription.setExportWarnings(true);

    // Add all the files in the source folders
    List<Object> filesToPackage = new ArrayList<Object>();
    for (IClasspathEntry entry : getSourceFolders(jp)) {

        // PETALSSTUD-130: use the project location and not the workspace root as a reference
        File f = jp.getProject().getLocation().append(entry.getPath().removeFirstSegments(1)).toFile();
        // PETALSSTUD-130

        IFolder folder = (IFolder) ResourceUtils.getResource(f);
        if (folder != null) {
            List<IFile> files = ResourceUtils.getFilesByRegexp(folder, ".*");
            filesToPackage.addAll(files);
        }
    }

    Object[] elements = new Object[filesToPackage.size()];
    jarDescription.setElements(filesToPackage.toArray(elements));

    // Create the JAR
    IPath jarLocation = new Path(System.getProperty("java.io.tmpdir"))
            .append(jp.getProject().getName() + ".jar");

    // Bug: Windows => path separator = "\"
    // But IPath#isAbsolute() only checks for "/" path separators
    // => We replace the path separator
    // Otherwise, the JAR is created, but not at the location we would expect
    jarLocation = new Path(jarLocation.toString().replaceAll("\\\\", "/"));
    jarDescription.setJarLocation(jarLocation);

    IJarExportRunnable runnable = jarDescription.createJarExportRunnable(null);
    runnable.run(monitor);
    IStatus status = runnable.getStatus();
    if (!status.isOK())
        PetalsCommonPlugin.getDefault().getLog().log(status);

    return jarLocation.toFile();
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JavaUtils.java

License:Open Source License

/**
 * Populates an {@link ExportableClassPath} instance from a class path entry.
 * @param result the {@link ExportableClassPath} instance to populate
 * @param entry a class path entry/*from  w  w w  .  j  a  v  a 2  s  . co  m*/
 * @param monitor the progress monitor
 */
private static void updateExportableResult(ExportableClassPath result, IClasspathEntry entry,
        IProgressMonitor monitor) {

    switch (entry.getEntryKind()) {

    case IClasspathEntry.CPE_PROJECT:
        String projectName = entry.getPath().toString();
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        IJavaProject jProject = JavaCore.create(project);

        ExportableClassPath subResult = getExportableClasspath(jProject, monitor);
        result.implementationJars.addAll(subResult.implementationJars);
        result.libraryJars.addAll(subResult.libraryJars);
        monitor.worked(1);
        break;

    case IClasspathEntry.CPE_LIBRARY:
        IPath path = entry.getPath();
        if (path != null) {
            File f = path.toFile();
            if (f.exists()) {
                if (f.getName().endsWith(".zip") || f.getName().endsWith(".jar"))
                    result.libraryJars.add(f);
            }
        }
        break;

    case IClasspathEntry.CPE_VARIABLE:
        entry = JavaCore.getResolvedClasspathEntry(entry);
        if (entry != null)
            updateExportableResult(result, entry, monitor);

        break;
    }
}

From source file:com.feup.contribution.druid.data.DruidProject.java

License:Open Source License

private String getClasspath() {
    String cp = "";
    try {/*  w w  w  .j  a  v a2  s . co  m*/
        IClasspathEntry[] classpath = project.getJavaProject().getResolvedClasspath(false);
        for (IClasspathEntry classpathEntry : classpath) {
            if (cp.equals(""))
                cp = classpathEntry.getPath().toOSString();
            else
                cp += ":" + classpathEntry.getPath().toOSString();
        }
    } catch (JavaModelException e) {
        DruidPlugin.getPlugin().logException(e);
    }
    return cp;
}

From source file:com.github.caofangkun.bazelipse.classpath.BazelClasspathContainer.java

License:Open Source License

private boolean isSourcePath(String path) throws JavaModelException, BackingStoreException {
    Path pp = new File(instance.getWorkspaceRoot().toString() + File.separator + path).toPath();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    for (IClasspathEntry entry : project.getRawClasspath()) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IResource res = root.findMember(entry.getPath());
            if (res != null) {
                String file = res.getLocation().toOSString();
                if (!file.isEmpty() && pp.startsWith(file)) {
                    IPath[] inclusionPatterns = entry.getInclusionPatterns();
                    if (!matchPatterns(pp, entry.getExclusionPatterns()) && (inclusionPatterns == null
                            || inclusionPatterns.length == 0 || matchPatterns(pp, inclusionPatterns))) {
                        return true;
                    }/*w w  w  .  j  a va2 s. c  o m*/
                }
            }
        }
    }
    return false;
}

From source file:com.github.ko2ic.plugin.eclipse.taggen.core.service.WorkspaceClassLoader.java

License:Open Source License

private Set<URL> createUrls(IJavaProject javaProject) throws CoreException, MalformedURLException {
    Set<URL> urlList = new HashSet<>();
    IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
    for (IClasspathEntry entry : classpathEntries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath path = entry.getOutputLocation();
            if (path != null) {
                IPath outputPath = javaProject.getOutputLocation().removeFirstSegments(1);
                IPath outputFullPath = javaProject.getProject().getFolder(outputPath).getLocation();
                outputFullPath.append(System.getProperty("line.separator"));
                URL url = outputFullPath.toFile().toURI().toURL();
                urlList.add(url);/*from  ww w .  j  av a  2 s.  c  om*/
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            URL url = entry.getPath().toFile().toURI().toURL();
            urlList.add(url);
        }
    }
    return urlList;
}

From source file:com.google.appengine.eclipse.core.orm.enhancement.AutoEnhancer.java

License:Open Source License

/**
 * Determines the path of the Java source file that produced the given class
 * file path. We don't know which source folder contains the original Java
 * source, so we'll just try each of them until we find it.
 * //from w w  w  . j  ava  2  s . c  o  m
 * @return the path of the Java source file, or <code>null</code> if a
 *         corresponding Java source file could not be found under any of the
 *         project's source folders.
 */
private IPath computeJavaFilePath(IPath classFilePath) throws JavaModelException {
    for (IClasspathEntry classpathEntry : javaProject.getRawClasspath()) {
        if (classpathEntry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            // Skip any classpath entry that is not a source entry
            continue;
        }

        IPath outputLocation = getOutputLocation(classpathEntry, javaProject.getOutputLocation());

        IPath javaFilePath = computeJavaFilePath(classFilePath, outputLocation, classpathEntry.getPath());
        if (javaFilePath != null && ResourcesPlugin.getWorkspace().getRoot().findMember(javaFilePath) != null) {
            return javaFilePath;
        }
    }

    AppEngineCorePluginLog.logWarning("Could not find Java source for class file " + classFilePath.toString());
    return null;
}

From source file:com.google.appengine.eclipse.core.sdk.AppEngineUpdateProjectSdkCommand.java

License:Open Source License

static IPath computeAppEngineSdkInstallPath(IJavaProject javaProject) throws CoreException {
    assert (ClasspathUtilities.findClasspathEntryContainer(javaProject.getRawClasspath(),
            GaeSdkContainer.CONTAINER_ID) == null);
    for (IClasspathEntry entry : javaProject.getRawClasspath()) {
        entry = JavaCore.getResolvedClasspathEntry(entry);
        IPath entryPath = entry.getPath();
        String fileName = entryPath.lastSegment();
        if (fileName.matches("appengine\\-tools\\-api.*\\.jar")) {
            if (entryPath.segmentCount() > 2) {
                // TODO: We should check that the jar exists on disk. Throw a
                // CoreException with a more informative message.
                return entryPath.removeLastSegments(2);
            }/*w  w  w  . j  a v a  2  s.  co  m*/
        }
    }
    return null;
}

From source file:com.google.cloud.tools.eclipse.appengine.compat.GpeMigrator.java

License:Apache License

@VisibleForTesting
static void removeGpeClasspathEntries(IProject project) {
    try {/*from  www.ja va 2 s .c  o  m*/
        IJavaProject javaProject = JavaCore.create(project);
        List<IClasspathEntry> newEntries = new ArrayList<>();
        for (IClasspathEntry entry : javaProject.getRawClasspath()) {
            String path = entry.getPath().toString(); // note: '/' is a path separator.
            if (!GPE_CLASSPATH_ENTRIES_PATH.contains(path)) {
                newEntries.add(entry);
            }
        }

        IClasspathEntry[] rawEntries = newEntries.toArray(new IClasspathEntry[0]);
        javaProject.setRawClasspath(rawEntries, new NullProgressMonitor());
        javaProject.save(new NullProgressMonitor(), true);

    } catch (JavaModelException ex) {
        logger.log(Level.WARNING, "Failed to remove GPE classpath entries.", ex);
    }
}

From source file:com.google.cloud.tools.eclipse.appengine.compat.GpeMigratorTest.java

License:Apache License

private static boolean containsLibrary(IJavaProject javaProject, String libraryPath) throws JavaModelException {
    for (IClasspathEntry entry : javaProject.getRawClasspath()) {
        String path = entry.getPath().toString(); // note: '/' is a path separator.
        if (path.equals(libraryPath)) {
            return true;
        }/*  w  w  w . ja v a  2  s. c  om*/
    }
    return false;
}

From source file:com.google.cloud.tools.eclipse.appengine.facets.FacetUninstallDelegate.java

License:Open Source License

private void removeAppEngineJarsFromClasspath(IProject project, IProgressMonitor monitor) throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    IClasspathEntry[] newClasspath = new IClasspathEntry[rawClasspath.length - 1];

    int appEngineContainerIndex = 0;
    boolean isAppEngineSdkPresent = false;
    for (IClasspathEntry entry : rawClasspath) {
        if (AppEngineSdkClasspathContainer.CONTAINER_ID.equals(entry.getPath().toString())) {
            isAppEngineSdkPresent = true;
        } else {//from www.  ja v a 2  s .co m
            newClasspath[appEngineContainerIndex++] = entry;
        }
    }

    if (isAppEngineSdkPresent) {
        javaProject.setRawClasspath(newClasspath, monitor);
    }
}