注解可以用于代码注入吗? [英] Can annotations be used for code injection?

查看:26
本文介绍了注解可以用于代码注入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这可能是一个已经被提出并回答过的问题,但请耐心等待.

I realise that this might be a question that has been asked and answered, but please bear with me.

我想知道是否可以使用注释将代码注入您的类编译时.经典示例是为对象的成员生成 getter 和 setter.这并不完全是我需要的,但它有助于说明基本思想.

I want to know if it is possible to use annotations to inject code into your classes compile time. The classic example is to generate a getter and setter for the members of your object. This is not exactly what I need it for, but it serves to illustrate the basic idea.

现在在互联网上我得到的基本答案是否定的,但这个人做到了:

Now on the internet the basic answer I get is no, but this guy did it:

链接文本

有谁知道他是如何做他所做的(以及他是否真的做到了他所说的)?

Does anyone know how he does what he does (and if he actually does what he says he does)?

主要是他没有使用注解处理器生成新的java文件来编译.我知道这种技术,但不会为我们的目的工作.

The main thing is that he does not use an annotation processor to generate a new java file to compile. This technique I am aware of and will not work for our purpose.

谢谢

推荐答案

不支持在编译时修改代码,但似乎可以通过使用不受支持的 javac-internal API,这里 是一篇引用 hanbuy-panno 解决方案的帖子,还有一个链接到 代码...

It is not supported to modify code at compile time but it seems to be possible by using non-supported javac-internal APIs, here is a post referencing the hanbuy-panno solution with also a link to the code...

这篇关于注解可以用于代码注入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