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

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

Introduction

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

Prototype

int CPE_LIBRARY

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

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a library.

Usage

From source file:org.codecover.eclipse.builder.CodeCoverCompilationParticipant.java

License:Open Source License

/**
 * This method is called for each project separately.
 *//*from w w  w.j a  v a  2 s.c  o  m*/
@Override
public void buildStarting(BuildContext[] files, boolean isBatch) {
    if (files.length == 0) {
        return;
    }

    final IProject iProject = files[0].getFile().getProject();
    final IPath projectFullPath = iProject.getFullPath();
    final IPath projectLocation = iProject.getLocation();
    final String projectFolderLocation = iProject.getLocation().toOSString();
    final Queue<SourceTargetContainer> sourceTargetContainers = new LinkedList<SourceTargetContainer>();
    final String instrumentedFolderLocation = CodeCoverPlugin.getDefault()
            .getPathToInstrumentedSources(iProject).toOSString();

    // try to get all source folders
    final Queue<String> possibleSourceFolders = new LinkedList<String>();
    try {
        final IJavaProject javaProject = JavaCore.create(iProject);
        for (IPackageFragmentRoot thisRoot : javaProject.getAllPackageFragmentRoots()) {
            IResource resource = thisRoot.getCorrespondingResource();
            if (resource != null) {
                possibleSourceFolders.add(resource.getLocation().toOSString());
            }
        }
    } catch (JavaModelException e) {
        eclipseLogger.fatal("JavaModelException in CompilationParticipant", e); //$NON-NLS-1$
        return;
    }

    InstrumentableItemsManager instrumentableItemsManager = CodeCoverPlugin.getDefault()
            .getInstrumentableItemsManager();
    TSContainerManager tscManager = CodeCoverPlugin.getDefault().getTSContainerManager();

    // //////////////////////////////////////////////////////////////////////
    // CREATE THE SOURCE TARGET CONTAINERS AND COPY THE UNINSTRUMENTED
    // FILES TO THE INSTRUMENTEDFOLDERLOCATION
    // //////////////////////////////////////////////////////////////////////
    fillSourceTargetContainers(files, possibleSourceFolders, sourceTargetContainers, instrumentedFolderLocation,
            eclipseLogger, instrumentableItemsManager);

    // //////////////////////////////////////////////////////////////////////
    // SEARCH IN ALL TSC OF THIS PROJECT IF A TSC CAN BE REUSED
    // //////////////////////////////////////////////////////////////////////

    TestSessionContainer tsc;
    if (SEARCH_IN_ALL_TSC) {
        tsc = searchUseableTSC(iProject, files, instrumentableItemsManager, tscManager);
    } else {
        tsc = getUseableTSC(files, instrumentableItemsManager, tscManager);
    }

    // //////////////////////////////////////////////////////////////////////
    // PREPARE INSTRUMENTATION
    // //////////////////////////////////////////////////////////////////////
    InstrumenterDescriptor descriptor = new org.codecover.instrumentation.java15.InstrumenterDescriptor();
    if (descriptor == null) {
        eclipseLogger.fatal("NullPointerException in CompilationParticipant"); //$NON-NLS-1$
    }

    // check whether TSC's criteria match
    // with the selected criteria of the project
    if (tsc != null) {
        Set<Criterion> tscCriteria = tsc.getCriteria();

        for (Criterion criterion : tscCriteria) {
            if (!CodeCoverPlugin.getCriterionSelectedState(iProject, criterion)) {
                // the TSC uses a criterion which is not selected for the project
                // therefore it can't be used
                tsc = null;
            }
        }

        // all selected criteria must be active for the TSC
        for (Criterion criterion : descriptor.getSupportedCriteria()) {
            if (CodeCoverPlugin.getCriterionSelectedState(iProject, criterion)) {
                if (!tscCriteria.contains(criterion)) {
                    // the TSC doesn't use a criterion which is selected
                    // for the project, this means we can't use the TSC
                    tsc = null;
                }
            }
        }
    }

    eclipseLogger.debug("can reuse TSC: " + (tsc != null ? tsc : "no")); //$NON-NLS-1$ //$NON-NLS-2$

    InstrumenterFactory factory = new DefaultInstrumenterFactory();
    factory.setDescriptor(descriptor);

    // only instrument with the selected criteria
    for (Criterion criterion : descriptor.getSupportedCriteria()) {
        if (CodeCoverPlugin.getCriterionSelectedState(iProject, criterion)) {
            factory.addCriterion(criterion);
        }
    }

    factory.setCharset(DEFAULT_CHARSET_FOR_COMPILING);

    Instrumenter instrumenter = null;
    try {
        instrumenter = factory.getInstrumenter();
    } catch (FactoryMisconfigurationException e) {
        eclipseLogger.fatal("FactoryMisconfigurationException in CompilationParticipant"); //$NON-NLS-1$
    }

    // //////////////////////////////////////////////////////////////////////
    // INSTRUMENT
    // //////////////////////////////////////////////////////////////////////
    File rootFolder = new File(projectFolderLocation);
    File targetFolder = new File(instrumentedFolderLocation);
    MASTBuilder builder = new MASTBuilder(eclipseLogger);
    Map<String, Object> instrumenterDirectives = descriptor.getDefaultDirectiveValues();
    CodeCoverPlugin plugin = CodeCoverPlugin.getDefault();
    eclipseLogger.debug("Plugin: " + plugin);
    IPath coverageLogPath = CodeCoverPlugin.getDefault().getPathToCoverageLogs(iProject);
    coverageLogPath = coverageLogPath.append("coverage-log-file.clf"); //$NON-NLS-1$

    instrumenterDirectives.put(
            org.codecover.instrumentation.java15.InstrumenterDescriptor.CoverageLogPathDirective.KEY,
            coverageLogPath.toOSString());

    if (tsc != null) {
        // we can reuse the TSC
        instrumenterDirectives.put(org.codecover.instrumentation.UUIDDirective.KEY, tsc.getId());
    }

    TestSessionContainer testSessionContainer;
    try {
        testSessionContainer = instrumenter.instrument(rootFolder, targetFolder, sourceTargetContainers,
                builder, instrumenterDirectives);
    } catch (InstrumentationIOException e) {
        eclipseLogger.fatal("InstrumentationIOException in CompilationParticipant", e); //$NON-NLS-1$
        return;
    } catch (InstrumentationException e) {
        eclipseLogger.fatal("InstrumentationException in CompilationParticipant", e); //$NON-NLS-1$
        return;
    }

    // //////////////////////////////////////////////////////////////////////
    // SAVE TSC
    // //////////////////////////////////////////////////////////////////////
    if (tsc == null) {
        // we have to create a new TSC
        try {
            tscManager.addTestSessionContainer(testSessionContainer, iProject, false, null, null);
        } catch (FileSaveException e) {
            eclipseLogger.fatal("FileSaveException in CompilationParticipant", e); //$NON-NLS-1$
        } catch (TSCFileCreateException e) {
            eclipseLogger.fatal("CoreException in CompilationParticipant", e); //$NON-NLS-1$
        } catch (FileLoadException e) {
            eclipseLogger.fatal("CoreException in CompilationParticipant", e); //$NON-NLS-1$
        } catch (InvocationTargetException e) {
            // can't happen because we didn't pass a runnable
            eclipseLogger.warning("InvocationTargetException in CompilationParticipant", e); //$NON-NLS-1$
        } catch (CancelException e) {
            eclipseLogger.warning("User canceled writing of" + //$NON-NLS-1$
                    " new test session container in" + //$NON-NLS-1$
                    " CompilationParticipant"); //$NON-NLS-1$
        }
    }

    // TODO handle compilation errors
    IJavaProject javaProject = JavaCore.create(iProject);

    // set up classpath
    StringBuilder runCommand = new StringBuilder(1024);
    IClasspathEntry[] cpEntries;
    try {
        cpEntries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException e) {
        eclipseLogger.fatal("JavaModelException in CompilationParticipant", e); //$NON-NLS-1$
        return;
    }

    for (int i = 0; i < cpEntries.length; i++) {
        IClasspathEntry thisEntry = cpEntries[i];
        if (thisEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            if (runCommand.length() == 0) {
                // this is the first entry -> create the class path option
                runCommand.append("-cp "); //$NON-NLS-1$
            } else {
                // this is not the first -> we need a separator
                runCommand.append(File.pathSeparatorChar);
            }
            runCommand.append("\""); //$NON-NLS-1$
            IPath itsIPath = thisEntry.getPath();
            if (projectFullPath.isPrefixOf(itsIPath)) {
                itsIPath = itsIPath.removeFirstSegments(1);
                itsIPath = projectLocation.append(itsIPath);
            }
            runCommand.append(itsIPath.toOSString());
            runCommand.append("\""); //$NON-NLS-1$
        }
    }

    // check java version related options
    String targetVersion = javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true);
    runCommand.append(" -target "); //$NON-NLS-1$
    runCommand.append(targetVersion);
    String sourceVersion = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
    runCommand.append(" -source "); //$NON-NLS-1$
    runCommand.append(sourceVersion);

    // no warnings
    runCommand.append(" -nowarn"); //$NON-NLS-1$

    // use the default charset for the encoding
    // all files have been instrumented or copied using this charset
    runCommand.append(" -encoding "); //$NON-NLS-1$
    runCommand.append(DEFAULT_CHARSET_FOR_COMPILING.toString());

    // the directory to compile
    // put the path in "", because the commandline tokenizes this path
    runCommand.append(" \""); //$NON-NLS-1$
    runCommand.append(instrumentedFolderLocation);
    runCommand.append("\""); //$NON-NLS-1$

    eclipseLogger.debug("I run this compile command now:\n" + runCommand); //$NON-NLS-1$
    StringWriter out = new StringWriter();
    StringWriter err = new StringWriter();
    boolean result;
    result = org.eclipse.jdt.internal.compiler.batch.Main.compile(runCommand.toString(), new PrintWriter(out),
            new PrintWriter(err));

    eclipseLogger.debug("ECJ Output: " + out.toString()); //$NON-NLS-1$
    eclipseLogger.debug("ECJ Error Output: " + err.toString()); //$NON-NLS-1$

    if (!result) {
        eclipseLogger.fatal("An error occured when trying to compile the instrumented sources."); //$NON-NLS-1$
    }

    super.buildStarting(files, isBatch);
}

