Skip to content

Commit 453d17a

Browse files
Adding wildcard and static method import examples
1 parent c1a16a9 commit 453d17a

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

rewrite-java/src/main/java/org/openrewrite/java/AddCommentToImport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public String getInstanceNameSuffix() {
6565

6666
@Override
6767
public TreeVisitor<?, ExecutionContext> getVisitor() {
68-
TypeMatcher typeMatcher = new TypeMatcher(typePattern, false);
68+
TypeMatcher typeMatcher = new TypeMatcher(typePattern);
6969
return new JavaIsoVisitor<ExecutionContext>() {
7070
@Override
7171
public J.Import visitImport(J.Import anImport, ExecutionContext ctx) {

rewrite-java/src/test/java/org/openrewrite/java/AddCommentToImportTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,48 @@ public class Foo {}
3737
"""
3838
package foo;
3939
public class Bar extends Foo {}
40+
""",
41+
"""
42+
package foo.bar;
43+
public class Baz {
44+
public static void someStaticMethod() {}
45+
}
4046
"""
4147
));
4248
}
4349

4450
@DocumentExample
51+
@Test
52+
void wildcardAndStaticImportsRecognized() {
53+
rewriteRun(
54+
spec -> spec.recipe(new AddCommentToImport(SHORT_COMMENT, "foo.bar.Baz")),
55+
//language=java
56+
java(
57+
"""
58+
package blah;
59+
// Will not match direct package
60+
import foo.*;
61+
import foo.bar.*;
62+
import foo.bar.Baz;
63+
import static foo.bar.Baz.someStaticMethod;
64+
class Other {}
65+
""",
66+
"""
67+
package blah;
68+
// Will not match direct package
69+
import foo.*;
70+
/* Short comment to add */
71+
import foo.bar.*;
72+
/* Short comment to add */
73+
import foo.bar.Baz;
74+
/* Short comment to add */
75+
import static foo.bar.Baz.someStaticMethod;
76+
class Other {}
77+
"""
78+
)
79+
);
80+
}
81+
4582
@Test
4683
void addSingleLineComment() {
4784
rewriteRun(

0 commit comments

Comments
 (0)