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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:42:45+00:00 2026-05-30T18:42:45+00:00

I’m looking to access the generic type of a declared field during runtime. I

  • 0

I’m looking to access the generic type of a declared field during runtime. I was previously under the impression that this was not possible due to the Java type erasure. However, this must not be the case because some well known frameworks leverage the generic type through reflection during runtime.

As an example, Guice will implement a Provider based upon the generic type you provide:

public class Injectable{

    @Inject
    private Provider<SomeType> someTypeProvider;

}

How does one access the ‘SomeType’ generic attribute of a field or any such type/method/etc through the reflection API?

Additionally it would be helpful to also know how to access these generic type attributes through the Java 6 Annotation Processor API.

Thanks.

Edit:

Thank you all for your great pointers. I found a way to do this using haylem’s links, specifically the one to Prenkov’s article Java Reflection: Generics.

Here’s the answer I was looking for:

/**
 * @author John Ericksen
 */
public class TypeReflectionExample {

    public class SomeType{}

    public class Injectable{
        @Inject  private Provider<SomeType> someTypeProvider;
    }

    public static void main(String[] args){

        try {
            Field providerField = Injectable.class.getDeclaredField("someTypeProvider");

            Type genericFieldType = providerField.getGenericType();

            if(genericFieldType instanceof ParameterizedType){
                ParameterizedType aType = (ParameterizedType) genericFieldType;
                Type[] fieldArgTypes = aType.getActualTypeArguments();
                for(Type fieldArgType : fieldArgTypes){
                    Class fieldArgClass = (Class) fieldArgType;
                    System.out.println("fieldArgClass = " + fieldArgClass);
                }
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }   
}

results in:

fieldArgClass = class test.TypeReflectionExample$SomeType

The same can be done for Methods, Constructors, Superclass extensions/implements, etc

I’m awarding haylem, as his post led me to this solution, even if it didn’t directly answer my question.

  • 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-30T18:42:46+00:00Added an answer on May 30, 2026 at 6:42 pm

    It is true that generics aren’t generally known at runtime in Java, because they are implemented with Type Erasure.

    Reflecting Generics?

    However, you can stil extract some valuable information about the declared types (NOT the runtime objects’ types), as presented in Ian Roberston’s article Reflecting Generics and Prenkov’s article Java Reflection: Generics.

    Background on Generics and Type Erasure

    Generics where introduced while conserving backwards compatibility at the source qnd binary level, hence some of their limitation, like:

    • the impossibility to have a short-hand form without at least some indicator for generics support (here, the so-called diamond operator <>),
    • the impossibility to inspect generic-types at runtime, because they had to be implemented with Type Erasure.

    Further Reading

    • From The Java Tutorial:
      • section on Generic Types
      • section on Type Inference and Instantiation of Generic Classes
    • From the Java Language Specifications (JLS):
      • Java SE 5’s JLS section on Types, Values and Variables
      • Java SE 7’s JLS section on Types, Values and Variables
    • From good StackOverflow questions:
      • Java Raw Type and generics interaction
    • Others:
      • IBM Developer Series: Java Theory and Practice: Generics Gotchas (especially the sections The Road Not Taken, Generifying Existing Classes and Implications of Erasure).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.