This discussion is archived
5 Replies Latest reply: Aug 6, 2011 4:59 PM by YoungWinston RSS

creating instance of generic type

880718 Newbie
Currently Being Moderated
Hi.

Here is my class

public class ManagerForm<M extends Stuff>{
private Class<M>clazz;
private M manager;

public void setWorker(M manager){
this.manager=manager;
}
public M getWorker(){
return this.manager;
}

private final Class<M> getGenericClass() {
Class<M> persistentClass = null;
Type genericType = getClass().getGenericSuperclass();
if (genericType instanceof ParameterizedType) {
ParameterizedType pType = ((ParameterizedType) genericType);
// obtaining first generic type class
persistentClass = (Class<M>) pType.getActualTypeArguments()[0];
}
return persistentClass;
}

public ManagerForm(){
this.clazz=getGenericClass();
this.manager=this.clazz.newInstance();
}
}

In default constructor I need to inicialize U instance this.user.

I have an nullPointerException in line this.manager=this.clazz.newInstance();

persistantClass is returned nullable because genericType is not instance of ParameterizedType.

Could anybody say me what is wrong or propose other methods to create generic instance. I need help. Will be very appreciable.

Thanks in advance.
  • 1. Re: creating instance of generic type
    801313 Explorer
    Currently Being Moderated
    You are making this more complicated than it needs to be.

    Generics are truly most helpful in doing compile-time (= static) type checking. If you are instantiating a parameterized class at runtime just create without the class parameter.
  • 2. Re: creating instance of generic type
    YoungWinston Expert
    Currently Being Moderated
    877715 wrote:
    Could anybody say me what is wrong or propose other methods to create generic instance. I need help. Will be very appreciable.
    I think you need to back up and explain exactly what you're trying to do here.
    It would appear that you're attempting to divine the actual type of a specific ManagerForm, but I'm not absolutely certain. Also, since you already specify the type as a field, why can't you just use
    manager.getClass()
    ? I suspect that all you'll get from getClass().getGenericSuperclass() is 'Stuff', but I have to admit I'm no expert on this.

    Far better to describe what you want than an implementation that plainly doesn't work.

    Winston
  • 3. Re: creating instance of generic type
    880718 Newbie
    Currently Being Moderated
    YoungWinston wrote:
    877715 wrote:
    Could anybody say me what is wrong or propose other methods to create generic instance. I need help. Will be very appreciable.
    I think you need to back up and explain exactly what you're trying to do here.
    It would appear that you're attempting to divine the actual type of a specific ManagerForm, but I'm not absolutely certain. Also, since you already specify the type as a field, why can't you just use
    manager.getClass()
    ? I suspect that all you'll get from getClass().getGenericSuperclass() is 'Stuff', but I have to admit I'm no expert on this.

    Far better to describe what you want than an implementation that plainly doesn't work.

    Winston
    Well. I have class ManagerForm<M extends Stuff>. there is private field manager that have generic type M. And I need to instantinate manager in default constructor.

    I cant write

    public ManagerForm(){
    this.manager=new M();//will be compiler error
    }

    Thats why exists a mechanism to get current generic type in runtyme through ParameterType.

    I have method getGenericClass that gets current class in runtime and then I create an instance of this class through <class>.newInstance(); And this is the standard way to instantinate generic type.

    I hoped line Type genericType = getClass().getGenericSuperclass(); of method returned ParameterizedType. But variable genericType cannot be casted to ParameterizedType. I don't know why. So I cannot get current generic param M and instantiate its.
  • 4. Re: creating instance of generic type
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Moderator action: Moved from New to Java
    Moderator advice: Please read the announcement(s) at the top of the forum listings and the FAQ linked from every page. They are there for a purpose.

    Then edit your post and format the code correctly.

    db
  • 5. Re: creating instance of generic type
    YoungWinston Expert
    Currently Being Moderated
    Andrew wrote:
    Well. I have class ManagerForm<M extends Stuff>. there is private field manager that have generic type M. And I need to instantinate manager in default constructor.
    You do? I thought you'd already written a
    public ManagerForm(M manager)
    constructor.

    Unless you don't know until runtime what type of ManagerForm you're going to be creating, I can't see much use for the code, particularly as you have to supply a 'manager' of some sort when you're creating it anyway.

    I suspect you're overthinking this by miles; and even if you aren't, this is NOT what generics was created for.

    Winston

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points