Search in sources :

Example 1 with BuildPathManager

use of org.eclipse.m2e.jdt.internal.BuildPathManager in project eclipse.jdt.ls by eclipse.

the class MavenSourceDownloader method discoverSource.

@Override
public void discoverSource(IClassFile classFile, IProgressMonitor monitor) throws CoreException {
    if (classFile == null) {
        return;
    }
    IJavaElement element = classFile;
    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 file = ResourcesPlugin.getWorkspace().getRoot().getFile(fragment.getPath());
                IPath path = file.getFullPath();
                if (path == null || !path.toFile().exists()) {
                    path = file.getLocation();
                    if (path == null) {
                        return;
                    }
                }
                Boolean downloaded = downloadRequestsCache.getIfPresent(path.toString());
                if (downloaded == null) {
                    downloadRequestsCache.put(path.toString(), true);
                    ArtifactKey artifact = new MavenPropertiesIdentifier().identify(path, monitor);
                    if (artifact == null) {
                        artifact = new MavenCentralIdentifier().identify(path, monitor);
                    }
                    if (artifact != null) {
                        if (!ProjectUtils.isMavenProject(element.getJavaProject().getProject())) {
                            // see https://github.com/eclipse-m2e/m2e-core/commit/b547ecc358c990e182a5eaf8d36f121e43f4a8c9#diff-3967743078be6a24ba1e3ec28bfc22bdf2c88a740695411f6d20e2444fef042fR943
                            long lastModified;
                            try {
                                File artifactFile = DependencyUtil.getArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getClassifier());
                                lastModified = artifactFile.lastModified();
                            } catch (FileNotFoundException | CoreException e1) {
                                lastModified = -1;
                            }
                            if (lastModified > -1) {
                                try {
                                    File sources = DependencyUtil.getSources(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
                                    sources.setLastModified(lastModified - 1);
                                } catch (FileNotFoundException | CoreException e) {
                                // ignore
                                }
                                try {
                                    File javadoc = DependencyUtil.getJavadoc(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
                                    javadoc.setLastModified(lastModified - 1);
                                } catch (FileNotFoundException | CoreException e) {
                                // ignore
                                }
                            }
                        }
                        BuildPathManager buildpathManager = (BuildPathManager) MavenJdtPlugin.getDefault().getBuildpathManager();
                        buildpathManager.scheduleDownload(fragment, artifact, true, true);
                        JobHelpers.waitForDownloadSourcesJobs(MAX_TIME_MILLIS);
                    }
                }
                break;
            }
        }
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IFile(org.eclipse.core.resources.IFile) ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) IPath(org.eclipse.core.runtime.IPath) FileNotFoundException(java.io.FileNotFoundException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) CoreException(org.eclipse.core.runtime.CoreException) BuildPathManager(org.eclipse.m2e.jdt.internal.BuildPathManager) IClassFile(org.eclipse.jdt.core.IClassFile) File(java.io.File) IFile(org.eclipse.core.resources.IFile)

Aggregations

File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 IClassFile (org.eclipse.jdt.core.IClassFile)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)1 ArtifactKey (org.eclipse.m2e.core.embedder.ArtifactKey)1 BuildPathManager (org.eclipse.m2e.jdt.internal.BuildPathManager)1