强制统一构造函数和方法注释值? [英] Force Uniform Constructor and Method Annotation Values?

查看:55
本文介绍了强制统一构造函数和方法注释值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Reflection编写自定义API,以将对象保存到文件中.我具有以下类结构:

I'm writing a custom API using Reflection to save Objects to file. I have the following class structure:

@Constructor
public XYZPOJO(@Key(key = "word") String word, @Key(key = "variations") ArrayList<String> varList) {
    this.word = word;
    this.varList = varList;
}


String word;
ArrayList<String> varList = new ArrayList<String>();


@Key(key = "word")
public String getWord() {
    return word;
}

@Key(key = "variations")
public ArrayList<String> getVarList() {
    return varList;
}

将对象保存到文件时,我的程序将检索每个用@Key注释的方法,调用方法并将调用的值使用@Key的值作为属性名保存到文件中.稍后,当我要构造Object的实例时,它将搜索用@Constructor注释的构造函数,然后在构造函数中检索每个参数的@Key值,并从文件中检索键(属性)的值.

When saving Object to file, my program retrieves each method annotated with @Key, invokes method and saves invoked value to file using the value of @Key as the property name. Later, when I want to construct instance of Object it will search for constructor annotated with @Constructor and then retrieve value of @Key of each parameter in constructor and retrieve value of key (property) from file.

我的主要问题是,对于每个要保留的字段,我需要在每个方法之前以及构造函数中相应参数之前复制@Key批注(和值).此外,如果两个构造函数/方法注释均不完全匹配,则将无法实例化Object.意外复制错误的值很容易.

My main issue is that for every field I want to persist I need to duplicate the @Key annotation (and value) before each method and before the corresponding parameter in constructor. Moreover, if both the constructor/method annotation do not match exactly it will fail to instantiate Object. It is very easy to accidentally copy the wrong values.

是否可以只定义一次每个@Key?

Is there a way to define each @Key just once?

我当时想在要保留的每个字段之前添加@Key一次,但是我相信(如果我错了,请更正我)我将不再能够通过构造函数实例化类(我相信我会需要通过反射直接设置每个字段的值来实例化类,从而绕过构造函数,对吗?).但是,由于构造函数在实例化类之前执行某些必要的功能,因此这不是理想的选择.

I was thinking of adding @Key just once before each field I wish to persist however I believe (please correct me if I'm wrong) that I would no longer be able to instantiate class via constructor (I believe I would need to instantiate class by directly setting value of each field via reflection, thereby circumventing constructor, correct?). However, this is not ideal since the constructor performs certain necessary functions before the class is instantiated.

还有什么其他解决方案?

What other solution(s) are there?

谢谢!

推荐答案

您可以像其他所有要序列化的库一样执行此操作(或切换到这些库之一,因为它们都支持您所做的一切),因此可能的解决方案是:

You could do that like every other library for serialization (or just switch to one of these libraries, as they all support everything you do), so possible solutions:

  1. 默认情况下跳过注释,仅使用getter的名称(如getMoney-> money)并仅在构造函数中使用注释.如果要使用序列化形式的其他名称,请在getter上使用.另外,您也可以查找具有相同名称的字段,以检查其上的注释,但是它是可选的,而不是必需的.

  1. Skip annotation by default and just use name of getter (like getMoney -> money) and use annotation only in constructor. And on getter if you want to use other name in serialized form. Additionally you can look for field with same name to check annotations on it too, but it's optional and not needed.

仅在构造函数中注释参数,但允许同时设置名称和属性名称(默认情况下,除非有人同时提供两个值,否则您可以假定名称==属性),然后您可以将其更改为getter方法名称,例如money-> getMoney(只需添加get并使首字母大写)

Annotate only parameters in constructor but allow to set both name and property name (by default you can assume that name == property unless someone provided both values) And later you can change it to getter method name, like that money -> getMoney (just add get and make first letter upper case)

应用第一个想法,但也要使用构造函数中的参数名称,如果有人使用-parameters标志编译代码,则可以在运行时使用它们.然后,您不需要任何注释,除非您希望以序列化形式使用其他名称,然后只需将注释添加到仅字段/获取器即可.

Apply 1st idea but also use parameter names from constructor that are available in runtime if someone compiles code with -parameters flag. And then you don't need any annotation, unless you want to use different name in serialized form, then just add annotation to only field/getter.

注意: 典型的库只扫描公共方法以查找属性.因此,他们寻找以getis开头,后跟大写字母,没有参数和某些返回类型的方法.典型的数据类看起来像这样.

Note: Typical libraries just scan for public methods to find properties. So they look for methods that starts with get or is followed by upper case letter, that have no arguments and some return type. As typical data class will look like that.

这篇关于强制统一构造函数和方法注释值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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