Example usage for org.eclipse.jdt.core IJavaProject getResolvedClasspath

List of usage examples for org.eclipse.jdt.core IJavaProject getResolvedClasspath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getResolvedClasspath.

Prototype

IClasspathEntry[] getResolvedClasspath(boolean ignoreUnresolvedEntry) throws JavaModelException;

Source Link

Document

This is a helper method returning the resolved classpath for the project as a list of simple (non-variable, non-container) classpath entries.

Usage

From source file:org.jboss.tools.ws.creation.core.utils.JBossWSCreationUtils.java

License:Open Source License

public static IResource[] getJavaSourceRoots(IJavaProject javaProject, boolean outputLocationIsNull)
        throws JavaModelException {
    if (javaProject == null)
        return null;
    List<IResource> resources = new ArrayList<IResource>();
    IClasspathEntry[] es = javaProject.getResolvedClasspath(true);
    for (int i = 0; i < es.length; i++) {
        if (es[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath outputLocation = es[i].getOutputLocation();
            IResource findMember = ResourcesPlugin.getWorkspace().getRoot().findMember(es[i].getPath());
            if (findMember != null && findMember.exists()) {
                // JBIDE-8642: if the output location is null, this is the default source path
                if (outputLocationIsNull && outputLocation == null) {
                    resources.add(findMember);
                } else {
                    resources.add(findMember);
                }/*from   ww  w .j  av a2  s  . c  o  m*/
            }
        }
    }
    return resources.toArray(new IResource[resources.size()]);
}

From source file:org.jbpm.eclipse.util.ProjectClassLoader.java

License:Apache License

public static List<URL> getProjectClassPathURLs(IJavaProject project, List<String> alreadyLoadedProjects) {
    List<URL> pathElements = new ArrayList<URL>();
    try {/*from   www .  j  a v  a 2 s . co  m*/
        IClasspathEntry[] paths = project.getResolvedClasspath(true);
        Set<IPath> outputPaths = new HashSet<IPath>();
        if (paths != null) {
            for (int i = 0; i < paths.length; i++) {
                IClasspathEntry path = paths[i];
                if (path.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    URL url = getRawLocationURL(path.getPath());
                    pathElements.add(url);
                } else if (path.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath output = path.getOutputLocation();
                    if (path.getOutputLocation() != null) {
                        outputPaths.add(output);
                    }
                }
            }
        }
        IPath location = getProjectLocation(project.getProject());
        IPath outputPath = location.append(project.getOutputLocation().removeFirstSegments(1));
        pathElements.add(0, outputPath.toFile().toURI().toURL());
        for (IPath path : outputPaths) {
            outputPath = location.append(path.removeFirstSegments(1));
            pathElements.add(0, outputPath.toFile().toURI().toURL());
        }

        // also add classpath of required projects
        for (String projectName : project.getRequiredProjectNames()) {
            if (!alreadyLoadedProjects.contains(projectName)) {
                alreadyLoadedProjects.add(projectName);
                IProject reqProject = project.getProject().getWorkspace().getRoot().getProject(projectName);
                if (reqProject != null) {
                    IJavaProject reqJavaProject = JavaCore.create(reqProject);
                    pathElements.addAll(getProjectClassPathURLs(reqJavaProject, alreadyLoadedProjects));
                }
            }
        }
    } catch (JavaModelException e) {
        JBPMEclipsePlugin.log(e);
    } catch (MalformedURLException e) {
        JBPMEclipsePlugin.log(e);
    } catch (Throwable t) {
        JBPMEclipsePlugin.log(t);
    }
    return pathElements;
}

From source file:org.jetbrains.kotlin.core.utils.ProjectUtils.java

License:Apache License

public static List<IProject> getDependencyProjects(@NotNull IJavaProject javaProject)
        throws JavaModelException {
    List<IProject> projects = Lists.newArrayList();
    for (IClasspathEntry classPathEntry : javaProject.getResolvedClasspath(true)) {
        if (classPathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IPath path = classPathEntry.getPath();
            IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.toString());
            if (project.exists()) {
                projects.add(project);//from www. ja va 2s .  c  o m
                getDependencyProjects(JavaCore.create(project));
            }
        }
    }

    return projects;
}

