Difficult generics issue (generifying jakarta commons)

H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEjus/e+7xMGD3itQRAj/mAJsEKXfPl97iDSUnyAPBIeHmzGysRwCfZz9t
KAzB6Gz5/LdqUkQysSkDkHI=
=pA0v
-----END PGP SIGNATURE-----
 
G

Gijs Peek

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEjus/e+7xMGD3itQRAj/mAJsEKXfPl97iDSUnyAPBIeHmzGysRwCfZz9t
KAzB6Gz5/LdqUkQysSkDkHI=
=pA0v
-----END PGP SIGNATURE-----
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iQIVAwUBRI+5Lbb66fbjXztVAQJ5/BAAg+WfpzZ+76OE+DMUInQWXHzPXEk88SSq
NMA+GXbHRX47FdXHzZFEI6Xr26dmIwJQ2RDe+orE7n7mFSch+mtov8jIEjdcgMre
vYcJCRhzZtgb7yxwDPKVkYVzd+EX7IJ2RSB7c6IKMFI5ElYx1oie1btMTankVBNs
imOvJ+00Avc8CrGTKcO4tFIFrGt/5fSKs0QmHZnH100zL6VoaxExzgir/V1K7aD+
CDwNt7qNMpnJmndlQw7RrRUmGpdJu9E1TaMbPnSryZsiofcR3/0OteKQoRlEPxKb
7NhfAnngiLdN+msJXjOfKOxtORoyZMvTv611k9Qtq8GsLS39EmYxq6V3m0WLEFgZ
MP6OrbtVtpaEB8EOrdit0qoRu3FvtvsonLkhP8e0H1nBFm5Xyzpblks9U7+Ko5EE
U4RuVdkJKNdv87ngZICd4tzu5iMWXYJSYWJGxiNONFfCKzMnf0CltibtIWHx4lAC
v5qhdu/tD5hNnEjMm5RuHa3nyfA36OMq8nG10NnLdsq+Il4181mgb/iIjkCXA77N
6mP/6vTXCcTPeMJ3znsXWgYZqoE5XAkzVXRQYP1hD6keumuMmOW+TzTTsoC9n5s2
isJtwMc5+iTg5mMn4bhnr/eJV3/OAY6XNEqWDeJpP6zl180YwEkF25tnJVdo2BsY
8Uh5oDmWfYU=
=he0l
-----END PGP SIGNATURE-----
 
H

Hendrik Maryns

-----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,

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.

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.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEkAJxe+7xMGD3itQRAuxhAJ9coR6/XZZqu6cxowtV1WCtvOIIQgCfbe9z
cLq763jL6k/LZ9KfT5eZvqA=
=roZj
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

generics puzzle 57
Issue with textbox script? 0
Hairy generics question 29
Generics 24
Generics double redirection compile error: why? 3
Problems using jakarta commons fileUpload 2
Generics Issue 13
Generics question 6

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top