Skip to content
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

Support using 'javac' as internal compiler. #3167

Merged
merged 6 commits into from
Oct 16, 2024
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up JDK 17
- name: Set up JDK 23
uses: actions/setup-java@v3
with:
java-version: '17'
java-version: '23'
distribution: 'temurin'
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.ui.text.java.IInvocationContext;
import org.eclipse.jdt.ui.text.java.IProblemLocation;
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
import org.eclipse.jdt.internal.ui.text.correction.SuppressWarningsBaseSubProcessor;
import org.eclipse.jdt.internal.ui.text.correction.SuppressWarningsProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.FixCorrectionProposalCore;
import org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler;
import org.eclipse.jdt.ui.cleanup.ICleanUp;
import org.eclipse.jdt.ui.text.java.IInvocationContext;
import org.eclipse.jdt.ui.text.java.IProblemLocation;
import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposalCore;
import org.eclipse.lsp4j.CodeActionKind;

Expand Down Expand Up @@ -50,4 +53,11 @@ protected ProposalKindWrapper createASTRewriteCorrectionProposal(String name, IC
return CodeActionHandler.wrap(new ASTRewriteCorrectionProposalCore(name, cu, rewrite, relevance), CodeActionKind.QuickFix);
}

@Override
protected ProposalKindWrapper createFixCorrectionProposal(IProposableFix fix, ICleanUp cleanUp, int relevance, IInvocationContext context) {
FixCorrectionProposalCore proposal = new FixCorrectionProposalCore(fix, cleanUp, relevance, context);
proposal.setCommandId("org.eclipse.jdt.ui.correction.addSuppressWarnings");
return CodeActionHandler.wrap(proposal, CodeActionKind.QuickFix);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IMarker;
Expand All @@ -33,6 +35,7 @@
import org.eclipse.jdt.core.IOpenable;
import org.eclipse.jdt.core.IProblemRequestor;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblem;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
Expand All @@ -59,6 +62,10 @@ public abstract class BaseDiagnosticsHandler implements IProblemRequestor {
public static final int NON_PROJECT_JAVA_FILE = 0x10;
public static final int NOT_ON_CLASSPATH = 0x20;

public static final String DIAG_JAVAC_CODE = "javacCode";
public static final String DIAG_ECJ_PROBLEM_ID = "ecjProblemId";
public static final String DIAG_ARGUMENTS = "arguments";

public BaseDiagnosticsHandler(JavaClientConnection conn, ICompilationUnit cu) {
problems = new ArrayList<>();
this.cu = cu;
Expand Down Expand Up @@ -248,12 +255,30 @@ public static List<Diagnostic> toDiagnosticsArray(IOpenable openable, List<IProb
diag.setCode(Integer.toString(problem.getID()));
diag.setSeverity(convertSeverity(problem));
diag.setRange(convertRange(openable, problem));
Map<String, Object> data = new HashMap<>();
if (problem.getID() == IProblem.UndefinedName || problem.getID() == IProblem.UndefinedType || problem.getID() == IProblem.UninitializedBlankFinalField) {
diag.setData(problem.getArguments());
data.put(DIAG_ARGUMENTS, problem.getArguments());
}
if (isDiagnosticTagSupported) {
diag.setTags(getDiagnosticTag(problem.getID()));
}
if (problem instanceof CategorizedProblem javaProblem) {
String[] extraAttributeNames = javaProblem.getExtraMarkerAttributeNames();
Object[] extraAttributeValues = javaProblem.getExtraMarkerAttributeValues();
if (extraAttributeNames != null && extraAttributeValues != null
&& extraAttributeNames.length == extraAttributeValues.length) {
for (int i = 0; i < extraAttributeNames.length; i++) {
if (DIAG_JAVAC_CODE.equals(extraAttributeNames[i])) {
diag.setCode(String.valueOf(extraAttributeValues[i]));
data.put(DIAG_ECJ_PROBLEM_ID, Integer.toString(problem.getID()));
break;
}
}
}
}
if (!data.isEmpty()) {
diag.setData(data);
}
array.add(diag);
}
return array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposalCore;
import org.eclipse.jdt.ls.core.internal.ChangeUtil;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JSONUtility;
import org.eclipse.jdt.ls.core.internal.JavaCodeActionKind;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.corrections.DiagnosticsHelper;
Expand Down Expand Up @@ -349,14 +350,19 @@ public static IProblemLocation[] getProblemLocationCores(ICompilationUnit unit,
boolean isError = diagnostic.getSeverity() == DiagnosticSeverity.Error;
int problemId = getProblemId(diagnostic);
List<String> arguments = new ArrayList<>();
if (diagnostic.getData() instanceof JsonArray data) {
for (JsonElement e : data) {
arguments.add(e.getAsString());
if (diagnostic.getData() != null) {
Map<String, Object> data = JSONUtility.toModel(diagnostic.getData(), Map.class);
Object args = data.get(BaseDiagnosticsHandler.DIAG_ARGUMENTS);
if (args instanceof JsonArray args1) {
for (JsonElement e : args1) {
arguments.add(e.getAsString());
}
} else if (args instanceof String[] args1) {
arguments = Arrays.asList(args1);
}
}
if (diagnostic.getData() instanceof String[] data) {
for (String s : data) {
arguments.add(s);
Object ecjProblemId = data.get(BaseDiagnosticsHandler.DIAG_ECJ_PROBLEM_ID);
if (ecjProblemId instanceof String id) {
problemId = Integer.valueOf(id);
}
}
locations[i] = new ProblemLocation(start, end - start, problemId, arguments.toArray(new String[0]), isError, IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -410,6 +411,17 @@ private static Diagnostic toDiagnostic(Range range, IMarker marker, boolean isDi
d.setTags(DiagnosticsHandler.getDiagnosticTag(problemId));
}
d.setRange(range);
try {
if (marker.getAttribute(BaseDiagnosticsHandler.DIAG_JAVAC_CODE) != null) {
String javacCode = (String) marker.getAttribute(BaseDiagnosticsHandler.DIAG_JAVAC_CODE);
Map<String, Object> data = new HashMap<>();
data.put(BaseDiagnosticsHandler.DIAG_ECJ_PROBLEM_ID, String.valueOf(problemId));
d.setCode(javacCode);
d.setData(data);
}
} catch (CoreException e) {
// do nothing
}
return d;
}

Expand Down Expand Up @@ -459,6 +471,17 @@ private static Diagnostic toDiagnostic(IDocument document, IMarker marker, boole
if (isDiagnosticTagSupported) {
d.setTags(DiagnosticsHandler.getDiagnosticTag(problemId));
}
try {
if (marker.getAttribute(BaseDiagnosticsHandler.DIAG_JAVAC_CODE) != null) {
String javacCode = (String) marker.getAttribute(BaseDiagnosticsHandler.DIAG_JAVAC_CODE);
Map<String, Object> data = new HashMap<>();
data.put(BaseDiagnosticsHandler.DIAG_ECJ_PROBLEM_ID, String.valueOf(problemId));
d.setCode(javacCode);
d.setData(data);
}
} catch (CoreException e) {
// do nothing
}
return d;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public void onBuildFinished(long buildFinishedTime) {
properties.add("buildFileNames", buildFileNamesList);
}
properties.addProperty("javaProjectCount", javaProjectCount);
properties.addProperty("javac.enabled", Boolean.toString(prefs.getPreferences().isJavacEnabled()));
if (sourceLevelMin != 0) {
properties.addProperty("compiler.source.min", Float.toString(sourceLevelMin));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,13 @@ public class Preferences {

public static final String JAVA_JDT_LS_PROTOBUF_SUPPORT_ENABLED = "java.jdt.ls.protobufSupport.enabled";
public static final String JAVA_JDT_LS_ANDROID_SUPPORT_ENABLED = "java.jdt.ls.androidSupport.enabled";
public static final String JAVA_JDT_LS_JAVAC_ENABLED = "java.jdt.ls.javac.enabled";

public static final String JAVA_COMPILE_NULLANALYSIS_NONNULL = "java.compile.nullAnalysis.nonnull";
public static final String JAVA_COMPILE_NULLANALYSIS_NULLABLE = "java.compile.nullAnalysis.nullable";
public static final String JAVA_COMPILE_NULLANALYSIS_NONNULLBYDEFAULT = "java.compile.nullAnalysis.nonnullbydefault";
public static final String JAVA_COMPILE_NULLANALYSIS_MODE = "java.compile.nullAnalysis.mode";
public static final String JAVA_COMPLETION_ENGINE = "java.completion.engine";

public static final String LIFECYCLE_MAPPING_METADATA_SOURCE_NAME = "lifecycle-mapping-metadata.xml";

Expand Down Expand Up @@ -702,6 +704,7 @@ public class Preferences {
private ProjectEncodingMode projectEncoding;
private boolean avoidVolatileChanges;
private boolean protobufSupportEnabled;
private boolean javacEnabled;
private boolean androidSupportEnabled;
private List<String> nonnullTypes;
private List<String> nullableTypes;
Expand Down Expand Up @@ -958,6 +961,7 @@ public Preferences() {
inlayHintsParameterMode = InlayHintsParameterMode.LITERALS;
projectEncoding = ProjectEncodingMode.IGNORE;
avoidVolatileChanges = true;
javacEnabled = false;
nonnullTypes = new ArrayList<>();
nullableTypes = new ArrayList<>();
nonnullbydefaultTypes = new ArrayList<>();
Expand Down Expand Up @@ -1335,6 +1339,8 @@ public static Preferences createFrom(Map<String, Object> configuration) {
prefs.setAvoidVolatileChanges(avoidVolatileChanges);
boolean protobufSupported = getBoolean(configuration, JAVA_JDT_LS_PROTOBUF_SUPPORT_ENABLED, false);
prefs.setProtobufSupportEnabled(protobufSupported);
boolean javacEnabled = getBoolean(configuration, JAVA_JDT_LS_JAVAC_ENABLED, false);
prefs.setJavacEnabled(javacEnabled);
boolean androidSupported = getBoolean(configuration, JAVA_JDT_LS_ANDROID_SUPPORT_ENABLED, false);
prefs.setAndroidSupportEnabled(androidSupported);
List<String> nonnullTypes = getList(configuration, JAVA_COMPILE_NULLANALYSIS_NONNULL, Collections.emptyList());
Expand Down Expand Up @@ -1379,10 +1385,10 @@ public static Preferences createFrom(Map<String, Object> configuration) {
}
}
prefs.setFilesAssociations(new ArrayList<>(associations));

String searchScope = getString(configuration, JAVA_SEARCH_SCOPE, null);
prefs.setSearchScope(SearchScope.fromString(searchScope, SearchScope.all));

return prefs;
}

Expand Down Expand Up @@ -2381,6 +2387,14 @@ public void setAndroidSupportEnabled(boolean androidSupportEnabled) {
this.androidSupportEnabled = androidSupportEnabled;
}

public boolean isJavacEnabled() {
return this.javacEnabled;
}

public void setJavacEnabled(boolean javacEnabled) {
this.javacEnabled = javacEnabled;
}

public List<String> getNonnullTypes() {
return this.nonnullTypes;
}
Expand Down
1 change: 1 addition & 0 deletions org.eclipse.jdt.ls.product/languageServer.product
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<plugin id="org.eclipse.equinox.security.win32" fragment="true"/>
<plugin id="org.eclipse.jdt.apt.pluggable.core"/>
<plugin id="org.eclipse.jdt.core"/>
<plugin id="org.eclipse.jdt.core.javac"/>
<plugin id="org.eclipse.jdt.launching"/>
<plugin id="org.eclipse.jdt.launching.macosx"/>
<plugin id="org.eclipse.jdt.ls.core"/>
Expand Down
7 changes: 4 additions & 3 deletions org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
<unit id="org.eclipse.pde.junit.runtime" version="0.0.0" />
<unit id="org.eclipse.jdt.junit4.runtime" version="0.0.0" />
<unit id="org.eclipse.jdt.core.manipulation" version="0.0.0" />
<repository location="https://download.eclipse.org/eclipse/updates/4.34-I-builds/I20240919-1840/"/>
<repository location="https://download.eclipse.org/eclipse/updates/4.34-I-builds/I20241014-1810/"/>
</location>
<location includeAllPlatforms="false" includeConfigurePhase="false" includeMode="planner" includeSource="true" type="InstallableUnit">
rgrunber marked this conversation as resolved.
Show resolved Hide resolved
<unit id="org.eclipse.jdt.core.compiler.batch" version="0.0.0"/>
<unit id="org.eclipse.jdt.core" version="0.0.0"/>
<unit id="org.eclipse.jdt.core.javac" version="0.0.0"/>
<unit id="org.eclipse.jdt.apt.core" version="0.0.0"/>
<repository location="https://download.eclipse.org/jdtls/jdt-core-incubator/snapshots/"/>
<repository location="https://ci.eclipse.org/ls/job/jdt-core-incubator/job/dom-with-javac/lastSuccessfulBuild/artifact/repository/target/repository/"/>
</location>
<location includeAllPlatforms="false" includeConfigurePhase="false" includeMode="planner" includeSource="true" type="InstallableUnit">
<unit id="org.eclipse.xtext.xbase.lib" version="0.0.0"/>
Expand Down Expand Up @@ -71,5 +72,5 @@
</dependencies>
</location>
</locations>
<targetJRE path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/>
<targetJRE path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-23"/>
</target>
2 changes: 1 addition & 1 deletion org.eclipse.jdt.ls.tests/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?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-17"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-23"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/">
<attributes>
Expand Down
6 changes: 3 additions & 3 deletions org.eclipse.jdt.ls.tests/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.codegen.targetPlatform=23
org.eclipse.jdt.core.compiler.compliance=23
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.discouragedReference=ignore
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17
org.eclipse.jdt.core.compiler.source=23
org.eclipse.jdt.core.formatter.align_fields_grouping_blank_lines=2147483647
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
org.eclipse.jdt.core.formatter.alignment_for_additive_operator=16
Expand Down
6 changes: 4 additions & 2 deletions org.eclipse.jdt.ls.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ Bundle-Name: %Bundle-Name
Bundle-SymbolicName: org.eclipse.jdt.ls.tests;singleton:=true
Bundle-Version: 1.41.0.qualifier
Export-Package: org.eclipse.jdt.ls.core.internal;x-friends:="org.eclipse.jdt.ls.tests.syntaxserver"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Import-Package: org.osgi.framework;version="1.3.0"
Bundle-RequiredExecutionEnvironment: JavaSE-23
Import-Package: org.osgi.framework;version="1.3.0",
org.eclipse.jdt.internal.javac,
org.eclipse.jdt.internal.javac.dom
Comment on lines +8 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of tweaking the MANIFEST.MF, you can consider adding the javac bundle as extraRequirement with target-platform-configuration block in the pom.xml. This bit of customization could also be encapsulated in the javac profile, so it can be easier to merge later.

Bundle-Localization: plugin
Bundle-ActivationPolicy: lazy
Require-Bundle: org.eclipse.jdt.ls.core,
Expand Down
43 changes: 41 additions & 2 deletions org.eclipse.jdt.ls.tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<configuration>
<deriveReleaseCompilerArgumentFromTargetLevel>false</deriveReleaseCompilerArgumentFromTargetLevel>
<compilerId>javac</compilerId>
<compilerArgs combine.self="override">
<arg>--add-exports</arg>
<arg>java.base/java.lang=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
</compilerArgs>
Comment on lines +24 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might possibly be removed

</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
Expand All @@ -41,7 +69,7 @@
</property>
</activation>
<properties>
<lombokArgs>-javaagent:${basedir}/lib/lombok-1.18.28.jar</lombokArgs>
<lombokArgs>-javaagent:${basedir}/lib/lombok-1.18.32.jar</lombokArgs>
</properties>
<build>
<plugins>
Expand All @@ -53,11 +81,22 @@
<artifactItem>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<version>1.18.32</version>
</artifactItem>
</artifactItems>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<configuration>
<toolchains>
<jdk>
<id>JavaSE-23</id>
</jdk>
</toolchains>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Expand Down
Loading
Loading