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:cn.ieclipse.pde.signer.handler.SignHandler.java

License:Apache License

private void openProject(IProject prj, IJavaProject jprj) {
    try {//from  w  ww.j  av  a 2  s .c o m
        if (prj.hasNature("com.android.ide.eclipse.adt.AndroidNature")) { //$NON-NLS-1$
            IPath p = prj.getLocation();
            if (jprj == null) {
                jprj = JavaCore.create(prj);
            }
            IPath bin = jprj.getOutputLocation().removeFirstSegments(1).removeLastSegments(1);
            File file = new File(p.append(bin).toOSString());
            File apk = new File(file, prj.getName() + Const.EXT_APK);
            openAndroid(apk.getAbsolutePath());
        } else if (prj.hasNature("org.eclipse.pde.UpdateSiteNature")) { //$NON-NLS-1$
            openPlugin(prj.getLocation().toOSString());
        } else {
            openJar(null);
        }
    } catch (CoreException e) {
        // TODO
    }
}

From source file:cn.ieclipse.pde.signer.popup.actions.SignAction.java

License:Apache License

/**
 * @see IActionDelegate#run(IAction)//from ww w .ja v  a 2  s .  c  o m
 */
public void run(IAction action) {
    if (selection instanceof IStructuredSelection) {
        Object obj = ((IStructuredSelection) selection).getFirstElement();
        // String path = null;
        // java project.
        if (obj instanceof IProject) {
            IProject prj = (IProject) obj;
            try {
                if (prj.hasNature("com.android.ide.eclipse.adt.AndroidNature")) {

                    IJavaProject jprj = JavaCore.create(prj);
                    String bin = jprj.getOutputLocation().toOSString();
                    File apk = new File(new File(bin).getParentFile(), prj.getName() + Const.EXT_APK);
                    openAndroid(apk.getAbsolutePath());
                } else if (prj.hasNature("org.eclipse.pde.UpdateSiteNature")) {
                    openPlugin(prj.getLocation().toOSString());
                } else {
                    openJar(null);
                }
            } catch (CoreException e) {
                // TODO
            }
        } else if (obj instanceof IResource) {
            IResource resource = (IResource) obj;
            String file = resource.getLocation().toOSString();
            if (file != null) {
                if (file.endsWith(Const.EXT_JAR)) {
                    openJar(file);
                } else if (file.endsWith(Const.EXT_APK)) {
                    openAndroid(file);
                } else {
                    openJar(null);
                }
            } else {
                openJar(null);
            }
        } else {
            openJar(null);
        }
    }
}

From source file:co.turnus.widgets.util.WidgetsUtils.java

License:Open Source License

/**
 * Returns the qualified name of the given file, i.e. qualified.name.of.File
 * for <code>/project/sourceFolder/qualified/name/of/File.fileExt</code> or
 * <code>/project/outputFolder/qualified/name/of/File.fileExt</code>.
 * /*from   w w  w  . ja v a2  s . c om*/
 * @param file
 *            a file
 * @return a qualified name, or <code>null</code> if the file is not in a
 *         source folder
 */
