如何在类中填充通用容器?
我正在尝试编写一个列表(用于系统类项目),该列表将通过套接字连接进行序列化。
需求规范规定,列表应该通过写入长度的 int,然后写入每个元素来序列化。
此外,应该有一个(非静态)readFrom(InputStream in) 方法从流中读取数据。
我想知道是否有一种方法可以创建一个通用的 WritableList 对象,该对象作为参数,并在调用 readFrom 时填充自身?
据我所知,如果没有一些黑客反射,你无法真正获得对象内部泛型类型的类型。所以我正在考虑将类作为构造函数中的参数传递,就像这样
public class WritableList<E extends Writable> extends ArrayList<E> implements Writable {
Class<E> storedClass;
protected WritableList(Class<E> storedClass)
{
this.storedClass = storedClass;
}
@Override
public void readFrom(InputStream in) throws IOException {
int length = DataTypeIO.readInt(in);
this.clear();
for (int i = 0; i < length; i++)
{
E e;
try {
e = storedClass.newInstance();
e.readFrom(in);
add(e);
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
但是,我现在不完全确定如何将 WritableList 作为类传递。当我尝试像这样实例化它时:
grid = new WritableList<WritableList<Location>>(**What goes here?**);
我不确定要传入什么样的类。我对 java 反射不太有经验,所以这里的任何帮助都会很棒。谢谢
I'm trying to write a List (for a systems class project) that is going to be serialized through a socket connection.
The requirement specs says that a List should be serialized by writing an int for the length, then writing each element.
Also, there should be a (non-static) readFrom(InputStream in) method that reads in the data from a stream.
I was wondering if there is a way to create a generic WritableList object that takes as a parameter, and populates itself when readFrom is called?
From what I know, you can't really get the type of the generic type inside the object without some hacky reflection. So I was thinking of passing the class as a parameter in the constructor like so
public class WritableList<E extends Writable> extends ArrayList<E> implements Writable {
Class<E> storedClass;
protected WritableList(Class<E> storedClass)
{
this.storedClass = storedClass;
}
@Override
public void readFrom(InputStream in) throws IOException {
int length = DataTypeIO.readInt(in);
this.clear();
for (int i = 0; i < length; i++)
{
E e;
try {
e = storedClass.newInstance();
e.readFrom(in);
add(e);
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
However, I'm not entirely sure how to pass in a WritableList as a class now. When I try to instantiate it like so:
grid = new WritableList<WritableList<Location>>(**What goes here?**);
I'm not sure what kind of class to pass in. I'm not too experienced with java reflection, so any help here would be great. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测应该是
new WritableList(Location.class)
My guess is it should bew
new WritableList<Location>(Location.class)
我认为问题出在这个设计上。需要首先使用
.newInstance()
实例化类,然后使用它来调用.readFrom()
,这是没有意义的。.newInstance()
在代码中使用时通常是一个坏兆头,因为它假设存在无参构造函数(在本例中,您的WritableList
不存在该构造函数)类),即使它存在,它也会强制您使用无参构造函数,从而阻止将数据传递到对象中。I think the problem is with the design of this. It doesn't make sense to need to first instantiate the class using
.newInstance()
and then use that to call.readFrom()
..newInstance()
is generally a bad sign when used in code, because it assumes the existence of a no-argument constructor (which does not exist in this case for yourWritableList
class), and even if it exists, it forces you to use the no-argument constructor, preventing one from passing data into the object.