From source file:org.jetbrains.kotlin.core.utils.ProjectUtils.java

License:Apache License

@NotNull
private static List<File> expandClasspath(@NotNull IJavaProject javaProject, boolean includeDependencies,
        boolean includeBinFolders, @NotNull Predicate<IClasspathEntry> entryPredicate)
        throws JavaModelException {
    Set<File> orderedFiles = Sets.newLinkedHashSet();

    for (IClasspathEntry classpathEntry : javaProject.getResolvedClasspath(true)) {
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT && includeDependencies) {
            orderedFiles//w  ww.j  a v a 2s  . c  o  m
                    .addAll(expandDependentProjectClasspath(classpathEntry, includeBinFolders, entryPredicate));
        } else { // Source folder or library
            if (entryPredicate.apply(classpathEntry)) {
                orderedFiles.addAll(getFileByEntry(classpathEntry, javaProject));
            }
        }
    }

    return Lists.newArrayList(orderedFiles);
}

From source file:org.jetbrains.kotlin.core.utils.ProjectUtils.java

License:Apache License

@NotNull
private static List<File> expandDependentProjectClasspath(@NotNull IClasspathEntry projectEntry,
        boolean includeBinFolders, @NotNull Predicate<IClasspathEntry> entryPredicate)
        throws JavaModelException {
    IPath projectPath = projectEntry.getPath();
    IProject dependentProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectPath.toString());
    IJavaProject javaProject = JavaCore.create(dependentProject);

    Set<File> orderedFiles = Sets.newLinkedHashSet();

    for (IClasspathEntry classpathEntry : javaProject.getResolvedClasspath(true)) {
        if (!(classpathEntry.isExported() || classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE)) {
            continue;
        }//from  w  ww  .ja va2 s  .c  o  m

        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            orderedFiles
                    .addAll(expandDependentProjectClasspath(classpathEntry, includeBinFolders, entryPredicate));
        } else {
            if (entryPredicate.apply(classpathEntry)) {
                orderedFiles.addAll(getFileByEntry(classpathEntry, javaProject));
            }
        }
    }

    if (includeBinFolders) {
        IFolder outputFolder = ProjectUtils.getOutputFolder(javaProject);
        if (outputFolder != null && outputFolder.exists()) {
            orderedFiles.add(outputFolder.getLocation().toFile());
        }
    }

    return Lists.newArrayList(orderedFiles);
}

From source file:org.jsweet.plugin.builder.JSweetBuilder.java

License:Apache License

