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

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

Introduction

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

Prototype

IPath getOutputLocation() throws JavaModelException;

Source Link

Document

Returns the default output location for this project as a workspace- relative absolute path.

Usage

From source file:org.eclipse.che.plugin.java.plain.server.rest.ClasspathUpdaterService.java

License:Open Source License

/**
 * Updates the information about classpath.
 *
 * @param projectPath/*from w w w . j a  v  a 2s . c  o m*/
 *         path to the current project
 * @param entries
 *         list of classpath entries which need to set
 * @throws JavaModelException
 *         if JavaModel has a failure
 * @throws ServerException
 *         if some server error
 * @throws ForbiddenException
 *         if operation is forbidden
 * @throws ConflictException
 *         if update operation causes conflicts
 * @throws NotFoundException
 *         if Project with specified path doesn't exist in workspace
 * @throws IOException
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void updateClasspath(@QueryParam("projectpath") String projectPath, List<ClasspathEntryDto> entries)
        throws JavaModelException, ServerException, ForbiddenException, ConflictException, NotFoundException,
        IOException {
    IJavaProject javaProject = model.getJavaProject(projectPath);

    javaProject.setRawClasspath(createModifiedEntry(entries), javaProject.getOutputLocation(),
            new NullProgressMonitor());

    updateProjectConfig(projectPath);
}

From source file:org.eclipse.dltk.freemarker.core.util.ClassUtils.java

License:Open Source License

/**
 * Add URL form the JavaProject.//from   w w w.  j  av  a  2 s.c  om
 * 
 * @param javaProject
 * @param urls
 * @param root
 * @param rootLocation
 * @param javaProjectsAlreadyDone
 * @throws JavaModelException
 * @throws CoreException
 */
private static void addPath(IJavaProject javaProject, List<URL> urls, IWorkspaceRoot root, IPath rootLocation,
        List<IJavaProject> javaProjectsAlreadyDone) throws JavaModelException, CoreException {
    if (javaProjectsAlreadyDone.contains(javaProject))
        return;

    javaProjectsAlreadyDone.add(javaProject);
    String projectName = javaProject.getElementName();
    IClasspathEntry javacp[] = javaProject.getResolvedClasspath(true);

    // Add bin folder
    IPath outputLocation = javaProject.getOutputLocation();
    addPath(urls, rootLocation.append(outputLocation));

    // Loop for .classpath
    IClasspathEntry entry = null;
    IPath entryPath = null;
    for (int i = 0; i < javacp.length; i++) {
        // load bin folder of referenced projects into classpath
        entry = javacp[i];
        entryPath = entry.getPath();
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            // add jars
            if (projectName.equals(entryPath.segment(0))) {
                // Jar belongs to the Java Project, add base dir
                addPath(urls, rootLocation.append(entryPath));
            } else {
                // External Jar (ex : C:/Program Files/xxx.jar)
                addPath(urls, entryPath);
            }
            break;
        case IClasspathEntry.CPE_SOURCE:
            // add the source folders of the project
            addPath(urls, rootLocation.append(entryPath));
            break;
        case IClasspathEntry.CPE_PROJECT:
            // add bin folder from referenced project
            IProject referencedProject = root.getProject(entryPath.segment(0));
            if (referencedProject != null && referencedProject.exists()
                    && referencedProject.hasNature(JavaCore.NATURE_ID)) {
                IJavaProject referencedJavaProject = JavaCore.create(referencedProject);
                addPath(referencedJavaProject, urls, root, rootLocation, javaProjectsAlreadyDone);
            }
            break;
        default:
            addPath(urls, entryPath);
            break;
        }
    }
}

From source file:org.eclipse.draw2d.preview.OpenFigureViewerHandler.java

License:Open Source License

