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

Problem parsing unknown Javadoc tag #3833

Merged
merged 21 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a057b69
Added recipe for modifying references to 'UUID.randomUUID()' to 'Tree…
TheMarvelFan Dec 7, 2023
57d4aed
Changed the recipe so that it only changes UUID.randomID() to Tree.ra…
TheMarvelFan Dec 7, 2023
448ec1a
Modified the recipe as instructed. Improved the test statements
TheMarvelFan Dec 11, 2023
2f906c5
Modified sourceBefore() and visitReturn() in ReloadableJava17JavadocV…
TheMarvelFan Dec 18, 2023
8201ed3
Delete rewrite-java/src/main/java/org/openrewrite/java/ReplaceUUIDRan…
timtebeek Dec 18, 2023
ae0f3d0
Delete rewrite-java/src/test/java/org/openrewrite/java/ReplaceUUIDRan…
timtebeek Dec 18, 2023
95da760
Add files via upload
TheMarvelFan Dec 21, 2023
833b93e
Delete rewrite-java-17/src/main/java/org/openrewrite/java/isolated/Re…
TheMarvelFan Dec 22, 2023
9dc1e11
Rename ReloadableJava17JavadocVisitor.java to rewrite-java-17/src/mai…
TheMarvelFan Dec 22, 2023
a0aed43
Rename UnknownJavadocTagTest.java to rewrite-java-test/src/test/java/…
TheMarvelFan Dec 22, 2023
acf3a08
Update JavadocTest.java to include tests for return annotation with l…
TheMarvelFan Dec 23, 2023
1a7e21a
Delete rewrite-java-test/src/test/java/org/openrewrite/java/UnknownJa…
TheMarvelFan Dec 23, 2023
5f3ee09
Update ReloadableJava17JavadocVisitor.java and restored sourceBefore
TheMarvelFan Dec 23, 2023
84ecc07
Update ReloadableJava17JavadocVisitor.java
TheMarvelFan Dec 23, 2023
cf87f8a
Update ReloadableJava17JavadocVisitor.java
TheMarvelFan Dec 23, 2023
73b07cc
Update ReloadableJava17JavadocVisitor.java
TheMarvelFan Dec 23, 2023
b65f40a
Apply suggestions from code review
timtebeek Dec 27, 2023
dffde35
Update rewrite-java-17/src/main/java/org/openrewrite/java/isolated/Re…
traceyyoshima Dec 27, 2023
b82f803
Update rewrite-java-17/src/main/java/org/openrewrite/java/isolated/Re…
traceyyoshima Dec 27, 2023
ab0479e
Update rewrite-java-17/src/main/java/org/openrewrite/java/isolated/Re…
traceyyoshima Dec 27, 2023
7251ec4
Renamed and moved marker, updated printer, and added changes to parser.
traceyyoshima Dec 27, 2023
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 @@ -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;
}