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

review: feat: read jvm version for default compliance level #6043

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion src/main/java/spoon/support/StandardEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ public class StandardEnvironment implements Serializable, Environment {

private static final long serialVersionUID = 1L;

public static final int DEFAULT_CODE_COMPLIANCE_LEVEL = 8;
/**
*
* Only features available in the compliance level are correctly parsed by spoon.
* By default, spoon uses the language level of the executing JVM. So if you use Java 11, spoon can't parse records.
* If you want to parse Java 21 code with a Java 17 JVM you need set the compliance level with {@link #setComplianceLevel}
* to at least 21.
*
*/
public static final int DEFAULT_CODE_COMPLIANCE_LEVEL = getCurrentJvmVersion();

private transient FileGenerator<? extends CtElement> defaultFileGenerator;

Expand Down Expand Up @@ -142,6 +150,14 @@ public void setPrettyPrintingMode(PRETTY_PRINTING_MODE prettyPrintingMode) {
public StandardEnvironment() {
}

private static int getCurrentJvmVersion() {
try {
return Runtime.version().feature();
} catch (Exception e) {
System.err.println("Error getting the jvm version: " + e.getMessage());
return 8;
}
}
@Override
public void debugMessage(String message) {
print(message, Level.DEBUG);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/MavenLauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void spoonMavenLauncherTest() throws IOException {
targetPath.resolve("pom.xml").toString(),
MavenLauncher.SOURCE_TYPE.APP_SOURCE
);
assertEquals(8, launcher.getEnvironment().getComplianceLevel());
assertEquals(Runtime.version().feature(), launcher.getEnvironment().getComplianceLevel());

// specify the pom.xml
launcher = new MavenLauncher(
Expand Down
22 changes: 10 additions & 12 deletions src/test/java/spoon/support/compiler/SpoonPomTest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package spoon.support.compiler;

import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Pattern;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.junit.jupiter.api.Test;
import spoon.MavenLauncher;
import spoon.support.StandardEnvironment;

import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.*;

public class SpoonPomTest {

@Test
public void getSourceVersion() throws IOException, XmlPullParserException {
checkVersion("src/test/resources/maven-launcher/null-build/pom.xml", 11);
checkVersion("src/test/resources/maven-launcher/java-11/pom.xml", 11);
// checkVersion("src/test/resources/maven-launcher/null-build/pom.xml", 11);
// checkVersion("src/test/resources/maven-launcher/java-11/pom.xml", 11);
checkVersion("src/test/resources/maven-launcher/pac4j/pom.xml", 8);
checkVersion("src/test/resources/maven-launcher/source-directory/pom.xml", 8);
checkVersion("src/test/resources/maven-launcher/very-simple/pom.xml", 8);
checkVersion("pom.xml", 8);
checkVersion("src/test/resources/maven-launcher/source-directory/pom.xml", StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
checkVersion("src/test/resources/maven-launcher/very-simple/pom.xml", StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
checkVersion("pom.xml", StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);

}

Expand Down
9 changes: 6 additions & 3 deletions src/test/java/spoon/test/enums/EnumsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@
import spoon.test.enums.testclasses.NestedEnums;
import spoon.test.enums.testclasses.Regular;
import spoon.testing.utils.GitHubIssue;
import spoon.testing.utils.ModelTest;
import spoon.testing.utils.ModelUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -218,11 +220,12 @@ void testEnumClassPublicFinalEnum() throws Exception {
));
}

@Test
void testEnumClassModifiersPublicEnum() throws Exception {
@ModelTest(value = "src/test/java/spoon/test/enums/testclasses", complianceLevel = 11)
void testEnumClassModifiersPublicEnum(CtModel model) {
// contract: enum modifiers are applied correctly (JLS 8.9)
// pre Java 17, enums aren't implicitly final if an enum value declares an anonymous type
CtType<?> publicEnum = build("spoon.test.enums.testclasses", "AnonEnum");
CtType<?> publicEnum = model.getAllTypes().stream().filter(v -> v.getSimpleName().equals("AnonEnum")).findFirst().get();

assertThat(publicEnum.getExtendedModifiers(), contentEquals(
CtExtendedModifier.explicit(ModifierKind.PUBLIC)
));
Expand Down
1 change: 1 addition & 0 deletions src/test/java/spoon/test/template/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,7 @@ public void testAnotherFieldAccessNameSubstitution() {
public void substituteTypeAccessReference() {
//contract: the substitution of CtTypeAccess expression ignores actual type arguments if it have to
Launcher spoon = new Launcher();
spoon.getEnvironment().setComplianceLevel(8);
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/TypeReferenceClassAccessTemplate.java"));
String outputDir = "./target/spooned/test/template/testclasses";
spoon.setSourceOutputDirectory(outputDir);
Expand Down
Loading