Example usage for org.eclipse.jdt.core IPackageFragmentRoot getSourceAttachmentPath

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getSourceAttachmentPath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getSourceAttachmentPath.

Prototype

IPath getSourceAttachmentPath() throws JavaModelException;

Source Link

Document

Returns the absolute path to the source archive attached to this package fragment root's binary archive.

Usage

From source file:it.unibz.instasearch.indexing.WorkspaceIndexerJDT.java

License:Open Source License

private List<IPackageFragmentRoot> getJars(IJavaModel javaModel) throws JavaModelException {

    IJavaProject[] projects = javaModel.getJavaProjects();
    TreeSet<String> jarNames = new TreeSet<String>();
    LinkedList<IPackageFragmentRoot> jars = new LinkedList<IPackageFragmentRoot>();

    for (IJavaProject javaProj : projects) {

        IPackageFragmentRoot[] roots = javaProj.getPackageFragmentRoots();

        for (IPackageFragmentRoot root : roots) {
            if (root.isArchive() && root.getSourceAttachmentPath() != null) {

                String name = root.getElementName();

                if (!jarNames.contains(name)) {
                    jarNames.add(name);//w w  w . ja  va2 s  .  c  o m
                    jars.add(root);
                }
            }
        }
    }

    return jars;
}

From source file:it.unibz.instasearch.indexing.WorkspaceIndexerJDT.java

License:Open Source License

/**
 * @param doc//from   w  ww.ja v a2  s .  c  o m
 * @return
 * @throws JavaModelException 
 */
private IStorage getNonJavaResource(SearchResultDoc doc) throws JavaModelException {

    IWorkspaceRoot workspaceRoot = InstaSearchPlugin.getWorkspaceRoot();
    IJavaModel javaModel = JavaCore.create(workspaceRoot);

    String javaProjectName = doc.getProject().segment(0);
    IJavaProject javaProj = javaModel.getJavaProject(javaProjectName);

    if (!javaProj.isOpen())
        javaProj.open(new NullProgressMonitor());

    javaModel.refreshExternalArchives(new IJavaElement[] { javaProj }, new NullProgressMonitor());

    String jarName = doc.getJarName();

    IPackageFragmentRoot[] roots = javaProj.getPackageFragmentRoots();
    IPackageFragmentRoot jar = null;

    for (IPackageFragmentRoot root : roots) {
        if (root.isArchive() && root.getSourceAttachmentPath() != null) {

            String name = root.getElementName();
            if (name.equals(jarName)) {
                jar = root;
                break;
            }
        }
    }

    if (jar == null)
        return null;

    String filePath = doc.getFilePath();
    IPath path = new Path(filePath);

    IJarEntryResource res = null;
    for (String segment : path.segments()) {
        if (res == null)
            res = findJarEntry(jar, segment);
        else
            res = findJarEntry(res.getChildren(), segment);
    }

    return res;
}

From source file:org.eclim.plugin.jdt.command.search.SearchCommand.java

License:Open Source License

/**
 * Creates a Position from the supplied SearchMatch.
 *
 * @param project The project searching from.
 * @param match The SearchMatch.//from w w  w. j  av  a  2s. c  o m
 * @return The Position.
 */
protected Position createPosition(IProject project, SearchMatch match) throws Exception {
    IJavaElement element = (IJavaElement) match.getElement();
    IJavaElement parent = JavaUtils.getPrimaryElement(element);

    String file = null;
    String elementName = JavaUtils.getFullyQualifiedName(parent);
    if (parent.getElementType() == IJavaElement.CLASS_FILE) {
        IResource resource = parent.getResource();
        // occurs with a referenced project as a lib with no source and class
        // files that are not archived in that project
        if (resource != null && resource.getType() == IResource.FILE && !isJarArchive(resource.getLocation())) {
            file = resource.getLocation().toOSString();

        } else {
            IPath path = null;
            IPackageFragmentRoot root = (IPackageFragmentRoot) parent.getParent().getParent();
            resource = root.getResource();
            if (resource != null) {
                if (resource.getType() == IResource.PROJECT) {
                    path = ProjectUtils.getIPath((IProject) resource);
                } else {
                    path = resource.getLocation();
                }
            } else {
                path = root.getPath();
            }

            String classFile = elementName.replace('.', File.separatorChar);
            if (isJarArchive(path)) {
                file = "jar:file://" + path.toOSString() + '!' + classFile + ".class";
            } else {
                file = path.toOSString() + '/' + classFile + ".class";
            }

            // android injects its jdk classes, so filter those out if the project
            // doesn't have the android nature.
            if (ANDROID_JDK_URL.matcher(file).matches() && project != null
                    && !project.hasNature(ANDROID_NATURE)) {
                return null;
            }

            // if a source path attachment exists, use it.
            IPath srcPath = root.getSourceAttachmentPath();
            if (srcPath != null) {
                String rootPath;
                IProject elementProject = root.getJavaProject().getProject();

                // determine if src path is project relative or file system absolute.
                if (srcPath.isAbsolute() && elementProject.getName().equals(srcPath.segment(0))) {
                    rootPath = ProjectUtils.getFilePath(elementProject, srcPath.toString());
                } else {
                    rootPath = srcPath.toOSString();
                }
                String srcFile = FileUtils.toUrl(rootPath + File.separator + classFile + ".java");

                // see if source file exists at source path.
                FileSystemManager fsManager = VFS.getManager();
                FileObject fileObject = fsManager.resolveFile(srcFile.replace("%", "%25"));
                if (fileObject.exists()) {
                    file = srcFile;

                    // jdk sources on osx are under a "src/" dir in the jar
                } else if (Os.isFamily(Os.FAMILY_MAC)) {
                    srcFile = FileUtils
                            .toUrl(rootPath + File.separator + "src" + File.separator + classFile + ".java");
                    fileObject = fsManager.resolveFile(srcFile.replace("%", "%25"));
                    if (fileObject.exists()) {
                        file = srcFile;
                    }
                }
            }
        }
    } else {
        IPath location = match.getResource().getLocation();
        file = location != null ? location.toOSString() : null;
    }

    elementName = JavaUtils.getFullyQualifiedName(element);
    return Position.fromOffset(file.replace('\\', '/'), elementName, match.getOffset(), match.getLength());
}