public static String getQualifiedName(IFile file) {
    IProject project = file.getProject();

    IJavaProject javaProject = JavaCore.create(project);
    if (!javaProject.exists()) {
        return null;
    }

    try {
        IPath path = file.getParent().getFullPath();
        IPackageFragment fragment = null;
        if (javaProject.getOutputLocation().isPrefixOf(path)) {
            // create relative path
            int count = path.matchingFirstSegments(javaProject.getOutputLocation());
            IPath relPath = path.removeFirstSegments(count);

            // creates full path to source
            for (IFolder folder : getSourceFolders(project)) {
                path = folder.getFullPath().append(relPath);
                fragment = javaProject.findPackageFragment(path);
                if (fragment != null) {
                    break;
                }
            }
        } else {
            fragment = javaProject.findPackageFragment(path);
        }

        if (fragment == null) {
            return null;
        }

        String name = file.getFullPath().removeFileExtension().lastSegment();
        if (fragment.isDefaultPackage()) {
            // handles the default package case
            return name;
        }
        return fragment.getElementName() + "." + name;
    } catch (JavaModelException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.amashchenko.eclipse.strutsclipse.StrutsXmlHyperlinkDetector.java

License:Apache License

private List<IHyperlink> createResultLocationLinks(final IDocument document, final String elementValue,
        final IRegion elementRegion, final String typeAttrValue, final String namespaceParamValue) {
    final List<IHyperlink> links = new ArrayList<IHyperlink>();

    // assume that default is dispatcher for now, TODO improve
    // that//  w w w  .jav  a  2 s . co m
    if (typeAttrValue == null || StrutsXmlConstants.DISPATCHER_RESULT.equals(typeAttrValue)
            || StrutsXmlConstants.FREEMARKER_RESULT.equals(typeAttrValue)) {
        IProject project = ProjectUtil.getCurrentProject(document);
        if (project != null && project.exists()) {
            IVirtualComponent rootComponent = ComponentCore.createComponent(project);
            final IVirtualFolder rootFolder = rootComponent.getRootFolder();
            IPath path = rootFolder.getProjectRelativePath().append(elementValue);

            IFile file = project.getFile(path);
            if (file.exists()) {
                links.add(new FileHyperlink(elementRegion, file));
            }
        }
    } else if (StrutsXmlConstants.REDIRECT_ACTION_RESULT.equals(typeAttrValue)) {
        Set<String> namespaces = new HashSet<String>();

        // if there is a namespaceParamValue then used it, else get
        // namespace from parent package
        String namespace = namespaceParamValue;
        if (namespace == null) {
            TagRegion packageTagRegion = strutsXmlParser.getParentTagRegion(document, elementRegion.getOffset(),
                    StrutsXmlConstants.PACKAGE_TAG);
            if (packageTagRegion != null) {
                namespace = packageTagRegion.getAttrValue(StrutsXmlConstants.NAMESPACE_ATTR, "");
            } else {
                namespace = "";
            }

            // if namespace came NOT from namespaceParamValue then add
            // special namespaces
            namespaces.add("");
            namespaces.add("/");
        }

        namespaces.add(namespace);

        IRegion region = strutsXmlParser.getActionRegion(document, namespaces, elementValue);
        if (region != null) {
            ITextFileBuffer textFileBuffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(document);
            if (textFileBuffer != null) {
                IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(textFileBuffer.getLocation());
                if (file.exists()) {
                    links.add(new FileHyperlink(elementRegion, file, region));
                }
            }
        }
    } else if (StrutsXmlConstants.TILES_RESULT.equals(typeAttrValue)) {
        try {
            final IDocumentProvider provider = new TextFileDocumentProvider();
            final IJavaProject javaProject = ProjectUtil.getCurrentJavaProject(document);
            if (javaProject != null && javaProject.exists()) {
                final IProject project = javaProject.getProject();
                final String outputFolder = javaProject.getOutputLocation()
                        .makeRelativeTo(project.getFullPath()).segment(0);
                project.accept(new IResourceVisitor() {
                    @Override
                    public boolean visit(IResource resource) throws CoreException {
                        // don't visit output folder
                        if (resource.getType() == IResource.FOLDER
                                && resource.getProjectRelativePath().segment(0).equals(outputFolder)) {
                            return false;
                        }
                        if (resource.isAccessible() && resource.getType() == IResource.FILE
                                && "xml".equalsIgnoreCase(resource.getFileExtension()) && resource.getName()
                                        .toLowerCase(Locale.ROOT).contains(StrutsXmlConstants.TILES_RESULT)) {
                            provider.connect(resource);
                            IDocument document = provider.getDocument(resource);
                            provider.disconnect(resource);

                            IRegion region = tilesXmlParser.getDefinitionRegion(document, elementValue);
                            if (region != null) {
                                IFile file = project.getFile(resource.getProjectRelativePath());
                                if (file.exists()) {
                                    links.add(new FileHyperlink(elementRegion, file, region));
                                }
                            }
                        }
                        return true;
                    }
                });
            }
        } catch (CoreException e) {
            e.printStackTrace();
        }
    }
    return links;
}

From source file:com.android.ide.eclipse.adt.build.ApkBuilder.java

License:Open Source License

/**
 * Returns the list of the output folders for the specified {@link IJavaProject} objects, if
 * they are Android projects./* www. j a v  a  2s. c  o  m*/
 * 
 * @param referencedJavaProjects the java projects.
 * @return an array, always. Can be empty.
 * @throws CoreException
 */
private String[] getProjectOutputs(IJavaProject[] referencedJavaProjects) throws CoreException {
    ArrayList<String> list = new ArrayList<String>();

    IWorkspace ws = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot wsRoot = ws.getRoot();

    for (IJavaProject javaProject : referencedJavaProjects) {
        // only include output from non android referenced project
        // (This is to handle the case of reference Android projects in the context of 
        // instrumentation projects that need to reference the projects to be tested).
        if (javaProject.getProject().hasNature(AndroidConstants.NATURE) == false) {
            // get the output folder
            IPath path = null;
            try {
                path = javaProject.getOutputLocation();
            } catch (JavaModelException e) {
                continue;
            }

            IResource outputResource = wsRoot.findMember(path);
            if (outputResource != null && outputResource.getType() == IResource.FOLDER) {
                String outputOsPath = outputResource.getLocation().toOSString();

                list.add(outputOsPath);
            }
        }
    }

    return list.toArray(new String[list.size()]);
}

From source file:com.android.ide.eclipse.adt.internal.actions.MultiApkExportAction.java

License:Open Source License

/**
 * Builds a particular variant of an APK
 * @param wsRoot the workspace root//from   ww  w.jav  a  2  s  .c om
 * @param projectState the project to export
 * @param appPackage the application package
 * @param versionCode the major version code.
 * @param apk the {@link ApkData} describing how the export should happen.
 * @param softVariant an optional soft variant info. The entry contains (name, resource filter).
 * @param binFolder the binFolder where the file must be created.
 * @throws CoreException
 */
private void buildVariant(IWorkspaceRoot wsRoot, ProjectState projectState, String appPackage, int versionCode,
        ApkData apk, Entry<String, String> softVariant, IFolder binFolder, AndroidPrintStream stdout,
        AndroidPrintStream stderr) throws CoreException {
    try {
        // get the libraries for this project
        List<IProject> libProjects = projectState.getFullLibraryProjects();

        IProject project = projectState.getProject();
        IJavaProject javaProject = JavaCore.create(project);

        int compositeVersionCode = apk.getCompositeVersionCode(versionCode);

        // figure out the file names
        String pkgName = project.getName() + "-" + apk.getBuildInfo();
        String finalNameRoot = appPackage + "-" + compositeVersionCode;
        if (softVariant != null) {
            String tmp = "-" + softVariant.getKey();
            pkgName += tmp;
            finalNameRoot += tmp;
        }

        pkgName += ".ap_";
        String outputName = finalNameRoot + "-unsigned.apk";

        BuildHelper helper = new BuildHelper(project, stdout, stderr, false /*debugMode*/, false/*verbose*/);

        // get the manifest file
        IFile manifestFile = project.getFile(SdkConstants.FN_ANDROID_MANIFEST_XML);
        // get the project bin folder
        IFolder projectBinFolder = wsRoot.getFolder(javaProject.getOutputLocation());
        String projectBinFolderPath = projectBinFolder.getLocation().toOSString();

        // package the resources
        helper.packageResources(manifestFile, libProjects, softVariant != null ? softVariant.getValue() : null,
                compositeVersionCode, projectBinFolderPath, pkgName);

        apk.setOutputName(softVariant != null ? softVariant.getKey() : null, outputName);

        // do the final export.
        IFile dexFile = projectBinFolder.getFile(SdkConstants.FN_APK_CLASSES_DEX);
        String outputFile = binFolder.getFile(outputName).getLocation().toOSString();

        // get the list of referenced projects.
        List<IProject> javaRefs = ProjectHelper.getReferencedProjects(project);
        List<IJavaProject> referencedJavaProjects = BuildHelper.getJavaProjects(javaRefs);

        helper.finalPackage(new File(projectBinFolderPath, pkgName).getAbsolutePath(),
                dexFile.getLocation().toOSString(), outputFile, javaProject, libProjects,
                referencedJavaProjects, apk.getAbi(), null, //key
                null, //certificate
                null); //ResourceMarker
    } catch (CoreException e) {
        throw e;
    } catch (Exception e) {
        throw new CoreException(new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID, e.getMessage(), e));
    }

}

From source file:com.android.ide.eclipse.adt.internal.build.ApkBuilder.java

License:Open Source License

/**
 * Returns the list of the output folders for the specified {@link IJavaProject} objects, if
 * they are Android projects.// ww  w  . ja  v a  2 s . c o m
 *
 * @param referencedJavaProjects the java projects.
 * @return an array, always. Can be empty.
 * @throws CoreException
 */
private String[] getProjectOutputs(IJavaProject[] referencedJavaProjects) throws CoreException {
    ArrayList<String> list = new ArrayList<String>();

    IWorkspace ws = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot wsRoot = ws.getRoot();

    for (IJavaProject javaProject : referencedJavaProjects) {
        // only include output from non android referenced project
        // (This is to handle the case of reference Android projects in the context of
        // instrumentation projects that need to reference the projects to be tested).
        if (javaProject.getProject().hasNature(AndroidConstants.NATURE) == false) {
            // get the output folder
            IPath path = null;
            try {
                path = javaProject.getOutputLocation();
            } catch (JavaModelException e) {
                continue;
            }

            IResource outputResource = wsRoot.findMember(path);
            if (outputResource != null && outputResource.getType() == IResource.FOLDER) {
                String outputOsPath = outputResource.getLocation().toOSString();

                list.add(outputOsPath);
            }
        }
    }

    return list.toArray(new String[list.size()]);
}

From source file:com.android.ide.eclipse.adt.internal.build.BuildHelper.java

License:Open Source License

/**
 * Computes all the project output and dependencies that must go into building the apk.
 *
 * @param resMarker/*from  www .j  a  v  a2 s.  co  m*/
 * @throws CoreException
 */
private void gatherPaths(ResourceMarker resMarker) throws CoreException {
    IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();

    // get a java project for the project.
    IJavaProject javaProject = JavaCore.create(mProject);

    // get the output of the main project
    IPath path = javaProject.getOutputLocation();
    IResource outputResource = wsRoot.findMember(path);
    if (outputResource != null && outputResource.getType() == IResource.FOLDER) {
        mCompiledCodePaths.add(outputResource.getLocation().toOSString());
    }

    // we could use IJavaProject.getResolvedClasspath directly, but we actually
    // want to see the containers themselves.
    IClasspathEntry[] classpaths = javaProject.readRawClasspath();
    if (classpaths != null) {
        for (IClasspathEntry e : classpaths) {
            // ignore non exported entries, unless they're in the DEPEDENCIES container,
            // in which case we always want it (there may be some older projects that
            // have it as non exported).
            if (e.isExported() || (e.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                    && e.getPath().toString().equals(AdtConstants.CONTAINER_DEPENDENCIES))) {
                handleCPE(e, javaProject, wsRoot, resMarker);
            }
        }
    }
}

From source file:com.android.ide.eclipse.adt.internal.build.BuildHelper.java

License:Open Source License

private void handleCPE(IClasspathEntry entry, IJavaProject javaProject, IWorkspaceRoot wsRoot,
        ResourceMarker resMarker) {//  ww w .j a v  a2  s  .  com

    // if this is a classpath variable reference, we resolve it.
    if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
        entry = JavaCore.getResolvedClasspathEntry(entry);
    }

    if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
        IProject refProject = wsRoot.getProject(entry.getPath().lastSegment());
        try {
            // ignore if it's an Android project, or if it's not a Java Project
            if (refProject.hasNature(JavaCore.NATURE_ID)
                    && refProject.hasNature(AdtConstants.NATURE_DEFAULT) == false) {
                IJavaProject refJavaProject = JavaCore.create(refProject);

                // get the output folder
                IPath path = refJavaProject.getOutputLocation();
                IResource outputResource = wsRoot.findMember(path);
                if (outputResource != null && outputResource.getType() == IResource.FOLDER) {
                    mCompiledCodePaths.add(outputResource.getLocation().toOSString());
                }
            }
        } catch (CoreException exception) {
            // can't query the project nature? ignore
        }

    } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
        handleClasspathLibrary(entry, wsRoot, resMarker);
    } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
        // get the container
        try {
            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), javaProject);
            // ignore the system and default_system types as they represent
            // libraries that are part of the runtime.
            if (container != null && container.getKind() == IClasspathContainer.K_APPLICATION) {
                IClasspathEntry[] entries = container.getClasspathEntries();
                for (IClasspathEntry cpe : entries) {
                    handleCPE(cpe, javaProject, wsRoot, resMarker);
                }
            }
        } catch (JavaModelException jme) {
            // can't resolve the container? ignore it.
            AdtPlugin.log(jme, "Failed to resolve ClasspathContainer: %s", entry.getPath());
        }
    }
}

