Hi,
As said before, as a pastime, I am generifying bit by bit the Jakarta
Commons Collections. (And a bit more, i.e. also add annotations, enums,
foreach-loops).
I encountered a difficult problem and was hoping for some help here.
There is the class PrototypeFactory, which has static inner class
PrototypeSerializationFactory. What the last one does, is take an
Serializable in its constructor, and produce a clone of it each time its
create() method is invoked, using serialization.
The most obvious choice in generifying it is to make it like this:
static class PrototypeSerializationFactory<T extends Serializable>
implements Factory<T>, Serializable
It creates a T every time create() is invoked. Accordingly, the
constructor takes a T as argument, where this used to be Serializable:
private PrototypeSerializationFactory(T prototype) {
super();
iPrototype = prototype;
}
This works fine. Until I want to invoke its constructor...
Because this constructor is invoked in a rather complicated method which
does some tests on an argument like:
does the argument have a clone() method -> return PrototypeCloneFactory
does it have a public constructor -> return a factory which uses the
constructor
only if both options above fail, comes the test:
is it instanceof Serializable -> return new
PrototypeSerializationFactory((Serializable)arg).
The obvious choice in generifying seems to make that
return new PrototypeSerializationFactory<T>(arg),
where arg is of type T (which is in turn the type parameter of the
generic method).
But this gives the following error:
Bound mismatch: The type T is not a valid substitute for the bounded
parameter <T extends Serializable> of the type PrototypeFactory.
PrototypeSerializationFactory<T>
I understand this: T is unbound. But is there some way to sort of cast
the type parameter? Because I am sure T is indeed an extension of
Serializable...
I fear something like passing a Class parameter will be necessary. Ideas
welcome...
Cheers, H.
- --
Hendrik Maryns
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Peter Van Weert - 13 Jun 2006 19:31 GMT
The method the previous mail discusses is the getInstance method of
http://svn.apache.org/viewvc/jakarta/commons/proper/collections/trunk/src/java/o
rg/apache/commons/collections/functors/PrototypeFactory.java?view=markup
This should help clarify the problem at hand. I'll think about it some
more if I find the time, unless of course it's already solved by then...
Cheers,
Peter
Hendrik Maryns schreef:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 71 lines]
> =pA0v
> -----END PGP SIGNATURE-----
Gijs Peek - 14 Jun 2006 08:22 GMT
Hi,
I don't think casting a type paramater makes any sense, since they are only
present at compile-time.
You can do something like
return (Factory<T>) new PrototypeSerializationFactory<Serializable>(arg)
and disable type safety warnings with a @SuppressWarnings("unchecked"), but
you cannot invoke the constructor with a type parameter that is determined
at runtime.
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 71 lines]
> =pA0v
> -----END PGP SIGNATURE-----
Hendrik Maryns - 14 Jun 2006 13:34 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message
Gijs Peek uitte de volgende tekst op 06/14/2006 09:22 AM:
> Hi,
>
[quoted text clipped - 5 lines]
> you cannot invoke the constructor with a type parameter that is determined
> at runtime.
Seems like a good option, as the @Suppresswarnings tag is needed in that
method anyway (other, unavoidable casts). The good thing is: it keeps
the PrototypeSerializationFactory clean, although this doesn?t weigh
much, as it is a non-public class.
Sidenote: is Jakarta this intensively developed in the Low Lands? I get
two answers from fellow Dutch speakers :-)
Cheers, H.

Signature
Hendrik Maryns
==================
www.lieverleven.be
http://aouw.org