private IFigure getFigure(IWorkbenchWindow activeWorkbenchWindow, Object item) {
    IType figureClass = null;/*w w  w  .j  a v a 2s  .  com*/
    try {
        if (item instanceof ICompilationUnit) {
            figureClass = ((ICompilationUnit) item).getTypes()[0];
        } else if (item instanceof IType) {
            figureClass = (IType) item;
        } else if (item instanceof IClassFile) {
            figureClass = ((IClassFile) item).getType();
        } else if (item instanceof IFile) {
            ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom((IFile) item);
            figureClass = compilationUnit.getTypes()[0];
        }
        boolean isFigure = false;
        for (IType type : figureClass.newSupertypeHierarchy(null).getAllInterfaces()) {
            if (type.getFullyQualifiedName().equals(IFigure.class.getName())) {
                isFigure = true;
            }
        }
        if (!isFigure) {
            MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.notADraw2DFigure_title,
                    Messages.notADraw2DFigure_message);
            return null;
        }
        List<URL> urls = new ArrayList<URL>();
        URL outputURL = toAbsoluteURL(figureClass.getJavaProject().getOutputLocation());
        urls.add(outputURL);
        for (IClasspathEntry entry : figureClass.getJavaProject().getResolvedClasspath(false)) {
            URL toAdd = null;
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                toAdd = entry.getPath().toFile().toURI().toURL();
                break;
            case IClasspathEntry.CPE_PROJECT:
                IProject project = ResourcesPlugin.getWorkspace().getRoot()
                        .getProject(entry.getPath().toString());
                IJavaProject javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
                toAdd = toAbsoluteURL(javaProject.getOutputLocation());
                break;
            }
            if (toAdd != null) {
                urls.add(toAdd);
            }
        }
        ClassLoader cl = new URLClassLoader(urls.toArray(new URL[urls.size()]),
                this.getClass().getClassLoader());
        Class<? extends IFigure> figureClazz = (Class<? extends IFigure>) cl
                .loadClass(figureClass.getFullyQualifiedName());
        for (Constructor<?> cons : figureClazz.getConstructors()) {
            cons.setAccessible(true);
        }
        IFigure figureObject = figureClazz.newInstance();
        return figureObject;
    } catch (JavaModelException ex) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.javaError_title,
                Messages.javaError_message);
        return null;
    } catch (MalformedURLException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.internalError_title,
                Messages.internalError_message);
        return null;
    } catch (CoreException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.internalError_title,
                Messages.internalError_message);
        Activator.getDefault().getLog().log(e.getStatus());
        return null;
    } catch (ClassNotFoundException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.internalError_title,
                Messages.internalError_message);
        return null;
    } catch (InstantiationException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.couldNotInstantiate_title,
                Messages.couldNotInstantiate_message);
        return null;
    } catch (IllegalAccessException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.couldNotInstantiate_title,
                Messages.couldNotInstantiate_message);
        return null;
    } catch (Error e) {
        ErrorDialog.openError(activeWorkbenchWindow.getShell(), Messages.errorInFigure_title,
                Messages.errorInFigure_message, new Status(IStatus.ERROR,
                        figureClass.getJavaProject().getElementName(), Messages.errorInFigure_message, e));
        return null;
    }
}

From source file:org.eclipse.e4.tm.ui.javascript.JavaProjectClassLoader.java

License:Open Source License

public JavaProjectClassLoader(IProject project, ClassLoader mainLoader) throws RuntimeException {
    super(new URL[] {});
    this.mainLoader = mainLoader;
    try {//  w  w  w .jav  a 2s . c om
        project.open(null);
        IJavaProject javaProject = JavaCore.create(project);
        IPath outputPath = javaProject.getOutputLocation();
        URI projectLocation = project.getLocationURI();
        String outputUri = projectLocation.toString() + IPath.SEPARATOR + outputPath.removeFirstSegments(1)
                + IPath.SEPARATOR;
        URL outputFolder = new URL(outputUri);
        addURL(outputFolder);
    } catch (JavaModelException jme) {
        throw new RuntimeException("Couldn't initialize IJavaProject: ", jme);
    } catch (CoreException ce) {
        throw new RuntimeException("Couldn't initialize IProject: ", ce);
    } catch (MalformedURLException mue) {
        throw new RuntimeException("Couldn't create output folder: ", mue);
    }
}

From source file:org.eclipse.e4.tools.ui.designer.session.JavaHelper.java

License:Open Source License

/**
 * Get output folders.<br>//w  w  w.j a va  2 s .c  o m
 * 
 * @param project
 * @return a List of IFolders
 */
