Example usage for org.eclipse.jdt.internal.core PackageFragmentRoot setSourceMapper

List of usage examples for org.eclipse.jdt.internal.core PackageFragmentRoot setSourceMapper

Introduction

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

Prototype

public void setSourceMapper(SourceMapper mapper) throws JavaModelException 

Source Link

Document

For use by AttachSourceOperation only.

Usage

From source file:jd.ide.eclipse.editors.JDClassFileEditor.java

License:Open Source License

@SuppressWarnings("rawtypes")
protected void setupSourceMapper(IClassFile classFile) {
    try {//  w  w w  .  j av a 2 s  . c o  m
        // Search package fragment root and classPath
        IJavaElement packageFragment = classFile.getParent();
        IJavaElement packageFragmentRoot = packageFragment.getParent();

        if (packageFragmentRoot instanceof PackageFragmentRoot) {
            // Setup a new source mapper.
            PackageFragmentRoot root = (PackageFragmentRoot) packageFragmentRoot;

            // Location of the archive file containing classes.
            IPath basePath = root.getPath();
            File baseFile = basePath.makeAbsolute().toFile();

            if (!baseFile.exists()) {
                IResource resource = root.getCorrespondingResource();
                basePath = resource.getLocation();
                baseFile = basePath.makeAbsolute().toFile();
            }

            // Class path
            String classPath = classFile.getElementName();
            String packageName = packageFragment.getElementName();
            if ((packageName != null) && (packageName.length() > 0))
                classPath = packageName.replace('.', '/') + '/' + classPath;

            // Location of the archive file containing source.
            IPath sourcePath = root.getSourceAttachmentPath();
            if (sourcePath == null)
                sourcePath = basePath;

            // Location of the package fragment root within the zip 
            // (empty specifies the default root).
            IPath sourceAttachmentRootPath = root.getSourceAttachmentRootPath();
            String sourceRootPath;

            if (sourceAttachmentRootPath == null) {
                sourceRootPath = null;
            } else {
                sourceRootPath = sourceAttachmentRootPath.toString();
                if ((sourceRootPath != null) && (sourceRootPath.length() == 0))
                    sourceRootPath = null;
            }

            // Options
            Map options = root.getJavaProject().getOptions(true);

            root.setSourceMapper(new JDSourceMapper(baseFile, sourcePath, sourceRootPath, options));
        }
    } catch (CoreException e) {
        JavaDecompilerPlugin.getDefault().getLog()
                .log(new Status(Status.ERROR, JavaDecompilerPlugin.PLUGIN_ID, 0, e.getMessage(), e));
    }
}