Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to get actual type parameters used in generics implementation?

Reply
Thread Tools

How to get actual type parameters used in generics implementation?

 
 
Todd
Guest
Posts: n/a
 
      08-15-2008
Hello,

I have determined that I have several classes that could be reduced to
a single generics class using type declarations. My issue arises from
other code using the class type to determine functionality. If I can
get the passed type declaration from the generic class, then I can
modify the other code to use the results of that query as opposed to a
direct getClass() call. A second benefit I would be able to reap from
getting the passed type is eliminating a possible user type-mismatch
error.

Here is the current class set-up (without methods) showing the generic
structure:

abstract public class ModificationPanel
<ModificationType,
ModificationPanelType extends Subpanel & ModifiableSubpanel>
{
}

public interface ModifiableSubpanel<ModificationType>
{
public ModificationType getModification();
}

abstract public class Subpanel extends JPanel
{
}

Now, here is a sample modification (called perturbation):

public class PerturbationSubpanel
extends Subpanel
implements ModifiableSubpanel<Perturbation>
{
}

public class PerturbationPanel
extends ModificationPanel<Perturbation, PerturbationSubpanel>
{
}

Since the PerturbationSubpanel uses the type Perturbation in the
implementation
of ModifiableSubpanel, if I could retrieve the type Perturbation, the
extension of
ModificationPanel by PerturbationPanel could just reference
PerturbationSubpanel
and I could get the Perturbation type internal to PerturbationPanel
(this is where
user type-mismatch would be eliminated).

When I tried getTypeParameters() what I got was ModificationType and
ModificationPanelType, not the desired Perturbation and
PerturbationPanel.

Is there a way to get the ModificationPanel to tell me what type(s)
have been
used when it is extended? Or more concisely, if the type can be
referenced,
ModificationPanel would no longer need to be abstract, so
PerturbationPanel
would not exist, but the type parameters would be used for later
differentiation.

Bottom line (question repeated for clarity):
Is there a way to get a generic class to return the actual type
parameters
used in declaration?

Thanks,
Toddd
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      08-15-2008
Todd <> writes:
>Since the PerturbationSubpanel uses the type Perturbation in
>the implementation of ModifiableSubpanel, if I could retrieve
>the type Perturbation


class Perturbation{}
interface ModifiableSubpanel<T>{}
class PerturbationSubpanel implements ModifiableSubpanel<Perturbation>{}

public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println
( ( ( java.lang.reflect.ParameterizedType )
PerturbationSubpanel.class.getGenericInterfaces()[ 0 ]).
getActualTypeArguments()[ 0 ]); }}

/*prints:

class Perturbation

*/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Type of actual ports is not compatible with type of ports of entity. mreister VHDL 1 05-25-2010 11:30 AM
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
Generics: how to read actual type parameters marek.dudek@gmail.com Java 4 10-01-2007 03:48 AM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57