19

I'm trying to bind an interface to its implementation as read from a configuration file so that I can feed it to my IoC container. Here's roughly what I'm trying to do:

public class PropertyImplementationBinder<T> {
    // ...
    public Class getInterfaceClass() {
        return T.class; // OR Class<T>, note T is not newable
    }
    public Class getImplementationClass() {
        return /* read config file to get implementation class */;
    }
}

Is it somehow possible to get T.class?

Kaleb Pederson
  • 45,767
  • 19
  • 102
  • 147

5 Answers5

31

You need to explicitly pass the class into the constructor (and store it yourself).

private final Class<T> clazz;

PropertyImplementationBinder(Class<T> clazz){
    this.clazz = clazz;
}

public Class<T> getInterfaceClass() {
    return clazz;
}
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • I think that should have been obvious to me... but it wasn't. Thank you! – Kaleb Pederson Feb 09 '10 at 05:12
  • The language could have done it for us, if it had been designed for it in version 1. It is a backwards compatibility thing. There was only so much that good be done by bolting this on to Java 5 without breaking old code. – Thilo Mar 07 '12 at 00:11
  • Everyone look below; in many cases it *is* possible to do without passing in the class. – Andy Oct 25 '13 at 15:46
9

You can get the actual type arguments for a generic superclass of a class. This blog post explores the possibilities presented by this, including a nice little trick using trivial anonymous inner classes. To quote directly:

It turns out that while the JVM will not track the actual type arguments for instances of a generic class, it does track the actual type arguments for subclasses of generic classes. In other words, while a new ArrayList<String>() is really just a new ArrayList() at runtime, if a class extends ArrayList<String>, then the JVM knows that String is the actual type argument for List's type parameter.

kloffy
  • 2,928
  • 2
  • 25
  • 34
  • 1
    But then the developer knows as well since the class has a static T. It won't change at runtime. – GuiSim Jun 24 '14 at 14:26
5

Contrary to what is widely accepted and rarely known type erasure can be avoided, which means that the callee do have the ability to know which generic parameters were employed during the call.

Please have a look at: Using TypeTokens to retrieve generic parameters

The article also talks about the experiences of our users with the technique. In a nutshell, we ended up falling back to the...

Conventional and widely used technique: "Pass class types in constructors"

Eloff
  • 20,828
  • 17
  • 83
  • 112
Richard Gomes
  • 5,675
  • 2
  • 44
  • 50
  • 1
    well, that's creepy but amazing (let this be last fascination comment). i actually believe this should be the right answer with remark "you can also pass class in constructor" – durilka Jan 12 '12 at 22:26
  • Link appears to be broken... I know that Google's GSON library uses this approach, and I wanted to read an article on how it works – Erin Drummond Dec 22 '12 at 05:54
  • @ErinDrummond fixed with link to web.archive.org – Eloff Jan 23 '15 at 16:11
1

Btw. The example static method getType in article from @Richard Gomes has two errors. it should go like this:

static public Class<?> getType(final Class<?> klass, final int pos) {
    // obtain anonymous, if any, class for 'this' instance
    final Type superclass = klass.getGenericSuperclass();

    // test if an anonymous class was employed during the call
    if ( !(superclass instanceof ParameterizedType) ) {
            throw new RuntimeException("This instance should belong to an anonymous class");
    }

    // obtain RTTI of all generic parameters
    final Type[] types = ((ParameterizedType) superclass).getActualTypeArguments();

    // test if enough generic parameters were passed
    if ( pos >= types.length ) {
            throw new RuntimeException(String.format("Could not find generic parameter #%d because only %d parameters were passed", pos, types.length));
    }

    if (!(types[pos] instanceof Class<?>)) {
            throw new RuntimeException("Generic type is not a class but declaration definition(all you get is \"[T]\") " + types[pos]);
    }
    // return the type descriptor of the requested generic parameter
    return (Class<?>) types[pos];
}

Unfortunately it's still not the magic bullet because it works if you have in code explicitly

getType(new SomeObject<String>(){}.class, 0) // you get String.class

but if you call this on something like

getType(new SomeObject<T>(){}.class, 0) // you get T as TypeVariable<D> and not actuall class of it

Just name T.

durilka
  • 1,399
  • 1
  • 10
  • 22
  • 1
    In the second example, it may still be possible to find the actual type parameter for T, if the `TypeVariable.getGenericDeclaration()` is a Class. It takes a lot of work to full the actual type arguments in any case (especially if they're defined by a superclass, there are multiple type parameters, etc. but I've written code to do it. – Andy Oct 25 '13 at 15:50
-1

No it's not possible.

The only exception to Java's type erasure is that via reflection you can find out the parameterized type via reflection on a class's fields.

cletus
  • 616,129
  • 168
  • 910
  • 942