Tag: compiler options

我可以使用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 { } 我想我可以开始设计一个策略来在运行时使用reflection实现它,但我想指示编译器或预处理器为我检查那些东西,所以我的注释将没有开销。 这是你认为“如果可以做到这一点,它已经在那里,如果是,我可以在哪里抓住它”的事情之一。 编辑 :在进一步考虑@Const和@Immutable之后,在记住java通过值传递指向对象的指针之后,我扩展了@Const的定义,摆脱了@Immutable ,并改变了@Out的定义,如下所示: /** * When Applied to a method, ensures the method doesn’t […]