public static List<IFolder> getOutputFolders(IJavaProject project) throws CoreException {
    List<IFolder> folders = new UniqueEList<IFolder>();
    if (project == null || project.exists() == false) {
        return folders;
    }
    // Default Output Location
    IFolder folder = findFolder(project.getOutputLocation());
    if (folder != null) {
        folders.add(folder);
    }
    // Lookup in source folders
    for (IClasspathEntry entry : project.getResolvedClasspath(true)) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IFolder innerFolder = findFolder(entry.getOutputLocation());
            if (innerFolder != null) {
                folders.add(innerFolder);
            }
        }
    }
    return folders;
}

From source file:org.eclipse.e4.tools.ui.designer.session.JavaHelper.java

License:Open Source License

/**
 * This will return the set of output folders name for the given project.
 * <p>/*  ww w. j  a va 2s .  c  om*/
 * For example, if a project has a source folder "src" with its output
 * folder set as "bin" and a source folder "src-gen" with its output folder
 * set as "bin-gen", this will return a LinkedHashSet containing both "bin"
 * and "bin-gen".
 * </p>
 * 
 * @param project
 *            The project we seek the output folders of.
 * @return The set of output folders name for the given (java) project.
 */
public static List<String> getStringOutputFolders(IJavaProject project) throws CoreException {
    List<String> folders = new UniqueEList<String>();
    if (project.exists() == false) {
        return folders;
    }
    List<IFolder> innerFolders = getOutputFolders(project);
    for (IFolder folder : innerFolders) {
        folders.add(folder.getFullPath().removeFirstSegments(1).toString());
    }
    folders.add(project.getOutputLocation().removeFirstSegments(1).toString());
    return folders;
}

From source file:org.eclipse.e4.tools.ui.designer.utils.ClassLoaderHelper.java

License:Open Source License

