简体   繁体   English

创建对象时如何处理注释和设置默认值

[英]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:多态构造函数步骤:

  1. Create an @PolyMyAnnotation polymorphic annotation, see Polymorphic Qualifiers 创建一个@PolyMyAnnotation多态注释,请参见多态限定符
  2. Create a PolyMorphic constructor for MyClass, eg @PolyMyAnnotation MyClass(@PolyMyAnnotation int a) {} 为MyClass创建PolyMorphic构造函数,例如@PolyMyAnnotation MyClass(@PolyMyAnnotation int a){}
  3. Create a TreeAnnotator that adds @MyAnnotation to the type's of int literals. 创建一个TreeAnnotator,将@MyAnnotation添加到int文字的类型。 See RegexTreeAnnotator.visitLiteral for an example. 有关示例 ,请参见RegexTreeAnnotator.visitLiteral
  4. Add your TreeAnnotator to the list of TreeAnnotators used by your AnnotatedTypeFactory. 将您的TreeAnnotator添加到AnnotatedTypeFactory使用的TreeAnnotator列表中。 (See RegexAnnotatedTypeFactory (请参见RegexAnnotatedTypeFactory

Option B: Use A TreeAnnotator Steps: 选项B:使用TreeAnnotator步骤:

  1. Create a class that extends TreeAnnotator 创建一个扩展TreeAnnotator的类
  2. Override visitNewClass 覆盖visitNewClass
  3. Identify if the object being constructed is an instance of MyClass (see ElementUtils.getQualifiedClassName and ElementUtils.isObjectType to see how you might do this). 标识正在构造的对象是否是MyClass的实例(请参阅ElementUtils.getQualifiedClassName和ElementUtils.isObjectType以了解如何执行此操作)。
  4. If the class being constructed is an instance of MyClass, if so, see if the argument is an Integer literal. 如果正在构造的类是MyClass的实例,则请查看参数是否为Integer文字。
  5. If the item is a literal, parse the literal and add it's value to the type being constructed. 如果该项是文字,则解析该文字并将其值添加到正在构造的类型中。 Note, if you would like this to handle non-literals, eg int a = 20; 注意,如果您希望这样处理非文字,例如int a = 20; new MyClass(a) 新的MyClass(a)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM