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

  • Home
  • SEARCH
  • 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 9163829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:32:15+00:00 2026-06-17T14:32:15+00:00

In Java, Imagine these interfaces and classes: public interface Iface<S, T> { … }

  • 0

In Java, Imagine these interfaces and classes:

public interface Iface<S, T> { ... }

public class Parent<A, B> implements Iface<B, A> { ... }

public class Sub extends Parent<String, Integer> { ... }

Is there a way to make a function that accepts an Iface and obtains which are the types of its generics. So for example, if you enter a Sub.class:

public void showTypes(Class<Iface<S, T>> ifaceClass) {
    // do something...
}

//showTypes(Sub.class) --> S = Integer.class, T = String.class

EDIT: About type erasure. If you have an ArrayList<?> you can’t know which generic type it is. But this case is different. If I have a class that extends ArrayList<String> I can know its generic type (String.class). Please look at this snippet:

@SuppressWarnings("serial")
public class StringList extends ArrayList<String> {
    public static void main(String... args) {
        ParameterizedType superType = 
            (ParameterizedType) StringList.class.getGenericSuperclass();
        Type[] types = superType.getActualTypeArguments();
        Class<?> clss = (Class<?>) types[0];
        System.out.println(clss);
    }
}
  • 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-06-17T14:32:16+00:00Added an answer on June 17, 2026 at 2:32 pm

    Well, I found what I was looking for. TypeTools is a small library with only one class, designed to do exactly this. The pity is that is not in Maven central repository.

    TypeResolver.resolveArguments(Sub.class, Iface.class) --> [java.lang.Integer, java.lang.String]
    

    Spring has a similar method in GenericTypeResolver:

    GenericTypeResolver.resolveTypeArguments(Sub.class, Iface.class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine having a main class -- Simulator -- that uses two other classes --
Imagine, an Event entity references a Status Entity: @Entity @Table(name = event) public class
Imagine two simple java applications. Both of them are implementing the same JAR file
Imagine you are developing a Java EE app using Hibernate and JBoss. You have
Java doesn't support multiple inheritance for abstract classes, but I'm trying to do the
imagine there are two interfaces arranged via composite pattern, one of them has a
Imagine I am using a class to bring back items from a database, say
I imagine this pertains to Hibernate only (I'm just now beginning to use these
With reference to the java.util.concurrent package and the Future interface I notice (unless I
Given the code: public static int sum(String a, String b) /* throws? WHAT? */

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.