英文:
custom annotations to modify values
问题
我需要你的支持,感谢你对Java/注解的支持。。
我想创建一个自定义注解,例如@ModifyRegex,来注释变量,
我的方法是修改/替换注释变量的值的某些部分。
例如:
@ModifyRegex
private String variable;
如果:
variable = "AbC-ABG-kkkk-4711";
那么:
variable = "ABC-4711-ABG-kkkk";
我不确定这是否可能,如果可以的话,请为我提供一个简单的代码示例。。
谢谢
英文:
I need your support and thanks for your Java/Annotations support..
I would to create a custom annotation f.e. @ModifyRegex to annotatate variables
and my approach is to modify/replace parts of the value of the annotated variables.
f.e.
@ModifyRegex
private String variable;
if:
variable = "AbC-ABG-kkkk-4711";
then:
variable = "ABC-4711-ABG-kkkk";
I am not sure if its possible or not, if yes please provide me a simple code example..
Thanks
答案1
得分: 1
注解处理器可以创建新的源文件。它们无法更改现有的文件。它们也无法读取方法内部的内容。实际上,它们不能读取任何代码,所以唯一可能看到的字符串字面量是它是否真的是字面量。Java语言规范定义了何时将赋给字段的值视为“编译时常量”,并直接写入。如果不是这样,可以说:
class Example {
// 所有这些都不是常量,因此无法使用注解处理器检索
long x = System.currentTimeMillis();
Pattern p = Pattern.compile("^AbC-ABG-$");
String s = null; // 对于某些原因,null 被视为非常量。
String z = "HELLO!".toLowerCase();
}
但是,如果真的很简单,比如 @Foo private String x = "Hello";
,其中 x
是一个字段(而不是方法中的局部变量),是的,你可以看到它在工作中。
但是你能做的只是创建新文件。你不能更改现有文件。所以,最多只能创建一个包含 public static final String variable2 = "ABC-4711-...";
的第二个类。
那么 Lombok 呢?
Project Lombok 能够做到我刚才说过的你无法做到的事情:它会检查实际的代码,并在运行时修改源文件。
不幸的是,Lombok 有数十万行的代码,其中大部分是为了完成所有这些操作而必要的:因为没有统一的方法来实现,所以需要大量的自定义代码。更重要的是,它本质上非常复杂:IDE 经常进行代码分析,如果更改结构,会影响从“保存时自动格式化我的文件”到重构脚本,再到“查找调用者”等方方面面。由于没有标准,你必须修改编辑器来弄清楚这一切。
更改字符串常量可能意味着你不需要进行太多的修补。即使如此,它仍然非常复杂。
如果你想调查,Lombok 是开源的。
注:我是 Project Lombok 的核心贡献者。
英文:
Annotation processors can make new source files. They cannot change existing ones. They also cannot read inside methods. They can't read any code, in fact, so the only string literal that you could possibly see is if it's literally (heh) a literal. The java lang spec defines when the value assigned to a field is considered 'compile time constant' and is written straight in. If it's not, say:
class Example {
// these are all NOT constant, therefore, cannot be
// retrieved with an annotation processor
long x = System.currentTimeMillis();
Pattern p = Pattern.compile("^AbC-ABG-$");
String s = null; // null is considered non-constant, for some reason.
String z = "HELLO!".toLowerCase();
}
But, if it's truly simple, such as @Foo private String x = "Hello";
where x
is a field (and not a local variable in a method someplace), yes, you can see it in action.
But all you can do, is make new files. You can't change an existing file. So, at best, you can make a second class that contains public static final String variable2 = "ABC-4711-...";
.
But what about lombok?
Project Lombok does everything I just said you can't do: It inspects actual code, and modifies source files in-flight.
Unfortunately, lombok is a few hundred thousand lines of code and most of it is neccessary to do all this: There is no uniform way to do it, so it's a ton of custom code. More to the point, it is also fundamentally extremely complicated: IDEs do code analysis all the time, and if you change structures, that has an effect on everything from 'auto-format my file as it is saved' to refactor scripts, to 'find callers', and so much more. Because there is no standard, you have to patch the editors to figure it out.
Changing a string constant may mean you don't need as much patching. Then it's still incredibly complicated.
Lombok is open source if you want to investigate.
NB: I'm a core contributor to Project Lombok.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论