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

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

问题描述

我正在使用 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 值并检索 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 标志编译代码,也可以使用在运行时可用的构造函数中的参数名称.然后你就不需要任何注解,除非你想以序列化的形式使用不同的名称,那么只需在字段/getter 中添加注解即可.

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天全站免登陆