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:bz.davide.dmeclipsesavehookplugin.builder.DMEclipseSaveHookPluginBuilder.java

License:Open Source License

static void findTransitiveDepProjects(IJavaProject current, ArrayList<IProject> projects,
        ArrayList<String> fullClasspath) throws CoreException {
    if (!projects.contains(current.getProject())) {
        projects.add(current.getProject());
    }/*from   w  w  w  .  j  a v  a  2 s  .  c  om*/

    fullClasspath.add(ResourcesPlugin.getWorkspace().getRoot().findMember(current.getOutputLocation())
            .getLocation().toOSString() + "/");
    ArrayList<IClasspathEntry> classPaths = new ArrayList<IClasspathEntry>();
    classPaths.addAll(Arrays.asList(current.getRawClasspath()));

    for (int x = 0; x < classPaths.size(); x++) {
        IClasspathEntry cp = classPaths.get(x);
        if (cp.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            String prjName = cp.getPath().lastSegment();
            IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject(prjName);
            if (prj.hasNature(JavaCore.NATURE_ID)) {
                IJavaProject javaProject = JavaCore.create(prj);
                findTransitiveDepProjects(javaProject, projects, fullClasspath);
            }
            continue;
        }
        if (cp.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            String fullContainerName = cp.getPath().toString();
            if (!fullContainerName.startsWith("org.eclipse.jdt.launching.JRE_CONTAINER/")) {
                System.out.println("CP C: " + fullContainerName);
                IClasspathContainer container = JavaCore.getClasspathContainer(cp.getPath(), current);
                classPaths.addAll(Arrays.asList(container.getClasspathEntries()));

            }
        }
        if (cp.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            IPath path = cp.getPath();
            // Check first if this path is relative to workspace
            IResource workspaceMember = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
            if (workspaceMember != null) {
                String fullPath = workspaceMember.getLocation().toOSString();
                fullClasspath.add(fullPath);
            } else {
                fullClasspath.add(path.toOSString());
            }
        }
    }
}

From source file:ca.mcgill.sable.soot.examples.NewSootExampleWizard.java

License:Open Source License

@Override
public boolean performFinish() {
    boolean performFinish = super.performFinish();

    if (performFinish) {
        IJavaProject newProject = (IJavaProject) getCreatedElement();
        try {//from  w  ww .  j  av  a  2  s  .c o m
            IClasspathEntry[] originalCP = newProject.getRawClasspath();
            IClasspathEntry ajrtLIB = JavaCore.newVariableEntry(
                    new Path(SootClasspathVariableInitializer.VARIABLE_NAME_CLASSES),
                    new Path(SootClasspathVariableInitializer.VARIABLE_NAME_SOURCE), null);
            // Update the raw classpath with the new entry
            int originalCPLength = originalCP.length;
            IClasspathEntry[] newCP = new IClasspathEntry[originalCPLength + 1];
            System.arraycopy(originalCP, 0, newCP, 0, originalCPLength);
            newCP[originalCPLength] = ajrtLIB;
            newProject.setRawClasspath(newCP, new NullProgressMonitor());
        } catch (JavaModelException e) {
        }

        String templateFilePath = fromFile;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            is = SootPlugin.getDefault().getBundle().getResource(templateFilePath).openStream();
            if (is == null) {
                new RuntimeException("Resource " + templateFilePath + " not found!").printStackTrace();
            } else {

                IClasspathEntry[] resolvedClasspath = newProject.getResolvedClasspath(true);
                IClasspathEntry firstSourceEntry = null;
                for (IClasspathEntry classpathEntry : resolvedClasspath) {
                    if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                        firstSourceEntry = classpathEntry;
                        break;
                    }
                }
                if (firstSourceEntry != null) {
                    IPath path = SootPlugin.getWorkspace().getRoot().getFile(firstSourceEntry.getPath())
                            .getLocation();
                    String srcPath = path.toString();
                    String newfileName = toFile;
                    final IPath newFilePath = firstSourceEntry.getPath().append(newfileName);
                    fos = new FileOutputStream(srcPath + File.separator + newfileName);
                    int temp = is.read();
                    while (temp > -1) {
                        fos.write(temp);
                        temp = is.read();
                    }
                    fos.close();
                    //refresh project
                    newProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());

                    final IWorkbenchPage activePage = JavaPlugin.getActivePage();
                    if (activePage != null) {
                        final Display display = getShell().getDisplay();
                        if (display != null) {
                            display.asyncExec(new Runnable() {
                                public void run() {
                                    try {
                                        IResource newResource = SootPlugin.getWorkspace().getRoot()
                                                .findMember(newFilePath);
                                        IDE.openEditor(activePage, (IFile) newResource, true);
                                    } catch (PartInitException e) {
                                        JavaPlugin.log(e);
                                    }
                                }
                            });
                        }
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null)
                    is.close();
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
            }
        }
    }

    return performFinish;
}

