diff --git a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11JavadocVisitor.java b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11JavadocVisitor.java index f4bd273dc0d..73077bad781 100644 --- a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11JavadocVisitor.java +++ b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11JavadocVisitor.java @@ -32,6 +32,7 @@ import org.openrewrite.Tree; import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.java.marker.LeadingBrace; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -750,8 +751,16 @@ private JavaType.Variable fieldReferenceType(DCTree.DCReference ref, @Nullable J @Override public Tree visitReturn(ReturnTree node, List body) { - body.addAll(sourceBefore("@return")); - return new Javadoc.Return(randomId(), Markers.EMPTY, convertMultiline(node.getDescription())); + List before; + Markers markers = Markers.EMPTY; + if (source.startsWith("{", cursor)) { + markers = markers.addIfAbsent(new LeadingBrace(Tree.randomId())); + before = sourceBefore("{@return"); + } else { + before = sourceBefore("@return"); + } + body.addAll(before); + return new Javadoc.Return(randomId(), markers, convertMultiline(node.getDescription())); } @Override diff --git a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java index 6a09a47cd85..32ad802724a 100644 --- a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java +++ b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java @@ -35,6 +35,7 @@ import org.openrewrite.Tree; import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.java.marker.LeadingBrace; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -753,8 +754,16 @@ private JavaType.Variable fieldReferenceType(DCTree.DCReference ref, @Nullable J @Override public Tree visitReturn(ReturnTree node, List body) { - body.addAll(sourceBefore("@return")); - return new Javadoc.Return(randomId(), Markers.EMPTY, convertMultiline(node.getDescription())); + List before; + Markers markers = Markers.EMPTY; + if (source.startsWith("{", cursor)) { + markers = markers.addIfAbsent(new LeadingBrace(Tree.randomId())); + before = sourceBefore("{@return"); + } else { + before = sourceBefore("@return"); + } + body.addAll(before); + return new Javadoc.Return(randomId(), markers, convertMultiline(node.getDescription())); } @Override @@ -978,6 +987,7 @@ private List sourceBefore(String delim) { if (endIndex < 0) { throw new IllegalStateException("Expected to be able to find " + delim); } + List before = whitespaceBefore(); cursor += delim.length(); return before; diff --git a/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21JavadocVisitor.java b/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21JavadocVisitor.java index b3276d3e748..0ae9c8ac9ce 100644 --- a/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21JavadocVisitor.java +++ b/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21JavadocVisitor.java @@ -35,6 +35,7 @@ import org.openrewrite.Tree; import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.java.marker.LeadingBrace; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -753,8 +754,16 @@ private JavaType.Variable fieldReferenceType(DCTree.DCReference ref, @Nullable J @Override public Tree visitReturn(ReturnTree node, List body) { - body.addAll(sourceBefore("@return")); - return new Javadoc.Return(randomId(), Markers.EMPTY, convertMultiline(node.getDescription())); + List before; + Markers markers = Markers.EMPTY; + if (source.startsWith("{", cursor)) { + markers = markers.addIfAbsent(new LeadingBrace(Tree.randomId())); + before = sourceBefore("{@return"); + } else { + before = sourceBefore("@return"); + } + body.addAll(before); + return new Javadoc.Return(randomId(), markers, convertMultiline(node.getDescription())); } @Override diff --git a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8JavadocVisitor.java b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8JavadocVisitor.java index 594455096be..b067c298ac6 100644 --- a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8JavadocVisitor.java +++ b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8JavadocVisitor.java @@ -32,6 +32,7 @@ import org.openrewrite.Tree; import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.java.marker.LeadingBrace; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -704,8 +705,16 @@ private JavaType.Variable fieldReferenceType(DCTree.DCReference ref, @Nullable J @Override public Tree visitReturn(ReturnTree node, List body) { - body.addAll(sourceBefore("@return")); - return new Javadoc.Return(randomId(), Markers.EMPTY, convertMultiline(node.getDescription())); + List before; + Markers markers = Markers.EMPTY; + if (source.startsWith("{", cursor)) { + markers = markers.addIfAbsent(new LeadingBrace(Tree.randomId())); + before = sourceBefore("{@return"); + } else { + before = sourceBefore("@return"); + } + body.addAll(before); + return new Javadoc.Return(randomId(), markers, convertMultiline(node.getDescription())); } @Override diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/JavadocTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/JavadocTest.java index 60251b5dd20..1842f577d03 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/JavadocTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/JavadocTest.java @@ -1748,4 +1748,36 @@ class Test { ) ); } + + @Test + void returnOpeningAndClosingBrace() { + rewriteRun( + java( + """ + interface Test { + /** + * {@return 42} + */ + int foo(); + } + """ + ) + ); + } + + @Test + void returnOpeningBraceOnly() { + rewriteRun( + java( + """ + interface Test { + /** + * {@return 42 + */ + int foo(); + } + """ + ) + ); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/JavaIsoVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/JavaIsoVisitor.java index 8be39cb973c..8029a7c4aac 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaIsoVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaIsoVisitor.java @@ -23,7 +23,7 @@ * This iso(morphic) refactoring visitor is the appropriate base class for most Java refactoring visitors. * It comes with an additional constraint compared to the non-isomorphic JavaRefactorVisitor: * Each visit method must return an AST element of the same type as the one being visited. - * + *

* For visitors that do not need the extra flexibility of JavaRefactorVisitor, this constraint * makes for a more pleasant visitor authoring experience as less casting will be required. */ diff --git a/rewrite-java/src/main/java/org/openrewrite/java/JavadocPrinter.java b/rewrite-java/src/main/java/org/openrewrite/java/JavadocPrinter.java index c36750c55fb..73ce44a1ed6 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavadocPrinter.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavadocPrinter.java @@ -18,6 +18,7 @@ import org.openrewrite.Cursor; import org.openrewrite.PrintOutputCapture; import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.java.marker.LeadingBrace; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Marker; import org.openrewrite.marker.Markers; @@ -206,6 +207,9 @@ public Javadoc visitProvides(Javadoc.Provides provides, PrintOutputCapture

p) @Override public Javadoc visitReturn(Javadoc.Return aReturn, PrintOutputCapture

p) { beforeSyntax(aReturn, p); + if (aReturn.getMarkers().findFirst(LeadingBrace.class).isPresent()) { + p.append("{"); + } p.append("@return"); visit(aReturn.getDescription(), p); afterSyntax(aReturn, p); diff --git a/rewrite-java/src/main/java/org/openrewrite/java/marker/LeadingBrace.java b/rewrite-java/src/main/java/org/openrewrite/java/marker/LeadingBrace.java new file mode 100644 index 00000000000..409bb38b35c --- /dev/null +++ b/rewrite-java/src/main/java/org/openrewrite/java/marker/LeadingBrace.java @@ -0,0 +1,31 @@ +/* + * Copyright 2023 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.marker; + +import lombok.Value; +import lombok.With; +import org.openrewrite.marker.Marker; + +import java.util.UUID; + +/** + * JavaDoc tags like @return that may be written with a leading brace. + */ +@Value +@With +public class LeadingBrace implements Marker { + UUID id; +}