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

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

Introduction

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

Prototype

public static File getFile(Object target) 

Source Link

Document

Helper method - returns the File item if target is a file (i.e., the target returns true to File#isFile() .

Usage

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

License:Open Source License

public IClasspathEntry resolveVariableEntry(IClasspathEntry entry, boolean usePreviousSession) {

    if (entry.getEntryKind() != IClasspathEntry.CPE_VARIABLE)
        return entry;

    IPath resolvedPath = getResolvedVariablePath(entry.getPath(), usePreviousSession);
    if (resolvedPath == null)
        return null;
    // By passing a null reference path, we keep it relative to workspace root.
    resolvedPath = ClasspathEntry.resolveDotDot(null, resolvedPath);

    Object target = JavaModel.getTarget(resolvedPath, false);
    if (target == null)
        return null;

    // inside the workspace
    if (target instanceof IResource) {
        IResource resolvedResource = (IResource) target;
        switch (resolvedResource.getType()) {

        case IResource.PROJECT:
            // internal project
            return JavaCore.newProjectEntry(resolvedPath, entry.getAccessRules(), entry.combineAccessRules(),
                    entry.getExtraAttributes(), entry.isExported());
        case IResource.FILE:
            // internal binary archive
            return JavaCore.newLibraryEntry(resolvedPath,
                    getResolvedVariablePath(entry.getSourceAttachmentPath(), usePreviousSession),
                    getResolvedVariablePath(entry.getSourceAttachmentRootPath(), usePreviousSession),
                    entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
        case IResource.FOLDER:
            // internal binary folder
            return JavaCore.newLibraryEntry(resolvedPath,
                    getResolvedVariablePath(entry.getSourceAttachmentPath(), usePreviousSession),
                    getResolvedVariablePath(entry.getSourceAttachmentRootPath(), usePreviousSession),
                    entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
        }//from ww  w.jav  a2s  . co  m
    }
    if (target instanceof File) {
        File externalFile = JavaModel.getFile(target);
        if (externalFile != null) {
            // external binary archive
            return JavaCore.newLibraryEntry(resolvedPath,
                    getResolvedVariablePath(entry.getSourceAttachmentPath(), usePreviousSession),
                    getResolvedVariablePath(entry.getSourceAttachmentRootPath(), usePreviousSession),
                    entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
        } else {
            // non-existing file
            if (resolvedPath.isAbsolute()) {
                return JavaCore.newLibraryEntry(resolvedPath,
                        getResolvedVariablePath(entry.getSourceAttachmentPath(), usePreviousSession),
                        getResolvedVariablePath(entry.getSourceAttachmentRootPath(), usePreviousSession),
                        entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
            }
        }
    }
    return null;
}