From source file:ca.mcgill.sable.soot.launching.DavaHandler.java

License:Open Source License

public void handleAfter() {
    ArrayList newMembers = new ArrayList();
    IPath jreLibPath = null;/*from ww  w  .  java  2 s .  com*/
    try {
        IResource[] elems = getSootOutputFolder().getFolder(Messages.getString("DavaHandler.dava"))
                .getFolder(Messages.getString("DavaHandler.src")).members();
        for (int i = 0; i < elems.length; i++) {
            if (getBeforeList() == null) {
                newMembers.add(elems[i]);
                if (elems[i] instanceof IFile) {

                    SootPlugin.getDefault().getManager().setToFalseRemove((IFile) elems[i]);
                }
            } else if (getBeforeList().contains(elems[i])) {
                if (elems[i] instanceof IFile) {

                    if (SootPlugin.getDefault().getManager().isFileMarkersRemove((IFile) elems[i])) {
                        newMembers.add(elems[i]);
                        // this sets changed bit to 0 - so file doesn't stay on list indefinitely

                        SootPlugin.getDefault().getManager().setToFalseRemove((IFile) elems[i]);
                    }
                }

            } else if (!getBeforeList().contains(elems[i])) {
                if (SootPlugin.getDefault().getManager().getChangedResources() == null) {
                } else if (SootPlugin.getDefault().getManager().getChangedResources().containsKey(elems[i])) {
                    newMembers.add(elems[i]);
                    // this sets changed bit to 0 - so file doesn't stay on list indefinitely
                    if (elems[i] instanceof IFile) {

                        SootPlugin.getDefault().getManager().setToFalseRemove((IFile) elems[i]);
                    }
                }
            }
        }

        // testing class lib copying
        IProject proj = getSootOutputFolder().getProject();
        IResource[] elements = proj.members();

        IJavaProject jProj = JavaCore.create(proj);
        IClasspathEntry[] paths = jProj.getRawClasspath();

        for (int i = 0; i < paths.length; i++) {
            switch (paths[i].getEntryKind()) {
            case IClasspathEntry.CPE_CONTAINER: {
                jreLibPath = paths[i].getPath();

                break;
            }

            }
        }
    } catch (CoreException e) {
    }

    if (!newMembers.isEmpty()) {

        // if is special dava project add src files there
        if (davaProjectExists()) {
            setDavaProj(JavaCore.create(SootPlugin.getWorkspace().getRoot().getProject(getDavaProjName())));
            if (getDavaProj().isOpen()) {
                if (shouldCopyFiles()) {
                    copyFiles(newMembers);
                }
            } else {
                openProject();
                if (shouldCopyFiles()) {
                    copyFiles(newMembers);
                }
            }
        }
        // if not special dava project ask user to create and add files there
        else {
            boolean result = createSpecialDavaProject(jreLibPath);
            if (result) {
                copyFiles(newMembers);
            }

        }
    }
}

From source file:ccw.wizards.NewClojureProjectWizard.java

License:Open Source License

