编译方法参数的时间验证 [英] Compile time validation of method arguments

查看:104
本文介绍了编译方法参数的时间验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里发现了一些类似的问题,但是不完整的答案没有帮助,并且产生了更多的混淆而不是澄清任何事情,所以这是我尝试提出一个更有条理的问题并获得有希望的答案,这将有助于更多用户。

I've found some similar questions here, but the incomplete answers didn't help and produced more confusion than clarifying anything, so here's my try to give a more structured question and get hopefully answers that will help more users.

我的简化示例:我有一个带有两个不同构造函数的Java类

My simplified example: I have a Java class with two different constructors

public class ObjectOfInterest {
  public ObjectOfInterest(String string, Integer int) { ... }
  public ObjectOfInterest(String string1, String string2) { ... }
  ...
}

我需要对这些构造函数的调用进行一些编译时验证。参数 string2 必须是一些文字,我想根据内容将调用标记为警告(即发出警告,当它不是文字或文字时有不正确的格式)。

I need some compile time validation on the calls of these constructors. The parameter string2 has to be some literal, and I want to mark the calls as warning depending on the content (i.e. give a warning, when it's no literal or when the literal has not the correct format).

不幸的是,使用Eclipse进行Java验证的文档不容易理解,有时已经过时,通常在我看来不完整,似乎那里没有足够短的实例可供教程使用。

Unfortunately documentation for validation in Java with Eclipse isn't easy to understand, sometimes outdated, most often it seems to me incomplete, and it seems that there's no working example out there that's short enough to be used in a tutorial.

我的目标
第一步:我我希望有一个验证器,用于警告那两个参数版本的调用 - 只是为了开始某个地方并了解基础知识。

My goal: First step: I'd like to have a validator that marks calls of that two parameter version with a warning - just to get started somewhere and get to understand the basics.

什么到目前为止我找到了
我见过的几个例子是一个公共类MyValidator实现了IValidator,ISourceValidator 其中 IValidator 需要实现一个方法 public void validate(IValidationContext arg0, IReporter arg1)抛出ValidationException ,似乎来自验证框架的旧版本(有时我发现只有空方法,注释无用),并且 ISourceValidator 需要实现一个方法 public void validate(IRegion arg0,IValidationContext arg1,IReporter arg2) - 这似乎是更新的版本。

What I've found so far: The few examples I've seen make a public class MyValidator implements IValidator, ISourceValidator where IValidator needs to implement a method public void validate(IValidationContext arg0, IReporter arg1) throws ValidationException and seem to be from an old version of the validation framework (sometimes I found just empty method with comment useless), and ISourceValidator needs to implement a method public void validate(IRegion arg0, IValidationContext arg1, IReporter arg2) - this seems to be the more up to date version.

然后你必须为一些 plugin.xml 添加一些扩展点(我不是很清楚哪里这个plugin.xml是)。

Then you have to add some extension points to some plugin.xml (I'm not totally clear where this plugin.xml is).

我在黑暗中刺伤:完全不清楚如何使用 IRegion IValidationContext IReporter - 也许我的方式完全错误,但是我能得到什么?如何在验证中找到该构造函数的调用?

Where I stab in the dark: it's totally unclear how to work with IRegion, IValidationContext, and IReporter - maybe I'm on the totally wrong way, but what do I get here? How to find calls of that constructor within validation?

在第一步更清楚之后,我会扩展这个问题。 Outlook,我想要为构造函数的两个String版本添加快速修复的可能性,并以这种方式操作代码 - 但这至少要提前两步,详情稍晚。

I'd extend this question after the first steps get more clear. Outlook, I'll want to add the possibility of quick fixes to the two String version of the constructor and manipulate the code this way - but that's at least two steps ahead, details coming later.

推荐答案

首先我要说的是你想要超越正常的Java编程。除了使用普通类型可以实现的功能之外,在编译时没有正常的方法进行验证。

First I have to say that what you are trying to goes beyond normal Java programming. There is no normal way do validation at compile time, except what can be achieved using normal types.

您想要做的事情超出使用注释处理器可以做的事情。注释处理器是一种半正常的,因为它们是标准化的并且是Java框架的一部分。它们是在编译期间运行的类,它们将类和方法的签名作为输入,并可用于验证和代码生成。

What you want to do also goes beyond what can be done using annotation processors. Annotation processors are kind of semi-normal, in that they are standardised and a part of the Java framework. They are classes that are run during compilation, they get as input the signature of classes and methods, and can be used for validation and code generation.

如果你仍然要做什么有异常的方式,但是:

If you still what to do this there are abnormal ways, however:

您似乎尝试的解决方案是编写一个Eclipse插件,该插件使用Eclipse Java工具进行验证。这应该有用,我不知道它会有多容易,验证只适用于使用Eclipse的用户。

The solution you seem to be trying is to write an Eclipse plug-in that uses the Eclipse Java tools to do validation. That should work, I don't know how easy it will be, and the validation will only work for users who are using Eclipse.

根据我(有限)知识看来最好的工具是:

The tool which seems best according to my (limited) knowledge is this:

用于静态分析的 Checker Framework

The Checker Framework for static analysis.

它用于你想要做的事情。它似乎有充分的文档记录,易于设置。它用于执行空值分析和正则表达式语法的编译时验证。有一个教程听起来与你的东西很相似。有关创建新检查程序的手册中有一章。

It is used for exactly the kind of things that you want to do. It seems reasonably well-documented and easy to set up. It is used for example for doing nullness analysis and compile time validation of regex syntax. There is a tutorial which sounds pretty similar to your thing. There is a chapter in the manual on creating a new checker.

用这个来制作解决方案可能需要花费很多时间和精力,但我认为它看起来很有趣!

It would probably take a lot of time and effort to make a solution using this, but I also think it seems very interesting!

这篇关于编译方法参数的时间验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