From source file:com.android.ide.eclipse.adt.internal.build.PostCompilerHelper.java

License:Open Source License

/**
 * Returns the list of the output folders for the specified {@link IJavaProject} objects, if
 * they are Android projects.//from   w w w  . j  a v a2s.co  m
 *
 * @param referencedJavaProjects the java projects.
 * @return an array, always. Can be empty.
 * @throws CoreException
 */
private String[] getProjectOutputs(IJavaProject[] referencedJavaProjects) throws CoreException {
    ArrayList<String> list = new ArrayList<String>();

    IWorkspace ws = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot wsRoot = ws.getRoot();

    for (IJavaProject javaProject : referencedJavaProjects) {
        // only include output from non android referenced project
        // (This is to handle the case of reference Android projects in the context of
        // instrumentation projects that need to reference the projects to be tested).
        if (javaProject.getProject().hasNature(AndroidConstants.NATURE_DEFAULT) == false) {
            // get the output folder
            IPath path = null;
            try {
                path = javaProject.getOutputLocation();
            } catch (JavaModelException e) {
                continue;
            }

            IResource outputResource = wsRoot.findMember(path);
            if (outputResource != null && outputResource.getType() == IResource.FOLDER) {
                String outputOsPath = outputResource.getLocation().toOSString();

                list.add(outputOsPath);
            }
        }
    }

    return list.toArray(new String[list.size()]);
}