2

I have following class, I need to get type in constructor, how can I do that?

public abstract class MyClass<T> {
    public MyClass()
    {
        // I need T type here ...
    }
}

EDIT:

Here is concrete example what I want to achieve:

public abstract class Dao<T> {
    public void save(GoogleAppEngineEntity entity)
    {
        // save entity to datastore here
    }

    public GoogleAppEngineEntity getEntityById(Long id)
    {
        // return entity of class T, how can I do that ??
    }
}

What I want to do is to have this class extended to all other DAOs, because other DAOs have some queries that are specific to those daos and cannot be general, but these simple queries should be generally available to all DAO interfaces/implementations...

5
  • Depends on what you want to do with it. Do you want to instantiate a T? Do you want to call another function with a T? Commented Jun 3, 2010 at 21:21
  • I have dao that all other daos extend, so this extended dao will have some basic operation that can be same for all daos, so I need to know class where resulted objects should be mapped Commented Jun 3, 2010 at 21:30
  • I'm not sure I understand, but since you have T, you can "map" it in the various other methods in your DAO, e.g., T doSomething(T obj) Commented Jun 4, 2010 at 13:06
  • You need to expand and give concrete examples of what you're trying to accomplish. What you want to do isn't generally possible due to erasure, and has a very bad design smell. Commented Jun 4, 2010 at 14:32
  • I updated post and gave concrete examples ... Commented Jun 4, 2010 at 17:15

4 Answers 4

7

You can get it, to some degree... not sure if this is useful:

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

abstract class MyClass<T> {
  public MyClass() {        
    Type genericSuperclass = this.getClass().getGenericSuperclass();
    if (genericSuperclass instanceof ParameterizedType) {
      ParameterizedType pt = (ParameterizedType) genericSuperclass;
      Type type = pt.getActualTypeArguments()[0];
      System.out.println(type); // prints class java.lang.String for FooClass
    }
  }
}

public class FooClass extends MyClass<String> { 
  public FooClass() {
    super();
  }
  public static void main(String[] args) {
    new FooClass();
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that if you did new ConcreteMyClass<String>(), where public class ConcreteMyClass<T> extends MyClass<T> you would just get "T", since the type isn't reified.
4

We've done this

public abstract BaseClass<T>{
protected Class<? extends T> clazz;

    public BaseClass(Class<? extends T> theClass)
    {
        this.clazz = theClass;
    }
...
}

And in the subclasses,

public class SubClass extends BaseClass<Foo>{
    public SubClass(){
       super(Foo.class);
    }
}

Comments

0

And you cannot simply add a constructor parameter?

public abstract class MyClass<T> { 
  public MyClass(Class<T> type) {
    // do something with type?
  }
}

Comments

0

If I'm not reading this wrong, wouldn't you just want

public <T> void save(T entity)

and

public <T> T getEntityById(Long id)

for your method signatures?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.