Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6018723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:18:57+00:00 2026-05-23T03:18:57+00:00

I have a class Foo<T, U> with the following constructor: public Foo() { clazz

  • 0

I have a class Foo<T, U> with the following constructor:

public Foo() {
  clazz = Class<U>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
}

What I do in the constructor is getting the class of the argument U. I need it because I use it for instantiating that class.

The problem is that it doesn’t work when I have a subclass of Foo that isn’t a direct sublcass of it. Let me put it with an example.

I have the class Bar<T> extends Foo<T, Class1>. Here, Class1 is not a variable but a class.

I also have the class Baz extends Bar<Class2>. Class2 is a class too, not a variable.

The problem is that it fails when I try to instantiate Baz (Baz -> Bar<Class2> -> Foo<T, Class2>). I get an ArrayIndexOutOfBoundsException because getActualTypeArguments() returns an array containing only the class Class2 (size 1) and I’m trying to get the second element of the array. That’s because I’m getting the arguments of Bar<Class2>, instead of the ones of Foo.

What I want is to modify the Foo constructor some way I can get the class in the paramter U, doesn’t matter if the class I instantiate is a direct subclass or not. I think I should can go up in the hierarchy of classes until reach the class Foo, cast it as ParameterizedType and get the arguments, but I couldn’t find how.

Any idea?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:18:58+00:00Added an answer on May 23, 2026 at 3:18 am

    This is a nice approach — it is the only way to actually get the generic type information compiled within the Java bytecode. Eveything else in terms of generics in Java is just type erasure. See below what appears to be a working solution. There are four scenarios:

    1. A fairly complex inheritance level
    2. Your inheritance scenario
    3. The class itself (clazz will be null)
    4. A subclass that does not provide actual values (clazz = null)

    This is the code (Test.java):

    import java.lang.reflect.*;
    import java.util.*;
    
    class A<T1, T2>
    {
      Class<T2> clazz;
    
      A()
      {
        Type sc = getClass().getGenericSuperclass();
        Map<TypeVariable<?>, Class<?>> map = new HashMap<TypeVariable<?>, Class<?>>();
        while (sc != null)
        {
          if (sc instanceof ParameterizedType)
          {
            ParameterizedType pt = (ParameterizedType) sc;
            Type[] ata = pt.getActualTypeArguments();
            TypeVariable[] tps = ((Class) pt.getRawType())
                .getTypeParameters();
            for (int i = 0; i < tps.length; i++)
            {
              Class<?> value;
              if (ata[i] instanceof TypeVariable)
              {
                value = map.get(ata[i]);
              }
              else
              {
                value = (Class) ata[i];
              }
              map.put(tps[i], value);
            }
            if (pt.getRawType() == A.class)
            {
              break;
            }
            if (ata.length >= 1)
            {
              sc = ((Class) pt.getRawType()).getGenericSuperclass();
            }
          }
          else
          {
            sc = ((Class) sc).getGenericSuperclass();
          }
        }
    
        TypeVariable<?> myVar = A.class.getTypeParameters()[1];
        clazz = map.containsKey(myVar) ? (Class<T2>) map.get(myVar) : null;
      }
    }
    
    class Bar<T> extends A<T, String> {}
    class Baz extends Bar<Integer> {}
    
    class A2<T3, T1, T2> extends A<T1, T2> { }
    class B<T> extends A2<Long, String, T> { }
    class C extends B<Integer> { }
    class D extends C { }
    
    class Plain<T1, T2> extends A<T1, T2> {}
    
    public class Test
    {
      public static void main(String[] args)
      {
        new D();
        new Baz();
        new A<String, Integer>();
        new Plain<String, Integer>();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have something like the following: class C { public Foo
Lets say I have the following: public class MyObject { [Bindable] public var foo:int
Let's say I have the following class: class Foo { public: Foo() { Bar();
Assume I have the following code: public static class Foo { public static void
I have the following: public class Foo<T extends Grok> extends ArrayList<T> { } How
I have the following problem using template instantiation [*]. file foo.h class Foo {
Say I have the following Objective-C class: @interface Foo { int someNumber; NSString *someString;
I have this class. public class Foo { public Guid Id { get; set;
Let's say I have a class: class Foo { public string Bar { get
Suppose we have a (toy) C++ class such as the following: class Foo {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.