Skip to content

Commit

Permalink
Problem parsing unknown Javadoc tag (openrewrite#3833)
Browse files Browse the repository at this point in the history
* Added recipe for modifying references to 'UUID.randomUUID()' to 'Tree.randomId()', and test

* Changed the recipe so that it only changes UUID.randomID() to Tree.randomId() only when it is used as a parameter in an LST constructor.

* Modified the recipe as instructed. Improved the test statements

* Modified sourceBefore() and visitReturn() in ReloadableJava17JavadocVisitor.java to recognize a leading curly brace '{' in cases such as '{@return' and '{@return}'. The next commit will include changes to the printer class
that prints the corrected javadoc comments.

* Delete rewrite-java/src/main/java/org/openrewrite/java/ReplaceUUIDRandomId.java

* Delete rewrite-java/src/test/java/org/openrewrite/java/ReplaceUUIDRandomIdTest.java

* Add files via upload

* Delete rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java

Replacing the modified ReloadableJava17JavadocVisitor.java with original file.

* Rename ReloadableJava17JavadocVisitor.java to rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java

* Rename UnknownJavadocTagTest.java to rewrite-java-test/src/test/java/org/openrewrite/java/UnknownJavadocTagTest.java

Changed location of added file.

* Update JavadocTest.java to include tests for return annotation with leading opening brace

Removed the file "UnkownJavadocTagTest.java", and added the tests it contained to this file.

* Delete rewrite-java-test/src/test/java/org/openrewrite/java/UnknownJavadocTagTest.java

Removed the now obselete file as the tests it contained are now in rewrite/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/JavadocTest.java

* Update ReloadableJava17JavadocVisitor.java and restored sourceBefore

Restored sourceBefore method to its original, and modified the visitReturn method to deal with the leading curly brace before the return annotation.
Also added a private UUID uuid to ReloadableJava17JavadocVisitor/LeadingBraceMarkers to follow convention.

* Update ReloadableJava17JavadocVisitor.java

Added overlooked annotations

* Update ReloadableJava17JavadocVisitor.java

* Update ReloadableJava17JavadocVisitor.java

* Apply suggestions from code review

* Update rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java

* Update rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java

* Update rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java

* Renamed and moved marker, updated printer, and added changes to parser.

---------

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: Tracey Yoshima <[email protected]>
  • Loading branch information
3 people authored Dec 27, 2023
1 parent ff1712e commit 9acb6d7
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -750,8 +751,16 @@ private JavaType.Variable fieldReferenceType(DCTree.DCReference ref, @Nullable J

@Override
public Tree visitReturn(ReturnTree node, List<Javadoc> body) {
body.addAll(sourceBefore("@return"));
return new Javadoc.Return(randomId(), Markers.EMPTY, convertMultiline(node.getDescription()));
List<Javadoc> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -753,8 +754,16 @@ private JavaType.Variable fieldReferenceType(DCTree.DCReference ref, @Nullable J

@Override
public Tree visitReturn(ReturnTree node, List<Javadoc> body) {
body.addAll(sourceBefore("@return"));
return new Javadoc.Return(randomId(), Markers.EMPTY, convertMultiline(node.getDescription()));
List<Javadoc> 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
Expand Down Expand Up @@ -978,6 +987,7 @@ private List<Javadoc> sourceBefore(String delim) {
if (endIndex < 0) {
throw new IllegalStateException("Expected to be able to find " + delim);
}

List<Javadoc> before = whitespaceBefore();
cursor += delim.length();
return before;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -753,8 +754,16 @@ private JavaType.Variable fieldReferenceType(DCTree.DCReference ref, @Nullable J

@Override
public Tree visitReturn(ReturnTree node, List<Javadoc> body) {
body.addAll(sourceBefore("@return"));
return new Javadoc.Return(randomId(), Markers.EMPTY, convertMultiline(node.getDescription()));
List<Javadoc> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -704,8 +705,16 @@ private JavaType.Variable fieldReferenceType(DCTree.DCReference ref, @Nullable J

@Override
public Tree visitReturn(ReturnTree node, List<Javadoc> body) {
body.addAll(sourceBefore("@return"));
return new Javadoc.Return(randomId(), Markers.EMPTY, convertMultiline(node.getDescription()));
List<Javadoc> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>
* 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -206,6 +207,9 @@ public Javadoc visitProvides(Javadoc.Provides provides, PrintOutputCapture<P> p)
@Override
public Javadoc visitReturn(Javadoc.Return aReturn, PrintOutputCapture<P> p) {
beforeSyntax(aReturn, p);
if (aReturn.getMarkers().findFirst(LeadingBrace.class).isPresent()) {
p.append("{");
}
p.append("@return");
visit(aReturn.getDescription(), p);
afterSyntax(aReturn, p);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
}

0 comments on commit 9acb6d7

Please sign in to comment.