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

The Archive Base Latest Questions

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

I have a class which takes a type token, and then generates objects of

  • 0

I have a class which takes a type token, and then generates objects of a type parameterized by that type (OK, it’s a bit more complicated than that, but this is a concise example):

public class Test {

    public static void main(String[] args) throws Exception {
        Holder<HashSet<Integer>> i = newObjectHolder(HashSet.class);  // this fails
    }

    static class Holder<T> {
        public Holder(T newInstance) {}
    }

    public static <T> Holder<T> newObjectHolder(Class<T> typeToken) throws Exception {
        Constructor<T> ctor = typeToken.getConstructor();
        return new Holder<T>(ctor.newInstance());
    }
}

This works fine if passed non-generic types, like:

Holder<Integer> i = Test.newObjectHolder(Integer.class);

If the passed type token is generic, however, it doesn’t work, as in the indicated line above:

Holder<HashSet<Integer>> i = Test.newObjectHolder(HashSet.class);  // this fails

I get the problem, but is there a solution? I’m OK to add @SuppressWarnings(“unused”) in the code for newObject, if it doesn’t reduce the safety. Intuitively, it seems that newObject should be able to to make a cast that works, we know that one “new” object of an erased generic type is the same as any other, and we haven’t otherwise used T in the method.

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

    So, sheepishly, answering my own question…

    I can’t find any way to do it with Class<?>, but this magic technique called super type tokens seems to work well. Here’s the updated code:

    public class Test {
    
        static class A {}
        static class B extends A {}
    
    
        public static void main(String[] args) throws Exception {
            Holder<HashSet<Integer>> i = newObjectHolder(new TypeReference<HashSet<Integer>>() {});  // works
            Holder<A> j = newObjectHolder(new TypeReference<A>() {});  // works
            Holder<A> k = newObjectHolder(new TypeReference<B>() {});  // works
            Holder<B> l = newObjectHolder(new TypeReference<A>() {});  // doesn't compile (good)
        }
    
        static class Holder<T> {
            public Holder(T newInstance) {}
            T get() { return null; }
        }
    
        public static <T,U extends TypeReference<? extends T>> Holder<T> newObjectHolder(U typeToken) throws Exception {
            T obj = typeToken.newInstance();
            return new Holder<T>(obj);
        }
    }
    

    The TypeReference code is given here, although I suspect that Guice’s TypeLiteral would work just as well (but it doesn’t have newInstance code, you’d have to implement it).

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

Sidebar

Related Questions

I have a class which constructor takes a Jakarta enums . I'm trying to
I have a repository Class which takes in a ObjectContext called TestDB. I when
I have a class A which takes a reference of B in its constructor.
I have a class in C++ which takes an std::ostream as an argument in
I have a class which takes an IEnumerable constructor parameter which I want to
I have a class 'Job' which takes 3 interfaces as paremters for depedency injection.
I have a class which enables forms with a file-type input to be submitted
I have a class which I'm serialising to send over a unix socket and
I have a class which implements UserControl. In .NET 2005, a Dispose method is
I have a class which is marked with a custom attribute, like this: public

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.