From source file:org.codehaus.aspectwerkz.ide.eclipse.core.AwCorePlugin.java

License:Open Source License

/**
 * Build the list of URL for the given project
 * Resolve container (ie JRE jars) and dependancies and project output folder
 * /*  w ww  .j  ava  2  s.  c  om*/
 * @param project
 * @return
 */
public List getProjectClassPathURLs(IJavaProject project) {
    List paths = new ArrayList();
    try {
        // configured classpath
        IClasspathEntry classpath[] = project.getResolvedClasspath(false);
        for (int i = 0; i < classpath.length; i++) {
            IClasspathEntry path = classpath[i];
            URL urlEntry = null;

            if (path.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
                Object target = JavaModel.getTarget(workspaceRoot, path.getPath(), false);
                if (target != null) {
                    // inside the workspace
                    if (target instanceof IResource) {
                        urlEntry = ((IResource) target).getLocation().toFile().toURL();
                    } else if (target instanceof File) {
                        urlEntry = ((File) target).toURL();
                    }
                }
            } else if (path.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath outPath = path.getOutputLocation();
                if (outPath != null) {
                    //TODO : don't know if I ll have absolute path here
                    urlEntry = outPath.toFile().toURL();
                }
            }
            if (urlEntry != null) {
                paths.add(urlEntry);
            } else {
                AwLog.logTrace("project loader - ignored " + path.toString());
            }
        }
        // project build output
        IPath location = getProjectLocation(project.getProject());
        IPath outputPath = location.append(project.getOutputLocation().removeFirstSegments(1));
        paths.add(outputPath.toFile().toURL());
    } catch (Exception e) {
        AwLog.logError("Could not build project path", e);
    }
    return paths;
}