private void setupJavaProjectClassPath(IJavaProject javaProject) throws CoreException {
    IClasspathEntry[] entriesOld = javaProject.getRawClasspath();
    IClasspathEntry[] entriesNew = new IClasspathEntry[entriesOld.length + 1];

    System.arraycopy(entriesOld, 0, entriesNew, 0, entriesOld.length);

    // Ensure a proper "src" directory is used for sources (and not the project)
    for (int i = 0; i < entriesOld.length; i++) {
        if (entriesOld[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IFolder src = javaProject.getProject().getFolder("src");
            if (!src.exists())
                src.create(true, true, null);
            entriesNew[i] = JavaCore.newSourceEntry(src.getFullPath());
        }/*from w  w w . j  a va 2 s.  co  m*/
    }

    entriesNew[entriesOld.length] = JavaCore
            .newContainerEntry(Path.fromPortableString(JavaRuntime.JRE_CONTAINER));

    javaProject.setRawClasspath(entriesNew, null);
    javaProject.save(null, true);
}

From source file:ch.mlutz.plugins.t4e.handlers.ClasspathContainerHandler.java

License:Open Source License

private Object onAddClasspathContainer(ExecutionEvent event) throws ExecutionException {

    // boolean oldValue = HandlerUtil.toggleCommandState(event.getCommand());

    String localRepositoryDir = MavenTools.getMavenLocalRepoPath();
    log.info("LocalRepositoryDir: " + localRepositoryDir);

    /*/* w  w  w .  j  ava  2 s .c om*/
         try {
    JavaCore.setClasspathContainer(containerSuggestion.getPath(), new IJavaProject[] {project},
       new IClasspathContainer[] {new Maven2ClasspathContainer(containerPath, bundleUpdater.newEntries)}, null);
         } catch(JavaModelException ex) {
    Maven2Plugin.getDefault().getConsole().logError(ex.getMessage());
         }
     */

    // get workbench window
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    // set selection service
    ISelectionService service = window.getSelectionService();
    // set structured selection
    IStructuredSelection structured = (IStructuredSelection) service.getSelection();

    //check if it is an IFile
    Object el = structured.getFirstElement();

    if (el instanceof IJavaProject) {

        IJavaProject javaProject = (IJavaProject) el;

        // Create new Classpath Container Entry
        IClasspathEntry containerEntry = JavaCore.newContainerEntry(CONTAINER_PATH);

        // Initialize Classpath Container
        ClasspathContainerInitializer containerInit = JavaCore
                .getClasspathContainerInitializer(Constants.CONTAINER_ID);
        try {
            containerInit.initialize(new Path(Constants.CONTAINER_ID), javaProject);

            // Set Classpath of Java Project
            List<IClasspathEntry> projectClassPath = new ArrayList<IClasspathEntry>(
                    Arrays.asList(javaProject.getRawClasspath()));
            projectClassPath.add(containerEntry);
            javaProject.setRawClasspath(projectClassPath.toArray(new IClasspathEntry[projectClassPath.size()]),
                    null);
        } catch (CoreException e) {
            log.error("Could not add Classpath container: ", e);
        }

        /*
        IClasspathEntry varEntry = JavaCore.newContainerEntry(
              new Path("JDKLIB/default"), // container 'JDKLIB' + hint 'default'
              false); //not exported
                
        try {
           JavaCore.setClasspathContainer(
          new Path("JDKLIB/default"),
          new IJavaProject[]{ (IJavaProject) el }, // value for 'myProject'
          new IClasspathContainer[] {
             new IClasspathContainer() {
                public IClasspathEntry[] getClasspathEntries() {
                   return new IClasspathEntry[]{
                         JavaCore.newLibraryEntry(new Path("d:/rt.jar"), null, null, false)
                   };
                }
                public String getDescription() { return "Basic JDK library container"; }
                public int getKind() { return IClasspathContainer.K_SYSTEM; }
                public IPath getPath() { return new Path("JDKLIB/basic"); }
             }
          },
          null);
        } catch (JavaModelException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }
        */
    }
    return null;
}

From source file:ch.mlutz.plugins.t4e.handlers.ClasspathContainerHandler.java

License:Open Source License

private Object onRemoveClasspathContainer(ExecutionEvent event) throws ExecutionException {
    // get workbench window
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    // set selection service
    ISelectionService service = window.getSelectionService();
    // set structured selection
    IStructuredSelection structured = (IStructuredSelection) service.getSelection();

    //check if it is an IFile
    Object el = structured.getFirstElement();

    if (el instanceof IJavaProject) {

        IJavaProject javaProject = (IJavaProject) el;

        try {/*from  www  .j  a v a2 s  . c  om*/
            // Set Classpath of Java Project
            List<IClasspathEntry> projectClassPath = new ArrayList<IClasspathEntry>(
                    Arrays.asList(javaProject.getRawClasspath()));

            IClasspathEntry entry;
            for (int i = projectClassPath.size() - 1; i >= 0; --i) {
                entry = projectClassPath.get(i);
                if (entry.getPath().equals(CONTAINER_PATH)) {
                    projectClassPath.remove(i);
                }
            }

            javaProject.setRawClasspath(projectClassPath.toArray(new IClasspathEntry[projectClassPath.size()]),
                    null);
        } catch (CoreException e) {
            log.error("Could not remove Classpath container: ", e);
        }
    }
    return null;
}

From source file:cn.dockerfoundry.ide.eclipse.server.core.internal.application.JavaWebApplicationDelegate.java

License:Open Source License

/**
 * Attempts to determine the framework based on the contents and nature of
 * the project. Returns null if no framework was determined.
 * @param project//from   w  w w  .j av  a  2s . co m
 * @return Framework type or null if framework was not determined.
 * @deprecated kept for reference as to how application type was being
 * determined from a Java project for legacy v1 CF servers. v2 Servers no
 * longer require a framework for an application, as frameworks have been
 * replaced with buildpacks.
 */
protected String getFramework(IProject project) {
    if (project != null) {
        IJavaProject javaProject = DockerFoundryProjectUtil.getJavaProject(project);
        if (javaProject != null) {
            if (DockerFoundryProjectUtil.hasNature(project, DockerFoundryConstants.GRAILS_NATURE)) {
                return DockerFoundryConstants.GRAILS;
            }

            // in case user has Grails projects without the nature
            // attached
            if (project.isAccessible() && project.getFolder("grails-app").exists() //$NON-NLS-1$
                    && project.getFile("application.properties").exists()) { //$NON-NLS-1$
                return DockerFoundryConstants.GRAILS;
            }

            IClasspathEntry[] entries;
            boolean foundSpringLibrary = false;
            try {
                entries = javaProject.getRawClasspath();
                for (IClasspathEntry entry : entries) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        if (isLiftLibrary(entry)) {
                            return DockerFoundryConstants.LIFT;
                        }
                        if (isSpringLibrary(entry)) {
                            foundSpringLibrary = true;
                        }
                    } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                        IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(),
                                javaProject);
                        if (container != null) {
                            for (IClasspathEntry childEntry : container.getClasspathEntries()) {
                                if (isLiftLibrary(childEntry)) {
                                    return DockerFoundryConstants.LIFT;
                                }
                                if (isSpringLibrary(childEntry)) {
                                    foundSpringLibrary = true;
                                }
                            }
                        }
                    }
                }
            } catch (JavaModelException e) {
                // Log the error but don't throw it again as there may be
                // other ways to detect the framework
                DockerFoundryPlugin.log(new Status(IStatus.WARNING, DockerFoundryPlugin.PLUGIN_ID,
                        "Unexpected error during auto detection of application type", e)); //$NON-NLS-1$
            }

            if (DockerFoundryProjectUtil.isSpringProject(project)) {
                return DockerFoundryConstants.SPRING;
            }

            if (foundSpringLibrary) {
                return DockerFoundryConstants.SPRING;
            }
        }
    }
    return null;
}

