[英]How to process annotation and set a default value while creating object
I wanted to write code, which looks like below. 我想编写代码,如下所示。
MyClass a = new @MyAnnotation MyClass()
Assume MyClass method look like below 假设MyClass方法如下所示
public class MyClass {
int a;
public void setValue(int a) {
this.a = a;
}
}
So when some one creats object of MyClass with the annotation, it should set a default value to some x(say 20). 因此,当有人用注释创建MyClass对象时,应将默认值设置为x(例如20)。
In checker framework, it is allowing us to annotate in the code as below. 在checker框架中,它允许我们在以下代码中进行注释。
MyClass a = new @MyAnnotation MyClass()
Does any one know how can we get split this into two parts at the time of compiling like below? 有人知道在进行如下所示的编译时如何将其分为两部分吗?
MyClass a = new MyClass();
a.setValue(20);
or if we can able to pass defauilt value like below is also fine. 或者我们是否可以像下面那样传递默认值也可以。
MyClass a = new @MyAnnotation(value=20) MyClass()
Can this be achieved using annotation processor/ need to use bytecode processor like ASM to get it done? 可以使用注释处理器来实现这一点吗?需要使用像ASM这样的字节码处理器来完成它吗? Updated with more meaningful way. 以更有意义的方式更新。
Are you writing a custom type-system via meta-annotations? 您是否正在通过元注释编写自定义类型系统? That is, are you using the SubtypingChecker or are have you created your own subtype of BaseTypeChecker? 也就是说,您正在使用SubtypingChecker还是创建了自己的BaseTypeChecker子类型?
The short answer is, with meta-annotations you cannot specify this sort of behavior. 简短的答案是,对于元注释,您无法指定这种行为。 The long answer is, if you have a custom checker, then there are a number of ways you can do this. 长的答案是,如果您有一个自定义检查器,那么可以通过多种方法来执行此操作。
Keep in mind, however, that writing a custom checker is a fair bit more complicated then using meta-annotations. 但是请记住,编写自定义检查器比使用元注释要复杂得多。 But, if you have a custom checker, you can do one of the following: 但是,如果您有一个自定义检查器,则可以执行以下操作之一:
Option A: Polymorphic Constructors Steps: 选项A:多态构造函数步骤:
Option B: Use A TreeAnnotator Steps: 选项B:使用TreeAnnotator步骤:
Then you can use the ValueChecker. 然后,您可以使用ValueChecker。 IF your type factory extends ValueAnnotatedTypeFactory it will have support for constant propagation. 如果您的类型工厂扩展了ValueAnnotatedTypeFactory,它将支持常量传播。 The data flow API also provides some support for constant propagation. 数据流API还为持续传播提供了一些支持。
Option C: Customize dataflow's CFAbstractTransfer Function However, if you have a custom checker, you can override CFAbstractTransfer.visitMethodInvocation to detect when setValue is called and refine it accordingly. 选项C:自定义数据流的CFAbstractTransfer函数但是,如果您有一个自定义检查器,则可以重写CFAbstractTransfer.visitMethodInvocation来检测何时调用setValue并相应地对其进行优化。 You can look at RegexTransfer for an example, or search for methods that extend CFAbstractTransfer or implement TransferFunction. 您可以查看RegexTransfer的示例,或搜索扩展CFAbstractTransfer或实现TransferFunction的方法。
That said, dataflow is not a substitute for a type-state system (see a discussion of this at Typestate checkers ). 也就是说,数据流不能替代类型状态系统(请参阅Typestate Checkers对此的讨论)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.