gpt4 book ai didi

Java自定义注解查找方法

转载 作者:行者123 更新时间:2023-12-02 04:38:40 24 4
gpt4 key购买 nike

我想创建注释(我可以为其设置某种id),我可以将其放在变量上,并编写通过注释设置的id返回该变量的getter或setter方法的方法;

作为示例,我做了类似的东西,但仅适用于方法,因此我需要为 setter 和 getter 方法提供两个注释,或者向一个注释添加更多参数。也许可以为变量创建一个注释,然后找到我需要的方法(setter 或 getter)?

我设法做到的:

注解类:

    public @interface FieldRawValue {

String value() default "";
}

具有 getter 和 setter 的模型类(带有 getter 注释):

公共(public)类 PajamuModel 扩展 AbstractModel{

private String id;
private String pajamuType;
private String pajamuSource;
private Date pajamuData;
private double pajamuSuma;
private String valiuta;
private String note;

@FieldRawValue(value = "pajamuType")
public String getPajamuType() {
return pajamuType;
}

public void setPajamuType(String pajamuType) {
this.pajamuType = pajamuType;
}

@FieldRawValue(value = "pajamuSource")
public String getPajamuSource() {
return pajamuSource;
}

public void setPajamuSource(String pajamuSource) {
this.pajamuSource = pajamuSource;
}

@FieldRawValue(value = "pajamuData")
public Date getPajamuData() {
return pajamuData;
}...

通过注释值返回数据的方法:

public class AbstractModel {

public Object get(String fieldName) {
Class<? extends AbstractModel> obj = this.getClass();

for (Method method : obj.getDeclaredMethods()) {
if (method.isAnnotationPresent(FieldRawValue.class)) {
FieldRawValue annotation = method
.getAnnotation(FieldRawValue.class);
if (annotation.value().equals(fieldName)) {
try {
return method.invoke(obj);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
}
}

return null;
}

顺便说一句,未测试 get(String fieldName) 方法,但我希望它能工作。

那么是否可以在变量上设置注释,然后在需要时找到 set 或 get 方法?

最佳答案

我不确定为什么要重复字段名并将其放在注释中,但是这是解决方案(需要 Java8):

public class AbstractModel {

public Object get(String fieldName) throws InvocationTargetException, IllegalAccessException {
FieldRawValue fieldRawValueAnnotation = validateExistsAndGetFieldRawValueAnnotation(fieldName);

String fieldRawValueString = fieldRawValueAnnotation.value();

Optional<Method> getterMethodOptional = Stream.of(getClass().getMethods())
.filter(method1 -> method1.getName().equalsIgnoreCase("get" + fieldRawValueString))
.findFirst();

return getterMethodOptional
.orElseThrow(() -> new RuntimeException("No getter found for @FieldRawValue with value: " + fieldRawValueString))
.invoke(this);
}

public Object set(String fieldName, Object value) throws InvocationTargetException, IllegalAccessException {
FieldRawValue fieldRawValueAnnotation = validateExistsAndGetFieldRawValueAnnotation(fieldName);

String fieldRawValueString = fieldRawValueAnnotation.value();

Optional<Method> getterMethodOptional = Stream.of(getClass().getMethods())
.filter(method1 -> method1.getName().equalsIgnoreCase("set" + fieldRawValueString))
.findFirst();

return getterMethodOptional
.orElseThrow(() -> new RuntimeException("No setter found for @FieldRawValue with value: " + fieldRawValueString))
.invoke(this, value);
}

private FieldRawValue validateExistsAndGetFieldRawValueAnnotation(String fieldName) {
Class<? extends AbstractModel> obj = this.getClass();

FieldRawValue fieldRawValueAnnotation = null;
try {
fieldRawValueAnnotation = obj.getDeclaredField(fieldName).getAnnotation(FieldRawValue.class);
} catch (NoSuchFieldException e) {
new RuntimeException("Field not found: " + fieldName);
}

if(fieldRawValueAnnotation == null){
throw new RuntimeException("FieldRawValue annotation not found for a field: " + fieldName);
}
return fieldRawValueAnnotation;
}

型号+用途:

public class TestModel extends AbstractModel{

@FieldRawValue("test2")
private String test;

public String getTest2() {
return test;
}

public void setTest2(String test) {
this.test = test;
}

public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
TestModel testModel = new TestModel();

Object tst = testModel.get("test"); //== null
testModel.set("test", "newVal"); //sets new value
testModel.get("test"); //== "newVal
}

}

关于Java自定义注解查找方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30443315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com