验证参数在编译时间内传递到构造函数

发布于 2025-01-29 08:32:57 字数 1135 浏览 2 评论 0原文

在一个具有数万个Java文件的庞大项目中,有几个Java类,

public byte[] getProductReport(List<String> products, Date from, Date to) {
    // ... do some stuff before...

    List<ReportParameterDto> reportParameters = new ArrayList<>();
    reportParameters.add(new ReportParameterDto("From (YYYY.MM.DD)", ParameterType.DATE, from));
    reportParameters.add(new ReportParameterDto("To_(YYYY.MM.DD)", ParameterType.DATE, to));
    reportParameters.add(new ReportParameterDto("Products", ParameterType.SELECT, someList));

    return ReportFromCRServerHelper.downloadReport("ProductReporot", reportParameters, ReportFormat.PDF);
}

如果开发人员使用错误的字符串值下载请求的报告(从远程报告服务器,则 开发人员可以将字符串作为参数传递给我实现的构造函数类别。 )在运行时会失败。

在此示例中,我想进行一些验证检查 - 在编译过程中 - 以避免在客户发现这些错误之前。

我有API方法可以从我希望使用的报告中获取参数值 在上述方法的汇编过程中。

在我的示例中,汇编应该失败并引发错误,突出显示参数应如何看:

 "From (JJJJ-MM)" is invalid --> should be "From_(JJJJ-MM)"
 "Products" is invalid --> should be "PRODUCT_LIST"

我可以通过Javax注释处理处理这些参数(在上面的ReportParameterDto构造函数中使用)吗?

我发现的一些教程 /博客涉及方法签名中的验证参数,而不是传递到方法中的值。

还是有更优雅的工具可用?

In a huge project with tens of thousands of Java files there are a couple of Java classes where developers may pass in strings as parameters to a constructor class I had implemented

public byte[] getProductReport(List<String> products, Date from, Date to) {
    // ... do some stuff before...

    List<ReportParameterDto> reportParameters = new ArrayList<>();
    reportParameters.add(new ReportParameterDto("From (YYYY.MM.DD)", ParameterType.DATE, from));
    reportParameters.add(new ReportParameterDto("To_(YYYY.MM.DD)", ParameterType.DATE, to));
    reportParameters.add(new ReportParameterDto("Products", ParameterType.SELECT, someList));

    return ReportFromCRServerHelper.downloadReport("ProductReporot", reportParameters, ReportFormat.PDF);
}

If a developer uses wrong string values downloading a requested report (from a remote Report server) will fail during runtime.

In this example I would like to have some validation checking - during compilation - in order to avoid these errors before they are found by the customer.

I have API methods to obtain parameter values from a report which I hope to use
during compilation of the above method.

In my example the compilation should fail and throw an error highlighting how parameters should look instead:

 "From (JJJJ-MM)" is invalid --> should be "From_(JJJJ-MM)"
 "Products" is invalid --> should be "PRODUCT_LIST"

Can I detect these parameters (used in above ReportParameterDto constructors) through JAVAX annotation processing?

The few tutorials / blogs that I found dealt with validating parameters in method signatures, not the values passed into methods.

Or are there a more elegant tools available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2025-02-05 08:32:57

Checker框架可以将参数验证为方法或构造函数。您可以注释参数类型以指示允许的值,然后当javac运行时,如果参数可能与参数不兼容,则会发出警告。

如果可能的值数量有限,则可以使用 nofollow noreferrer“>假枚举检查器将字符串或整数视为枚举值。 (使用Java enum也是一个好主意,但由于期望字符串或整数的其他代码可能是不可能或方便的。)

如果可能的值数量是无限的,那么您可以使用常数值检查器字符串参数必须满足。

您还可以定义自己的编译时验证与Checker Framework分发的Checkers中可用。

A compile-time tool like the Checker Framework can validate the arguments to a method or constructor. You annotate the parameter types to indicate the permitted values, and then when javac runs it issues a warning if an argument may not be compatible with the parameter.

If there is a limited number of possible values, then you can use the Fake Enum Checker to treat strings or integers as enumerated values. (Using a Java enum is also a good idea, but may not be possible or convenient because of other code that expects a string or integer.)

If the number of possible values is unlimited, then you can use the Constant Value Checker -- for example, to supply a regular expression that any constant string argument must satisfy.

You can also define your own compile-time validation if you want different functionality than is available in the checkers that are distributed with the Checker Framework.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文