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

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

Introduction

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

Prototype

IClasspathEntry[] getRawClasspath() throws JavaModelException;

Source Link

Document

Returns the raw classpath for the project, as a list of classpath entries.

Usage

From source file:de.ovgu.featureide.core.framework.FrameworkProjectCreator.java

License:Open Source License

/**
 * Creates a new subproject inside a folder
 * //from   www.j  ava 2s  . c o  m
 * @param name - project name
 * @param destination - folder which contains the subproject
 * @throws CoreException
 */
public static void createSubprojectFolder(String name, IFolder destination) throws CoreException {
    final IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(name);
    description.setLocation(destination.getLocation());

    final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
    if (project.exists()) {
        return;
    }
    project.create(description, null);
    project.open(null);

    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);

    final IJavaProject javaProject = JavaCore.create(project);
    javaProject.open(null);

    final IFolder binFolder = project.getFolder("bin");
    binFolder.create(true, true, null);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);

    final List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    final IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    final LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (final LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);
    final IFolder sourceFolder = project.getFolder("src");
    sourceFolder.create(false, true, null);

    final IPackageFragmentRoot srcRoot = javaProject.getPackageFragmentRoot(sourceFolder);
    final IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    final int oldLength = oldEntries.length;
    final IClasspathEntry[] newEntries = new IClasspathEntry[oldLength + 2];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldLength] = JavaCore.newSourceEntry(srcRoot.getPath());
    newEntries[oldLength + 1] = JavaCore.newProjectEntry(destination.getProject().getFullPath());
    javaProject.setRawClasspath(newEntries, null);

    final IFile infoXML = destination.getFile("info.xml");
    if (!infoXML.exists()) {
        createInfoXML(destination);
    }
}

From source file:de.se_rwth.langeditor.util.Misc.java

License:Open Source License

