How to build a differentiable for a Generic class?

Asked

Viewed 220 times

2

How would it be possible to build a differentiated constructor for classes that extend a generic class?

For example, for the situation below, I would like to create a Generic builder that can perform the instruction Entity = E.class;, but for now I could not. So that it could be invoked in the constructor of the Itemmanager class.

How could we do that?

public class Generic<E> {
    private Class<E> entity;

    public void setEntity(Class<E> entity) {
        this.entity = entity;
    }

    public Generic() {
        entity = E.class;
    }
}

class ItemManager extends Generic<Item> {
    public ItemManager() {
        super(); 
    }
}
  • entity should reference an object, it cannot reference a class. Tell us what is the intention of this application to try to find a solution. Actually I didn’t understand that line either: private Class<E> entity; can do this? What is the result?

  • Yes, that line is possible. And except the builder public generic, everything works. But, I need in each constructor to make a setEntity, that is, within the constructor of Itemmanager, instead of just calling super, I need to make a setEntity(Entity.class) for example. But this Entity is just the E parameter that I’m passing to the Generic, so I wanted to automate that. The reason for this is to make a generic class to operate on the database with various automated methods.

3 answers

3

The intent of your code is confused. But if I understand correctly you want to store the type of entity and not an instance of it.

So just make your builder like this:

public abstract class Generic<E> {
    private Class<E> entityClass;

    public Generic() {
        this.entityClass = (Class<E>) 
            ((ParameterizedType) getClass().getGenericSuperclass())
                .getActualTypeArguments()[0];
    }
}

Note: I changed the attribute entity for entityClass to make it clear that it is not a entity which is being stored but the type (class) of the entity.


However, if you want to receive the instance of the same entity, the code is like this:

public abstract class Generic<E> {
    private E entity;

    public void setEntity(E entity) {
        this.entity = entity;
    }
}
  • It is good to note that this magic does not extend to the possibility of executing, for example, new Generic<Item>(). For Generic would make more sense as a class abstract.

  • @Dangetz Very good! abstract added.

2

It may be simpler to use Item.class where it is possible, and pass it to the builder of Generic:

public class Generic<E> {
    ...
    public Generic(Class<E> entity) {
        this.entity = entity;
    }
}

class ItemManager extends Generic<Item> {
    public ItemManager() {
        super(Item.class);
    }
}

0

Example:

Class Generic

public class Generic <E> {
    private Class<E> entity;

    public void setEntity(Class<E> entity) {
        this.entity = entity;
    }

    public Generic(Class<E> entity) {
        this.entity = entity;
    }
}

Class ItemManager

public class ItemManager<E> extends Generic<E> {
    public ItemManager(Class<E> entity){        
        super(entity);
    }
}

Instantiating

public static void main(String[] args) 
{
    ItemManager<Item> itemManager = new ItemManager<>(Item.class); 
    System.out.print(itemManager.getEntity());

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.