Re: [Simple-support] problem with generic list
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2007-07-15 01:57:41
|
Hi,
The code you have sketched out would need reified generics, however
Java generics are not reified. As a result the object type parameters
are erased at runtime and you code ends up looking like:
class A {
@ElementList // LINE (1)
List elements; // LINE (2)
A(){
elements = new ArrayList(); // LINE(3)
}
void add(Object t) {
elements.add(t);
}
}
As you can see there is no schema information available here. The only
way to capture generic information is to give it a non template type.
For instance:
class A<T extends Foo> {
@ElementList // LINE (1)
List<Foo> elements; // LINE (2)
A(){
elements = new ArrayList<T>(); // LINE(3)
}
void add(T t) {
elements.add(t);
}
}
Here you can make use of the known type Foo. Of course there are other
ways around this. But unfortunately because of the lack of runtime
generic information it is impossible to acquire T.class.
You may ask why bother finding the class statically, you don't I just
implement it to get Object.getClass() at runtime. This would be great
for serialization, but for deserialization there would be problems as
the resulting XML would not match any available schema definition in
you oject definition.
For info on reified generics and why they suck, look here.
http://gafter.blogspot.com/2006/11/reified-generics-for-java.html
Also, if you have any suggestions on how you think this could be
improved I'd welcome suggestions.
Niall
----- Original Message ----
From: Paulo Fonseca <pau...@gm...>
To: sim...@li...
Sent: Saturday, July 14, 2007 12:00:53 PM
Subject: [Simple-support] problem with generic list
hello,
i'm trying to use simple and so far it's been helpful. however, i'm
facing some difficulty with the problem sketched below:
@Root
class C {
@Element
String data;
C(String data){
this.data=data;
}
}
class A<T> {
@ElementList // LINE (1)
List<T> elements; // LINE (2)
A(){
elements = new ArrayList<T>(); // LINE(3)
}
void add(T t) {
elements.add(t);
}
}
@Root
class B {
@Element
A<C> aC;
B() {
aC = new A<C>();
}
}
public void test() {
B b = new B();
b.aC.add(new C("hello"));
b.aC.add(new C("world"));
Serializer serializer = new Persister();
File dest = new File("test.xml");
try {
serializer.write(b,dest);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
with this code, it throws
org.simpleframework.xml.load.ElementException: Unable to determine
type for @org.simpleframework.xml.ElementList(inline=false, type=void,
required=true, data=false, name=, parent=)
i think i must add some type attribute to LINE (1), but don't know
exactly how to do it.
i also tryed to replace LINE(2) and LINE (3) by
List<Object> elements; // LINE (2*)
elements = new ArrayList<Object>(); // LINE(3*)
but then it gives
org.simpleframework.xml.transform.TransformRequiredException:
Transform memex.common.test.MapSerialisationAdaptorTest$CTransform is
required for class memex.common.test.MapSerialisationAdaptorTest$C
i might be missing something very elementary and i'd really appreciate
if anyone could help me.
thanks in advance,
paulo
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
____________________________________________________________________________________
Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469
____________________________________________________________________________________
Get the Yahoo! toolbar and be alerted to new email wherever you're surfing.
http://new.toolbar.yahoo.com/toolbar/features/mail/index.php
|