Example usage for org.eclipse.jdt.internal.core JavaModel getExternalTarget

List of usage examples for org.eclipse.jdt.internal.core JavaModel getExternalTarget

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core JavaModel getExternalTarget.

Prototype

public static Object getExternalTarget(IPath path, boolean checkResourceExistence) 

Source Link

Document

Helper method - returns either the linked IFolder or the File corresponding to the provided IPath .

Usage

From source file:org.eclipse.jdt.internal.core.JavaProject.java

License:Open Source License

/**
 * @param path IPath//from   w  w  w  .  j  a va 2s . c o m
 * @return A handle to the package fragment root identified by the given path.
 * This method is handle-only and the element may or may not exist. Returns
 * <code>null</code> if unable to generate a handle from the path (for example,
 * an absolute path that has less than 1 segment. The path may be relative or
 * absolute.
 */
public IPackageFragmentRoot getPackageFragmentRoot(IPath path) {
    if (!path.isAbsolute()) {
        path = getPath().append(path);
    }
    int segmentCount = path.segmentCount();
    if (segmentCount == 0) {
        return null;
    }
    if (path.getDevice() != null || JavaModel.getExternalTarget(path, true/*check existence*/) != null) {
        // external path
        return getPackageFragmentRoot0(path);
    }
    IWorkspaceRoot workspaceRoot = this.project.getWorkspace().getRoot();
    IResource resource = workspaceRoot.findMember(path);
    if (resource == null) {
        // resource doesn't exist in workspace
        if (path.getFileExtension() != null) {
            if (!workspaceRoot.getProject(path.segment(0)).exists()) {
                // assume it is an external ZIP archive
                return getPackageFragmentRoot0(path);
            } else {
                // assume it is an internal ZIP archive
                resource = workspaceRoot.getFile(path);
            }
        } else if (segmentCount == 1) {
            // assume it is a project
            String projectName = path.segment(0);
            if (getElementName().equals(projectName)) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75814
                // default root
                resource = this.project;
            } else {
                // lib being another project
                resource = workspaceRoot.getProject(projectName);
            }
        } else {
            // assume it is an internal folder
            resource = workspaceRoot.getFolder(path);
        }
    }
    return getPackageFragmentRoot(resource);
}