Skip to content

Commit 3e31be2

Browse files
committed
Provide support for JavaSE-20
Change-Id: I66fef00ce58ad3e848fa416385f9df714115e43d Signed-off-by: Fred Bricon <[email protected]>
1 parent 270e356 commit 3e31be2

File tree

3 files changed

+54
-78
lines changed

3 files changed

+54
-78
lines changed

org.eclipse.m2e.jdt/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %Bundle-Name
44
Bundle-SymbolicName: org.eclipse.m2e.jdt;singleton:=true
5-
Bundle-Version: 2.0.5.qualifier
5+
Bundle-Version: 2.0.6.qualifier
66
Bundle-Localization: plugin
77
Export-Package: org.eclipse.m2e.jdt,
88
org.eclipse.m2e.jdt.internal;x-friends:="org.eclipse.m2e.jdt.ui",

org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/AbstractJavaProjectConfigurator.java

Lines changed: 52 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515

1616
import java.io.File;
1717
import java.util.ArrayList;
18-
import java.util.Arrays;
1918
import java.util.Collections;
2019
import java.util.HashMap;
2120
import java.util.LinkedHashMap;
2221
import java.util.List;
2322
import java.util.Map;
23+
import java.util.Set;
2424

25+
import org.osgi.framework.Version;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
2728

@@ -99,37 +100,31 @@ public abstract class AbstractJavaProjectConfigurator extends AbstractProjectCon
99100
protected static final LinkedHashMap<String, String> ENVIRONMENTS = new LinkedHashMap<>();
100101