private void fullBuild(BuildingContext context, final IProgressMonitor monitor) throws CoreException {
    Log.info("JSweet: full build...");
    context.project.deleteMarkers(JSWEET_PROBLEM_MARKER_TYPE, true, IResource.DEPTH_INFINITE);
    List<IPath> sourceDirs = new ArrayList<>();
    if (!StringUtils.isEmpty(Preferences.getSourceFolders(context.project, context.profile))) {
        String[] names = Preferences.getSourceFolders(context.project, context.profile).split("[,;]");
        try {/* w  w w.j  a  v  a 2s. c o  m*/
            for (String name : names) {
                sourceDirs.add(context.project.getFolder(name).getFullPath());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    String jdkHome = null;
    boolean lookupSourceFolder = sourceDirs.isEmpty();
    if (context.project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
        IJavaProject javaProject = JavaCore.create(context.project);
        IClasspathEntry[] classPathEntries = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry e : classPathEntries) {
            if (lookupSourceFolder && e.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                sourceDirs.add(e.getPath());
            } else if (e.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                if (e.getPath().toString().endsWith("/lib/rt.jar")) {
                    jdkHome = e.getPath().removeLastSegments(2).toString();
                }
            }
        }
    }
    Log.info("source dirs: " + sourceDirs);
    GrabJavaFilesVisitor v = new GrabJavaFilesVisitor(context, sourceDirs);

    context.project.accept(v);
    context.sourceFiles.clear();
    Log.info("init classpath, jdkHome: " + jdkHome);
    JSweetConfig.initClassPath(jdkHome);
    createJSweetTranspiler(context);
    transpileFiles(context, v.javaFiles.toArray(new File[0]));
}

From source file:org.jsweet.plugin.builder.JSweetBuilder.java

License:Apache License

private void createJSweetTranspiler(BuildingContext context) throws CoreException {
    if (context.USE_WATCH_MODE && context.transpiler != null) {
        Log.info("stopping tsc watch mode");
        context.transpiler.setTscWatchMode(false);
        Log.info("tsc watch mode stopped");
    }/*from  w  ww  .  j a v a  2  s . c o  m*/
    StringBuilder classPath = new StringBuilder();
    if (context.project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
        IJavaProject javaProject = JavaCore.create(context.project);
        IClasspathEntry[] classPathEntries = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry e : classPathEntries) {
            classPath.append(resolve(context.project, e.getPath()).toString());
            classPath.append(File.pathSeparator);
        }
    }
    Log.info("compiling with classpath: " + classPath.toString());
    File jsOutputFolder = new File(context.project.getLocation().toFile(),
            Preferences.getJsOutputFolder(context.project, context.profile));
    try {
        context.transpiler = new JSweetTranspiler(
                new File(context.project.getLocation().toFile(), JSweetTranspiler.TMP_WORKING_DIR_NAME),
                new File(context.project.getLocation().toFile(),
                        Preferences.getTsOutputFolder(context.project, context.profile)),
                jsOutputFolder,
                new File(context.project.getLocation().toFile(),
                        Preferences.getCandyJsOutputFolder(context.project, context.profile)),
                classPath.toString());
        context.transpiler
                .setPreserveSourceLineNumbers(Preferences.isJavaDebugMode(context.project, context.profile));
        String moduleString = Preferences.getModuleKind(context.project, context.profile);
        context.transpiler.setModuleKind(
                StringUtils.isBlank(moduleString) ? ModuleKind.none : ModuleKind.valueOf(moduleString));
        String bundleDirectory = Preferences.getBundlesDirectory(context.project, context.profile);
        if (!StringUtils.isBlank(bundleDirectory)) {
            File f = new File(bundleDirectory);
            if (!f.isAbsolute()) {
                f = new File(context.project.getLocation().toFile(), bundleDirectory);
            }
            context.transpiler.setBundlesDirectory(f);
        }
        context.transpiler.setBundle(Preferences.getBundle(context.project, context.profile));
        context.transpiler
                .setGenerateDeclarations(Preferences.getDeclaration(context.project, context.profile));
        String declarationDirectory = Preferences.getDeclarationDirectory(context.project, context.profile);
        if (!StringUtils.isBlank(declarationDirectory)) {
            File f = new File(declarationDirectory);
            if (!f.isAbsolute()) {
                f = new File(context.project.getLocation().toFile(), declarationDirectory);
            }
            context.transpiler.setDeclarationsOutputDir(f);
        }

        // transpiler.setTsDefDirs(new
        // File(context.project.getLocation().toFile(),
        // Preferences
        // .getTsOutputFolder(context.project)));
        if (context.USE_WATCH_MODE) {
            context.transpiler.setTscWatchMode(true);
        }
    } catch (NoClassDefFoundError error) {
        new JSweetTranspilationHandler(context).report(JSweetProblem.JAVA_COMPILER_NOT_FOUND, null,
                JSweetProblem.JAVA_COMPILER_NOT_FOUND.getMessage());
    }
}

From source file:org.key_project.key4eclipse.resources.util.KeYResourcesUtil.java

License:Open Source License

/**
 * Returns a {@link LinkedList} with all Java source folders ais {@link IPath}.
 * @param project - the project to search the source folders for.
 * @return the {@link LinkedList} with the source folders
 *///from   ww w  .j  ava 2 s .co m
public static LinkedList<IPath> getAllJavaSrcFolders(IProject project) {
    LinkedList<IPath> srcFolders = new LinkedList<IPath>();

    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] classpathEntries;
    try {
        classpathEntries = javaProject.getResolvedClasspath(true);
        for (int i = 0; i < classpathEntries.length; i++) {
            IClasspathEntry entry = classpathEntries[i];
            if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                IPath path = entry.getPath();
                srcFolders.add(path);
            }
        }
    } catch (JavaModelException e) {
        srcFolders = new LinkedList<IPath>();
    }
    return srcFolders;
}

