fix(java): show Maven-generated sources in Problems (#3944)

jdtls/m2e only import source folders declared in the POM. Generators such as protobuf/gRPC, jOOQ, Avro, OpenAPI and QueryDSL add theirs with addCompileSourceRoot while the build runs, so Problems stayed red after a successful Maven compile. After a Maven project update, and after Build, Rebuild and Compile File, the IDE now scans target/generated-sources and generated-test-sources and puts those folders on the jdtls classpath through openidea.java.generatedSources (java-usages bundle 1.16.0), marked so a later scan can drop them; annotation-processor output, which jdtls compiles itself, is left out. A Maven build never starts jdtls by itself.

Co-authored-by: tomgor <12905387+tomgor@users.noreply.github.com>
This commit is contained in:
tomgor
2026-11-17 20:06:44 +08:00
committed by GitHub
co-authored by tomgor
parent 407561e59a
commit cb7bddec14
10 changed files with 706 additions and 10 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: OpenIDEA Java usages
Bundle-SymbolicName: ai.openidea.usages;singleton:=true
Bundle-Version: 1.15.0
Bundle-Version: 1.16.0
Bundle-RequiredExecutionEnvironment: JavaSE-21
Require-Bundle: org.eclipse.jdt.ls.core,org.eclipse.jdt.core,
org.eclipse.jdt.core.manipulation,org.eclipse.core.runtime,
+4 -1
View File
@@ -88,7 +88,7 @@ is not a claim of full IntelliJ feature parity.
## Rebuild and delivery
The checked-in jar is embedded by ide-sdk `include_bytes!`, installed as
`java-usages-1.15.0.jar`, and added alongside existing initialization bundles.
`java-usages-1.16.0.jar`, and added alongside existing initialization bundles.
Users need no compiler, Maven or Python. Manifest/install version must change
when replacing the bundle because OSGi caches bundles.
@@ -136,6 +136,9 @@ Version 1.12.0 caches usages answers until the Java model or an unsaved edit cha
Version 1.15.0 adds `openidea.java.implementationLocations`, which resolves the method an
implementation in a dependency declares (the standard request starts at its class file); its
reproducible SHA-256 is `c29e5455c02c245523b2e7a5861e00ae670cefc66cda33ec19fdce280f94c40c`.
Version 1.16.0 adds `openidea.java.generatedSources`, which puts Maven plugin output such as
protobuf and gRPC stubs on the project classpath; its reproducible SHA-256 is
`a5feda1cae11bdea1044fb311404840b048601af037f65685973721f1eefff8b`.
```sh
export PATH=/path/to/jdk25/bin:$PATH
Binary file not shown.
+3
View File
@@ -20,6 +20,9 @@
<delegateCommandHandler class="ai.openidea.archives.RefreshArchivesCommand">
<command id="openidea.java.refreshArchives"/>
</delegateCommandHandler>
<delegateCommandHandler class="ai.openidea.sources.GeneratedSourcesCommand">
<command id="openidea.java.generatedSources"/>
</delegateCommandHandler>
<delegateCommandHandler class="ai.openidea.implementations.ImplementationTargetsCommand">
<command id="openidea.java.implementationLocations"/>
</delegateCommandHandler>
@@ -0,0 +1,162 @@
package ai.openidea.sources;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IFolder;
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.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathAttribute;
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.IDelegateCommandHandler;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
/**
* Adds Maven plugin output (protobuf, gRPC, jOOQ, …) as source folders.
*
* <p>The argument is {@code [{path, test}, …]}: absolute directories the build already
* generated. Folders this command added are marked {@code openidea.generatedSource} and
* dropped again when they are no longer in that list, so a later reload does not keep a
* source folder Maven has cleaned. Entries m2e or build-helper already created are left
* as they are.
*/
public final class GeneratedSourcesCommand implements IDelegateCommandHandler {
static final String MARKER = "openidea.generatedSource";
private static final String MAVEN = "org.eclipse.m2e.core.maven2Nature";
private record Spec(IPath path, boolean test) {}
@Override
public Object executeCommand(String command, List<Object> arguments, IProgressMonitor monitor) throws Exception {
List<Spec> specs = specs(arguments);
IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
int changed = 0;
for (IJavaProject project : projects) {
if (monitor.isCanceled()) throw new OperationCanceledException();
if (!isMaven(project)) continue;
if (sync(project, owned(projects, project, specs), monitor)) changed++;
}
return changed;
}
private static List<Spec> specs(List<Object> arguments) {
List<Spec> specs = new ArrayList<>();
if (arguments == null) return specs;
for (Object argument : arguments) {
if (!(argument instanceof Map<?, ?> map)) continue;
if (!(map.get("path") instanceof String path) || path.isBlank()) continue;
specs.add(new Spec(Path.fromOSString(path), Boolean.TRUE.equals(map.get("test"))));
}
return specs;
}
/** Specs whose longest Maven project prefix is `project`. A reactor parent also prefixes its modules. */
private static List<Spec> owned(IJavaProject[] projects, IJavaProject project, List<Spec> specs) {
IPath location = project.getProject().getLocation();
List<Spec> owned = new ArrayList<>();
if (location == null) return owned;
for (Spec spec : specs) {
if (!location.isPrefixOf(spec.path)) continue;
IJavaProject best = project;
int segments = location.segmentCount();
for (IJavaProject other : projects) {
if (other == project || !isMaven(other)) continue;
IPath otherLocation = other.getProject().getLocation();
if (otherLocation != null && otherLocation.isPrefixOf(spec.path)
&& otherLocation.segmentCount() > segments) {
best = other;
segments = otherLocation.segmentCount();
}
}
if (best == project) owned.add(spec);
}
return owned;
}
private static boolean sync(IJavaProject project, List<Spec> specs, IProgressMonitor monitor) {
try {
IPath location = project.getProject().getLocation();
if (location == null) return false;
Map<IPath, Boolean> wanted = new LinkedHashMap<>();
for (Spec spec : specs) {
if (monitor.isCanceled()) throw new OperationCanceledException();
IPath relative = spec.path.makeRelativeTo(location);
if (relative.isAbsolute() || relative.segmentCount() == 0) continue;
IFolder folder = project.getProject().getFolder(relative);
refresh(folder, monitor);
if (folder.exists()) wanted.put(project.getPath().append(relative), spec.test);
}
IClasspathEntry[] raw = project.getRawClasspath();
List<IClasspathEntry> next = new ArrayList<>();
boolean changed = false;
for (IClasspathEntry entry : raw) {
if (!marked(entry)) {
next.add(entry);
continue;
}
if (wanted.containsKey(entry.getPath())) next.add(entry);
else changed = true;
}
for (Map.Entry<IPath, Boolean> entry : wanted.entrySet()) {
if (hasSource(next, entry.getKey())) continue;
IClasspathAttribute[] attributes = entry.getValue()
? new IClasspathAttribute[] {
JavaCore.newClasspathAttribute(IClasspathAttribute.TEST, "true"),
JavaCore.newClasspathAttribute(MARKER, "true")
}
: new IClasspathAttribute[] { JavaCore.newClasspathAttribute(MARKER, "true") };
next.add(JavaCore.newSourceEntry(entry.getKey(), new IPath[0], new IPath[0], null, attributes));
changed = true;
}
if (!changed) return false;
project.setRawClasspath(next.toArray(IClasspathEntry[]::new), monitor);
return true;
} catch (CoreException | OperationCanceledException e) {
JavaLanguageServerPlugin.logException("Adding generated Maven sources to " + project.getElementName(), e);
return false;
}
}
private static void refresh(IFolder folder, IProgressMonitor monitor) throws CoreException {
IResource resource = folder;
while (resource.getType() != IResource.ROOT && !resource.exists()) {
IResource parent = resource.getParent();
if (parent == null) return;
resource = parent;
}
resource.refreshLocal(IResource.DEPTH_INFINITE, monitor);
}
private static boolean marked(IClasspathEntry entry) {
if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) return false;
for (IClasspathAttribute attribute : entry.getExtraAttributes()) {
if (MARKER.equals(attribute.getName()) && "true".equals(attribute.getValue())) return true;
}
return false;
}
private static boolean hasSource(List<IClasspathEntry> entries, IPath path) {
for (IClasspathEntry entry : entries) {
if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getPath().equals(path)) return true;
}
return false;
}
private static boolean isMaven(IJavaProject project) {
IProject resource = project.getProject();
try {
return resource.isAccessible() && resource.hasNature(MAVEN);
} catch (CoreException e) {
return false;
}
}
}