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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:21:50+00:00 2026-05-17T19:21:50+00:00

I have a question about generics. I have a function that knows about a

  • 0

I have a question about generics.

I have a function that knows about a class type of object that I want to create:

public static <T> XmlParserInterface<T> createXmlParser(Class<T> rootType, String currentTagName) {
 XmlParserInterface<T> result = null;

 if (rootType.equals(List.class)) { 
  result = new XmlParserList<T>();
 }
 // ...

    result.init(rootType, currentTagName);
    return result;
}

In that function, for this special case, I call it with rootType being List<String>.

In my init function, I pass again the rootType so that I can create an ArrayList<something>. My problem here is to find the something since I will need it to generate further objects. In this case, it would be String, but it could go even deeper with being a List<List<String>> or even a custom POJO that I check for their set functions to fill them.

I tried doing rootType.getTypeParameters()[0], but it only returns me E. I need to know the actual class of the E.

Thanks if anyone can answer that 🙂

  • 1 1 Answer
  • 1 View
  • 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-17T19:21:51+00:00Added an answer on May 17, 2026 at 7:21 pm

    You need to add the type information explicitly – it’s not available at runtime because generic type parameters are deleted during compilation. And since you need to specify this explicitly you cannot do what you want (as far as I can see) using just generics. Instead you will need to do some casting (and pass in some kind of structure of classes that indicates what you want to construct).

    See the excellent book on Java Generics by Wadler (really, you need to buy and read that book if you are trying to write code like this).

    If you always had, say List<X> then you could do this (you would still need to extend the signature, but the code would compile without any type warnings). Or even X<Y> where X is itself a variable that, say, subclasses List. What you cannot do is handle both X<Y> and X<Y<Z>> without forcing the system to do “unsafe” operations.

    The X<Y> case would look something like:

    public <Y, X extends List<Y>> X<Y> myMethod(Class<X<Y>> xClass, Class<Y> yClass, ...) { ...}
    

    (that’s just from memory but you can get the idea, I hope – you pass both X and Y, and you use qualified type annotations to express the relationship between them. Really, read Wadler’s book!).

    And if you wanted to force things by hand, you’d have to just pass in a list of Class values without types, and add a lot of casting.

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

Sidebar

Related Questions

I have a question about generic list. Assume that I have a database which
I have a question about handling exception. I have a Winform that uses a
I have a question about Java Generics and Collections. It's considered good practice to
Suppose I have a static class in C#: public static class Math { [Microsoft.SqlServer.Server.SqlFunction]
I have a question about design. I have a series of generic functions that
I have a question about Java Generics. In code below we have interface B
No, this is not a question about generics. I have a Factory pattern with
I have a generic question about scope and encapsulation. Take two scenarios: Scenario 1:
I have question about parsing in Html helper : I have sth like: @foreach
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass

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.