From source file:org.libreoffice.ide.eclipse.java.JavaBuilder.java

License:LGPL

/**
 * {@inheritDoc}//from w  w  w.  ja  v  a 2s  . c  o m
 */
@Override
public String[] getBuildEnv(IUnoidlProject pUnoProject) {

    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(pUnoProject.getName());

    String[] env = new String[2];

    // compute the classpath for the project's OOo instance
    String classpath = "CLASSPATH="; //$NON-NLS-1$
    String sep = System.getProperty("path.separator"); //$NON-NLS-1$

    File javaHomeFile = null;

    // Compute the classpath for the project dependencies
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject != null) {
        try {
            IClasspathEntry[] cpEntry = javaProject.getResolvedClasspath(true);
            for (int i = 0; i < cpEntry.length; i++) {
                IClasspathEntry entry = cpEntry[i];

                // Transform into the correct path for the entry.
                if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                    classpath += entry.getPath().toOSString();
                }
                if (i < cpEntry.length - 1) {
                    classpath += sep;
                }
            }

            IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject);
            javaHomeFile = vmInstall.getInstallLocation();

        } catch (JavaModelException e) {
            PluginLogger.error(Messages.getString("Language.GetClasspathError"), e); //$NON-NLS-1$
        } catch (CoreException e) {
            // TODO log a problem to find the JVM associated to the project
        }
    }

    env[0] = classpath;
    if (javaHomeFile != null) {
        String libs = ""; //$NON-NLS-1$
        String filesep = System.getProperty("file.separator"); //$NON-NLS-1$
        try {
            String arch = System.getProperty("os.arch"); //$NON-NLS-1$
            libs = javaHomeFile.getCanonicalPath() + filesep + "lib" + filesep + arch; //$NON-NLS-1$
        } catch (IOException e) {
        }
        env[1] = "LD_LIBRARY_PATH=" + libs; //$NON-NLS-1$
    }

    return env;
}

From source file:org.libreoffice.ide.eclipse.java.JavaBuilder.java

License:LGPL

/**
 * Get the libraries in the classpath that are located in the project
 * directory or one of its subfolder.//from   w ww . ja v  a  2 s .  c  om
 *
 * @param pJavaPrj the project from which to extract the libraries
 * @return a list of all the File pointing to the libraries.
 */
private ArrayList<IFile> getLibs(IJavaProject pJavaPrj) {
    ArrayList<IFile> libs = new ArrayList<IFile>();
    try {
        IClasspathEntry[] entries = pJavaPrj.getResolvedClasspath(true);
        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                /*
                 * At first, add only the libraries located in the project
                 * or one of its children. All others libraries have to be
                 * managed by the user.
                 */
                IPath path = entry.getPath();
                if (!new File(path.toOSString()).exists() && path.isAbsolute()
                        && path.toString().startsWith("/" + pJavaPrj.getProject().getName())) { //$NON-NLS-1$
                    // Relative to the project
                    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
                    if (file != null && file.exists()) {
                        libs.add(file);
                    }
                }
            }
        }

    } catch (JavaModelException e) {
        // Enable to add some missing library
    }

    return libs;
}