diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/EventType.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/EventType.java index 848fea0b32..64b9183de3 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/EventType.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/EventType.java @@ -40,7 +40,12 @@ public enum EventType { /** * Source attachments have been updated for some jar libraries. */ - SourceInvalidated(500); + SourceInvalidated(500), + + /** + * Incompatibility between release version and preview features + */ + PreviewFeaturesNotAllowed(600); private final int value; diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/BuildWorkspaceHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/BuildWorkspaceHandler.java index 4bd10c12a8..37d937f867 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/BuildWorkspaceHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/BuildWorkspaceHandler.java @@ -80,6 +80,7 @@ public BuildWorkspaceStatus buildWorkspace(boolean forceReBuild, IProgressMonito if (errors.isEmpty()) { return BuildWorkspaceStatus.SUCCEED; } else { + WorkspaceDiagnosticsHandler.checkPreviewFeatureValidity(problemMarkers); // for default project, problem markers aren't sent. Add logs here for trouble shooting. String newline = System.getProperty("line.separator"); logError("Error occured while building workspace. Details: " + newline + String.join(newline, errors)); diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java index 6e56765e9e..43e2bfeb74 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java @@ -44,6 +44,8 @@ import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.compiler.IProblem; +import org.eclipse.jdt.ls.core.internal.EventNotification; +import org.eclipse.jdt.ls.core.internal.EventType; import org.eclipse.jdt.ls.core.internal.JDTUtils; import org.eclipse.jdt.ls.core.internal.JavaClientConnection; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; @@ -62,6 +64,9 @@ import org.eclipse.m2e.core.internal.IMavenConstants; import org.eclipse.m2e.core.internal.Messages; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + /** * Listens to the resource change events and converts {@link IMarker}s to {@link Diagnostic}s. * @@ -342,6 +347,31 @@ private void publishDiagnostics(List markers) { connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), diagnostics)); } } + + checkPreviewFeatureValidity(markers); + } + + public static void checkPreviewFeatureValidity(List problemMarkers) { + // Preview feature support enabled on incompatible release version + List previewFeatureMarkers = problemMarkers.stream().filter(m -> m.getAttribute(IJavaModelMarker.ID, 0) == IProblem.PreviewFeaturesNotAllowed).collect(Collectors.toList()); + JsonArray errorList = new JsonArray(); + if (!previewFeatureMarkers.isEmpty()) { + for (IMarker marker : previewFeatureMarkers) { + // error message mentions invalid release level, and the supported level + String errorMessage = ResourceUtils.getMessage(marker); + String projectUri = JDTUtils.getFileURI(marker.getResource().getProject()); + JsonObject entry = new JsonObject(); + entry.addProperty("uri", projectUri); + entry.addProperty("message", errorMessage); + if (!errorList.contains(entry)) { + errorList.add(entry); + } + } + if (JavaLanguageServerPlugin.getProjectsManager().getConnection() != null) { + EventNotification prevFeatNotAllowedNotification = new EventNotification().withType(EventType.PreviewFeaturesNotAllowed).withData(errorList); + JavaLanguageServerPlugin.getProjectsManager().getConnection().sendEventNotification(prevFeatNotAllowedNotification); + } + } } @Deprecated