From source file:org.codehaus.groovy.eclipse.launchers.AbstractGroovyLaunchShortcut.java

License:Apache License

/**
 * Need to recursively walk the classpath and visit all dependent projects
 * Not looking at classpath containers yet.
 *
 * @param javaProject//from ww w.j a v  a 2  s  . com
 * @param entries
 */
private void addClasspathEntriesForProject(IJavaProject javaProject, SortedSet<String> sourceEntries,
        SortedSet<String> binEntries) {
    List<IJavaProject> dependingProjects = new ArrayList<IJavaProject>();
    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (IClasspathEntry entry : entries) {
            int kind = entry.getEntryKind();
            switch (kind) {
            case IClasspathEntry.CPE_LIBRARY:
                IPath libPath = entry.getPath();
                if (!isPathInWorkspace(libPath)) {
                    sourceEntries.add(libPath.toOSString());
                    break;
                }
                //$FALL-THROUGH$
            case IClasspathEntry.CPE_SOURCE:
                IPath srcPath = entry.getPath();
                String sloc = getProjectLocation(srcPath);
                if (srcPath.segmentCount() > 1) {
                    sloc += File.separator + srcPath.removeFirstSegments(1).toOSString();
                }
                sourceEntries.add(sloc);

                IPath outPath = entry.getOutputLocation();
                if (outPath != null) {
                    String bloc = getProjectLocation(outPath);
                    if (outPath.segmentCount() > 1) {
                        bloc += File.separator + outPath.removeFirstSegments(1).toOSString();
                    }
                    binEntries.add(bloc);
                }
                break;

            case IClasspathEntry.CPE_PROJECT:
                dependingProjects.add(javaProject.getJavaModel().getJavaProject(entry.getPath().lastSegment()));
                break;
            }
        }
        IPath defaultOutPath = javaProject.getOutputLocation();
        if (defaultOutPath != null) {
            String bloc = getProjectLocation(javaProject);
            if (defaultOutPath.segmentCount() > 1) {
                bloc += File.separator + defaultOutPath.removeFirstSegments(1).toOSString();
            }
            binEntries.add(bloc);
        }
    } catch (JavaModelException e) {
        GroovyCore.logException("Exception generating classpath for launching groovy script", e);
    }
    // recur through dependent projects
    for (IJavaProject dependingProject : dependingProjects) {
        if (dependingProject.getProject().isAccessible()) {
            addClasspathEntriesForProject(dependingProject, sourceEntries, binEntries);
        }
    }
}

