Skip to content

Commit

Permalink
FindFields matches on type patterns and field globs (#3579)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider authored Sep 30, 2023
1 parent 6ac7b35 commit 6873fbd
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 178 deletions.
33 changes: 0 additions & 33 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,3 @@ jobs:
ossrh_token: ${{ secrets.OSSRH_TOKEN }}
ossrh_signing_key: ${{ secrets.OSSRH_SIGNING_KEY }}
ossrh_signing_password: ${{ secrets.OSSRH_SIGNING_PASSWORD }}
# test-downstream:
# needs: build
# strategy:
# fail-fast: false
# matrix:
# repository: [ rewrite-java-security , rewrite-spring, rewrite-migrate-java ]
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v3
# with:
# path: rewrite
# fetch-depth: 0
# - name: Checkout ${{ matrix.repository }} repo
# uses: actions/checkout@v3
# with:
# repository: openrewrite/${{ matrix.repository }}
# path: ${{ matrix.repository }}
# fetch-depth: 0
# - name: Setup Java
# uses: actions/[email protected]
# with:
# distribution: temurin
# java-version: 17
# - name: Build
# uses: gradle/gradle-build-action@v2
# with:
# arguments: --console=plain --info --stacktrace --warning-mode=all --no-daemon --include-build ../rewrite build
# build-root-directory: ${{ matrix.repository }}
# env:
# GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
# GRADLE_ENTERPRISE_CACHE_USERNAME: ${{ secrets.GRADLE_ENTERPRISE_CACHE_USERNAME }}
# GRADLE_ENTERPRISE_CACHE_PASSWORD: ${{ secrets.GRADLE_ENTERPRISE_CACHE_PASSWORD }}
11 changes: 0 additions & 11 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
plugins {
id("org.openrewrite.build.root") version("latest.release")
id("org.openrewrite.build.java-base") version("latest.release")
id("org.openrewrite.rewrite") version("latest.release")
}

repositories {
mavenCentral()
}

dependencies {
rewrite(project(":rewrite-core"))
}

rewrite {
failOnDryRunResults = true
activeRecipe("org.openrewrite.self.Rewrite")
}


allprojects {
group = "org.openrewrite"
description = "Eliminate tech-debt. Automatically."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class D {
@Test
void ignoreDeprecationsInDeprecatedMethod() {
rewriteRun(
spec -> spec.recipe(new FindDeprecatedFields("org.old.types..*", true)),
spec -> spec.recipe(new FindDeprecatedFields("org.old.types..*", null, true)),
java(
"""
import org.old.types.D;
Expand All @@ -59,7 +59,7 @@ void test(int n) {
@Test
void ignoreDeprecationsInDeprecatedClass() {
rewriteRun(
spec -> spec.recipe(new FindDeprecatedFields("org.old.types..*", true)),
spec -> spec.recipe(new FindDeprecatedFields("org.old.types..*", null, true)),
java(
"""
import org.old.types.D;
Expand All @@ -79,7 +79,7 @@ void test(int n) {
@Test
void findDeprecations() {
rewriteRun(
spec -> spec.recipe(new FindDeprecatedFields("org.old.types..*", false)),
spec -> spec.recipe(new FindDeprecatedFields("org.old.types..*", null, false)),
java(
"""
import org.old.types.D;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package org.openrewrite.java.search;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.Issue;
import org.openrewrite.test.RewriteTest;

Expand All @@ -28,7 +29,7 @@ class FindFieldsOfTypeTest implements RewriteTest {
@Test
void findFieldNotVariable() {
rewriteRun(
spec -> spec.recipe(new FindFieldsOfType("java.io.File")),
spec -> spec.recipe(new FindFieldsOfType("java.io.File", null)),
java(
"""
import java.io.*;
Expand All @@ -42,24 +43,26 @@ public static void main(String[] args) {
);
}

@DocumentExample
@Test
void findPrivateNonInheritedField() {
@ParameterizedTest
@ValueSource(strings = {"java.util.List", "java.util.*", "java.util..*"})
void findPrivateNonInheritedField(String type) {
rewriteRun(
spec -> spec.recipe(new FindFieldsOfType("java.util.List")),
spec -> spec.recipe(new FindFieldsOfType(type, null)),
java(
"""
import java.util.*;
import java.security.SecureRandom;
import java.util.List;
public class A {
private List<?> list;
private Set<?> set;
private SecureRandom rand;
}
""",
"""
import java.util.*;
import java.security.SecureRandom;
import java.util.List;
public class A {
/*~~>*/private List<?> list;
private Set<?> set;
private SecureRandom rand;
}
"""
)
Expand All @@ -69,7 +72,7 @@ public class A {
@Test
void findArrayOfType() {
rewriteRun(
spec -> spec.recipe(new FindFieldsOfType("java.lang.String")),
spec -> spec.recipe(new FindFieldsOfType("java.lang.String", null)),
java(
"""
import java.util.*;
Expand All @@ -83,15 +86,15 @@ public class A {
/*~~>*/private String[] s;
}
"""
)
)
);
}

@SuppressWarnings("EmptyTryBlock")
@Test
void skipsMultiCatches() {
rewriteRun(
spec -> spec.recipe(new FindFieldsOfType("java.io.File")),
spec -> spec.recipe(new FindFieldsOfType("java.io.File", null)),
java(
"""
import java.io.*;
Expand All @@ -116,5 +119,4 @@ public void test() {
)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,26 @@ class FindFieldsTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new FindFields("java.nio.charset.StandardCharsets", "UTF_8"));
spec.recipe(new FindFields("java.nio.charset.StandardCharsets", null,"UTF_8"));
}

@Test
void fieldMatch() {
rewriteRun(
spec -> spec.recipe(new FindFields("java.nio..*", true, "*")),
java(
"""
class Test {
Object o = java.nio.charset.StandardCharsets.UTF_8;
}
""",
"""
class Test {
Object o = /*~~>*/java.nio.charset.StandardCharsets.UTF_8;
}
"""
)
);
}

@DocumentExample
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FindImplementationsTest implements RewriteTest {
@Test
void found() {
rewriteRun(
spec -> spec.recipe(new FindImplementations("java.lang.Runnable")),
spec -> spec.recipe(new FindImplementations("java.lang.Runnable", null)),
java(
"""
class Test implements Runnable {
Expand All @@ -50,7 +50,7 @@ public void run() {
@Test
void notFound() {
rewriteRun(
spec -> spec.recipe(new FindImplementations("java.lang.Runnable")),
spec -> spec.recipe(new FindImplementations("java.lang.Runnable", null)),
java(
"""
class Test {
Expand All @@ -62,10 +62,11 @@ public void run() {
);
}

@SuppressWarnings("NullableProblems")
@Test
void genericType() {
rewriteRun(
spec -> spec.recipe(new FindImplementations("java.lang.Comparable<java.lang.String>")),
spec -> spec.recipe(new FindImplementations("java.lang.Comparable<java.lang.String>", null)),
java(
"""
class Test implements Comparable<String> {
Expand All @@ -87,10 +88,11 @@ public int compareTo(String o) {
);
}

@SuppressWarnings("NullableProblems")
@Test
void genericType2() {
rewriteRun(
spec -> spec.recipe(new FindImplementations("java.lang.Comparable")),
spec -> spec.recipe(new FindImplementations("java.lang.Comparable", null)),
java(
"""
class Test implements Comparable<String> {
Expand All @@ -112,10 +114,11 @@ public int compareTo(String o) {
);
}

@SuppressWarnings("NullableProblems")
@Test
void unmatchedGenericType() {
rewriteRun(
spec -> spec.recipe(new FindImplementations("java.lang.Comparable<java.lang.Runnable>")),
spec -> spec.recipe(new FindImplementations("java.lang.Comparable<java.lang.Runnable>", null)),
java(
"""
class Test implements Comparable<String> {
Expand All @@ -132,7 +135,7 @@ public int compareTo(String o) {
@Test
void transitiveImplementsExtends() {
rewriteRun(
spec -> spec.recipe(new FindImplementations("org.x.A")),
spec -> spec.recipe(new FindImplementations("org.x.A", null)),
java(
"""
package org.x;
Expand Down Expand Up @@ -189,7 +192,7 @@ public void bar() {
@Test
void transitiveExtendsImplements() {
rewriteRun(
spec -> spec.recipe(new FindImplementations("org.x.A")),
spec -> spec.recipe(new FindImplementations("org.x.A", null)),
java(
"""
package org.x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FindImportsTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new FindImports("java.util..*"));
spec.recipe(new FindImports("java.util..*", null));
}

@Test
Expand Down Expand Up @@ -79,7 +79,7 @@ class Test {
@Test
void starImportMatchesExact() {
rewriteRun(
spec -> spec.recipe(new FindImports("java.util.List")),
spec -> spec.recipe(new FindImports("java.util.List", null)),
java(
"""
import java.util.*;
Expand Down
23 changes: 10 additions & 13 deletions rewrite-java/src/main/antlr/TemplateParameterLexer.tokens
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
LPAREN=1
RPAREN=2
LBRACK=3
RBRACK=4
DOT=5
COMMA=6
SPACE=7
FullyQualifiedName=8
Number=9
Identifier=10
DOT=3
COLON=4
COMMA=5
FullyQualifiedName=6
Number=7
Identifier=8
S=9
'('=1
')'=2
'['=3
']'=4
'.'=5
','=6
' '=7
'.'=3
':'=4
','=5
Loading

0 comments on commit 6873fbd

Please sign in to comment.