Skip to content

Commit abddd59

Browse files
committed
Initial setup of a ScopedVariable Trait
1 parent 0522a61 commit abddd59

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.openrewrite.java.migrate.trait;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import lombok.Value;
5+
import org.jspecify.annotations.Nullable;
6+
import org.openrewrite.Cursor;
7+
import org.openrewrite.java.tree.J;
8+
import org.openrewrite.trait.SimpleTraitMatcher;
9+
import org.openrewrite.trait.Trait;
10+
11+
@Value
12+
public class ScopedVariable implements Trait<J.VariableDeclarations.NamedVariable> {
13+
Cursor cursor;
14+
Cursor scope;
15+
J.Identifier identifier;
16+
17+
@RequiredArgsConstructor
18+
public static class Matcher extends SimpleTraitMatcher<ScopedVariable> {
19+
20+
@Override
21+
protected @Nullable ScopedVariable test(Cursor cursor) {
22+
if (cursor.getValue() instanceof J.VariableDeclarations.NamedVariable) {
23+
J.VariableDeclarations.NamedVariable variable = cursor.getValue();
24+
return new ScopedVariable(cursor, variable.getDeclaringScope(cursor), variable.getName());
25+
}
26+
return null;
27+
}
28+
}
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
@NullMarked
17+
@NonNullFields
18+
package org.openrewrite.java.migrate.trait;
19+
20+
import org.jspecify.annotations.NullMarked;
21+
import org.openrewrite.internal.lang.NonNullFields;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.trait;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.InMemoryExecutionContext;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.java.tree.J;
23+
import org.openrewrite.marker.SearchResult;
24+
import org.openrewrite.test.RecipeSpec;
25+
import org.openrewrite.test.RewriteTest;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
import static org.openrewrite.java.Assertions.java;
30+
31+
class ScopedVariableTest implements RewriteTest {
32+
33+
@Override
34+
public void defaults(RecipeSpec spec) {
35+
spec.recipe(RewriteTest.toRecipe(() ->
36+
new ScopedVariable.Matcher().asVisitor(a -> SearchResult.found(a.getTree(),
37+
String.format("Identified variable %s within scope %s", a.getIdentifier(), a.getScope().getValue().getClass().getSimpleName())
38+
)
39+
)
40+
));
41+
}
42+
43+
@Test
44+
@DocumentExample
45+
void markAllFieldsInClass() {
46+
rewriteRun(
47+
//language=java
48+
java(
49+
"""
50+
class Test {
51+
private String field;
52+
53+
void doSome() {
54+
String anotherField;
55+
}
56+
}
57+
""",
58+
"""
59+
class Test {
60+
private String /*~~(Identified variable field with scope Block)~~>*/field;
61+
62+
void doSome() {
63+
String /*~~(Identified variable anotherField with scope Block)~~>*/anotherField;
64+
}
65+
}
66+
"""
67+
)
68+
);
69+
}
70+
71+
@Test
72+
void markAllFieldsInMethod() {
73+
rewriteRun(
74+
spec -> spec.recipe(Recipe.noop()),
75+
//language=java
76+
java(
77+
"""
78+
class Test {
79+
private String field;
80+
81+
void doSome() {
82+
String anotherField;
83+
}
84+
}
85+
""",
86+
spec -> spec.beforeRecipe((source) -> {
87+
J.MethodDeclaration md = (J.MethodDeclaration) source.getClasses().get(0).getBody().getStatements().get(1);
88+
new ScopedVariable.Matcher().asVisitor(var -> {
89+
assertThat(((J.Block)var.getScope().getValue())).isEqualTo(md.getBody());
90+
return var.getTree();
91+
}).visit(md, new InMemoryExecutionContext());
92+
})
93+
)
94+
);
95+
}
96+
97+
}

0 commit comments

Comments
 (0)