Skip to content

Commit

Permalink
Provide support for Java 22 (#3111)
Browse files Browse the repository at this point in the history
* Provide support for Java 22

Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>

* Update build against I20240325-1800

Signed-off-by: Fred Bricon <fbricon@gmail.com>

* Update Java 22 tests

Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>

---------

Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>
Signed-off-by: Fred Bricon <fbricon@gmail.com>
Co-authored-by: Fred Bricon <fbricon@gmail.com>
  • Loading branch information
snjeza and fbricon committed Mar 26, 2024
1 parent ea09920 commit 0acbde9
Show file tree
Hide file tree
Showing 24 changed files with 213 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import org.eclipse.jdt.internal.corext.fix.UnusedCodeFixCore;
import org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.ui.fix.UnusedCodeCleanUp;
import org.eclipse.jdt.ui.text.java.IInvocationContext;
import org.eclipse.jdt.ui.text.java.IProblemLocation;
import org.eclipse.jdt.internal.ui.fix.UnusedCodeCleanUpCore;
import org.eclipse.jdt.internal.ui.text.correction.IProposalRelevance;
import org.eclipse.jdt.internal.ui.text.correction.ReorgCorrectionsBaseSubProcessor;
import org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsBaseSubProcessor;
Expand All @@ -43,6 +41,8 @@
import org.eclipse.jdt.ls.core.internal.corrections.CorrectionMessages;
import org.eclipse.jdt.ls.core.internal.corrections.ProposalKindWrapper;
import org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler;
import org.eclipse.jdt.ui.text.java.IInvocationContext;
import org.eclipse.jdt.ui.text.java.IProblemLocation;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
Expand Down Expand Up @@ -128,7 +128,7 @@ public void addRemoveImportStatementProposals(IInvocationContext context, IProbl
* @see org.eclipse.jdt.internal.ui.text.correction.ReorgCorrectionsBaseSubProcessor#createRemoveUnusedImportProposal(org.eclipse.jdt.internal.corext.fix.IProposableFix, org.eclipse.jdt.internal.ui.fix.UnusedCodeCleanUp, int, org.eclipse.jdt.ui.text.java.IInvocationContext)
*/
@Override
protected ProposalKindWrapper createRemoveUnusedImportProposal(IProposableFix fix, UnusedCodeCleanUp unusedCodeCleanUp, int relevance, IInvocationContext context) {
protected ProposalKindWrapper createRemoveUnusedImportProposal(IProposableFix fix, UnusedCodeCleanUpCore unusedCodeCleanUp, int relevance, IInvocationContext context) {
if (fix != null) {
try {
CompilationUnitChange change = fix.createChange(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ protected ProposalKindWrapper createChangeMethodSignatureProposal(String label,
*/
@Override
protected ProposalKindWrapper createNewVariableCorrectionProposal(String label, ICompilationUnit cu, int local, SimpleName simpleName, ITypeBinding senderBinding, int relevance) {
NewVariableCorrectionProposalCore p = new NewVariableCorrectionProposalCore(label, cu, NewVariableCorrectionProposalCore.LOCAL, simpleName, null, relevance);
NewVariableCorrectionProposalCore p = new NewVariableCorrectionProposalCore(label, cu, NewVariableCorrectionProposalCore.LOCAL, simpleName, null, relevance, false);
return CodeActionHandler.wrap(p, CodeActionKind.QuickFix);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class AssignToVariableAssistCommandProposal extends AssignToVariableAssis
private List<Object> commandArguments;

public AssignToVariableAssistCommandProposal(ICompilationUnit cu, int variableKind, ExpressionStatement node, ITypeBinding typeBinding, int relevance, String command, List<Object> commandArguments) {
super(cu, variableKind, node, typeBinding, relevance);
super(cu, variableKind, node, typeBinding, relevance, false);
this.command = command;
this.commandArguments = commandArguments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,15 @@ private static boolean getAssignParamToFieldProposals(IInvocationContext context
ASTNode fieldDeclFrag = root.findDeclaringNode(curr);
if (fieldDeclFrag instanceof VariableDeclarationFragment fragment) {
if (fragment.getInitializer() == null) {
AssignToVariableAssistProposalCore p = new AssignToVariableAssistProposalCore(context.getCompilationUnit(), paramDecl, fragment, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_EXISTING_FIELD);
AssignToVariableAssistProposalCore p = new AssignToVariableAssistProposalCore(context.getCompilationUnit(), paramDecl, fragment, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_EXISTING_FIELD, false);
resultingCollections.add(CodeActionHandler.wrap(p, JavaCodeActionKind.QUICK_ASSIST));
}
}
}
}
}

AssignToVariableAssistProposalCore fieldProposal = new AssignToVariableAssistProposalCore(context.getCompilationUnit(), paramDecl, null, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_NEW_FIELD);
AssignToVariableAssistProposalCore fieldProposal = new AssignToVariableAssistProposalCore(context.getCompilationUnit(), paramDecl, null, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_NEW_FIELD, false);
resultingCollections.add(CodeActionHandler.wrap(fieldProposal, JavaCodeActionKind.QUICK_ASSIST));
return true;
}
Expand Down Expand Up @@ -378,7 +378,7 @@ private static boolean getAssignAllParamsToFieldsProposals(IInvocationContext co
return true;
}

AssignToVariableAssistProposalCore fieldProposal = new AssignToVariableAssistProposalCore(context.getCompilationUnit(), parameters, IProposalRelevance.ASSIGN_ALL_PARAMS_TO_NEW_FIELDS);
AssignToVariableAssistProposalCore fieldProposal = new AssignToVariableAssistProposalCore(context.getCompilationUnit(), parameters, IProposalRelevance.ASSIGN_ALL_PARAMS_TO_NEW_FIELDS, false);
resultingCollections.add(CodeActionHandler.wrap(fieldProposal, JavaCodeActionKind.QUICK_ASSIST));
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ public static ProposalKindWrapper getAssignVariableProposal(CodeActionParams par
Arrays.asList(ASSIGN_VARIABLE_COMMAND, params));
return CodeActionHandler.wrap(p, JavaCodeActionKind.REFACTOR_ASSIGN_VARIABLE);
} else {
AssignToVariableAssistProposalCore p = new AssignToVariableAssistProposalCore(cu, AssignToVariableAssistProposalCore.LOCAL, expressionStatement, typeBinding, relevance);
AssignToVariableAssistProposalCore p = new AssignToVariableAssistProposalCore(cu, AssignToVariableAssistProposalCore.LOCAL, expressionStatement, typeBinding, relevance, false);
return CodeActionHandler.wrap(p, JavaCodeActionKind.REFACTOR_ASSIGN_VARIABLE);
}
}
Expand Down Expand Up @@ -553,7 +553,7 @@ public static ProposalKindWrapper getAssignFieldProposal(CodeActionParams params
Arrays.asList(ASSIGN_FIELD_COMMAND, params));
return CodeActionHandler.wrap(proposal, JavaCodeActionKind.REFACTOR_ASSIGN_FIELD);
} else {
AssignToVariableAssistProposalCore proposal = new AssignToVariableAssistProposalCore(cu, AssignToVariableAssistProposalCore.FIELD, expressionStatement, typeBinding, relevance);
AssignToVariableAssistProposalCore proposal = new AssignToVariableAssistProposalCore(cu, AssignToVariableAssistProposalCore.FIELD, expressionStatement, typeBinding, relevance, false);
return CodeActionHandler.wrap(proposal, JavaCodeActionKind.REFACTOR_ASSIGN_FIELD);
}
}
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<unit id="org.mockito.mockito-core" version="0.0.0"/>
<unit id="org.apache.commons.commons-io" version="0.0.0"/>
<unit id="org.eclipse.jdt.apt.pluggable.core" version="0.0.0"/>
<repository location="https://download.eclipse.org/eclipse/updates/4.31/"/>
<repository location="https://download.eclipse.org/eclipse/updates/4.32-I-builds/I20240325-1800/"/>
</location>
<location includeAllPlatforms="false" includeConfigurePhase="false" includeMode="planner" includeSource="true" type="InstallableUnit">
<unit id="org.eclipse.jdt.core.compiler.batch" version="0.0.0"/>
Expand Down
Binary file added org.eclipse.jdt.ls.tests/fakejdk/22/rtstubs.jar
Binary file not shown.
10 changes: 10 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/eclipse/java22/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?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-22">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
17 changes: 17 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/eclipse/java22/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>java22</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=22
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=22
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=enabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=22

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
String foo() {
return "foo";
}

void main() {
System.out.println(foo());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package foo.bar;

/**
* It's a Foo class
*/
public class Foo {
public Foo() {
System.out.println();
super();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.sample;

public class Test {
}
27 changes: 27 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/maven/salut-java22/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-22">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/maven/salut-java22/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>salut-java22</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=22
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=22
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=enabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=22


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
26 changes: 26 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/maven/salut-java22/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo.bar</groupId>
<artifactId>salut-java22</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>22</release>
<compilerArgument>--enable-preview</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.sample;

/**
* This is Bar
*/
public class Bar {

public static void main(String[] args) {
Object foo = "x";
if (foo instanceof String str) {
System.out.println(str);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1203,11 +1203,9 @@ public void testClassExtendFinalClass() throws Exception {

@Test
public void testAddSealedMissingClassModifierProposal() throws Exception {
Map<String, String> options21 = new HashMap<>();
JavaModelUtil.setComplianceOptions(options21, JavaCore.VERSION_21);
options21.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
options21.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
fJProject.setOptions(options21);
Map<String, String> options17 = new HashMap<>();
JavaModelUtil.setComplianceOptions(options17, JavaCore.VERSION_17);
fJProject.setOptions(options17);
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
assertNoErrors(fJProject.getResource());

Expand Down Expand Up @@ -1250,11 +1248,9 @@ public void testAddSealedMissingClassModifierProposal() throws Exception {

@Test
public void testAddSealedAsDirectSuperClass() throws Exception {
Map<String, String> options21 = new HashMap<>();
JavaModelUtil.setComplianceOptions(options21, JavaCore.VERSION_21);
options21.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
options21.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
fJProject.setOptions(options21);
Map<String, String> options17 = new HashMap<>();
JavaModelUtil.setComplianceOptions(options17, JavaCore.VERSION_17);
fJProject.setOptions(options17);
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
assertNoErrors(fJProject.getResource());

Expand All @@ -1279,11 +1275,9 @@ public void testAddSealedAsDirectSuperClass() throws Exception {

@Test
public void testAddPermitsToDirectSuperClass() throws Exception {
Map<String, String> options21 = new HashMap<>();
JavaModelUtil.setComplianceOptions(options21, JavaCore.VERSION_21);
options21.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
options21.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
fJProject.setOptions(options21);
Map<String, String> options17 = new HashMap<>();
JavaModelUtil.setComplianceOptions(options17, JavaCore.VERSION_17);
fJProject.setOptions(options17);
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
assertNoErrors(fJProject.getResource());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1492,11 +1492,9 @@ public void testDontImportTestClassesInMainCode() throws Exception {

@Test
public void testTypeInSealedTypeDeclaration() throws Exception {
Map<String, String> options21 = new HashMap<>();
JavaModelUtil.setComplianceOptions(options21, JavaCore.VERSION_21);
options21.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
options21.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
fJProject1.setOptions(options21);
Map<String, String> options17 = new HashMap<>();
JavaModelUtil.setComplianceOptions(options17, JavaCore.VERSION_17);
fJProject1.setOptions(options17);

IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void testPreviewFeatures16() throws Exception {

@Test
public void testPreviewFeaturesDisabledByDefault() throws Exception {
String name = "java21";
String name = "java22";
importProjects("eclipse/" + name);
IProject project = getProject(name);
assertIsJavaProject(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void getPackageName() throws Exception {
public void testPreviewFeaturesEnabledByDefault() throws Exception {
String defaultJVM = JavaRuntime.getDefaultVMInstall().getId();
try {
TestVMType.setTestJREAsDefault("21");
TestVMType.setTestJREAsDefault("22");
IProject invisibleProject = copyAndImportFolder("singlefile/java14", "foo/bar/Foo.java");
assertTrue(invisibleProject.exists());
assertNoErrors(invisibleProject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,17 @@ public void testJava21Project() throws Exception {
IJavaProject javaProject = JavaCore.create(project);
assertEquals(JavaCore.ENABLED, javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, false));
assertEquals(JavaCore.IGNORE, javaProject.getOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, false));
assertHasErrors(project, "Preview features enabled at an invalid source release level");
}

@Test
public void testJava22Project() throws Exception {
IProject project = importMavenProject("salut-java22");
assertIsJavaProject(project);
assertEquals("22", getJavaSourceLevel(project));
IJavaProject javaProject = JavaCore.create(project);
assertEquals(JavaCore.ENABLED, javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, false));
assertEquals(JavaCore.IGNORE, javaProject.getOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, false));
assertNoErrors(project);
}

Expand Down

0 comments on commit 0acbde9

Please sign in to comment.