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.jboss.tools.common.EclipseUtil.java

License:Open Source License

public static String getJavaProjectOutputLocation(IProject project) {
    IJavaProject javaProject = getJavaProject(project);
    if (javaProject == null)
        return null;
    try {//from w  ww  .  j a  v a2 s.  co  m
        IPath p = javaProject.getOutputLocation();
        IResource r = project.getWorkspace().getRoot().findMember(p);
        return (r == null || r.getLocation() == null) ? null : r.getLocation().toString();
    } catch (CoreException e) {
        CommonPlugin.getPluginLog().logError(e);
        return null;
    }
}

From source file:org.jboss.tools.common.model.util.EclipseResourceUtil.java

License:Open Source License

/**
 * Returns true only if project has no sources but output contains *.class file 
 *//*from   w  w  w .  ja v  a  2s .c  o m*/
public static boolean isContainedInOutput(IProject project, String className) {
    if (className == null || className.length() == 0)
        return false;
    IJavaProject javaProject = getJavaProject(project);
    if (javaProject == null)
        return false;
    try {
        IPath p = javaProject.getOutputLocation();
        IResource r = project.getWorkspace().getRoot().findMember(p);
        if (r == null || r.getLocation() == null)
            return false;
        String output = r.getLocation().toString();
        String f = output + XModelObjectConstants.SEPARATOR + className.replace('.', '/') + ".class"; //$NON-NLS-1$
        File file = new File(f);
        return file.isFile() && FileUtils.isSameFile(file);
    } catch (JavaModelException t) {
        ModelPlugin.getPluginLog().logError("Error checking class " + className, t); //$NON-NLS-1$
        return false;
    }
}

From source file:org.jboss.tools.esb.core.module.JBossESBModuleDelegate.java

License:Open Source License

public IModuleResource[] legacyMembers() throws CoreException {
    IProject project = getProject();//  w w w .  j  av  a 2 s. c om
    final IVirtualComponent c = ComponentCore.createComponent(project);
    IVirtualFolder vf = c.getRootFolder();
    IContainer[] folders = vf.getUnderlyingFolders();

    if (folders == null || folders.length == 0) {
        throw new CoreException(StatusUtils.errorStatus("The project is not a valid JBoss ESB project."));
    }

    //this is because of the beta1 workspace meta data, so try to get esbcontent folder name by this way,
    //in the JBT CR1, the length of folders will be 1.
    IContainer contentFolder = folders.length > 1 ? folders[1] : folders[0];

    IFolder configFolder = getProject().getFolder(contentFolder.getProjectRelativePath());
    IJavaProject javaPrj = JavaCore.create(getProject());
    IPath output = javaPrj.getOutputLocation();
    // if the jboss-esb.xml file is not in META-INF folder, try to get it from other folder of the project
    // block this logic as Max's suggestion
    //List<IModuleResource> mrs = getConfigModuleFile(project, configFolder);

    List<IModuleResource> mrs = new ArrayList<IModuleResource>();

    IModuleResource[] esbContent = getModuleResources(Path.EMPTY, configFolder);
    IModuleResource[] classes = getModuleResources(Path.EMPTY,
            getProject().getWorkspace().getRoot().getFolder(output));
    IModuleResource[] allResource = new IModuleResource[esbContent.length + classes.length + mrs.size()];
    System.arraycopy(esbContent, 0, allResource, 0, esbContent.length);
    System.arraycopy(classes, 0, allResource, esbContent.length, classes.length);
    if (mrs.size() > 0) {
        IModuleResource[] mr = mrs.toArray(new IModuleResource[] {});
        System.arraycopy(mr, 0, allResource, esbContent.length + classes.length, mr.length);
    }
    return allResource;
}

From source file:org.jboss.tools.fuse.transformation.editor.internal.util.JavaUtil.java

License:Open Source License

