From 79c6bcd329fa074390809e3172f745cf9dbdfae7 Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Thu, 18 Apr 2019 21:06:42 +0200 Subject: [PATCH] JAR files are not refreshed after they are updated Signed-off-by: Snjezana Peco --- .../managers/EclipseBuildSupport.java | 32 ++++++++++ .../InvisibleProjectBuildSupport.java | 9 +-- .../internal/managers/ProjectsManager.java | 11 ++++ .../projects/eclipse/updatejar/.classpath | 7 +++ .../projects/eclipse/updatejar/.project | 17 ++++++ .../projects/eclipse/updatejar/foo.jar | Bin 0 -> 649 bytes .../projects/eclipse/updatejar/src/Test.java | 10 ++++ .../managers/EclipseBuildSupportTest.java | 55 ++++++++++++++++++ 8 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/.classpath create mode 100644 org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/.project create mode 100644 org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/foo.jar create mode 100644 org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/src/Test.java create mode 100644 org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/EclipseBuildSupportTest.java 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..79201c4b3e 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,17 @@ 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.managers.ProjectsManager.CHANGE_TYPE; import com.google.common.collect.Sets; @@ -49,4 +57,28 @@ 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(); + 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); + } + } + } + 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..86cf5c727a 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.exists() && !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 0000000000000000000000000000000000000000..1a6a51ba16b45503f8d52b67a898e2092c156ccf GIT binary patch literal 649 zcmWIWW@Zs#;Nak3&~1qHWk3QV3@i-3t|5-Po_=onzK(vLZmz*0dcJP|PBAci_C0gj z$6HtLBCofu*10q1HwPJ9F@Es$(NiXd0B?4VP4!Igq=80B0&xJ`g5X+I3wVLDY5Dp3 zNr^>z$vKI|#kzg_{g@mDj_u#Mzd@m%Wr_l45F_h#32s*@VPT;wOwLIeF$q@>HyaoH zU@`k87G`D^rY2_7kpGywW3&3~H#NqajrafESHFK6!?(Z<3kBau-!ErNN;|BurLCVSDsC!!_g_}o*UkEST3PfvQ_>9p)I zf9rK&}ks_EOlJaO^6>9R<= zu(2GRxB<&#%Fme#&XS(tZVjyKRMw!CyVTD%Vfz+vIKx`;zwe)rA;2iK^GUCz~R(WW>wY&-S;PT6FC7tK~oB zE{Ry4S8)xV9qn^7O;=g!UkIOW*aRo5k|4LX^p5q7I_|ud@6s2pz0s1YeEwc1>mFa9 zAZzP-P=wt2Ih`Slk%8d^5C?cOGKnxC;v6}$Kyi)=aHRlbQ$P`mYy&7_5g-xBM2Y4A UZ&o&tG!qau0_jgcM=>w}0P`#06aWAK literal 0 HcmV?d00001 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/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()); + + } + + +}