Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.sun.source.util.TreePathScanner;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.tree.DocCommentTable;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree;
Expand All @@ -37,10 +38,9 @@
import org.openrewrite.java.JavaParsingException;
import org.openrewrite.java.JavaPrinter;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.java.marker.CompactConstructor;
import org.openrewrite.java.marker.OmitParentheses;
import org.openrewrite.java.marker.TrailingComma;
import org.openrewrite.java.marker.*;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Marker;
import org.openrewrite.marker.Markers;
import org.openrewrite.style.NamedStyles;

Expand Down Expand Up @@ -397,9 +397,11 @@ public J visitClass(ClassTree node, Space fmt) {
Map<Integer, JCAnnotation> annotationPosTable = mapAnnotations(node.getModifiers().getAnnotations(),
new HashMap<>(node.getModifiers().getAnnotations().size()));

int saveCursor = cursor;
ReloadableJava25ModifierResults modifierResults = sortedModifiersAndAnnotations(node.getModifiers(), annotationPosTable);

List<J.Annotation> kindAnnotations = collectAnnotations(annotationPosTable);
CompactSourceFile compactSourceFile = null;

J.ClassDeclaration.Kind kind;
if (hasFlag(node.getModifiers(), Flags.ENUM)) {
Expand All @@ -412,10 +414,17 @@ public J visitClass(ClassTree node, Space fmt) {
} else if (hasFlag(node.getModifiers(), Flags.RECORD)) {
kind = new J.ClassDeclaration.Kind(randomId(), sourceBefore("record"), Markers.EMPTY, kindAnnotations, J.ClassDeclaration.Kind.Type.Record);
} else {
kind = new J.ClassDeclaration.Kind(randomId(), sourceBefore("class"), Markers.EMPTY, kindAnnotations, J.ClassDeclaration.Kind.Type.Class);
Space prefix = whitespace();
if (source.startsWith("class", cursor)) {
skip("class");
} else {
compactSourceFile = new CompactSourceFile(randomId());
cursor = saveCursor;
}
kind = new J.ClassDeclaration.Kind(randomId(), prefix, Markers.EMPTY, kindAnnotations, J.ClassDeclaration.Kind.Type.Class);
}

J.Identifier name = new J.Identifier(randomId(), sourceBefore(node.getSimpleName().toString()),
J.Identifier name = new J.Identifier(randomId(), compactSourceFile != null ? EMPTY : sourceBefore(node.getSimpleName().toString()),
Markers.EMPTY, emptyList(), ((JCClassDecl) node).getSimpleName().toString(), typeMapping.type(node), null);

JContainer<J.TypeParameter> typeParams = node.getTypeParameters().isEmpty() ? null : JContainer.build(
Expand Down Expand Up @@ -498,7 +507,7 @@ public J visitClass(ClassTree node, Space fmt) {
);
}

Space bodyPrefix = sourceBefore("{");
Space bodyPrefix = compactSourceFile != null ? whitespace() : sourceBefore("{");

// enum values are required by the grammar to occur before any ordinary field, constructor, or method members
List<Tree> jcEnums = new ArrayList<>(node.getMembers().size());
Expand Down Expand Up @@ -587,10 +596,13 @@ public J visitClass(ClassTree node, Space fmt) {
members.addAll(convertStatements(membersMultiVariablesSeparated));
addPossibleEmptyStatementsBeforeClosingBrace(members);

J.Block body = new J.Block(randomId(), bodyPrefix, Markers.EMPTY, new JRightPadded<>(false, EMPTY, Markers.EMPTY),
members, sourceBefore("}"));
J.Block body = new J.Block(randomId(), bodyPrefix, compactSourceFile != null ? Markers.build(List.of(new OmitBraces(randomId()))) : Markers.EMPTY,
new JRightPadded<>(false, EMPTY, Markers.EMPTY),
members,
compactSourceFile != null ? whitespace() : sourceBefore("}"));

return new J.ClassDeclaration(randomId(), fmt, Markers.EMPTY, modifierResults.getLeadingAnnotations(), modifierResults.getModifiers(), kind, name, typeParams,
return new J.ClassDeclaration(randomId(), fmt, compactSourceFile != null ? Markers.build(List.of(compactSourceFile)) : Markers.EMPTY,
modifierResults.getLeadingAnnotations(), modifierResults.getModifiers(), kind, name, typeParams,
primaryConstructor, extendings, implementings, permitting, body, (JavaType.FullyQualified) typeMapping.type(node));
}

Expand All @@ -611,17 +623,21 @@ public J visitCompilationUnit(CompilationUnitTree node, Space fmt) {
Map<Integer, JCAnnotation> annotationPosTable = mapAnnotations(node.getPackageAnnotations(),
new HashMap<>(node.getPackageAnnotations().size()));
List<J.Annotation> packageAnnotations = collectAnnotations(annotationPosTable);
List<Marker> markers = new ArrayList<>(styles);

J.Package packageDecl = null;
if (cu.getPackageName() != null) {
Space packagePrefix = sourceBefore("package");
packageDecl = new J.Package(randomId(), packagePrefix, Markers.EMPTY,
packageDecl = new J.Package(randomId(), sourceBefore("package"), Markers.EMPTY,
convert(cu.getPackageName()), packageAnnotations);
} else if (cu.getPackageName() == null &&
cu.modle.equals(Symtab.instance(context).unnamedModule) &&
cu.packge.equals(Symtab.instance(context).unnamedModule.unnamedPackage)) {
int saveCursor = cursor;
}
return new J.CompilationUnit(
randomId(),
fmt,
Markers.build(styles),
Markers.build(markers),
sourcePath,
fileAttributes,
charset.name(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
package org.openrewrite.java.tree;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MinimumJava17;
import org.openrewrite.java.MinimumJava25;
import org.openrewrite.test.RewriteTest;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -276,4 +278,134 @@ class A {
)
);
}

@Issue("https://openjdk.org/jeps/512")
@MinimumJava25
@Nested
class ImplicitClasses implements RewriteTest {

@Test
void implicitClassWithMainMethod() {
rewriteRun(
java(
"""
void main() {
System.out.println("Hello from implicit class!");
}
""",
spec -> spec.path("com/example/Main.java")
)
);
}

@Test
void implicitClassWithAbundantSpacing() {
rewriteRun(
java(
"""

void main() {

System.out.println("Hello from implicit class!");

}

""",
spec -> spec.path("com/example/Main.java")
)
);
}

@Test
void implicitClassWithMultipleMethods() {
rewriteRun(
java(
"""
void main() {
greet("World");
}

void greet(String name) {
System.out.println("Hello, " + name + "!");
}
""",
spec -> spec.path("com/example/Main.java")
)
);
}

@Test
void implicitClassWithFields() {
rewriteRun(
java(
"""
String greeting = "Hello";

void main() {
System.out.println(greeting + ", World!");
}
""",
spec -> spec.path("com/example/Main.java")
)
);
}

@Test
void implicitClassUtilizingJavaBase() {
rewriteRun(
java(
"""
void main() {
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
System.out.println(names);
}
""",
spec -> spec.path("com/example/Main.java")
)
);
}

@Test
void implicitClassWithConstructor() {
rewriteRun(
java(
"""
private String name;

Main(String name) {
this.name = name;
}

void main() {
System.out.println("Name: " + name);
}
""",
spec -> spec.path("com/example/Main.java")
)
);
}

@Test
void implicitClassWithInnerClass() {
rewriteRun(
java(
"""
void main() {
Helper helper = new Helper();
helper.help();
}

class Helper {
void help() {
System.out.println("Helping!");
}
}
""",
spec -> spec.path("com/example/Main.java")
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package org.openrewrite.java.tree;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.java.MinimumJava25;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
Expand Down Expand Up @@ -116,4 +119,91 @@ public void foo() { }/*Comments*/
);
}

@Issue("https://openjdk.org/jeps/512")
@MinimumJava25
@Nested
class InstanceMainMethods implements RewriteTest {

@Test
void instanceMainMethodWithNoParameters() {
rewriteRun(
java(
"""
class HelloWorld {
void main() {
System.out.println("Hello, World!");
}
}
"""
)
);
}

@Test
void instanceMainMethodWithStringArrayParameter() {
rewriteRun(
java(
"""
class HelloWorld {
void main(String[] args) {
System.out.println("Hello, World!");
}
}
"""
)
);
}

@Test
void instanceMainMethodWithInstanceFields() {
rewriteRun(
java(
"""
class Counter {
private int count = 0;

void main() {
count++;
System.out.println("Count: " + count);
}
}
"""
)
);
}

@Test
void instanceMainMethodCallingInstanceMethods() {
rewriteRun(
java(
"""
class Greeter {
void main() {
greet("World");
}

void greet(String name) {
System.out.println("Hello, " + name + "!");
}
}
"""
)
);
}

@Test
void staticMainMethodStillSupported() {
rewriteRun(
java(
"""
class TraditionalMain {
public static void main(String[] args) {
System.out.println("Traditional main method");
}
}
"""
)
);
}
}
}
Loading