public static byte[] doGetClassContent(IJavaProject javaProject, String className) {
    if (javaProject == null || !javaProject.exists())
        return null;

    String resourceName = className.replace('.', '/') + ".class";
    try {//from ww  w  .  j  a  v a 2s  .  co m
        IPath outPath = javaProject.getProject().getLocation().removeLastSegments(1)
                .append(javaProject.getOutputLocation());
        outPath = outPath.addTrailingSeparator();
        {
            URL url = toURL(outPath.append(resourceName));
            if (url != null) {
                InputStream inputStream = url.openStream();
                byte[] content = new byte[inputStream.available()];
                inputStream.read(content);
                return content;
            }
            for (IProject project : javaProject.getProject().getReferencedProjects()) {
                if (!project.isOpen()) {
                    continue;
                }
                IJavaProject javaReferencedProject = JavaCore.create(project);
                if (javaReferencedProject.exists()) {
                    byte[] content = getClassContent(javaReferencedProject, className);
                    if (content != null) {
                        return content;
                    }
                }
            }
        }
        IType type = javaProject.findType(className);
        if (type != null && type.exists()) {
            if (type.isBinary()) {
                return type.getClassFile().getBytes();
            } else {
                IJavaProject typeProject = type.getJavaProject();
                if (!javaProject.equals(typeProject)) {
                    return getClassContent(typeProject, className);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.eclipse.e4.tools.ui.designer.utils.ClassLoaderHelper.java

License:Open Source License

private static URL findResourceURL(IJavaProject javaProject, Set<IJavaProject> visited, boolean isFirstProject,
        String name) {//  w ww  .  j  a v a 2  s. com
    if (visited.contains(javaProject))
        return null;
    visited.add(javaProject);
    try {
        IPath outPath = javaProject.getProject().getLocation().removeLastSegments(1)
                .append(javaProject.getOutputLocation());
        outPath = outPath.addTrailingSeparator();
        {
            URL url = toURL(outPath.append(name));
            if (url != null) {
                return url;
            }
        }
        for (IPackageFragmentRoot fragment : javaProject.getPackageFragmentRoots()) {
            if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
                URL url = toURL(fragment.getResource().getLocation().append(name));
                if (url != null) {
                    return url;
                }
            }
        }
        // urls.add(out);
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry entry : entries) {
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY: {
                // TODO
                IClasspathEntry resolveEntry = JavaCore.getResolvedClasspathEntry(entry);
                File file = resolveEntry.getPath().toFile();
                IPath path = resolveEntry.getPath();
                if (!file.exists()) {
                    String projectName = path.segment(0);
                    IProject project = javaProject.getProject().getWorkspace().getRoot()
                            .getProject(projectName);
                    path = project.getLocation().append(path.removeFirstSegments(1));
                }
                String spec = "jar:file:" + path.toString() + "!/" + name;
                try {
                    URL url2 = new URL(spec);
                    url2.getContent();
                    return url2;
                } catch (Exception e) {
                }
            }
                break;
            case IClasspathEntry.CPE_CONTAINER:

                break;
            case IClasspathEntry.CPE_VARIABLE: {
                {
                    // TODO
                    URL url = toURL(outPath.append(name));
                    if (url != null) {
                        return url;
                    }
                }
            }
                break;
            case IClasspathEntry.CPE_PROJECT: {
                if (isFirstProject || entry.isExported()) {
                    URL url = findResourceURL(getJavaProject(entry), visited, false, name);
                    if (url != null) {
                        return url;
                    }
                }
                break;
            }
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.eclipse.e4.tools.ui.designer.utils.ClassLoaderHelper.java

License:Open Source License

private static void collectClasspathURLs(IJavaProject javaProject, List<URL> urls, Set<IJavaProject> visited,
        boolean isFirstProject) {
    if (visited.contains(javaProject))
        return;//from   w  w  w  .  j ava 2s .c o m
    visited.add(javaProject);
    try {
        IPath outPath = javaProject.getProject().getWorkspace().getRoot().getFullPath()
                .append(javaProject.getOutputLocation());
        outPath = outPath.addTrailingSeparator();
        URL out = createFileURL(outPath);
        urls.add(out);
        IClasspathEntry[] entries = null;
        entries = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry entry : entries) {
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
            case IClasspathEntry.CPE_CONTAINER:
            case IClasspathEntry.CPE_VARIABLE:
                collectClasspathEntryURL(entry, urls);
                break;
            case IClasspathEntry.CPE_PROJECT: {
                if (isFirstProject || entry.isExported())
                    collectClasspathURLs(getJavaProject(entry), urls, visited, false);
                break;
            }
            }
        }
    } catch (JavaModelException e) {
        return;
    }
}

From source file:org.eclipse.e4.xwt.tools.ui.designer.loader.XWTVisualLoader.java

License:Open Source License

public synchronized Control loadWithOptions(URL url, Map<String, Object> options) throws Exception {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    String fileStr = url.getFile();
    if (fileStr.indexOf(PathHelper.WHITE_SPACE_ASCII) != -1) {
        fileStr = fileStr.replace(PathHelper.WHITE_SPACE_ASCII, " ");
    }//from w  w w  . j av  a2s.  c om
    IFile file = root.getFileForLocation(new Path(fileStr));
    if (file != null) {
        try {
            // the url given an binary file of project, we need find the source file of it and the load and open.
            IProject project = file.getProject();
            String fullPath = file.getFullPath().toString();
            IJavaProject javaProject = JavaCore.create(project);
            String outputPath = javaProject.getOutputLocation().toString();
            if (fullPath != null && outputPath != null && fullPath.startsWith(outputPath)) {
                String fileSourcePath = fullPath.substring(outputPath.length());
                IPackageFragmentRoot[] allPackageFragmentRoots = javaProject.getAllPackageFragmentRoots();
                for (IPackageFragmentRoot pRoot : allPackageFragmentRoots) {
                    if (pRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                        IFolder resource = (IFolder) pRoot.getResource();
                        IFile sourceFile = resource.getFile(new Path(fileSourcePath));
                        if (sourceFile != null && sourceFile.exists()) {
                            file = sourceFile;
                            break;
                        }
                    }
                }
            }
        } catch (Exception e) {
        }
    }
    if (file != null) {
        IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        IEditorPart activeEditor = activePage.getActiveEditor();
        try {
            XWTDesigner designer = (XWTDesigner) activePage.openEditor(new FileEditorInput(file),
                    XWTDesigner.EDITOR_ID, false);
            XamlDocument xamlDocument = (XamlDocument) designer.getDocumentRoot();
            XWTModelBuilder builder = null;
            if (xamlDocument == null) {
                builder = new XWTModelBuilder();
                builder.doLoad(designer, null);
                xamlDocument = builder.getDiagram();
            }
            Control control = (Control) new XWTProxy(file).load(xamlDocument.getRootElement(), options);
            if (builder != null) {
                builder.dispose();
            }
            return control;
        } finally {
            activePage.activate(activeEditor);
        }
    }
    return null;
}