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:org.cloudfoundry.ide.eclipse.server.core.internal.debug.DebugProvider.java

License:Open Source License

protected boolean containsDebugFiles(IJavaProject project) {
    try {/*from www. j a  v  a 2s.co m*/

        IClasspathEntry[] entries = project.getResolvedClasspath(true);

        if (entries != null) {
            for (IClasspathEntry entry : entries) {
                if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath projectPath = project.getPath();
                    IPath relativePath = entry.getPath().makeRelativeTo(projectPath);
                    IFolder folder = project.getProject().getFolder(relativePath);
                    if (getFile(folder, ".profile.d", "ngrok.sh") != null) {//$NON-NLS-1$ //$NON-NLS-2$
                        return true;
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        CloudFoundryPlugin.logError(e);
    } catch (CoreException ce) {
        CloudFoundryPlugin.logError(ce);
    }
    return false;
}

From source file:org.cloudfoundry.ide.eclipse.server.standalone.internal.application.JavaPackageFragmentRootHandler.java

License:Open Source License

/**
 * /*from   w  w  w  . j a va  2  s . c  o m*/
 * Determines if the given package fragment root corresponds to the class
 * path entry path.
 * <p/>
 * Note that different package fragment roots may point to the same class
 * path entry.
 * <p/>
 * Example:
 * <p/>
 * A Java project may have the following package fragment roots:
 * <p/>
 * - src/main/java
 * <p/>
 * - src/main/resources
 * <p/>
 * Both may be using the same output folder:
 * <p/>
 * target/classes.
 * <p/>
 * In this case, the output folder will have a class path entry -
 * target/classes - and it will be the same for both roots, and this method
 * will return true for both roots if passed the entry for target/classes
 * 
 * @param root
 *            to check if it corresponds to the given class path entry path
 * @param entry
 * @return true if root is at the given entry
 */
private static boolean isRootAtEntry(IPackageFragmentRoot root, IPath entry) {
    try {
        IClasspathEntry cpe = root.getRawClasspathEntry();
        if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath outputLocation = cpe.getOutputLocation();
            if (outputLocation == null) {
                outputLocation = root.getJavaProject().getOutputLocation();
            }

            IPath location = ResourcesPlugin.getWorkspace().getRoot().findMember(outputLocation).getLocation();
            if (entry.equals(location)) {
                return true;
            }
        }
    } catch (JavaModelException e) {
        CloudFoundryPlugin.logError(e);
    }

    IResource resource = root.getResource();
    if (resource != null && entry.equals(resource.getLocation())) {
        return true;
    }

    IPath path = root.getPath();
    if (path != null && entry.equals(path)) {
        return true;
    }

    return false;
}

From source file:org.cloudfoundry.ide.eclipse.server.standalone.internal.application.StandaloneRuntimeResolver.java

License:Open Source License

/**
 * Returns either test sources, or non-test sources, based on a flag
 * setting. If nothing is found, returns empty list.
 *///from  w w  w . j av a 2s  .  c  o m
protected Collection<IClasspathEntry> getSourceEntries(boolean istest) {
    try {
        IClasspathEntry[] rawEntries = javaProject.getRawClasspath();
        if (rawEntries != null) {
            Collection<IClasspathEntry> sourceEntries = new HashSet<IClasspathEntry>();
            for (IClasspathEntry entry : rawEntries) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath path = entry.getPath();
                    if (path != null) {
                        boolean isTestSource = isTestSource(path.toOSString());
                        if ((istest && isTestSource) || (!istest && !isTestSource)) {
                            sourceEntries.add(entry);
                        }
                    }
                }
            }
            return sourceEntries;
        }
    } catch (JavaModelException e) {
        CloudFoundryPlugin.log(e);
    }
    return Collections.emptyList();
}

From source file:org.cloudfoundry.ide.eclipse.server.standalone.internal.startcommand.JavaTypeUIAdapter.java

License:Open Source License

