Java 注释处理——如何处理已经处理过的代码?
Java annotation processing - how to handle already processed code?
如果我有 class
@GenerateInterface
public class _FooImpl {}
我要生成界面
public interface Foo {}
基本上包含了_FooImpl
的所有方法(即getters和setters)
还有如果我有继承权,比如
@GenerateInterface
public class _ParentImpl {}
@GenerateInterface
public class _ChildImpl extends _ParentImpl {}
这也应该导致接口继承
public interface Parent {}
public interface Child extends Parent {}
我大致知道如何做到这一点。但是如果 _ParentImpl
是图书馆的一部分呢?该库还必须包含 Parent
.
在注释处理器中,我该如何处理?我无法再次生成 Parent
界面,因为那样我将有两次相同的界面。我能否以某种方式检测到它已经存在但将其与同样已经存在但不属于库的文件并因此可以被覆盖的文件区分开来?
我实际上只需要一个 class/interface 可以扩展的东西。因此我可以从 _ParentImpl
中删除 @GenerateInterface
并对 Parent
接口的继承进行硬编码。不过,这将是我最后的选择。 编辑: 或者我可以设置 @Retention(RetentionPolicy.SOURCE)
,而不是完全删除界面,对吧?
已经编译类不再处理
我创建了一个完整的测试设置并亲自探索了这个问题。事实证明,只有真正需要编译的文件是 运行 通过注释处理。这意味着我可以安全地将 _ParentImpl
和 Parent
包含在库中。 _ParentImpl
可以保留注释,然后可以使用 @Retention(RetentionPolicy.CLASS)
结合 @Inherited
以非常方便的方式将注释应用于 sub类。
如果我有 class
@GenerateInterface
public class _FooImpl {}
我要生成界面
public interface Foo {}
基本上包含了_FooImpl
的所有方法(即getters和setters)
还有如果我有继承权,比如
@GenerateInterface
public class _ParentImpl {}
@GenerateInterface
public class _ChildImpl extends _ParentImpl {}
这也应该导致接口继承
public interface Parent {}
public interface Child extends Parent {}
我大致知道如何做到这一点。但是如果 _ParentImpl
是图书馆的一部分呢?该库还必须包含 Parent
.
在注释处理器中,我该如何处理?我无法再次生成 Parent
界面,因为那样我将有两次相同的界面。我能否以某种方式检测到它已经存在但将其与同样已经存在但不属于库的文件并因此可以被覆盖的文件区分开来?
我实际上只需要一个 class/interface 可以扩展的东西。因此我可以从 _ParentImpl
中删除 @GenerateInterface
并对 Parent
接口的继承进行硬编码。不过,这将是我最后的选择。 编辑: 或者我可以设置 @Retention(RetentionPolicy.SOURCE)
,而不是完全删除界面,对吧?
已经编译类不再处理
我创建了一个完整的测试设置并亲自探索了这个问题。事实证明,只有真正需要编译的文件是 运行 通过注释处理。这意味着我可以安全地将 _ParentImpl
和 Parent
包含在库中。 _ParentImpl
可以保留注释,然后可以使用 @Retention(RetentionPolicy.CLASS)
结合 @Inherited
以非常方便的方式将注释应用于 sub类。