/**
 * Creates a ClassLoader using the project's build path.
 *
 * @param javaProject the Java project./*w  w w . j  a  v a 2  s . c  om*/
 * @param parentClassLoader the parent class loader, may be null.
 *
 * @return a new ClassLoader based on the project's build path.
 *
 * @throws Exception if something goes wrong.
 */
public static ClassLoader getProjectClassLoader(IJavaProject javaProject, ClassLoader parentClassLoader)
        throws Exception {
    IProject project = javaProject.getProject();
    IWorkspaceRoot root = project.getWorkspace().getRoot();
    List<URL> urls = new ArrayList<>();
    urls.add(new File(project.getLocation() + "/" + javaProject.getOutputLocation().removeFirstSegments(1) //$NON-NLS-1$
            + "/") //$NON-NLS-1$
                    .toURI().toURL());
    for (IClasspathEntry classpathEntry : javaProject.getResolvedClasspath(true)) {
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IPath projectPath = classpathEntry.getPath();
            IProject otherProject = root.getProject(projectPath.segment(0));
            IJavaProject otherJavaProject = JavaCore.create(otherProject);
            urls.add(new File(otherProject.getLocation() + "/" //$NON-NLS-1$
                    + otherJavaProject.getOutputLocation().removeFirstSegments(1) + "/").toURI().toURL()); //$NON-NLS-1$
        } else if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            urls.add(new File(classpathEntry.getPath().toOSString()).toURI().toURL());
        }
    }
    if (parentClassLoader == null) {
        return new URLClassLoader(urls.toArray(new URL[urls.size()]));
    }
    return new URLClassLoader(urls.toArray(new URL[urls.size()]), parentClassLoader);
}

From source file:org.jboss.tools.jst.web.kb.internal.taglib.html.FileNameAttributeProvider.java

License:Open Source License

@Override
protected CustomTagLibAttribute getAttribute() {
    IFile file = context.getResource();/*from  w ww . j a va 2s .c  o  m*/
    IProject project = file.getProject();
    Set<IResource> ignore = new HashSet<IResource>();
    IResource[] sourceRoots = EclipseUtil.getJavaSourceRoots(project);
    if (sourceRoots != null && sourceRoots.length > 0) {
        for (IResource resource : sourceRoots) {
            ignore.add(resource);
        }
        IJavaProject javaProject = EclipseUtil.getJavaProject(project);
        try {
            IPath path = javaProject.getOutputLocation();
            IResource outputResource = project.getWorkspace().getRoot().findMember(path);
            if (outputResource != null) {
                ignore.add(outputResource);
            }
        } catch (JavaModelException e) {
            WebKbPlugin.getDefault().logError(e);
        }
    }

    List<String> result = new ArrayList<String>();
    IContainer parent = file.getParent();
    if (parent != null) {
        try {
            addParent(file, "", result, ignore);
        } catch (CoreException e) {
            WebKbPlugin.getDefault().logError(e);
        }
    }
    FileNameAttribute attribute = null;
    if (!result.isEmpty()) {
        String[] paths = result.toArray(new String[result.size()]);
        attribute = new FileNameAttribute(getAttributeName(), "", paths);
    }

    return attribute;
}

From source file:org.jboss.tools.maven.core.MavenCoreActivator.java

License:Open Source License

public static String getOutputDirectory(IJavaProject javaProject) throws CoreException {
    IPath path = javaProject.getOutputLocation();
    path = path.makeRelativeTo(javaProject.getPath());
    if (path == null) {
        return null;
    }/*from  w w w  . ja  v  a 2s .  c  om*/
    String value = path.toString();
    if (value.startsWith(SEPARATOR)) {
        return MavenCoreActivator.BASEDIR + path.toString();
    } else {
        return MavenCoreActivator.BASEDIR + SEPARATOR + path.toString();
    }
}

From source file:org.jboss.tools.seam.internal.core.project.facet.WtpUtils.java

License:Open Source License

