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

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

Introduction

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

Prototype

int CPE_SOURCE

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

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a folder containing package fragments with source code to be compiled.

Usage

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

License:Open Source License

private static void appendBundleToClasspath(BundleDescription bd, List<String> pdeClassPath,
        IPath defaultOutputLocation) {/*ww w .jav a2  s .c o 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);
        }
    }
}

From source file:distributed.plugin.ui.actions.ProcessActions.java

License:Open Source License

private ClassLoader getClassLoader() {
    ClassLoader loader = null;/*from  w ww  . j a v a 2  s.c o  m*/
    IJavaProject javaProject = this.getClientProject();
    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        List<URL> urls = new ArrayList<URL>(entries.length);
        for (int i = 0; i < entries.length; i++) {
            IPath classpathEntryPath = entries[i].getPath();
            File classpathEntryFile = null;
            switch (entries[i].getEntryKind()) {
            case IClasspathEntry.CPE_SOURCE:
                IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                IPath out = root.getProject(classpathEntryPath.lastSegment()).getLocation();
                if (out != null)
                    classpathEntryFile = out.toFile();
                else
                    classpathEntryFile = root.getFolder(javaProject.getOutputLocation()).getLocation().toFile();

                try {
                    URI uri = classpathEntryFile.toURI();
                    urls.add(uri.toURL());
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                break;
            case IClasspathEntry.CPE_CONTAINER:
                break;

            // FIXME Must handle 2 more cases to handle the location of 
            // client source code
            }
        }
        // set default output (replay) file location w.r.t user bin location
        this.engine.setOutputLocation((URL) urls.get(0));

        // create class loader
        loader = new URLClassLoader((URL[]) urls.toArray(new URL[urls.size()]),
                ProcessActions.class.getClassLoader());

    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    return loader;
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockOpenEditorBubbleActionDelegate.java

License:Open Source License

@Override
public void run(IAction action) {
    IWorkbenchPage page = our_window.getActivePage();

    if (page != null) {
        if (!(page.getActiveEditor() instanceof ITextEditor))
            return;

        ITextEditor fileEditor = (ITextEditor) page.getActiveEditor();

        IFileEditorInput fileEditorInput = (IFileEditorInput) fileEditor.getEditorInput();
        String path = fileEditorInput.getFile().getProjectRelativePath().toOSString();
        String filePath = path;//  www . j  a  v  a 2s.  co  m
        IProject project = fileEditorInput.getFile().getProject();

        IJavaProject javaProject = JavaModelManager.getJavaModelManager().getJavaModel()
                .getJavaProject(project.getName());

        try {
            for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    String sourcePath = entry.getPath().toOSString().substring(project.getName().length() + 2);

                    if (path.startsWith(sourcePath)) {
                        path = path.substring(sourcePath.length() + 1);
                        path = path.replace(File.separatorChar, '$');
                        path = path.substring(0, path.indexOf("."));

                        filePath = filePath.substring(sourcePath.length() + 1);

                        break;
                    }
                }
            }
        } catch (Exception e1) {
            BedrockPlugin.log("Exception : " + e1.getMessage() + ", " + e1.getClass().toString());
        }

        try {
            IJavaElement javaElement = javaProject.findElement(new Path(filePath));

            if (!(javaElement instanceof ICompilationUnit))
                return;

            ICompilationUnit icu = (ICompilationUnit) javaElement;

            ISelectionProvider selectionProvider = fileEditor.getSelectionProvider();
            ISelection selection = selectionProvider.getSelection();

            if (selection instanceof ITextSelection) {
                ITextSelection textSelection = (ITextSelection) selection;
                int offset = textSelection.getOffset();

                IJavaElement element = icu.getElementAt(offset);

                IvyXmlWriter xw = BedrockPlugin.getPlugin().beginMessage("OPENEDITOR");
                xw.field("PROJECT", project.getName());

                if (element == null) {
                    xw.field("RESOURCEPATH", path);
                } else {
                    boolean isFirstElement = true;
                    boolean isMethod = false;

                    String fileName = path.substring(path.lastIndexOf('$') + 1);

                    List<String> list = new ArrayList<String>();

                    while (element != null && (!element.getElementName().equals(fileName)
                            || element.getElementType() == IJavaElement.METHOD)) {
                        if (isFirstElement && (element.getElementType() == IJavaElement.METHOD
                                || element.getElementType() == IJavaElement.TYPE)) {
                            list.add(element.getElementName());

                            if (element.getElementType() == IJavaElement.METHOD) {
                                isMethod = true;
                            }

                            isFirstElement = false;
                        } else if (!isFirstElement) {
                            list.add(element.getElementName());
                        }

                        element = element.getParent();

                        if ("".equals(element.getElementName())) {
                            xw.field("RESOURCEPATH", path);
                            BedrockPlugin.getPlugin().finishMessage(xw);

                            return;
                        }
                    }

                    String[] aryPath = new String[list.size()];
                    list.toArray(aryPath);

                    for (int i = aryPath.length - 1; i >= 0; i--) {
                        path += ("$" + aryPath[i]);
                    }

                    xw.field("RESOURCEPATH", path);

                    if (isMethod)
                        xw.field("RESOURCETYPE", "Function");
                }

                BedrockPlugin.getPlugin().finishMessage(xw);
            }
        } catch (Exception e2) {
            BedrockPlugin.log("Exception : " + e2.getMessage() + ", " + e2.getClass().toString());
        }
    }
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockProject.java

License:Open Source License

ICompilationUnit getCompilationUnit(String proj, String file) throws BedrockException {
    //TODO: This should find the current working copy, not the underlying unit

    IProject ip = findProjectForFile(proj, file);
    if (ip == null)
        return null;

    IJavaProject ijp = JavaCore.create(ip);
    ICompilationUnit icu = checkFilePrefix(ijp, null, file);
    if (icu != null)
        return icu;
    if (ijp == null)
        return null;

    try {//from www . j ava  2s  .c  o m
        IClasspathEntry[] ents = ijp.getResolvedClasspath(true);
        for (int i = 0; i < ents.length; ++i) {
            if (ents[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath p = ents[i].getPath();
                File f = BedrockUtil.getFileForPath(p, ip);
                if (!f.exists())
                    continue;
                icu = checkFilePrefix(ijp, f.getAbsolutePath(), file);
                if (icu != null)
                    return icu;
            }
        }
    } catch (JavaModelException e) {
        BedrockPlugin.logE("Problem getting compilation unit: " + e);
    }

    return null;
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockProject.java

License:Open Source License

private void addPath(IvyXmlWriter xw, IJavaProject jp, IClasspathEntry ent, boolean nest) {
    IPath p = ent.getPath();/* w  w w . ja va2  s .c  o  m*/
    IPath op = ent.getOutputLocation();
    IPath sp = ent.getSourceAttachmentPath();
    IProject ip = jp.getProject();

    String jdp = null;
    boolean opt = false;
    IClasspathAttribute[] atts = ent.getExtraAttributes();
    for (IClasspathAttribute att : atts) {
        if (att.getName().equals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME))
            jdp = att.getValue();
        else if (att.getName().equals(IClasspathAttribute.OPTIONAL)) {
            String v = att.getValue();
            if (v.equals("true"))
                opt = true;
        }
    }

    if (p == null && op == null)
        return;
    File f1 = null;
    if (p != null) {
        f1 = BedrockUtil.getFileForPath(p, ip);
        if (!f1.exists()) {
            BedrockPlugin.logD("Path file " + p + " not found as " + f1);
            // f1 = null;
        }
    }
    File f2 = null;
    if (op != null) {
        f2 = BedrockUtil.getFileForPath(op, ip);
        if (!f2.exists()) {
            BedrockPlugin.logD("Path file " + op + " not found");
            f2 = null;
        }
    }
    File f3 = null;
    if (sp != null) {
        f3 = BedrockUtil.getFileForPath(sp, ip);
        if (!f3.exists()) {
            BedrockPlugin.logD("Path file " + sp + " not found");
            f3 = null;
        }
    }
    if (f1 == null && f2 == null)
        return;

    // references to nested projects are handled in addClassPaths
    if (ent.getEntryKind() == IClasspathEntry.CPE_PROJECT)
        return;

    xw.begin("PATH");
    xw.field("ID", ent.hashCode());
    if (nest)
        xw.field("NESTED", "TRUE");

    switch (ent.getEntryKind()) {
    case IClasspathEntry.CPE_SOURCE:
        xw.field("TYPE", "SOURCE");
        f3 = f1;
        f1 = null;
        break;
    case IClasspathEntry.CPE_PROJECT:
        xw.field("TYPE", "BINARY");
        break;
    case IClasspathEntry.CPE_LIBRARY:
        xw.field("TYPE", "LIBRARY");
        break;
    }
    if (ent.isExported())
        xw.field("EXPORTED", true);
    if (opt)
        xw.field("OPTIONAL", true);

    if (f1 != null)
        xw.textElement("BINARY", f1.getAbsolutePath());
    if (f2 != null)
        xw.textElement("OUTPUT", f2.getAbsolutePath());
    if (f3 != null)
        xw.textElement("SOURCE", f3.getAbsolutePath());
    if (jdp != null)
        xw.textElement("JAVADOC", jdp);

    IAccessRule[] rls = ent.getAccessRules();
    for (IAccessRule ar : rls) {
        xw.begin("ACCESS");
        xw.field("KIND", ar.getKind());
        xw.field("PATTERN", ar.getPattern().toString());
        xw.field("IGNOREIFBETTER", ar.ignoreIfBetter());
        xw.end("ACCESS");
    }

    xw.end("PATH");
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockProject.java

License:Open Source License

private void addClasses(IJavaProject jp, IvyXmlWriter xw) {
    try {/*from   w w w.  j a v a  2s  .  com*/
        IClasspathEntry[] ents = jp.getResolvedClasspath(true);
        for (int k = 0; k < ents.length; ++k) {
            if (ents[k].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPackageFragmentRoot[] rts = jp.findPackageFragmentRoots(ents[k]);
                for (int l = 0; l < rts.length; ++l) {
                    IJavaElement[] elts = rts[l].getChildren();
                    for (int m = 0; m < elts.length; ++m) {
                        if (elts[m] instanceof IPackageFragment) {
                            IPackageFragment frag = (IPackageFragment) elts[m];
                            xw.textElement("PACKAGE", frag.getElementName());
                            for (ICompilationUnit icu : frag.getCompilationUnits()) {
                                for (IType typ : icu.getTypes()) {
                                    outputType(typ, jp, xw);
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        BedrockPlugin.logE("Problem getting class list: " + e);
    }
}

From source file:edu.rice.cs.drjava.plugins.eclipse.repl.EclipseInteractionsModel.java

License:BSD License

private void _addProjectToClasspath(IJavaProject jProj, IJavaModel jModel, IWorkspaceRoot root)
        throws CoreException {
    // Get the project's location on disk
    IProject proj = jProj.getProject();// www.j a  v a2s  .  c om
    URI projRoot = proj.getDescription().getLocationURI();
    // Note: getLocation returns null if the default location is used
    //  (brilliant...)

    // Get the resolved classpath entries - this should filter out
    //   all CPE_VARIABLE and CPE_CONTAINER entries.
    IClasspathEntry entries[] = jProj.getResolvedClasspath(true);

    // For each of the classpath entries...
    for (int j = 0; j < entries.length; j++) {
        IClasspathEntry entry = entries[j];

        // Check what kind of entry it is...
        int kind = entry.getEntryKind();

        // And get the appropriate path.
        IPath path;
        switch (kind) {
        case IClasspathEntry.CPE_LIBRARY:
            // The raw location of a JAR.
            path = entry.getPath();
            //System.out.println("Adding library: " + path.toOSString());
            addToClassPath(path.toOSString());
            break;
        case IClasspathEntry.CPE_SOURCE:
            // The output location of source.
            // Need to append it to the user's workspace directory.
            path = entry.getOutputLocation();
            if (path == null) {
                path = jProj.getOutputLocation();
                //System.out.println(" output location from proj: " + path);
            }

            // At this point, the output location contains the project
            //  name followed by the actual output folder name

            if (projRoot != null && (!projRoot.isAbsolute() || projRoot.getScheme().equals("file"))) {
                // We have a custom project location, so the project name
                //  is not part of the *actual* output directory.  We need
                //  to remove the project name (first segment) and then
                //  append the rest of the output location to projRoot.
                path = path.removeFirstSegments(1);
                path = new Path(projRoot.getPath()).append(path);
            } else {
                // A null projRoot means use the default location, which
                //  *does* include the project name in the output directory.
                path = root.getLocation().append(path);
            }

            //System.out.println("Adding source: " + path.toOSString());
            //addToClassPath(path.toOSString());
            addBuildDirectoryClassPath(path.toOSString());
            break;
        case IClasspathEntry.CPE_PROJECT:
            // In this case, just the project name is given.
            // We don't actually need to add anything to the classpath,
            //  since the project is open and we will get its classpath
            //  on another pass.
            break;
        default:
            // This should never happen.
            throw new RuntimeException("Unsupported classpath entry type.");
        }
    }
}

From source file:edu.uci.lighthouse.core.util.WorkbenchUtility.java

License:Open Source License

public static String[] getSourceFolders(IJavaProject project) {
    List<String> result = new LinkedList<String>();
    try {//from   w  ww  . j  ava2 s. com
        IClasspathEntry[] classPaths = project.getRawClasspath();
        for (IClasspathEntry cp : classPaths) {
            if (cp.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                result.add(cp.getPath().toPortableString());
            }
        }
    } catch (JavaModelException e) {
        logger.error(e, e);
    }
    return result.toArray(new String[0]);
}

From source file:edu.uci.lighthouse.services.persistence.ThreadTest.java

License:Open Source License

public static String[] getSourceFolders(IJavaProject project) {
    List<String> result = new LinkedList<String>();
    try {/*from   ww  w . ja  v  a2s  .  c o m*/
        IClasspathEntry[] classPaths = project.getRawClasspath();
        for (IClasspathEntry cp : classPaths) {
            if (cp.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                result.add(cp.getPath().toString());
            }
        }
    } catch (JavaModelException e) {
        //logger.error(e,e);
    }
    return result.toArray(new String[0]);
}

From source file:es.bsc.servicess.ide.PackagingUtils.java

License:Apache License

/** Get the classpath of the project
 * @param project//w w  w .j a v  a2  s . c o  m
 * @return
 * @throws JavaModelException
 */
public static String getClasspath(IJavaProject project) throws JavaModelException {
    String classpath = new String();
    IPath path = project.getProject().getWorkspace().getRoot().getLocation();
    boolean first = true;
    for (IClasspathEntry e : project.getResolvedClasspath(true)) {
        if (!first) {
            classpath = classpath.concat(":");

        } else {
            first = false;
        }
        if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath entryPath = e.getPath();
            path.isPrefixOf(entryPath);
            classpath = classpath.concat(path.append(entryPath.makeRelative()).toOSString());
        } else {
            classpath = classpath.concat(e.getPath().toOSString());
        }
    }
    return classpath;
}