Skip to content

Cleanup javac on remove/rename java class #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/org/javacs/JavaCompilerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void loadCompile(Collection<? extends JavaFileObject> sources) {
cachedCompile.borrow.close();
}
cachedCompile = doCompile(sources);
cachedModified.clear();
clearCachedModified();
for (var f : sources) {
cachedModified.put(f, f.getLastModified());
}
Expand Down Expand Up @@ -350,5 +350,9 @@ public CompileTask compile(Collection<? extends JavaFileObject> sources) {
return new CompileTask(compile.task, compile.roots, diags, compile::close);
}

void clearCachedModified() {
cachedModified.clear();
}

private static final Logger LOG = Logger.getLogger("main");
}
24 changes: 22 additions & 2 deletions src/main/java/org/javacs/JavaLanguageServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.google.gson.*;
import com.sun.source.util.Trees;
import com.sun.tools.javac.tree.JCTree;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
Expand Down Expand Up @@ -142,6 +143,7 @@ private Set<Path> docPath() {
}
return paths;
}

private Set<String> addExports() {
if (!settings.has("addExports")) return Set.of();
var array = settings.getAsJsonArray("addExports");
Expand Down Expand Up @@ -241,10 +243,10 @@ public void didChangeWatchedFiles(DidChangeWatchedFilesParams params) {
FileStore.externalChange(file);
break;
case FileChangeType.Deleted:
FileStore.externalDelete(file);
removeClass(file);
break;
}
return;
continue;
}
var name = file.getFileName().toString();
switch (name) {
Expand Down Expand Up @@ -480,6 +482,24 @@ private RenameVariable renameVariable(CompileTask task, VariableElement variable
return new RenameVariable(file, (int) position, newName);
}

private void removeClass(Path file) {
var className = cacheCompiler.fileManager.getClassName(file);
FileStore.externalDelete(file);
var compiler = compiler();
var referencePaths =
Arrays.stream(compiler.findTypeReferences(className)).filter(ref -> !ref.equals(file)).toList();
if (referencePaths.isEmpty()) {
return;
}
for (var referencePath : referencePaths) {
try (var task = compiler.compile(referencePath)) {
compiler.compiler.removeClass((JCTree.JCCompilationUnit) task.root(), className);
}
}
compiler.clearCachedModified();
lint(referencePaths);
}

private boolean uncheckedChanges = false;
private Path lastEdited = Paths.get("");

Expand Down
22 changes: 17 additions & 5 deletions src/main/java/org/javacs/ReusableCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@
import com.sun.source.util.TaskListener;
import com.sun.tools.javac.api.*;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.comp.Annotate;
import com.sun.tools.javac.comp.Check;
import com.sun.tools.javac.comp.CompileStates;
import com.sun.tools.javac.comp.Enter;
import com.sun.tools.javac.comp.Modules;
import com.sun.tools.javac.comp.*;
import com.sun.tools.javac.main.Arguments;
import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
Expand Down Expand Up @@ -128,6 +125,10 @@ Borrow getTask(
return new Borrow(task, currentContext);
}

public void removeClass(JCTree.JCCompilationUnit root, String className) {
currentContext.removeClass(root, className);
}

class Borrow implements AutoCloseable {
final JavacTask task;
boolean closed;
Expand Down Expand Up @@ -209,6 +210,10 @@ <T> void drop(Class<T> c) {
ht.remove(key(c));
}

void removeClass(JCTree.JCCompilationUnit root, String className) {
((ReusableJavaCompiler) get(JavaCompiler.compilerKey)).removeClass(root, className);
}

/**
* Reusable JavaCompiler; exposes a method to clean up the component from leftovers associated with previous
* compilations.
Expand All @@ -226,6 +231,13 @@ public void close() {
// do nothing
}

void removeClass(JCTree.JCCompilationUnit root, String className) {
for (var classSymbol : syms.getClassesForName(names.fromString(className))) {
syms.removeClass(root.modle, classSymbol.flatname);
chk.removeCompiled(classSymbol);
}
}

void clear() {
newRound();
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/javacs/SourceFileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ private JavaFileObject asJavaFileObject(Path file) {
public String inferBinaryName(Location location, JavaFileObject file) {
if (location == StandardLocation.SOURCE_PATH) {
var source = (SourceFileObject) file;
var packageName = FileStore.packageName(source.path);
var className = removeExtension(source.path.getFileName().toString());
if (!packageName.isEmpty()) className = packageName + "." + className;
return className;
return getClassName(source.path);
} else {
return super.inferBinaryName(location, file);
}
}

String getClassName(Path path) {
var packageName = FileStore.packageName(path);
var className = removeExtension(path.getFileName().toString());
if (!packageName.isEmpty()) className = packageName + "." + className;
return className;
}

private String removeExtension(String fileName) {
var lastDot = fileName.lastIndexOf(".");
return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/javacs/CompletionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.javacs.completion.CompletionProvider;
Expand Down Expand Up @@ -752,4 +753,40 @@ public void multilineChain() {
var inserts = filterText("/org/javacs/example/MultilineChain.java", 6, 14);
assertThat(inserts, hasItem("concat"));
}

@Test
public void removeCompiledClassShouldPublishCorrectDiagnostic() throws IOException {
var xClass = FindResource.path("/org/javacs/example/X.java");
var yClass = FindResource.path("/org/javacs/example/Y.java");
try {
try (var writer = Files.newBufferedWriter(xClass, StandardOpenOption.CREATE_NEW)) {
writer.write(
"package org.javacs.example;\n"
+ "class X {\n "
+ "static void test() {\n"
+ "Y.test();\n"
+ "}}");
}
try (var writer = Files.newBufferedWriter(yClass, StandardOpenOption.CREATE_NEW)) {
writer.write(
"package org.javacs.example;\nclass Y {\n" + "static int test() {\n" + "return 1;\n" + "}}");
}
List<String> lintErrors = new ArrayList<>();
var server = LanguageServerFixture.getJavaLanguageServer(diagnostic -> lintErrors.add(diagnostic.message));
server.compiler().compile(xClass, yClass).close();

var deleteEvent = new FileEvent();
deleteEvent.uri = yClass.toUri();
deleteEvent.type = FileChangeType.Deleted;
var deleteFileParams = new DidChangeWatchedFilesParams();
deleteFileParams.changes = List.of(deleteEvent);
server.didChangeWatchedFiles(deleteFileParams);

assertEquals(1, lintErrors.size());
assertTrue(lintErrors.stream().anyMatch(e -> e.contains("cannot find symbol")));
} finally {
Files.delete(xClass);
Files.delete(yClass);
}
}
}