[java] Update Tests for JEP 463: Implicitly Declared Classes and Instance Main Methods

This commit is contained in:
Andreas Dangel
2024-02-15 11:26:12 +01:00
parent fbb9da24f2
commit cb3ceef17a
11 changed files with 78 additions and 15 deletions
+3 -1
View File
@@ -1,4 +1,6 @@
/**
* Support "JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview)" (Java 22)
* No changes.
* Support "JEP 459: String Templates (Second Preview)" (Java 22)
* Use ASTTemplate.setContent instead of setImage.
* Remove support for Record pattern in enhanced for statements. This was only a Java 20 Preview feature.
@@ -1153,7 +1155,7 @@ ASTCompilationUnit CompilationUnit() :
// OrdinaryCompilationUnit: -> TopLevelClassOrInterfaceDeclaration
( TypeDeclaration() ( EmptyDeclaration() )* )*
// UnnamedClassCompilationUnit:
// SimpleCompilationUnit:
[
( LOOKAHEAD(3) ClassMemberDeclarationNoMethod() )*
ModifierList() MethodDeclaration()
@@ -138,6 +138,7 @@ public class LanguageLevelChecker<T> {
/**
* Unnamed Classes and Instance Main Methods
* @see <a href="https://openjdk.org/jeps/445">JEP 445: Unnamed Classes and Instance Main Methods (Preview)</a> (Java 21)
* @see <a href="https://openjdk.org/jeps/463">JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview)</a> (Java 22)
*/
UNNAMED_CLASSES(21, 22, false),
@@ -66,38 +66,43 @@ class Java22PreviewTreeDumpTest extends BaseJavaTreeDumpTest {
}
@Test
void unnamedClasses1() {
doTest("Jep445_UnnamedClasses1");
ASTCompilationUnit compilationUnit = java22p.parseResource("Jep445_UnnamedClasses1.java");
void jep463UnnamedClasses1() {
doTest("Jep463_UnnamedClasses1");
ASTCompilationUnit compilationUnit = java22p.parseResource("Jep463_UnnamedClasses1.java");
assertTrue(compilationUnit.isUnnamedClass());
ASTMethodCall methodCall = compilationUnit.descendants(ASTMethodCall.class).first();
assertNotNull(methodCall.getTypeMirror());
}
@Test
void unnamedClasses2() {
doTest("Jep445_UnnamedClasses2");
void jep463UnnamedClasses2() {
doTest("Jep463_UnnamedClasses2");
}
@Test
void unnamedClasses3() {
doTest("Jep445_UnnamedClasses3");
void jep463UnnamedClasses3() {
doTest("Jep463_UnnamedClasses3");
}
@Test
void unnamedClassesBeforeJava22Preview() {
ParseException thrown = assertThrows(ParseException.class, () -> java22.parseResource("Jep445_UnnamedClasses1.java"));
void jep463UnnamedClasses4WithImports() {
doTest("Jep463_UnnamedClasses4WithImports");
}
@Test
void jep463UnnamedClassesBeforeJava22Preview() {
ParseException thrown = assertThrows(ParseException.class, () -> java22.parseResource("Jep463_UnnamedClasses1.java"));
assertThat(thrown.getMessage(), containsString("Unnamed classes is a preview feature of JDK 22, you should select your language version accordingly"));
}
@Test
void testOrdinaryCompilationUnit() {
void jep463TestOrdinaryCompilationUnit() {
ASTCompilationUnit compilationUnit = java22.parse("public class Foo { public static void main(String[] args) {}}");
assertFalse(compilationUnit.isUnnamedClass());
}
@Test
void testModularCompilationUnit() {
void jep463TestModularCompilationUnit() {
ASTCompilationUnit compilationUnit = java22.parse("module foo {}");
assertFalse(compilationUnit.isUnnamedClass());
}
@@ -5,7 +5,8 @@
/**
* @see <a href="https://openjdk.org/jeps/445">JEP 445: Unnamed Classes and Instance Main Methods (Preview)</a>
* @see <a href="https://openjdk.org/jeps/445">JEP 445: Unnamed Classes and Instance Main Methods (Preview)</a> (Java 21)
* @see <a href="https://openjdk.org/jeps/463">JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview)</a> (Java 22)
*/
void main() {
@@ -5,7 +5,8 @@
/**
* @see <a href="https://openjdk.org/jeps/445">JEP 445: Unnamed Classes and Instance Main Methods (Preview)</a>
* @see <a href="https://openjdk.org/jeps/445">JEP 445: Unnamed Classes and Instance Main Methods (Preview)</a> (Java 21)
* @see <a href="https://openjdk.org/jeps/463">JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview)</a> (Java 22)
*/
String greeting() { return "Hello, World!"; }
@@ -5,7 +5,8 @@
/**
* @see <a href="https://openjdk.org/jeps/445">JEP 445: Unnamed Classes and Instance Main Methods (Preview)</a>
* @see <a href="https://openjdk.org/jeps/445">JEP 445: Unnamed Classes and Instance Main Methods (Preview)</a> (Java 21)
* @see <a href="https://openjdk.org/jeps/463">JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview)</a> (Java 22)
*/
String greeting = "Hello, World!";
@@ -0,0 +1,17 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
import java.util.Arrays;
import java.util.stream.Collectors;
/**
* @see <a href="https://openjdk.org/jeps/445">JEP 445: Unnamed Classes and Instance Main Methods (Preview)</a> (Java 21)
* @see <a href="https://openjdk.org/jeps/463">JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview)</a> (Java 22)
*/
String greeting = Arrays.asList("Hello", "World!").stream().collect(Collectors.joining(", "));
void main() {
System.out.println(greeting);
}
@@ -0,0 +1,35 @@
+- CompilationUnit[@PackageName = ""]
+- ImportDeclaration[@ImportOnDemand = false, @ImportedName = "java.util.Arrays", @ImportedSimpleName = "Arrays", @PackageName = "java.util", @Static = false]
+- ImportDeclaration[@ImportOnDemand = false, @ImportedName = "java.util.stream.Collectors", @ImportedSimpleName = "Collectors", @PackageName = "java.util.stream", @Static = false]
+- FieldDeclaration[@EffectiveVisibility = Visibility.V_PACKAGE, @Static = false, @Visibility = Visibility.V_PACKAGE]
| +- ModifierList[@EffectiveModifiers = "{}", @ExplicitModifiers = "{}"]
| +- ClassType[@FullyQualified = false, @SimpleName = "String"]
| +- VariableDeclarator[@Initializer = true, @Name = "greeting"]
| +- VariableId[@ArrayType = false, @EffectiveVisibility = Visibility.V_PACKAGE, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = true, @Final = false, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = false, @LambdaParameter = false, @LocalVariable = false, @Name = "greeting", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = false, @TypeInferred = false, @Visibility = Visibility.V_PACKAGE]
| +- MethodCall[@CompileTimeConstant = false, @Image = "collect", @MethodName = "collect", @ParenthesisDepth = 0, @Parenthesized = false]
| +- MethodCall[@CompileTimeConstant = false, @Image = "stream", @MethodName = "stream", @ParenthesisDepth = 0, @Parenthesized = false]
| | +- MethodCall[@CompileTimeConstant = false, @Image = "asList", @MethodName = "asList", @ParenthesisDepth = 0, @Parenthesized = false]
| | | +- TypeExpression[@CompileTimeConstant = false, @ParenthesisDepth = 0, @Parenthesized = false]
| | | | +- ClassType[@FullyQualified = false, @SimpleName = "Arrays"]
| | | +- ArgumentList[@Empty = false, @Size = 2]
| | | +- StringLiteral[@CompileTimeConstant = true, @ConstValue = "Hello", @Empty = false, @Image = "\"Hello\"", @Length = 5, @LiteralText = "\"Hello\"", @ParenthesisDepth = 0, @Parenthesized = false, @TextBlock = false]
| | | +- StringLiteral[@CompileTimeConstant = true, @ConstValue = "World!", @Empty = false, @Image = "\"World!\"", @Length = 6, @LiteralText = "\"World!\"", @ParenthesisDepth = 0, @Parenthesized = false, @TextBlock = false]
| | +- ArgumentList[@Empty = true, @Size = 0]
| +- ArgumentList[@Empty = false, @Size = 1]
| +- MethodCall[@CompileTimeConstant = false, @Image = "joining", @MethodName = "joining", @ParenthesisDepth = 0, @Parenthesized = false]
| +- TypeExpression[@CompileTimeConstant = false, @ParenthesisDepth = 0, @Parenthesized = false]
| | +- ClassType[@FullyQualified = false, @SimpleName = "Collectors"]
| +- ArgumentList[@Empty = false, @Size = 1]
| +- StringLiteral[@CompileTimeConstant = true, @ConstValue = ", ", @Empty = false, @Image = "\", \"", @Length = 2, @LiteralText = "\", \"", @ParenthesisDepth = 0, @Parenthesized = false, @TextBlock = false]
+- MethodDeclaration[@Abstract = false, @Arity = 0, @EffectiveVisibility = Visibility.V_PACKAGE, @Final = false, @Image = "main", @MainMethod = true, @Name = "main", @Overridden = false, @Static = false, @Varargs = false, @Visibility = Visibility.V_PACKAGE, @Void = true]
+- ModifierList[@EffectiveModifiers = "{}", @ExplicitModifiers = "{}"]
+- VoidType[]
+- FormalParameters[@Empty = true, @Size = 0]
+- Block[@Empty = false, @Size = 1, @containsComment = false]
+- ExpressionStatement[]
+- MethodCall[@CompileTimeConstant = false, @Image = "println", @MethodName = "println", @ParenthesisDepth = 0, @Parenthesized = false]
+- FieldAccess[@AccessType = AccessType.READ, @CompileTimeConstant = false, @Image = "out", @Name = "out", @ParenthesisDepth = 0, @Parenthesized = false]
| +- TypeExpression[@CompileTimeConstant = false, @ParenthesisDepth = 0, @Parenthesized = false]
| +- ClassType[@FullyQualified = false, @SimpleName = "System"]
+- ArgumentList[@Empty = false, @Size = 1]
+- VariableAccess[@AccessType = AccessType.READ, @CompileTimeConstant = false, @Image = "greeting", @Name = "greeting", @ParenthesisDepth = 0, @Parenthesized = false]