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 271581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:11:12+00:00 2026-05-12T00:11:12+00:00

I’d like to get the generic type of a collection, using reflection, at runtime.

  • 0

I’d like to get the generic type of a collection, using reflection, at runtime.

Code (JAVA):

Field collectionObject = object.getClass().getDeclaredField(
    collectionField.getName());
//here I compare to see if a collection
if (Collection.class.isAssignableFrom(collectionObject.getType())) {
   // here I have to use the generic type of the collection 
   // to see if it's from a specific type - in this case Persistable
   if (Persistable.class.isAssignableFrom(GENERIC_TYPE_COLLECTION.class)) {
   }
}

Is there a way of getting the generic type of the collection in java at runtime? In my case I need the .class of the collection’s generic type.

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-12T00:11:13+00:00Added an answer on May 12, 2026 at 12:11 am

    Type erasure means that information about the generic type of an object simply isn’t present at execution time.

    (The link is to the relevant section of Angelika Langer’s Java Generics FAQ which should answer virtually every question you could possibly ask about Java generics 🙂

    However, you’re not really interested in the type of an object – you’re interested in the type of a field. I misread the question, and although the answer has been accepted I hope to make amends by fixing it now 🙂

    If the field doesn’t use a type parameter itself, it can be done. For example:

    import java.lang.reflect.*;
    import java.util.*;
    
    public class Test
    {
        public List<String> names;
    
        public static void main(String [] args)
            throws Exception // Just for simplicity!
        {
            Field field = Test.class.getDeclaredField("names");
    
            ParameterizedType type = (ParameterizedType) field.getGenericType();
    
            // List
            System.out.println(type.getRawType());
    
            // Just String in this case
            for (Type typeArgument : type.getActualTypeArguments())
            {
                System.out.println("  " + typeArgument);
            }
        }
    }
    

    If the field were in a class T with the field being List<T> then you’d have to know the type argument for the instance in order to know the type argument for the collection.

    Translating this into your required code is somewhat tricky though – you really need to know the type argument at the point of the collection class. For instance, if someone declared:

    public class StringCollection implements Collection<String>
    

    and then had a field of type StringCollection, that field itself wouldn’t have any type arguments. You’d then need to check getGenericSuperType and getGenericInterfaces recursively until you found what you wanted.

    It’s really not going to be easy to do that, even though it’s possible. If I were you I’d try to change your design so that you don’t need this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 156k
  • Answers 156k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Such are the problems with varargs! print2.invoke(instance, new Object[] {… May 12, 2026 at 10:51 am
  • Editorial Team
    Editorial Team added an answer take a look at this SO question. It pertains to… May 12, 2026 at 10:51 am
  • Editorial Team
    Editorial Team added an answer Your initToolTip function sets an on-click handler for the entire… May 12, 2026 at 10:51 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.