英文:
Write an Openrewrite java recipe test ignoring compilation errors
问题
It's possible to write an Openrewrite Java recipe test while ignoring compilation errors caused by unknown class types that are not included in the JavaParser classpath. In the example you provided, the spring-web artifact needs to be included in the test classpath for the org.springframework.web.bind.annotation.RequestParam type to be recognized.
Here's the translated content without code:
可以编写Openrewrite Java配方测试,同时忽略由于JavaParser类路径中未包含的未知类类型而引起的编译错误。在您提供的示例中,需要在测试类路径中包含spring-web工件,以便识别org.springframework.web.bind.annotation.RequestParam类型。
英文:
Is it possible to write an Openrewrite java recipe test ignoring compilation errors due to unknown class types not included in JavaParser classpath?
Example:
    @Test
    void requiredRequestParam() {
        rewriteRun(
                spec -> spec
                        .recipe(new MandatoryRequestParameter())
                        .parser(JavaParser.fromJavaVersion().classpath("spring-web")),
                java(
                        """
                          import org.springframework.web.bind.annotation.RequestParam;
                          
                          class ControllerClass {
                            public String sayHello (
                              @RequestParam(value = "fred") String fred,
                              @RequestParam(value = "lang") String lang,
                              @RequestParam(value = "aNumber") Long aNumber
                            ) {
                              return "Hello";
                            }
                          }
                        """,
                        """
                          import org.springframework.web.bind.annotation.RequestParam;
                          
                          class ControllerClass {
                            public String sayHello (
                              @RequestParam(required = true, value = "fred") String fred,
                              @RequestParam(value = "lang") String lang,
                              @RequestParam(required = true, value = "aNumber") Long aNumber
                            ) {
                              return "Hello";
                            }
                          }
                        """
                )
        );
    }
In this case, spring-web artifact must be included in test classpath for the org.springframework.web.bind.annotation.RequestParam type to be recognized.
答案1
得分: 0
你可以在 RecipeSpec 上调用 typeValidationOptions() 来实现此目的(参见下面的示例)。要禁用所有类型检查,您可以调用 typeValidationOptions(TypeValidation.none()),如果需要更精细的控制,TypeValidation 提供了一个构建器 API 以禁用特定的类型检查。
但正如 Tim 指出的,通常您会希望配置配方的类路径(使用 JavaParser#classpath() 或 JavaParser#classpathFromResources()),请参阅文档以获取详细信息,以便不会出现任何类型验证错误。在某些情况下仍然需要禁用类型验证错误(例如,当您希望检查配方在存在类型错误的情况下是否仍然执行有用的操作)。
@Test
void requiredRequestParam() {
    rewriteRun(
        spec -> spec
                .recipe(new MandatoryRequestParameter())
                .parser(JavaParser.fromJavaVersion().classpath("spring-web"))
                .typeValidationOptions(TypeValidation.none()),
        java(
            // ...
        )
    );
}
英文:
You can call typeValidationOptions() on the RecipeSpec for this purpose (see example below). To disable all type checks you can call typeValidationOptions(TypeValidation.none()) and to be more fine-grained, TypeValidation provides a builder API to disable specific type checks.
As pointed out by Tim however, you would normally want to configure the recipe classpath (using either JavaParser#classpath() or JavaParser#classpathFromResources(), see docs for details) so that you don't get any type validation errors. Disabling type validation errors can still be necessary in some situations (e.g. when you want to check that your recipe also does something useful in the presence of type errors).
    @Test
    void requiredRequestParam() {
        rewriteRun(
                spec -> spec
                        .recipe(new MandatoryRequestParameter())
                        .parser(JavaParser.fromJavaVersion().classpath("spring-web"))
                        .typeValidationOptions(TypeValidation.none()),
                java(
                    // ...
                )
        );
    }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论