public IPackageFragment getDefaultPackageFragment(IJavaProject javaProject) {

    if (javaProject == null) {
        return null;
    }/* w w  w.j  a v  a 2 s. c  o  m*/

    List<IPackageFragmentRoot> packFragRoots = new ArrayList<IPackageFragmentRoot>();
    try {

        IClasspathEntry[] entries = javaProject.getRawClasspath();

        for (IClasspathEntry entry : entries) {

            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
                if (roots != null) {
                    for (IPackageFragmentRoot rt : roots) {
                        if (!packFragRoots.contains(rt)) {
                            packFragRoots.add(rt);
                        }
                    }
                }
            }
        }

    } catch (JavaModelException e) {
        CloudFoundryPlugin.log(e);
    }

    IPackageFragment fragment = null;
    for (IPackageFragmentRoot root : packFragRoots) {
        try {
            IJavaElement[] members = root.getChildren();
            if (members != null) {
                for (IJavaElement element : members) {
                    if (element instanceof IPackageFragment) {
                        IPackageFragment frag = (IPackageFragment) element;
                        if (frag.isDefaultPackage()) {
                            fragment = frag;
                            break;
                        }
                    }
                }
            }
            if (fragment != null) {
                break;
            }
        } catch (JavaModelException e) {
            CloudFoundryPlugin.log(e);
        }
    }
    return fragment;
}

From source file:org.cloudfoundry.ide.eclipse.server.ui.internal.CloudRebelUIHandler.java

License:Open Source License

protected List<String> getClasspathSourceOutputPaths(IProject project) {

    IJavaProject javaProject = CloudFoundryProjectUtil.getJavaProject(project);
    List<String> outputPaths = new ArrayList<String>();
    if (javaProject != null) {
        try {//  w  w w. j a  va 2 s .co  m
            IClasspathEntry[] classpath = javaProject.getResolvedClasspath(true);

            if (classpath != null) {
                for (IClasspathEntry entry : classpath) {
                    if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                        String outputPath = entry.getOutputLocation() != null
                                ? entry.getOutputLocation().toString()
                                : null;
                        if (outputPath != null && !outputPaths.contains(outputPath)
                                && !outputPath.contains("target/test-classes")) {//$NON-NLS-1$
                            outputPaths.add(outputPath);
                        }
                    }
                }
            }

            String outputPath = javaProject.getOutputLocation() != null
                    ? javaProject.getOutputLocation().toString()
                    : null;
            if (outputPath != null && !outputPaths.contains(outputPath)) {
                outputPaths.add(outputPath);
            }
        } catch (JavaModelException e) {
            CloudFoundryPlugin.logError(e);
        }
    }

    return outputPaths;
}

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

License:Open Source License

/**
 * This method is called for each project separately.
 */// ww 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
 * /*  ww w . ja v a2s. c  o m*/
 * @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.core.model.GroovyRuntime.java

License:Apache License

public static boolean hasClasspathContainer(final IJavaProject javaProject, final IPath libraryPath)
        throws CoreException {
    if (javaProject == null || !javaProject.getProject().isAccessible())
        return false;
    final IClasspathEntry[] entries = javaProject.getRawClasspath();
    for (int i = 0; i < entries.length; i++) {
        final IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            if (ObjectUtils.equals(entry.getPath(), libraryPath) || libraryPath.isPrefixOf(entry.getPath())) {
                return true;
            }/*from   w w w  .  ja  va  2 s  . c om*/
        }
    }
    return false;
}

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.ja  v  a2 s.c om
 * @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.codehaus.jdt.groovy.internal.compiler.ScriptFolderCompilationParticipant.java

License:Open Source License

private Map<IContainer, IContainer> generateSourceToOut(IJavaProject project) throws JavaModelException {
    IProject p = project.getProject();/*from w  w  w  .  j  a  v a2s. c  o  m*/
    IWorkspaceRoot root = (IWorkspaceRoot) p.getParent();
    IClasspathEntry[] cp = project.getRawClasspath();

    // determine default out folder
    IPath defaultOutPath = project.getOutputLocation();
    IContainer defaultOutContainer;
    if (defaultOutPath.segmentCount() > 1) {
        defaultOutContainer = root.getFolder(defaultOutPath);
    } else {
        defaultOutContainer = p;
    }

    Map<IContainer, IContainer> sourceToOut = new TreeMap<IContainer, IContainer>(comparator);
    for (IClasspathEntry cpe : cp) {
        if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {

            // determine source folder
            IContainer sourceContainer;
            IPath sourcePath = cpe.getPath();
            if (sourcePath.segmentCount() > 1) {
                sourceContainer = root.getFolder(sourcePath);
            } else {
                sourceContainer = p;
            }

            // determine out folder
            IPath outPath = cpe.getOutputLocation();
            IContainer outContainer;
            if (outPath == null) {
                outContainer = defaultOutContainer;
            } else if (outPath.segmentCount() > 1) {
                outContainer = root.getFolder(outPath);
            } else {
                outContainer = p;
            }

            // if the two containers are equal, that means no copying should be done
            // do not add to map
            if (!sourceContainer.equals(outContainer)) {
                sourceToOut.put(sourceContainer, outContainer);
            }
        }

    }
    return sourceToOut;
}