101102
static {
103+
Set<String> supportedExecutionEnvironmentTypes = Set.of("JRE", "J2SE", "JavaSE");
102104

103-
List<String> sources = new ArrayList<>(Arrays.asList("1.1,1.2,1.3,1.4,1.5,5,1.6,6,1.7,7,1.8,8".split(","))); //$NON-NLS-1$ //$NON-NLS-2$
105+
List<String> sources = new ArrayList<>();
104106

105-
List<String> targets = new ArrayList<>(Arrays.asList("1.1,1.2,1.3,1.4,jsr14,1.5,5,1.6,6,1.7,7,1.8,8".split(","))); //$NON-NLS-1$ //$NON-NLS-2$
107+
List<String> targets = new ArrayList<>();
108+
//Special case
109+
targets.add("jsr14"); //$NON-NLS-1$
110+
ENVIRONMENTS.put("jsr14", "J2SE-1.5"); //$NON-NLS-1$ //$NON-NLS-2$
106111

107-
List<String> releases = new ArrayList<>(Arrays.asList("6,7,8".split(","))); //$NON-NLS-1$ //$NON-NLS-2$
112+
List<String> releases = new ArrayList<>(List.of("6", "7", "8"));
108113

109-
ENVIRONMENTS.put("1.1", "JRE-1.1"); //$NON-NLS-1$ //$NON-NLS-2$
110-
ENVIRONMENTS.put("1.2", "J2SE-1.2"); //$NON-NLS-1$ //$NON-NLS-2$
111-
ENVIRONMENTS.put("1.3", "J2SE-1.3"); //$NON-NLS-1$ //$NON-NLS-2$
112-
ENVIRONMENTS.put("1.4", "J2SE-1.4"); //$NON-NLS-1$ //$NON-NLS-2$
113-
ENVIRONMENTS.put("1.5", "J2SE-1.5"); //$NON-NLS-1$ //$NON-NLS-2$
114-
ENVIRONMENTS.put("jsr14", "J2SE-1.5"); //$NON-NLS-1$ //$NON-NLS-2$
115-
ENVIRONMENTS.put("1.6", "JavaSE-1.6"); //$NON-NLS-1$ //$NON-NLS-2$
116-
ENVIRONMENTS.put("1.7", "JavaSE-1.7"); //$NON-NLS-1$ //$NON-NLS-2$
117-
ENVIRONMENTS.put("1.8", "JavaSE-1.8"); //$NON-NLS-1$ //$NON-NLS-2$
118-
119-
for(int i = 9; i < 20; i++ ) { //Check from Java 9 to 20, because yeah, Java evolves that fast
120-
String level = String.valueOf(i);
121-
IExecutionEnvironment modernJavaSe = JavaRuntime.getExecutionEnvironmentsManager()
122-
.getEnvironment("JavaSE-" + level);//$NON-NLS-1$
123-
if(modernJavaSe == null) {
124-
break;//we didn't find that level, so we bail because there's nothing after that
125-
}
126-
String level1 = "1." + level;//$NON-NLS-1$
127-
sources.add(level1);
128-
sources.add(level);
129-
targets.add(level1);
130-
targets.add(level);
131-
releases.add(level);
132-
ENVIRONMENTS.put(level, modernJavaSe.getId());
114+
for(var ee : JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments()) {
115+
var eeId = ee.getId();
116+
if(supportedExecutionEnvironmentTypes.stream().filter(type -> eeId.startsWith(type)).findAny().isEmpty()) {
117+
continue;
118+
}
119+
var compliance = ee.getComplianceOptions().get(JavaCore.COMPILER_COMPLIANCE);
120+
if(compliance != null) {
121+
sources.add(compliance);
122+
targets.add(compliance);
123+
if(JavaCore.ENABLED.equals(ee.getComplianceOptions().get(JavaCore.COMPILER_RELEASE))) {
124+
releases.add(compliance);
125+
}
126+
ENVIRONMENTS.put(compliance, eeId);
127+
}
133128
}
134129

135130
SOURCES = Collections.unmodifiableList(sources);
@@ -652,9 +647,7 @@ protected void addJavaProjectOptions(Map<String, String> options, ProjectConfigu
652647
}
653648
}
654649

655-
if(release != null)
656-
657-
{
650+
if(release != null) {
658651
source = release;
659652
target = release;
660653
} else {
@@ -670,11 +663,6 @@ protected void addJavaProjectOptions(Map<String, String> options, ProjectConfigu
670663

671664
}
672665

673-
// While "5" and "6" ... are valid synonyms for Java 5, Java 6 ... source/target,
674-
// Eclipse expects the values 1.5 and 1.6 and so on.
675-
source = sanitizeJavaVersion(source);
676-
target = sanitizeJavaVersion(target);
677-
678666
options.put(JavaCore.COMPILER_SOURCE, source);
679667
options.put(JavaCore.COMPILER_COMPLIANCE, source);
680668
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, target);
@@ -771,6 +759,9 @@ private boolean isEnablePreviewFeatures(MavenProject mavenProject, MojoExecution
771759
}
772760

773761
private String sanitizeJavaVersion(String version) {
762+
if(version == null) {
763+
return null;
764+
}
774765
return switch(version) {
775766
case "5", "6", "7", "8" -> "1." + version;
776767
default -> {
@@ -799,53 +790,38 @@ protected List<MojoExecution> getCompilerMojoExecutions(ProjectConfigurationRequ
799790
monitor, GOAL_COMPILE, GOAL_TESTCOMPILE);
800791
}
801792

802-
private String getCompilerLevel(MavenProject mavenProject, MojoExecution execution, String parameter, String source,
803-
List<String> levels, IProgressMonitor monitor) {
804-
int levelIdx = getLevelIndex(source, levels);
805-
793+
private String getCompilerLevel(MavenProject mavenProject, MojoExecution execution, String parameter,
794+
String prevVersion, List<String> supportedVersions, IProgressMonitor monitor) {
806795
try {
807-
source = maven.getMojoParameterValue(mavenProject, execution, parameter, String.class, monitor);
808-
} catch(CoreException ex) {
809-
log.error("Failed to determine compiler " + parameter + " setting, assuming default", ex);
810-
}
811-
812-
int newLevelIdx = getLevelIndex(source, levels);
813-
814-
if(newLevelIdx > levelIdx) {
815-
levelIdx = newLevelIdx;
816-
}
817-
818-
if(levelIdx < 0) {
819-
return null;
820-
}
821-
822-
return levels.get(levelIdx);
823-
}
824-
825-
private int getLevelIndex(String level, List<String> levels) {
826-
int idx = -1;
827-
if(level != null) {
828-
idx = levels.indexOf(level);
829-
if(idx < 0) {
830-
//JDK level probably not yet supported by JDT
831-
int highestIdx = levels.size() - 1;
796+
String version = maven.getMojoParameterValue(mavenProject, execution, parameter, String.class, monitor);
797+
if(version == null) {
798+
return prevVersion;
799+
}
800+
if(!"release".equals(parameter)) {
801+
// While "5" and "6" ... are valid synonyms for Java 5, Java 6 ... source/target,
802+
// Eclipse expects the values 1.5 and 1.6 and so on.
803+
version = sanitizeJavaVersion(version);
804+
}
805+
if(!supportedVersions.isEmpty() && !supportedVersions.contains(version)) {
806+
// supported versions are sorted, so last === highest
807+
String highestVersion = supportedVersions.get(supportedVersions.size() - 1);
832808
try {
833-
if(asDouble(level) > asDouble(levels.get(highestIdx))) {
834-
//take highest known value
835-
idx = highestIdx;
809+
if(Version.valueOf(highestVersion).compareTo(Version.valueOf(version)) < 0) {
810+
// project (javac) version is probably not yet supported by JDT,
811+
// so return the highest available version available at that time
812+
return highestVersion;
836813
}
837-
} catch(NumberFormatException ignore) {
814+
} catch(Exception malformedVersion) {
815+
//Ignore malformed version
838816
}
817+
return prevVersion;
839818
}
840-
}
841-
return idx;
842-
}
819+
return JavaCore.compareJavaVersions(prevVersion, version) < 0 ? version : prevVersion;
843820

844-
private double asDouble(String level) {
845-
if(level == null || level.isEmpty()) {
846-
return -1;
821+
} catch(CoreException ex) {
822+
log.error("Failed to determine compiler " + parameter + " setting, assuming default", ex);
847823
}
848-
return Double.parseDouble(sanitizeJavaVersion(level));
824+
return prevVersion;
849825
}
850826

851827
@Override

org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/ModuleSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static void configureClasspath(IMavenProjectFacade facade, IClasspathDesc
109109
try {
110110
targetCompliance = Integer.parseInt(option);
111111
} catch(NumberFormatException ex) {
112-
log.error(ex.getMessage(), ex);
112+
// ignore jsr14
113113
}
114114
}
115115
if(targetCompliance < 9) {

0 commit comments

Comments
 (0)