public static IResource createSourceFolder(IProject project, IPath path, IPath exclude, IPath outputFolder) {
    IJavaProject javaProject;
    IClasspathEntry[] javaProjectEntries;
    IPath outputLocation;//from   w  ww .  j a  v a  2 s. c  om

    if (project == null || !project.exists()) {
        return null;
    }
    if (!project.isOpen()) {
        return null;
    }
    try {
        if (!project.hasNature(JavaCore.NATURE_ID))
            return null;

        javaProject = JavaCore.create(project);
        javaProjectEntries = javaProject.getRawClasspath();
        outputLocation = javaProject.getOutputLocation();

        IPath projPath = javaProject.getProject().getFullPath();
        IPath newSourceFolderPath = projPath.append(path);
        IPath excludeSourceFolderPath = projPath.append(exclude);

        ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(javaProjectEntries.length + 1);
        int projectEntryIndex = -1;

        for (int i = 0; i < javaProjectEntries.length; i++) {
            IClasspathEntry curr = javaProjectEntries[i];
            IClasspathEntry resolved = curr;
            if (resolved.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                try {
                    resolved = JavaCore.getResolvedClasspathEntry(resolved);
                } catch (AssertionFailedException e) {
                    continue;
                }
            }
            if (resolved.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                if (newSourceFolderPath.equals(resolved.getPath())) {
                    return null;
                }
                if (projPath.equals(resolved.getPath())) {
                    projectEntryIndex = i;
                }
                if (excludeSourceFolderPath.equals(resolved.getPath())) {
                    continue;
                }
            }
            newEntries.add(curr);
        }
        if (outputFolder != null) {
            CoreUtility.createDerivedFolder(project.getFolder(outputFolder), true, true,
                    new NullProgressMonitor());
        }
        IFolder newSourceFolder = javaProject.getProject().getFolder(path);
        if (!newSourceFolder.exists()) {
            CoreUtility.createFolder(newSourceFolder, true, true, new NullProgressMonitor());
        }

        IClasspathEntry newEntry = JavaCore.newSourceEntry(newSourceFolderPath, new Path[] {}, new Path[] {},
                outputFolder != null ? project.getFullPath().append(outputFolder) : null);

        if (projectEntryIndex != -1) {
            newEntries.set(projectEntryIndex, newEntry);
        } else {
            insertClasspathEntry(newEntry, newEntries);
        }

        IClasspathEntry[] newClasspathEntries = newEntries.toArray(new IClasspathEntry[newEntries.size()]);
        IPath newOutputLocation = outputLocation;

        IJavaModelStatus result = JavaConventions.validateClasspath(javaProject, newClasspathEntries,
                newOutputLocation);
        if (!result.isOK()) {
            if (outputLocation.equals(projPath)) {
                newOutputLocation = projPath.append(
                        PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
                result = JavaConventions.validateClasspath(javaProject, newClasspathEntries, newOutputLocation);
                if (!result.isOK()) {
                    return null;
                }
            } else {
                return null;
            }
        }

        javaProject.setRawClasspath(newClasspathEntries, newOutputLocation, new NullProgressMonitor());
        return newSourceFolder;
    } catch (CoreException e) {
        SeamCorePlugin.getPluginLog().logError(e);
    }
    return null;
}

From source file:org.jboss.tools.seam.internal.core.SeamResourceVisitor.java

License:Open Source License

void getJavaSourceRoots(IProject project) {
    IJavaProject javaProject = EclipseResourceUtil.getJavaProject(project);
    if (javaProject == null)
        return;//from  w w  w .j ava  2  s.co m
    List<IPath> ps = new ArrayList<IPath>();
    List<IPath> os = new ArrayList<IPath>();
    try {
        IPath output = javaProject.getOutputLocation();
        if (output != null)
            os.add(output);
        IClasspathEntry[] es = javaProject.getResolvedClasspath(true);
        for (int i = 0; i < es.length; i++) {
            if (es[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IResource findMember = ResourcesPlugin.getWorkspace().getRoot().findMember(es[i].getPath());
                if (findMember != null && findMember.exists()) {
                    ps.add(findMember.getFullPath());
                }
                IPath out = es[i].getOutputLocation();
                if (out != null && !os.contains(out)) {
                    os.add(out);
                }
            }
        }
        srcs = ps.toArray(new IPath[ps.size()]);
        outs = os.toArray(new IPath[os.size()]);
    } catch (CoreException ce) {
        ModelPlugin.getPluginLog().logError("Error while locating java source roots for " + project, ce);
    }
}

From source file:org.jboss.tools.ws.creation.core.commands.AbstractGenerateCodeCommand.java

License:Open Source License

protected void addCommonArgs(List<String> command, IJavaProject javaProject) throws Exception {
    String projectRoot = model.getJavaProject().getProject().getLocation().toOSString();
    command.add("-k"); //$NON-NLS-1$
    command.add("-s"); //$NON-NLS-1$
    command.add(JBossWSCreationUtils.getCustomSrcLocation(model.getJavaSourceFolder()));
    command.add("-o"); //$NON-NLS-1$
    StringBuffer opDir = new StringBuffer();
    opDir.append(projectRoot).append(Path.SEPARATOR)
            .append(javaProject.getOutputLocation().removeFirstSegments(1).toOSString());
    command.add(opDir.toString());/*from  ww w  .  j a v a2  s  . c  o  m*/
    if (model.getAddOptions() != null && !"".equals(model.getAddOptions())) { //$NON-NLS-1$
        String str = model.getAddOptions().trim();
        String[] strArray = str.split(" +"); //$NON-NLS-1$
        for (int i = 0; i < strArray.length; i++) {
            command.add(strArray[i]);
        }
    }
    if (model.getWsdlURI() != null) {
        command.add(model.getWsdlURI());
    }
}

From source file:org.jbpm.eclipse.util.ProjectClassLoader.java

License:Apache License

public static List<URL> getProjectClassPathURLs(IJavaProject project, List<String> alreadyLoadedProjects) {
    List<URL> pathElements = new ArrayList<URL>();
    try {/*from w  ww.  ja va2  s .  co m*/
        IClasspathEntry[] paths = project.getResolvedClasspath(true);
        Set<IPath> outputPaths = new HashSet<IPath>();
        if (paths != null) {
            for (int i = 0; i < paths.length; i++) {
                IClasspathEntry path = paths[i];
                if (path.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    URL url = getRawLocationURL(path.getPath());
                    pathElements.add(url);
                } else if (path.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath output = path.getOutputLocation();
                    if (path.getOutputLocation() != null) {
                        outputPaths.add(output);
                    }
                }
            }
        }
        IPath location = getProjectLocation(project.getProject());
        IPath outputPath = location.append(project.getOutputLocation().removeFirstSegments(1));
        pathElements.add(0, outputPath.toFile().toURI().toURL());
        for (IPath path : outputPaths) {
            outputPath = location.append(path.removeFirstSegments(1));
            pathElements.add(0, outputPath.toFile().toURI().toURL());
        }

        // also add classpath of required projects
        for (String projectName : project.getRequiredProjectNames()) {
            if (!alreadyLoadedProjects.contains(projectName)) {
                alreadyLoadedProjects.add(projectName);
                IProject reqProject = project.getProject().getWorkspace().getRoot().getProject(projectName);
                if (reqProject != null) {
                    IJavaProject reqJavaProject = JavaCore.create(reqProject);
                    pathElements.addAll(getProjectClassPathURLs(reqJavaProject, alreadyLoadedProjects));
                }
            }
        }
    } catch (JavaModelException e) {
        JBPMEclipsePlugin.log(e);
    } catch (MalformedURLException e) {
        JBPMEclipsePlugin.log(e);
    } catch (Throwable t) {
        JBPMEclipsePlugin.log(t);
    }
    return pathElements;
}