简体   繁体   English

在运行时更改类结构或将其放入匿名类

[英]Change class structure at runtime or get it into a anonymous class

I have 我有

class A
{
String a;
String b;
//..getters, setters
}

Now I have ArrayList<? extends Object> resultData 现在我有了ArrayList<? extends Object> resultData ArrayList<? extends Object> resultData holding objects of class A and some other classes. ArrayList<? extends Object> resultData ,其中包含类A和其他一些类的对象。 Basically I need this list 'resultData' to generate a Json file in some other API. 基本上,我需要此列表“ resultData”以在其他一些API中生成Json文件。

Now my question is while adding the class A objects to the list & some condition(X) is true I need a modified class A object (or any other anonymous class object) like: 现在我的问题是在将A类对象添加到列表中并且某些条件(X)为true时,我需要一个经过修改的A类对象(或任何其他匿名类对象),例如:

class A
{
String a;
//..getters, setters
}

that is one particular object of class A shouldn't have field String b (before criticising, I'm doing this because I need such modified object for my particular JSon format & I don't want to define a new class definition that is used only once) 那是类A的一个特定对象,不应具有字段String b (在批评之前,我这样做是因为我需要为我的特定JSon格式使用这种修改后的对象,并且我不想定义一个新的使用的类定义只有一次)

my last option is to make anonymous class like this: (& then add it to my List) 我的最后一个选择是使匿名类如下:(&然后将其添加到我的列表中)

Object ob = new Object{
String b;
//..getters, setters
}

Also pls. 也请。 suggest any other method of creating anonymous class with required structure. 建议使用所需结构创建匿名类的任何其他方法。

Java is not meant for changing classes or creating new classes at runtime. Java并非旨在在运行时更改类或创建新类。

It is possible with a lot of effort, like generating java bytecode on the fly using a bytecode library like BCEL( http://commons.apache.org/proper/commons-bcel/ ) or even generate .java files and run javac to generate bytecode. 可能需要付出很多努力,例如使用BCEL( http://commons.apache.org/proper/commons-bcel/ )这样的字节码库动态生成Java字节码,甚至生成.java文件并运行javac来生成字节码。

You could simply use a hash map like Map<String,Object> that "simulates" an object that can receive arbitrary fields. 您可以简单地使用诸如Map<String,Object>类的哈希映射来“模拟”一个可以接收任意字段的对象。 If you really need totally configurable classes, I would go this way. 如果您真的需要完全可配置的类,我会采用这种方式。 Of course, you would not have nice getters and setters for each property. 当然,对于每个属性,您都不会有很好的getter和setter方法。

But why would you need nice setters / a nice class anyway? 但是,为什么您仍然需要出色的二传手/一流的课程呢? As the structure of the class is determined at runtime, you can write no code that depends on this class, as you do not know how it will look like. 由于类的结构是在运行时确定的,因此您无法编写依赖该类的代码,因为您不知道它的外观。

if i'm getting you correctly, you need to get rid off field for serialization, to json format, if im right, then make your field transient 如果我正确地找到了您,则需要删除要进行序列化的字段,转换为json格式,如果即时消息正确,则使您的字段transient

other solution is to make super class with field which you want to serialize, and make A to extend it 另一种解决方案是使用要序列化的字段制作超类,并让A进行扩展

but modifying class on fly, it is not right way to go 但是在飞行中修改课程,这不是正确的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM