java中通过反射填充默认值

发布于 2024-12-04 19:41:40 字数 612 浏览 2 评论 0原文

我有一个复杂的对象层次结构,有几个扩展。

我正在寻找一个可以在所有字段上反射插入默认值的库。

例如:

class Person {
    String name;
    Color color;
    List<Clothes> clothes;
}

class Child extends Person {
    Sibling sibling;
}

class Foo {
   Person person;
   Child child;
}

我想要一个将对象作为参数的库,在本例中为 Foo 类,然后在所有字段上反射性地插入默认值(如果我可以定义默认值就更好了)。另外,所有地图、列表、集合等都应该得到一个新的,

我已经看过 BeanUtils,但据我所知,它并不完全支持我正在寻找的东西。

注意:这些只是示例,我的对象更加复杂和庞大。它们有很多对象,每个对象又有很多对象等等。两者都带有地图、列表等。

将 BeanUtils 和 Google Guava 等库结合起来并使其成为我自己的库是否更好?

I have a complex object hierarchy that has a couple of extends.

I am looking for a library that can reflectively insert default values on all fields.

For instance:

class Person {
    String name;
    Color color;
    List<Clothes> clothes;
}

class Child extends Person {
    Sibling sibling;
}

class Foo {
   Person person;
   Child child;
}

I would like a library that take an object as parameter, in this case the Foo class, and then reflectively insert default values (even better if I can define default values) on all fields. Also all maps,list,sets etc should get a new

I have looked at BeanUtils, but to my knowledge, it doesn't support exactly what I am looking for.

NB: These are just examples, and my objects are much more complex and big. They have many objects, and each object has many objects and so on. Both with maps, lists etc.

Is it maybe better to combine some libraries like BeanUtils and Google Guava and make it my own?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海少女心 2024-12-11 19:41:40

如果您已经构建了结构,那么用一种方法来完成应该相当简单(在构建时设置它们是一种更合乎逻辑的方法的情况下)

如果您提前知道默认值,为什么不在类中设置它们呢? (即默认,默认值;)

为一个人设置默认名称有很多价值吗(除了空)您能举个例子说明您希望在哪里动态指定默认值吗?

It should be fairly simple to do in one method provided you have the structure already built (in when case setting them as you build is a more logical approach)

If you know the default values in advance, why not just set them in the class? (i.e. default, default values ;)

Is there much value in setting a default name for a person (other than null) Can you give an example of where you would want to specify the default value dynamically?

青柠芒果 2024-12-11 19:41:40

就我个人而言,我只是尝试使用普通的 java 构造函数和/或 getter 和 setter 等。但是从这个问题来看,我猜测您想要一些可以在不知道类的确切结构的情况下工作的东西。

因此,如果您确实必须这样做,您可能可以执行以下操作:

public void setFields(Object myObject) {
    Class<?> clazz = myObject.getClass();
    Field[] fields = clazz.getFields();
    for(Field field : fields) {
        String name = field.getName();
        if(name.equals("person")) {
            field.set(myObject, new Person());
        } else if (name.equals("color")) {
             // etc...
        }
    }

}

Personally I would just try to use normal java constructors, and/or getters and setters etc. However from the question I'm guessing you want something that can work without knowing the exact structure of your classes.

So if you really have to do this, you could probably do something along the lines of the following:

public void setFields(Object myObject) {
    Class<?> clazz = myObject.getClass();
    Field[] fields = clazz.getFields();
    for(Field field : fields) {
        String name = field.getName();
        if(name.equals("person")) {
            field.set(myObject, new Person());
        } else if (name.equals("color")) {
             // etc...
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文