openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.19k stars 329 forks source link

NullPointerException when using FindDeprecatedUses #2196

Closed mricken closed 2 years ago

mricken commented 2 years ago

I'm trying to use the FindDeprecatedUses recipe, but on my very simple example, I get a NullPointerException.

I have the following in my pom.xml file:

      <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>4.33.0</version>
        <configuration>
          <activeRecipes>
            <recipe>org.openrewrite.java.search.FindDeprecatedUses</recipe>
          </activeRecipes>
        </configuration>
      </plugin>

and this is my test file:

public class HasDeprecatedUses {
    @Deprecated
    public static void foo() {
        System.out.println("Deprecated!");
    }

    public static void main(String[] args) {
        // method is deprecated
        // https://github.com/oracle/oci-java-sdk/blob/v2.38.0/bmc-common/src/main/java/com/oracle/bmc/ServiceDetails.java#L28
        com.oracle.bmc.ServiceDetails.setServiceDetails(null, null, null, null);
        foo();
    }
}

When I run mvn rewrite:dryRun -X, I get:

[INFO] Using active recipe(s) [org.openrewrite.java.search.FindDeprecatedUses]
[INFO] Using active styles(s) []
[INFO] Validating active recipes...
[INFO] Project [open-rewrite-find-deprecated-uses-test] Resolving Poms...
[DEBUG] Project [open-rewrite-find-deprecated-uses-test] Base Directory : '/Users/mricken/Documents/Projects/micronaut/bugs/deprecateduses'
[DEBUG] Project [open-rewrite-find-deprecated-uses-test]   Collected Pom : '/Users/mricken/Documents/Projects/micronaut/bugs/deprecateduses/pom.xml'
[DEBUG] Project [open-rewrite-find-deprecated-uses-test]   Maven Source : '/Users/mricken/Documents/Projects/micronaut/bugs/deprecateduses/pom.xml'
[INFO] Project [open-rewrite-find-deprecated-uses-test] Parsing Source Files
[DEBUG] Project [open-rewrite-find-deprecated-uses-test] Parsed 1 java source files in main scope.
[DEBUG] Project [open-rewrite-find-deprecated-uses-test] Parsed 0 resource files in main scope.
[DEBUG] Project [open-rewrite-find-deprecated-uses-test] Parsed 0 java source files in test scope.
[DEBUG] Project [open-rewrite-find-deprecated-uses-test] Parsed 0 resource files in test scope.
[DEBUG] Project [open-rewrite-find-deprecated-uses-test] Parsed 0 additional files found within the project.
[INFO] Running recipe(s)...
line 1:0 extraneous input ' ' expecting {'.', '!', '*', '..', Identifier}
line 1:0 extraneous input ' ' expecting {'.', '!', '*', '..', Identifier}
line 1:2 mismatched input '(' expecting {'<constructor>', '.', '*', '#', ' ', Identifier}
line 1:2 mismatched input '(' expecting {'<constructor>', '.', '*', '#', ' ', Identifier}
[WARNING]
[WARNING]
[DEBUG]
java.lang.NullPointerException
    at org.openrewrite.java.MethodMatcher$1.visitMethodPattern (MethodMatcher.java:90)
    at org.openrewrite.java.MethodMatcher$1.visitMethodPattern (MethodMatcher.java:86)
    at org.openrewrite.java.internal.grammar.MethodSignatureParser$MethodPatternContext.accept (MethodSignatureParser.java:147)
    at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit (AbstractParseTreeVisitor.java:18)
    at org.openrewrite.java.MethodMatcher.<init> (MethodMatcher.java:97)
    at org.openrewrite.java.search.FindDeprecatedMethods.getSingleSourceApplicableTest (FindDeprecatedMethods.java:63)
    at org.openrewrite.java.search.FindDeprecatedMethods.getSingleSourceApplicableTest (FindDeprecatedMethods.java:35)
    at org.openrewrite.RecipeScheduler.lambda$scheduleVisit$5 (RecipeScheduler.java:204)
    at org.openrewrite.RecipeScheduler.lambda$mapAsync$0 (RecipeScheduler.java:49)
    at org.openrewrite.scheduling.ForkJoinScheduler.lambda$schedule$0 (ForkJoinScheduler.java:41)
    at java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec (ForkJoinTask.java:1386)
    at java.util.concurrent.ForkJoinTask.doExec (ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask (ForkJoinPool.java:1056)
    at java.util.concurrent.ForkJoinPool.runWorker (ForkJoinPool.java:1692)
    at java.util.concurrent.ForkJoinWorkerThread.run (ForkJoinWorkerThread.java:157)
[DEBUG]
java.lang.NullPointerException
    at org.openrewrite.java.MethodMatcher$1.visitMethodPattern (MethodMatcher.java:90)
    at org.openrewrite.java.MethodMatcher$1.visitMethodPattern (MethodMatcher.java:86)
    at org.openrewrite.java.internal.grammar.MethodSignatureParser$MethodPatternContext.accept (MethodSignatureParser.java:147)
    at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit (AbstractParseTreeVisitor.java:18)
    at org.openrewrite.java.MethodMatcher.<init> (MethodMatcher.java:97)
    at org.openrewrite.java.search.FindDeprecatedMethods.getSingleSourceApplicableTest (FindDeprecatedMethods.java:63)
    at org.openrewrite.java.search.FindDeprecatedMethods.getSingleSourceApplicableTest (FindDeprecatedMethods.java:35)
    at org.openrewrite.RecipeScheduler.lambda$scheduleVisit$5 (RecipeScheduler.java:204)
    at org.openrewrite.RecipeScheduler.lambda$mapAsync$0 (RecipeScheduler.java:49)
    at org.openrewrite.scheduling.ForkJoinScheduler.lambda$schedule$0 (ForkJoinScheduler.java:41)
    at java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec (ForkJoinTask.java:1386)
    at java.util.concurrent.ForkJoinTask.doExec (ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask (ForkJoinPool.java:1056)
    at java.util.concurrent.ForkJoinPool.runWorker (ForkJoinPool.java:1692)
    at java.util.concurrent.ForkJoinWorkerThread.run (ForkJoinWorkerThread.java:157)
line 1:0 extraneous input ' ' expecting {'.', '!', '*', '..', Identifier}
line 1:0 extraneous input ' ' expecting {'.', '!', '*', '..', Identifier}
line 1:2 mismatched input '(' expecting {'<constructor>', '.', '*', '#', ' ', Identifier}
line 1:2 mismatched input '(' expecting {'<constructor>', '.', '*', '#', ' ', Identifier}
line 1:0 extraneous input ' ' expecting {'.', '!', '*', '..', Identifier}
line 1:0 extraneous input ' ' expecting {'.', '!', '*', '..', Identifier}
line 1:2 mismatched input '(' expecting {'<constructor>', '.', '*', '#', ' ', Identifier}
line 1:2 mismatched input '(' expecting {'<constructor>', '.', '*', '#', ' ', Identifier}
line 1:0 extraneous input ' ' expecting {'.', '!', '*', '..', Identifier}
line 1:2 mismatched input '(' expecting {'<constructor>', '.', '*', '#', ' ', Identifier}
line 1:0 extraneous input ' ' expecting {'.', '!', '*', '..', Identifier}
line 1:0 extraneous input ' ' expecting {'.', '!', '*', '..', Identifier}
line 1:2 mismatched input '(' expecting {'<constructor>', '.', '*', '#', ' ', Identifier}
line 1:2 mismatched input '(' expecting {'<constructor>', '.', '*', '#', ' ', Identifier}
line 1:0 extraneous input ' ' expecting {'.', '!', '*', '..', Identifier}
line 1:2 mismatched input '(' expecting {'<constructor>', '.', '*', '#', ' ', Identifier}
[INFO] Applying recipes would make no changes. No patch file generated.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

Full zip file with small Maven project:

deprecateduses.zip

$ javac -version
javac 1.8.0_121
(575) 20:32:54 - mricken@mricken-Mac
$ mvn -version
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /usr/local/Cellar/maven/3.8.6/libexec
Java version: 1.8.0_121, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.7", arch: "x86_64", family: "mac"
jkschneider commented 2 years ago

Hi @mricken. We should make this more clear in the docs, so sorry for the trouble. This is a recipe that has a required input (unlike some which are able to simply run).

I suggest you define a YAML that contains a specification for the recipe with the options for what you are looking for. For example:

type: specs.openrewrite.org/v1beta/recipe
name: io.moderne.FindDeprecated
displayName: Find some deprecated use
description: Fix all the things.
recipeList:
  - org.openrewrite.java.search.FindDeprecatedMethods:
      methodPattern: org.springframework..* *(..)

Point configLocation at the place where you put this YAML file as indicated in https://docs.openrewrite.org/reference/rewrite-maven-plugin.

Hope this helps!

You can play with the values here: https://public.moderne.io/recipes/org.openrewrite.java.search.FindDeprecatedMethods?defaults=W3sibmFtZSI6Im1ldGhvZFBhdHRlcm4iLCJ2YWx1ZSI6Im9yZy5zcHJpbmdmcmFtZXdvcmsuLiogKiguLikifSx7Im5hbWUiOiJpZ25vcmVEZXByZWNhdGVkU2NvcGVzIiwidmFsdWUiOiIifV0=

mricken commented 2 years ago

Now it passes, but it's not complaining about any deprecated uses. Am I wrong that it should detect both of the method invocations here?

        com.oracle.bmc.ServiceDetails.setServiceDetails(null, null, null, null);
        foo();