From source file:org.compiere.mfg_scm.eclipse.db.DbfBootstrap.java

License:Apache License

private void getClassPathEntries(IJavaProject prj, ArrayList data, List selectedPaths,
        ArrayList visitedProjects) {
    IClasspathEntry[] entries = null;//from  ww  w . ja  v a 2s .  co  m

    IPath outputPath = null;
    try {
        outputPath = prj.getOutputLocation();
        if (selectedPaths.contains(outputPath.toFile().toString().replace('\\', '/'))) {
            add(data, prj.getProject().getWorkspace().getRoot().findMember(outputPath));
        }
        entries = prj.getRawClasspath();
    } catch (JavaModelException e) {
        DbfLauncherPlugin.log(e);
    }
    if (entries == null)
        return;
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        IPath path = entry.getPath();
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            path = entry.getOutputLocation();
            if (path == null)
                continue;
        }
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            String prjName = entry.getPath().lastSegment();
            if (!visitedProjects.contains(prjName)) {
                visitedProjects.add(prjName);
                getClassPathEntries(prj.getJavaModel().getJavaProject(prjName), data, selectedPaths,
                        visitedProjects);
            }
            continue;
        } else if (!selectedPaths.contains(path.toFile().toString().replace('\\', '/')))
            continue;

        IClasspathEntry[] tmpEntry = null;
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            try {
                tmpEntry = JavaCore.getClasspathContainer(path, prj).getClasspathEntries();
            } catch (JavaModelException e1) {
                DbfLauncherPlugin.log(e1);
                continue;
            }
        } else {
            tmpEntry = new IClasspathEntry[1];
            tmpEntry[0] = JavaCore.getResolvedClasspathEntry(entry);
        }

        for (int j = 0; j < tmpEntry.length; j++) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IResource res = prj.getProject().getWorkspace().getRoot().findMember(tmpEntry[j].getPath());
                if (res != null)
                    add(data, res);
                else
                    add(data, tmpEntry[j].getPath());
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath srcPath = entry.getOutputLocation();
                if (srcPath != null && !srcPath.equals(outputPath)) {
                    add(data, prj.getProject().getWorkspace().getRoot().findMember(srcPath));
                }
            } else {
                add(data, tmpEntry[j].getPath());
            }
        }
    }
}

From source file:org.continuousassurance.swamp.eclipse.ImprovedClasspathHandler.java

License:Apache License

/**
 * Constructor for ImprovedClasspathHandler
 * @param project the Java project that we will generate a build file for
 * @param root the ImprovedClasspathHandler object for the project (this is the root of the recursive tree that we build from projects having dependencies)
 * @param exclSysLibs if true, Java system libraries get copied into the package at submission
 * @param subMonitor submonitor for tracking progress
 *//*  ww  w  .j  av  a2 s.  c  o  m*/
