openrewrite / rewrite-maven-plugin

OpenRewrite's Maven plugin.
https://openrewrite.github.io/rewrite-maven-plugin/plugin-info.html
Apache License 2.0
146 stars 76 forks source link

Recipes invoked by the rewrite-maven-plugin throw IllegalArgument exceptions when initializing a JavaParsers classpath. #239

Closed pway99 closed 3 years ago

pway99 commented 3 years ago

Running the rewrite-spring#GetErrorAttributes recipe from the rewrite-maven-plugin throws an IllegalArgumentException

The rewrite-spring GetErrorAttributes recipes leverages a JavaTemplate having a JavaParser with a defined classpath.

mi = mi.withTemplate(JavaTemplate.builder(this::getCursor, template)
                .imports("org.springframework.boot.web.error.ErrorAttributeOptions")
                .javaParser(() -> JavaParser.fromJavaVersion()
                        .classpath("spring-boot", "spring-web")
                        .build())
                .build(),
        mi.getCoordinates().replaceArguments(),
        mi.getArguments().get(0)
[DEBUG] 
java.lang.IllegalArgumentException: Unable to find runtime dependencies beginning with: 'spring-boot', 'spring-web'
    at org.openrewrite.java.JavaParser.dependenciesFromClasspath (JavaParser.java:86)
    at org.openrewrite.java.JavaParser$Builder.classpath (JavaParser.java:220)
    at org.openrewrite.java.spring.boot2.GetErrorAttributes$GetErrorAttributesVisitor.lambda$visitMethodInvocation$0 (GetErrorAttributes.java:75)
    at org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate (JavaTemplateParser.java:243)
    at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$parseMethodArguments$9 (JavaTemplateParser.java:193)
    at org.openrewrite.java.internal.template.JavaTemplateParser.cache (JavaTemplateParser.java:252)
    at org.openrewrite.java.internal.template.JavaTemplateParser.parseMethodArguments (JavaTemplateParser.java:192)
    at org.openrewrite.java.JavaTemplate$2.visitMethodInvocation (JavaTemplate.java:388)
    at org.openrewrite.java.JavaTemplate$2.visitMethodInvocation (JavaTemplate.java:102)
    at org.openrewrite.java.tree.J$MethodInvocation.acceptJava (J.java:3237)
    at org.openrewrite.java.tree.J.accept (J.java:64)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:193)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:144)
    at org.openrewrite.java.JavaTemplate.withTemplate (JavaTemplate.java:486)
    at org.openrewrite.java.JavaTemplate.withTemplate (JavaTemplate.java:42)
    at org.openrewrite.java.tree.J.withTemplate (J.java:93)
    at org.openrewrite.java.spring.boot2.GetErrorAttributes$GetErrorAttributesVisitor.visitMethodInvocation (GetErrorAttributes.java:72)
pway99 commented 3 years ago

This is actually a case where setting the class path on the parser is not a viable option, using depends on is the correct approach.