public static void addToClasspath(IJavaProject javaProject, List<IClasspathEntry> newEntries) {
    try {/*from ww  w .  j av  a 2s .  com*/
        IClasspathEntry[] oldClasspath = javaProject.getRawClasspath();
        IClasspathEntry[] newClasspath = Arrays.copyOf(oldClasspath, oldClasspath.length + newEntries.size());
        for (int i = 0; i < newEntries.size(); i++) {
            newClasspath[oldClasspath.length + i] = newEntries.get(i);
        }
        javaProject.setRawClasspath(newClasspath, null);
    } catch (JavaModelException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.se_rwth.langeditor.util.Misc.java

License:Open Source License

public static boolean removeFromClasspath(IJavaProject javaProject, Predicate<IClasspathEntry> predicate) {
    try {//from  w w w  .j  a  v a2  s .c  o m
        IClasspathEntry[] oldClasspath = javaProject.getRawClasspath();
        List<IClasspathEntry> filteredClasspath = Arrays.stream(oldClasspath).filter(predicate.negate())
                .collect(Collectors.toList());
        IClasspathEntry[] newClasspath = filteredClasspath
                .toArray(new IClasspathEntry[filteredClasspath.size()]);
        javaProject.setRawClasspath(newClasspath, null);
        return oldClasspath.length > newClasspath.length;
    } catch (JavaModelException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.se_rwth.langeditor.util.ResourceLocator.java

License:Open Source License

public static Optional<IClasspathEntry> getModelPathClasspathEntry(IJavaProject javaProject) {
    try {// www .ja va 2s .  c  om
        return Arrays.stream(javaProject.getRawClasspath())
                .filter(classpathEntry -> classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER)
                .filter(classpathEntry -> classpathEntry.getPath().equals(Constants.MODELPATH)).findFirst();
    } catch (JavaModelException e) {
        return Optional.empty();
    }
}

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

License:Open Source License

@SuppressWarnings("restriction")
private static Set<String> createJavaClasspath(IJavaProject javaProject, Set<IProject> projectOnCp) {
    LinkedHashSet<String> classPath = new LinkedHashSet<String>();
    try {//from ww  w  .  j a  v  a  2  s  . c  o  m
        // doesn't return jre libraries
        String[] defaultClassPath = JavaRuntime.computeDefaultRuntimeClassPath(javaProject);
        for (String classpathEntry : defaultClassPath) {
            IPath path = new Path(classpathEntry);
            if (isValidPath(path)) {
                classPath.add(path.toOSString());
            }
        }
        // add CPE_CONTAINER classpathes
        IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
        for (IClasspathEntry entry : rawClasspath) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(entry.getPath(),
                        javaProject);
                if (classpathContainer != null) {
                    if (classpathContainer instanceof JREContainer) {
                        IClasspathEntry[] classpathEntries = classpathContainer.getClasspathEntries();
                        for (IClasspathEntry iClasspathEntry : classpathEntries) {
                            IPath path = iClasspathEntry.getPath();
                            // smallest possible fix for #1228 Eclipse plugin always uses host VM to resolve JDK classes
                            if (isValidPath(path) && "rt.jar".equals(path.lastSegment())) {
                                classPath.add(path.toOSString());
                                break;
                            }
                        }
                    } else {
                        IClasspathEntry[] classpathEntries = classpathContainer.getClasspathEntries();
                        for (IClasspathEntry classpathEntry : classpathEntries) {
                            IPath path = classpathEntry.getPath();
                            // shortcut for real files
                            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                                    && isValidPath(path)) {
                                classPath.add(path.toOSString());
                            } else {
                                resolveInWorkspace(classpathEntry, classPath, projectOnCp);
                            }
                        }
                    }
                }
            }
        }
    } catch (CoreException e) {
        FindbugsPlugin.getDefault().logException(e,
                "Could not compute aux. classpath for project " + javaProject);
    }
    return classPath;
}

From source file:de.uni_hildesheim.sse.easy.ui.project_management.EASyJavaConfigurator.java

License:Apache License

@Override
public void configure(IProject project) {
    JavaCapabilityConfigurationPage jcpage = new JavaCapabilityConfigurationPage();
    IJavaProject javaProject = JavaCore.create(project);

    jcpage.init(javaProject, null, null, false);
    try {//from w  w  w  .  j  ava 2  s . co  m
        jcpage.configureJavaProject(null);
    } catch (CoreException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // Create default java folders and settings
    try {
        project.getFolder(ProjectConstants.FOLDER_LIBS).create(false, true, null);
        IFolder resFolder = project.getFolder(ProjectConstants.FOLDER_RES);
        resFolder.create(false, true, null);
        IClasspathEntry resEntry = JavaCore.newSourceEntry(resFolder.getFullPath());
        IClasspathEntry[] currentEntries = javaProject.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[currentEntries.length + 1];
        System.arraycopy(currentEntries, 0, newEntries, 0, currentEntries.length);
        newEntries[newEntries.length - 1] = resEntry;
        javaProject.setRawClasspath(newEntries, null);
    } catch (CoreException e) {
        // Every caught exception is painful, but not harmful
        e.printStackTrace();
    }
}

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

License:Open Source License

private ClassLoader getClassLoader() {
    ClassLoader loader = null;//from  ww  w .j ava  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;//from w ww  . j  av a  2s .c  o  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

void localEditProject(Element pxml, IvyXmlWriter xw) throws BedrockException {
    String pnm = IvyXml.getAttrString(pxml, "NAME");
    IProject ip = findProject(pnm);/*www .  j  a v  a  2s  .  c  o m*/
    IJavaProject ijp = JavaCore.create(ip);
    List<IClasspathEntry> ents = new ArrayList<IClasspathEntry>();
    try {
        for (Element oe : IvyXml.children(pxml, "OPTION")) {
            String k = IvyXml.getAttrString(oe, "NAME");
            String v = IvyXml.getAttrString(oe, "VALUE");
            if (k.startsWith("edu.brown.cs.bubbles.bedrock.")) {
                String sfx = k.substring(29);
                QualifiedName qn = new QualifiedName("edu.brown.cs.bubbles.bedrock", sfx);
                try {
                    ip.setPersistentProperty(qn, v);
                } catch (CoreException e) {
                    BedrockPlugin.logD("Problem setting property " + qn + ": " + e);
                }
            } else
                ijp.setOption(k, v);
        }

        for (Element xe : IvyXml.children(pxml, "XPREF")) {
            String q = IvyXml.getAttrString(xe, "NODE");
            String k = IvyXml.getAttrString(xe, "KEY");
            String v = IvyXml.getAttrString(xe, "VALUE");
            IPreferencesService ps = Platform.getPreferencesService();
            Preferences rn = ps.getRootNode();
            Preferences qn = rn.node(q);
            qn.put(k, v);
        }

        for (IClasspathEntry cpe : ijp.getRawClasspath())
            ents.add(cpe);
        for (Element pe : IvyXml.children(pxml, "PATH")) {
            updatePathElement(ents, pe);
        }
        IClasspathEntry[] enta = new IClasspathEntry[ents.size()];
        enta = ents.toArray(enta);
        ijp.setRawClasspath(enta, new BedrockProgressMonitor(our_plugin, "Update Paths"));
        ijp.save(null, false);
    } catch (CoreException e) {
        throw new BedrockException("Problem editing project", e);
    }
}

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

License:Open Source License

/********************************************************************************/

private void outputProject(IProject p, boolean fil, boolean pat, boolean cls, boolean opt, boolean imps,
        IvyXmlWriter xw) {/*from  ww w.  j  a  v  a2s.  co m*/
    if (p.getLocation() == null)
        return;

    xw.begin("PROJECT");
    xw.field("NAME", p.getName());
    xw.field("PATH", p.getLocation().toOSString());
    xw.field("WORKSPACE", p.getWorkspace().getRoot().getLocation().toOSString());
    xw.field("BEDROCKDIR", p.getWorkingLocation(BEDROCK_PLUGIN).toOSString());
    try {
        if (p.hasNature("org.eclipse.jdt.core.javanature"))
            xw.field("ISJAVA", true);
        if (p.hasNature("com.android.ide.eclipse.adt.AndroidNature"))
            xw.field("ISANDROID", true);
    } catch (CoreException e) {
    }

    IJavaProject jp = JavaCore.create(p);
    if (jp != null && pat) {
        xw.begin("CLASSPATH");
        addClassPaths(jp, xw, null, false);
        xw.end("CLASSPATH");
        xw.begin("RAWPATH");
        try {
            IClasspathEntry[] ents = jp.getRawClasspath();
            for (IClasspathEntry ent : ents) {
                addPath(xw, jp, ent, false);
            }
        } catch (JavaModelException e) {
        }
        xw.end("RAWPATH");
    }

    if (fil) {
        xw.begin("FILES");
        addSourceFiles(p, xw, null);
        xw.end("FILES");
    }

    if (jp != null && cls) {
        xw.begin("CLASSES");
        addClasses(jp, xw);
        xw.end("CLASSES");
    }

    try {
        IProject[] rp = p.getReferencedProjects();
        IProject[] up = p.getReferencingProjects();
        for (int j = 0; j < rp.length; ++j) {
            xw.textElement("REFERENCES", rp[j].getName());
        }
        for (int j = 0; j < up.length; ++j) {
            xw.textElement("USEDBY", up[j].getName());
        }
    } catch (Exception e) {
    }

    if (opt && jp != null) {
        Map<?, ?> opts = jp.getOptions(false);
        for (Map.Entry<?, ?> ent : opts.entrySet()) {
            xw.begin("OPTION");
            xw.field("NAME", ent.getKey().toString());
            xw.field("VALUE", ent.getValue().toString());
            xw.end("OPTION");
        }
        Map<?, ?> allopts = jp.getOptions(true);
        for (Map.Entry<?, ?> ent : allopts.entrySet()) {
            String knm = (String) ent.getKey();
            if (opts.containsKey(knm))
                continue;
            if (knm.startsWith("org.eclipse.jdt.core.formatter"))
                continue;
            xw.begin("OPTION");
            xw.field("DEFAULT", true);
            xw.field("NAME", ent.getKey().toString());
            xw.field("VALUE", ent.getValue().toString());
            xw.end("OPTION");
        }
        try {
            Map<?, ?> pm = p.getPersistentProperties();
            for (Map.Entry<?, ?> ent : pm.entrySet()) {
                QualifiedName qn = (QualifiedName) ent.getKey();
                xw.begin("PROPERTY");
                xw.field("QUAL", qn.getQualifier());
                xw.field("NAME", qn.getLocalName());
                xw.field("VALUE", ent.getValue().toString());
                xw.end("PROPERTY");
            }
        } catch (CoreException e) {
        }
    }

    if (imps && jp != null) {
        try {
            for (IPackageFragment ipf : jp.getPackageFragments()) {
                outputImports(xw, ipf);
            }
        } catch (JavaModelException e) {
        }
    }

    xw.end("PROJECT");
}