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 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

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.