我可以使用 Java 注释来定义编译时检查吗?
例如,我想创建@Out目标参数的注释。然后,我将以某种方式使用编译器来检查在函数返回之前是否设置了参数值。这可能吗?
还正在考虑一个@Immutable注释,该注释将不允许调用任何未标有@Const的方法或访问任何公共字段。(编译时,可能是运行时?
到目前为止,我有这个:
//I'm assuming Class retention is a subset of Runtime retention
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Out
{
//no idea what to go in here.
}
这是另一个注释。同样,我对它没有完整的定义:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Immutable
{
}
我想我可以开始设计一个策略,在运行时使用反射来实现它,但我想指示编译器或预处理器为我检查这些东西,这样我的注释就不会有任何开销。
这是你认为“如果这可以完成,它已经在那里,如果是这样,我在哪里可以抓住它”的事情之一。
编辑:经过进一步的思考,并且在记住java按值传递指向对象的指针之后,我扩展了的定义,摆脱了,并改变了的定义,如下所示:@Const
@Immutable
@Const
@Immutable
@Out
/**
* When Applied to a method, ensures the method doesn't change in any
* way the state of the object used to invoke it, i.e., all the fields
* of the object must remain the same, and no field may be returned,
* unless the field itself is marked as {@code @Const}. A method
* annotated with {@code @Const} can only invoke other {@code @Const}
* methods of its class, can only use the class's fields to invoke
* {@code @Const} methods of the fields classes and can only pass fields
* as parameters to methods that annotate that formal parameter as
* {@code @Const}.
*
* When applied to a formal parameter, ensures the method will not
* modify the value referenced by the formal parameter. A formal
* parameter annotated as {@code @Const} will not be aliased inside the
* body of the method. The method is not allowed to invoke another
* method and pass the annotated parameter, save if the other method
* also annotates the formal parameter as {@code @Const}. The method is
* not allowed to use the parameter to invoke any of its type's methods,
* unless the method being invoked is also annotated as {@code @Const}
*
* When applied to a field, ensures the field cannot be aliased and that
* no code can alter the state of that field, either from inside the
* class that owns the field or from outside it. Any constructor in any
* derived class is allowed to set the value of the field and invoke any
* methods using it. As for methods, only those annotated as
* {@code @Const} may be invoked using the field. The field may only be
* passed as a parameter to a method if the method annotates the
* corresponding formal parameter as {@code @Const}
*
* When applied to a local variable, ensures neither the block where the
* variable is declared or any nested block will alter the value of that
* local variable. The local variable may be defined only once, at any
* point where it is in scope. Only methods annotated as
* {@code @Const} may be invoked using this variable, and the variable
* may only be passed as a parameter to another method if said method
* annotates its corresponding formal parameter as {@code @Const}
*
*/
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD,
ElementType.LOCAL_VARIABLE})
@Inherited
public @interface Const
{
}
现在:@Out
/**
* The formal parameter annotated with {@code @Out} must be undefined in
* the scope of the caller, and it's the responsibility of the method to
* define it. If allowNull is true, the parameter can be explicitly set
* to null in the body of the method.
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.PARAMETER)
public @interface Out
{
boolean allowNull() default false;
}
编辑:我试图将其实现为eclipse插件,但我完全无法阅读手册。我写了一个插件,其中包含访问AST和访问方法和字段的基本逻辑。然后我做了一堆我的插件应该检测到的虚拟注释,然后我尝试打印结果,但我甚至不确定会发生什么。我的插件是“增量构建”插件。这是它的代码,如果有人可以看一下,只是向我解释一些事情。我完全迷失在这个API中。