public ImprovedClasspathHandler(IJavaProject project, ImprovedClasspathHandler root, boolean exclSysLibs,
        SubMonitor subMonitor) {
    this.excludeSysLibs = exclSysLibs;
    sources = new ArrayList<IClasspathEntry>();
    libs = new ArrayList<IClasspathEntry>();
    systemLibs = new ArrayList<IClasspathEntry>();
    dependentProjects = new ArrayList<ImprovedClasspathHandler>();
    exportedEntries = new ArrayList<IClasspathEntry>();

    this.project = project;
    this.srcVersion = this.project.getOption(SOURCE_VERSION_OPTION, true);
    this.targetVersion = this.project.getOption(TARGET_VERSION_OPTION, true);

    if (root == null) {
        this.root = this;
        this.subMonitor = subMonitor;
        this.subMonitor.setWorkRemaining(100);
        visitedProjects = new HashMap<String, ImprovedClasspathHandler>();
        SWAMPBIN_PATH = setupBinDir(project.getProject());
        filesToArchive = new HashSet<String>();
    } else {
        this.root = root;
        visitedProjects = root.visitedProjects;
        SWAMPBIN_PATH = root.SWAMPBIN_PATH;
        filesToArchive = root.filesToArchive;
    }

    try {
        project.getProject().build(IncrementalProjectBuilder.CLEAN_BUILD, null);
    } catch (CoreException e1) {
        // TODO Auto-generated catch block
        System.err.println("Unable to do a clean build on the project for some reason");
        e1.printStackTrace();
    }

    IClasspathEntry[] entries = null;
    try {
        entries = project.getRawClasspath();
        if (entries == null || entries.length == 0) {
            return;
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return;
    }
    IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
    try {
        for (IClasspathEntry entry : entries) {
            int kind = entry.getEntryKind();
            if (this.subMonitor != null) {
                if (this.subMonitor.isCanceled()) {
                    System.out.println("Sub monitor got cancelled!");
                }
                this.subMonitor.split(100 / SwampSubmitter.CLASSPATH_ENTRY_TICKS);
            }
            if (kind == IClasspathEntry.CPE_SOURCE) {
                handleSource(entry, wsRoot);
            } else if (kind == IClasspathEntry.CPE_LIBRARY) {
                handleLibrary(entry, wsRoot);
            } else if (kind == IClasspathEntry.CPE_PROJECT) {
                handleProject(entry, wsRoot);
            } else if (kind == IClasspathEntry.CPE_VARIABLE) {
                handleVariable(entry, wsRoot);
            } else { // kind == IClasspathEntry.CPE_CONTAINER
                handleContainer(entry, wsRoot);
            }
        }
    } catch (IOException | JavaModelException e) {
        // TODO Report this error! This is very bad
        e.printStackTrace();
    }
    if (hasSwampbinDependencies) {
        filesToArchive.add(SWAMPBIN_PATH.toOSString());
    }
}

From source file:org.continuousassurance.swamp.eclipse.ImprovedClasspathHandler.java

License:Apache License

/**
 * Handles a variable entry in the classpath. A variable entry can be resolved to either a library entry or a project entry
 * @param entry the variable entry/* w ww . java2s .co  m*/
 * @param root the workspace entry
 * @throws IOException
 */
private void handleVariable(IClasspathEntry entry, IWorkspaceRoot root) throws IOException {
    IClasspathEntry resolvedEntry = JavaCore.getResolvedClasspathEntry(entry);
    if (resolvedEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
        handleLibrary(resolvedEntry, root);
    } else {
        handleProject(resolvedEntry, root);
    }
}

From source file:org.continuousassurance.swamp.eclipse.ImprovedClasspathHandler.java

License:Apache License

/**
 * Handles a container entry in the classpath. A container entry contains 1+ entries each of which is either a library entry or a project entry
 * @param entry the container entry/*from w w w.  j  av  a2  s .c o m*/
 * @param root the workspace root
 * @throws IOException
 * @throws JavaModelException
 */
public void handleContainer(IClasspathEntry entry, IWorkspaceRoot root) throws IOException, JavaModelException {
    IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project);
    System.out.println("Here's a container" + container);
    int kind = container.getKind();
    if ((this.excludeSysLibs)
            && (kind == IClasspathContainer.K_APPLICATION || kind == IClasspathContainer.K_DEFAULT_SYSTEM)) {
        System.out.println("System library container");
        System.out.println(entry.getPath());
        return;
    }
    for (IClasspathEntry subEntry : container.getClasspathEntries()) {
        if (subEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            handleLibrary(subEntry, root);
        } else {
            handleProject(subEntry, root);
        }
    }
}

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

License:Apache License

public static List<URL> getProjectClassPathURLs(IJavaProject project, List<String> alreadyLoadedProjects) {
    List<URL> pathElements = new ArrayList<URL>();
    try {//w  w w  .j av a  2s.  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) {
        DroolsEclipsePlugin.log(e);
    } catch (MalformedURLException e) {
        DroolsEclipsePlugin.log(e);
    } catch (Throwable t) {
        DroolsEclipsePlugin.log(t);
    }
    return pathElements;
}