From source file:org.eclipse.che.plugin.maven.server.core.classpath.ClasspathManager.java

License:Open Source License

public boolean downloadSources(String projectPath, String fqn) {
    IJavaProject javaProject = JavaModelManager.getJavaModelManager().getJavaModel()
            .getJavaProject(projectPath);
    try {/*from   ww  w.j  a  v  a  2s . co m*/
        IType type = javaProject.findType(fqn);
        if (type != null && type.isBinary()) {
            IClassFile classFile = type.getClassFile();
            if (classFile.getSourceRange() == null) {
                IJavaElement element = classFile;
                while (element.getParent() != null) {
                    element = element.getParent();
                    if (element instanceof IPackageFragmentRoot) {
                        IPackageFragmentRoot root = (IPackageFragmentRoot) element;

                        if (root.getSourceAttachmentPath() == null) {
                            return downloadSources(root);
                        }
                    }
                }
            }
        }

    } catch (JavaModelException e) {
        LOG.error(e.getMessage(), e);
    }
    return false;
}

From source file:org.eclipse.m2e.jdt.internal.DownloadSourcesActionDelegate.java

License:Open Source License

public void setActiveEditor(IAction action, IEditorPart part) {

    if (part != null) {
        try {/*  ww w.  j  a v a2s  .  c  om*/
            IClasspathManager buildpathManager = MavenJdtPlugin.getDefault().getBuildpathManager();

            IClassFileEditorInput input = (IClassFileEditorInput) part.getEditorInput();
            IJavaElement element = input.getClassFile();
            while (element.getParent() != null) {
                element = element.getParent();
                if (element instanceof IPackageFragmentRoot) {
                    IPackageFragmentRoot root = (IPackageFragmentRoot) element;

                    if (root.getSourceAttachmentPath() != null) {
                        // do nothing if sources attached already
                        break;
                    }

                    buildpathManager.scheduleDownload(root, true/*sources*/, false/*javadoc*/);
                }
            }
        } catch (Exception ex) {
            log.error("Could not schedule source download", ex); //$NON-NLS-1$
        }
    }
}

From source file:org.eclipse.m2e.jdt.ui.internal.DownloadSourcesActionDelegate.java

License:Open Source License

public void setActiveEditor(IAction action, IEditorPart part) {

    if (part != null && part.getEditorInput() instanceof IClassFileEditorInput) {
        try {//from  w  w  w . ja v  a2s . c om
            IClasspathManager buildpathManager = MavenJdtPlugin.getDefault().getBuildpathManager();
            IClassFileEditorInput input = (IClassFileEditorInput) part.getEditorInput();
            IJavaElement element = input.getClassFile();
            while (element.getParent() != null) {
                element = element.getParent();
                if (element instanceof IPackageFragmentRoot) {
                    IPackageFragmentRoot root = (IPackageFragmentRoot) element;

                    if (root.getSourceAttachmentPath() != null) {
                        // do nothing if sources attached already
                        break;
                    }

                    buildpathManager.scheduleDownload(root, true/*sources*/, false/*javadoc*/);
                }
            }
        } catch (Exception ex) {
            log.error("Could not schedule source download", ex); //$NON-NLS-1$
        }
    }
}

