niklassaers
niklassaers

Reputation: 8810

Getting the class of a Java generic, and interface implementation of generics

I'd like to make a class that looks basically like this:

public class MyClass<T implements Serializable) {

   void function() {
      Class c = T.class;
   }
}

Two errors:
- I cannot call T.class, even though I can do that with any other object type
- I cannot enforce that T implements Serializable in this way

How do I solve my two generics problems?

Cheers

Nik

Upvotes: 5

Views: 6189

Answers (5)

sepp2k
sepp2k

Reputation: 370112

You can't do T.class because java does not actually know which class T is at runtime.

All that information is lost at compilation.

To get the class object for T you can either call getClass() on an instance of T (if you have access to one) or require the user to pass the class object as an argument to function, like:

void function(Class<T> c)

Upvotes: 1

Chad Okere
Chad Okere

Reputation: 4578

you do this:

public class MyClass<T implements Serializable) {

   void function(Class<T> tc) {
      ...
   }
}

Basically, you have to pass in the class at run time in order to see it. You could also do something like this:

public class MyClass<T implements Serializable) {
   Class<T> ct; 
   public MyClass(Class<T> ct){this.ct = ct;}

   void function() {
       ... //you know what the class is here
   }
}

It's kind of annoying, but not really that big of a hassle overall.

Upvotes: 5

Nick Holt
Nick Holt

Reputation: 34301

You can't get the type.

Generics are implemented using something called type-erasure.

When a generic type is instantiated, the compiler translates those types by a technique called type erasure — a process where the compiler removes all information related to type parameters and type arguments within a class or method. Type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created before generics.

The essence of this is that the type information is used by the compiler and discarded, hence not available at runtime.

With regards to the enforcing T implements Serializable, you just need the following:

public class MyClass<T extends Serializable>)
{
  public void function(T obj) 
  {
    ...
  }
}

This is simply referring to the is a relationship, so an class that implements Serializable, is a Serializable and can be passed to function.

Upvotes: 7

Wolfgang
Wolfgang

Reputation: 3490

This is not possible without tricks. The Java Generics FAQ provides an idea for a workaround.

Upvotes: 0

gjrwebber
gjrwebber

Reputation: 2658

Something along these lines should do it.

private Class<T> dataType;
Type type = getClass().getGenericSuperclass();
if (type instanceof ParameterizedType) {
    ParameterizedType paramType = (ParameterizedType) type;
    dataType = (Class<T>) paramType.getActualTypeArguments()[0];
} else if (type instanceof Class) {
    dataType = (Class<T>) type;
}

Upvotes: 3

Related Questions