Skip to content
Merged
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 @@ -24,6 +24,7 @@
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.staticanalysis.java.MoveFieldAnnotationToType;

import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -92,7 +93,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
.build()
.apply(getCursor(), md.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(annotatedMethod));
return (J.MethodDeclaration) new NullableOnMethodReturnType().getVisitor().visitNonNull(annotatedMethod, ctx, getCursor().getParentTreeCursor());
doAfterVisit(new MoveFieldAnnotationToType(fullyQualifiedName).getVisitor());
return (J.MethodDeclaration) new NullableOnMethodReturnType().getVisitor()
.visitNonNull(annotatedMethod, ctx, getCursor().getParentTreeCursor());
}
return md;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.SourceSpec;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.java.Assertions.java;
Expand Down Expand Up @@ -334,4 +335,42 @@ public String getString() {
)
);
}

@Test
void nestedType() {
rewriteRun(
//language=java
java(
"""
package a;
public class B {
public static class C {}
}
""",
SourceSpec::skip
),
//language=java
java(
"""
import a.B;
public class Foo {
public B.C bar() {
return null;
}
}
""",
"""
import a.B;
import org.jspecify.annotations.Nullable;

public class Foo {

public B.@Nullable C bar() {
Comment on lines +367 to +368
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite happy with this extra newline & double space yet, which are part of moving the annotation after it was added.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this should be fixed upstream, so we'll leave this here for now.

// auto format should handle this, but evidently doesn't
if (at.getAnnotations().isEmpty() && !(getCursor().getParentTreeCursor().getValue() instanceof J.MethodDeclaration)) {
at = at.withTypeExpression(te.withPrefix(te.getPrefix().withWhitespace("")));
}
at = autoFormat(at, at.getTypeExpression(), ctx, getCursor().getParentOrThrow());

return null;
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.SourceSpec;

import static org.openrewrite.java.Assertions.java;

Expand Down Expand Up @@ -55,6 +56,47 @@ class Test {
);
}

@Test
void nestedType() {
rewriteRun(
//language=java
java(
"""
package a;
public class B {
public static class C {}
}
""",
SourceSpec::skip
),
//language=java
java(
"""
import a.B;
import org.openrewrite.internal.lang.Nullable;

public class Foo {
@Nullable
public B.C bar() {
return null;
}
}
""",
"""
import a.B;
import org.openrewrite.internal.lang.Nullable;

public class Foo {

public @Nullable B.C bar() {
return null;
}
}
"""
)
);
}

@Test
void dontTouchArguments() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.SourceSpec;

import static org.openrewrite.java.Assertions.java;

Expand Down Expand Up @@ -222,4 +223,45 @@ public void someFunction(org.openrewrite.internal.@Nullable MetricsHelper metric
)
);
}

@Test
void nestedType() {
rewriteRun(
//language=java
java(
"""
package a;
public class B {
public static class C {}
}
""",
SourceSpec::skip
),
//language=java
java(
"""
import a.B;
import org.openrewrite.internal.lang.Nullable;

public class Foo {
@Nullable
public B.C bar() {
return null;
}
}
""",
"""
import a.B;
import org.openrewrite.internal.lang.Nullable;

public class Foo {

public B.@Nullable C bar() {
return null;
}
}
"""
)
);
}
}