From source file:org.eclipse.pde.internal.ui.views.plugins.PluginsLabelProvider.java

License:Open Source License

public Image getImage(Object obj) {
    if (obj instanceof IPluginModelBase) {
        return getImage((IPluginModelBase) obj);
    }/*w  w w.  j a  v  a  2  s  . c om*/

    if (obj instanceof FileAdapter) {
        return getImage((FileAdapter) obj);
    }

    if (obj instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) obj;
        boolean hasSource = false;

        try {
            hasSource = root.getSourceAttachmentPath() != null;
        } catch (JavaModelException e) {
        }
        return JavaUI.getSharedImages()
                .getImage(hasSource ? org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE_WITH_SOURCE
                        : org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE);
    }

    if (obj instanceof IPackageFragment) {
        return JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_PACKAGE);
    }

    if (obj instanceof ICompilationUnit) {
        return JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_CUNIT);
    }

    if (obj instanceof IClassFile) {
        return JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_CFILE);
    }

    if (obj instanceof IStorage) {
        if (obj instanceof IJarEntryResource && !((IJarEntryResource) obj).isFile())
            return folderImage;
        return getFileImage(((IStorage) obj).getName());
    }

    return null;
}

From source file:org.eclipse.recommenders.jdt.JavaElementsFinder.java

License:Open Source License

public static boolean hasSourceAttachment(IPackageFragmentRoot fragmentRoot) {
    try {//from   www .j a v a  2  s .co m
        return fragmentRoot.getSourceAttachmentPath() != null;
    } catch (Exception e) {
        log(ERROR_CANNOT_FETCH_SOURCE_ATTACHMENT_PATH, e, fragmentRoot);
        return false;
    }
}

From source file:org.eclipse.xtext.common.types.ui.trace.TraceForTypeRootProvider.java

License:Open Source License

protected IPath getSourcePath(final ITypeRoot derivedJavaType) {
    IJavaElement current = derivedJavaType.getParent();
    while (current != null) {
        if (current instanceof IPackageFragmentRoot) {
            IPackageFragmentRoot fragmentRoot = (IPackageFragmentRoot) current;
            try {
                IPath attachmentPath = fragmentRoot.getSourceAttachmentPath();
                if (attachmentPath != null)
                    return attachmentPath;
            } catch (JavaModelException e) {
            }/*from  w  w w  . j  a va  2  s .c o  m*/
            if (current instanceof JarPackageFragmentRoot)
                return fragmentRoot.getPath();

        }
        current = current.getParent();
    }
    return null;
}

From source file:org.jboss.tools.as.sourcelookup.ui.actions.AttachSourcesActionDelegate.java

License:Open Source License

@Override
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
    if (targetEditor != null) {
        try {//  www.  j  a va2s .  c  o m
            boolean isAuto = SourceLookupActivator.getDefault().isAutoAddSourceContainer();
            if (!isAuto) {
                return;
            }
            IClassFileEditorInput input = (IClassFileEditorInput) targetEditor.getEditorInput();
            IJavaElement element = input.getClassFile();
            while (element.getParent() != null) {
                element = element.getParent();
                if (element instanceof IPackageFragmentRoot) {
                    final IPackageFragmentRoot fragment = (IPackageFragmentRoot) element;
                    IPath attachmentPath = fragment.getSourceAttachmentPath();
                    if (attachmentPath != null && !attachmentPath.isEmpty()
                            && attachmentPath.toFile().exists()) {
                        break;
                    }
                    if (fragment.isArchive()) {
                        IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(fragment.getPath());
                        File file = iFile == null || iFile.getLocation() == null ? fragment.getPath().toFile()
                                : iFile.getLocation().toFile();
                        ZipFile jar = new ZipFile(file);
                        final ArtifactKey artifact = JBossSourceContainer.getArtifact(file, jar);
                        if (artifact != null) {
                            IPath sourcePath = JBossSourceContainer.getSourcePath(artifact);
                            if (sourcePath == null || !sourcePath.toFile().exists()) {
                                Job job = JBossSourceContainer.downloadArtifact(file, artifact);
                                job.addJobChangeListener(new IJobChangeListener() {

                                    @Override
                                    public void sleeping(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void scheduled(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void running(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void done(IJobChangeEvent event) {
                                        IPath sourcePath = JBossSourceContainer.getSourcePath(artifact);
                                        if (sourcePath != null && sourcePath.toFile().exists()) {
                                            attachSource(fragment, sourcePath);
                                        }
                                    }

                                    @Override
                                    public void awake(IJobChangeEvent event) {
                                    }

                                    @Override
                                    public void aboutToRun(IJobChangeEvent event) {
                                    }
                                });
                                job.schedule();
                            } else {
                                attachSource(fragment, sourcePath);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            SourceLookupUIActivator.log(e);
        }
    }
}