From source file:org.ebayopensource.turmeric.eclipse.utils.plugin.JDTUtil.java

License:Open Source License

private static void resolveClasspathToURLs(final IJavaProject javaProject, final Set<URL> resolvedEntries,
        final Set<String> visited) throws JavaModelException, IOException {
    if (javaProject == null || !javaProject.exists())
        return;//from w  ww . jav  a 2 s  .co  m
    final String projectName = javaProject.getProject().getName();
    if (visited.contains(projectName))
        return;
    visited.add(projectName);
    for (final IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            resolveClasspathToURLs(JavaCore.create(WorkspaceUtil.getProject(entry.getPath())), resolvedEntries,
                    visited);
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            resolvedEntries.add(WorkspaceUtil.getLocation(entry.getPath()).toFile().toURI().toURL());

        } else if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath location = entry.getOutputLocation() != null ? entry.getOutputLocation()
                    : javaProject.getOutputLocation();
            if (location.toString().startsWith(WorkspaceUtil.PATH_SEPERATOR + projectName)) {
                //it happens that the path is not absolute
                location = javaProject.getProject().getFolder(location.removeFirstSegments(1)).getLocation();
            }

            resolvedEntries.add(location.toFile().toURI().toURL());
        }
    }
}

From source file:org.ebayopensource.vjet.eclipse.core.ClassPathUtils.java

License:Open Source License

public static URL[] getProjectDependencyUrls(List<IJavaProject> javaProjectList, List<URL> currentUrlList,
        HashMap<String, String> projectMap) throws JavaModelException, MalformedURLException {

    List<URL> projectDependencyUrlList = currentUrlList;

    if (projectDependencyUrlList == null) {
        projectDependencyUrlList = new ArrayList<URL>();
    }//from w  w w. ja va2s.c  o m

    for (IJavaProject project : javaProjectList) {

        if (projectMap.containsKey(project.getElementName()) == true) {
            continue;
        } else {
            projectMap.put(project.getElementName(), project.getElementName());
        }

        // Add the dependencies to the URL list
        IClasspathEntry[] entries;
        entries = project.getResolvedClasspath(true);

        for (IClasspathEntry entry : entries) {

            IPath path = entry.getPath();
            File f = path.toFile();
            URL entryUrl;
            entryUrl = f.toURI().toURL();
            switch (entry.getEntryKind()) {

            case IClasspathEntry.CPE_LIBRARY:
                if (projectDependencyUrlList.contains(entryUrl) == false) {
                    projectDependencyUrlList.add(entryUrl);
                }
                break;

            case IClasspathEntry.CPE_PROJECT:
                List<IJavaProject> subjavaProjectList = new ArrayList<IJavaProject>();
                IResource subResource = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
                if (subResource == null) {
                    // String projectName = entry.getPath().toString();
                    // String parentProjectName = project.getElementName();
                    // throw new EclipseProjectNotFoundException(
                    // projectName,
                    // MessageFormat
                    // .format(
                    // "The dependent project {0} of project {1} is not
                    // available.\nPlease update your workspace to include
                    // this project",
                    // projectName, parentProjectName));
                }
                if (subResource != null && subResource.getType() == IResource.PROJECT) {
                    IProject subProject = (IProject) subResource;
                    IJavaProject subJavaProject = JavaCore.create(subProject);
                    if (subJavaProject != null && subJavaProject.exists()) {
                        subjavaProjectList.add(subJavaProject);

                        // Recursively call our selves to populate the
                        // project
                        // dependency's for the sub projects.
                        getProjectDependencyUrls(subjavaProjectList, projectDependencyUrlList, projectMap);
                    }

                }
                break;

            default:
                break;
            }
        }

        IPath path = project.getOutputLocation();
        IPath projectResourceLocation = project.getResource().getLocation();
        File projectFilePath = projectResourceLocation.append(path.removeFirstSegments(1)).toFile();
        URL projectOutputUrl;
        projectOutputUrl = projectFilePath.toURI().toURL();

        if (projectDependencyUrlList.contains(projectOutputUrl) == false) {
            projectDependencyUrlList.add(projectOutputUrl);
        }
    }

    URL[] arrayList = new URL[projectDependencyUrlList.size()];
    URL[] returnURLArray = projectDependencyUrlList.toArray(arrayList);

    return returnURLArray;

}