Skip to content

Commit

Permalink
JAR files are not refreshed after they are updated
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza authored and fbricon committed May 1, 2019
1 parent 8b830d9 commit fafbf93
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -592,6 +593,16 @@ public List<FileSystemWatcher> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="lib/foo.jar"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>updatejar</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Binary file not shown.
10 changes: 10 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/eclipse/updatejar/src/Test.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
}
}
Original file line number Diff line number Diff line change
@@ -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<IMarker> 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());

}


}

0 comments on commit fafbf93

Please sign in to comment.