diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/ResourceUtils.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/ResourceUtils.java index eebdeeeaee..cc19c3c2b1 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/ResourceUtils.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/ResourceUtils.java @@ -254,10 +254,13 @@ public static String toGlobPattern(IPath path) { // file detection, at least on vscode globPattern = globPattern.replace(path.getDevice(), "**"); } - if (!globPattern.endsWith("/")) { - globPattern += "/"; + if (!globPattern.endsWith(".jar") && !globPattern.endsWith(".zip")) { + if (!globPattern.endsWith("/")) { + globPattern += "/"; + } + globPattern += "**"; } - return globPattern + "**"; + return globPattern; } } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/EclipseBuildSupport.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/EclipseBuildSupport.java index 8f0ab7530a..5212a43bca 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/EclipseBuildSupport.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/EclipseBuildSupport.java @@ -12,9 +12,18 @@ import java.util.Set; +import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.ls.core.internal.ProjectUtils; +import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager.CHANGE_TYPE; import com.google.common.collect.Sets; @@ -49,4 +58,31 @@ public boolean isBuildFile(IResource resource) { return false; } + @Override + public boolean fileChanged(IResource resource, CHANGE_TYPE changeType, IProgressMonitor monitor) throws CoreException { + if (resource == null || !applies(resource.getProject())) { + return false; + } + refresh(resource, changeType, monitor); + IProject project = resource.getProject(); + if (ProjectUtils.isJavaProject(project)) { + IJavaProject javaProject = JavaCore.create(project); + IClasspathEntry[] classpath = javaProject.getRawClasspath(); + for (IClasspathEntry entry : classpath) { + if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { + IPath path = entry.getPath(); + IFile r = ResourcesPlugin.getWorkspace().getRoot().getFile(path); + if (r != null && r.equals(resource)) { + IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); + javaProject.setRawClasspath(new IClasspathEntry[0], monitor); + javaProject.setRawClasspath(rawClasspath, monitor); + break; + } + } + } + } + return false; + } + + } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/InvisibleProjectBuildSupport.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/InvisibleProjectBuildSupport.java index d443185e1d..488dd39dca 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/InvisibleProjectBuildSupport.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/InvisibleProjectBuildSupport.java @@ -27,14 +27,7 @@ public class InvisibleProjectBuildSupport extends EclipseBuildSupport implements static final String LIB_FOLDER = "lib"; - private UpdateClasspathJob updateClasspathJob; - public InvisibleProjectBuildSupport() { - this(UpdateClasspathJob.getInstance()); - } - - public InvisibleProjectBuildSupport(UpdateClasspathJob updateClasspathJob) { - this.updateClasspathJob = updateClasspathJob; } @Override @@ -53,7 +46,7 @@ public boolean fileChanged(IResource resource, CHANGE_TYPE changeType, IProgress if (realFolderPath != null) { IPath libFolderPath = realFolderPath.append(LIB_FOLDER); if (libFolderPath.isPrefixOf(resource.getLocation())) { - updateClasspathJob.updateClasspath(JavaCore.create(invisibleProject), libFolderPath); + UpdateClasspathJob.getInstance().updateClasspath(JavaCore.create(invisibleProject), libFolderPath); } } return false; diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ProjectsManager.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ProjectsManager.java index 99084681f5..e42e8af8c7 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ProjectsManager.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ProjectsManager.java @@ -36,6 +36,7 @@ import java.util.stream.Stream; import org.codehaus.plexus.util.StringUtils; +import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; @@ -589,6 +590,16 @@ public List registerWatchers() { } } + if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { + IPath path = entry.getPath(); + IFile resource = ResourcesPlugin.getWorkspace().getRoot().getFile(path); + if (resource != null && !resource.isDerived()) { + IPath location = resource.getLocation(); + if (location != null && !isContainedIn(location, sources)) { + sources.add(location); + } + } + } } if (!ProjectUtils.isVisibleProject(project)) { //watch lib folder for invisible projects diff --git a/org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/.classpath b/org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/.classpath new file mode 100644 index 0000000000..cb995bbb4a --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/.project b/org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/.project new file mode 100644 index 0000000000..1b34b9b195 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/.project @@ -0,0 +1,17 @@ + + + updatejar + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/foo.jar b/org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/foo.jar new file mode 100644 index 0000000000..1a6a51ba16 Binary files /dev/null and b/org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/foo.jar differ diff --git a/org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/src/Test.java b/org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/src/Test.java new file mode 100644 index 0000000000..dde72cdf97 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/src/Test.java @@ -0,0 +1,10 @@ +import foo.bar; + +public class Test { + + public static void main(String[] args) { + int sum = bar.add(1, 2, 3); + System.out.println(sum); + } + +} \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/ResourceUtilsTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/ResourceUtilsTest.java index f00cf4cd1b..c4681af75e 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/ResourceUtilsTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/ResourceUtilsTest.java @@ -26,5 +26,6 @@ public void testToGlobPattern() { assertEquals("/foo/bar/**", toGlobPattern(Path.forPosix("/foo/bar/"))); assertEquals("**/foo/bar/**", toGlobPattern(Path.forWindows("c:/foo/bar/"))); assertEquals("**/foo/bar/**", toGlobPattern(Path.forWindows("c:\\foo\\bar"))); + assertEquals("/foo/bar/foo.jar", toGlobPattern(Path.forPosix("/foo/bar/foo.jar"))); } } diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/EclipseBuildSupportTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/EclipseBuildSupportTest.java new file mode 100644 index 0000000000..828d148145 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/EclipseBuildSupportTest.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2019 Red Hat Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.ls.core.internal.managers; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.util.List; + +import org.apache.commons.io.FileUtils; +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.jdt.ls.core.internal.ResourceUtils; +import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager.CHANGE_TYPE; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; + +/** + * @author snjeza + * + */ +@RunWith(MockitoJUnitRunner.class) +public class EclipseBuildSupportTest extends AbstractProjectsManagerBasedTest { + + @Test + public void testUpdateJar() throws Exception { + importProjects("eclipse/updatejar"); + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("updatejar"); + assertIsJavaProject(project); + List errors = ResourceUtils.getErrorMarkers(project); + assertEquals("Unexpected errors " + ResourceUtils.toString(errors), 2, errors.size()); + File projectFile = project.getLocation().toFile(); + File validFooJar = new File(projectFile, "foo.jar"); + File destLib = new File(projectFile, "lib"); + FileUtils.copyFileToDirectory(validFooJar, destLib); + File newJar = new File(destLib, "foo.jar"); + projectsManager.fileChanged(newJar.toPath().toUri().toString(), CHANGE_TYPE.CREATED); + waitForBackgroundJobs(); + errors = ResourceUtils.getErrorMarkers(project); + assertEquals("Unexpected errors " + ResourceUtils.toString(errors), 0, errors.size()); + + } + + +}