From source file:cn.ieclipse.adt.ext.helpers.ProjectHelper.java

License:Apache License

public static void cleanOrmClasspath(IJavaProject javaProject) throws JavaModelException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    int idx = -1;
    int i = 0;// w w w . j a  v  a  2s.co m
    for (IClasspathEntry entry : entries) {
        if (AormClasspathContainerInitializer.invalidOrmClassPath(entry.getPath())) {
            idx = i;
            break;
        }
        i++;
    }
    if (idx >= 0) {
        entries = removeEntryFromClasspath(entries, idx);
        javaProject.setRawClasspath(entries, new NullProgressMonitor());
    }
}

From source file:cn.ieclipse.adt.ext.helpers.ProjectHelper.java

License:Apache License

public static void addOrRemoveEntryToClasspath(IJavaProject javaProject, IClasspathEntry newEntry)
        throws JavaModelException {
    int idx = isEntryInClasspath(javaProject, newEntry);
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    if (idx >= 0) {
        entries = removeEntryFromClasspath(entries, idx);
    } else {/*from  w ww .  jav a  2  s  .co m*/
        entries = addEntryToClasspath(entries, newEntry);
    }
    javaProject.setRawClasspath(entries, new NullProgressMonitor());
}

From source file:cn.ieclipse.adt.ext.helpers.ProjectHelper.java

License:Apache License

public static int isEntryInClasspath(IJavaProject javaProject, IClasspathEntry newEntry)
        throws JavaModelException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    int idx = -1;
    int i = 0;/*from  w  w  w .j  av  a  2  s.co m*/
    for (IClasspathEntry entry : entries) {
        if (entry.equals(newEntry)) {
            idx = i;
            break;
        }
        i++;
    }
    return idx;
}