How to get actual type parameters used in generics implementation?

Discussion in 'Java' started by Todd, Aug 15, 2008.

  1. Todd

    Todd Guest

    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
    Todd, Aug 15, 2008
    #1
    1. Advertising

  2. Todd

    Stefan Ram Guest

    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

    */
    Stefan Ram, Aug 15, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Avi Kak
    Replies:
    1
    Views:
    246
    Josiah Carlson
    Feb 20, 2004
  2. Replies:
    0
    Views:
    264
  3. Replies:
    6
    Views:
    325
    Piotr Kobzda
    Jul 26, 2007
  4. Replies:
    4
    Views:
    392
    Daniel Pitts
    Oct 1, 2007
  5. mreister
    Replies:
    1
    Views:
    3,158
    mreister
    May 25, 2010
Loading...

Share This Page