I need to know the type of class [duplicated]

1

Hi, I have this problem, I have a method that recives a Class class and I need to know what is it to know if I execute a method, please I need to know what method I can compare the class to if it is an instance of another

static public void deleteEntity(Class<?> a,Integer id){
    EntityManager manager = JPAUtil.getEntityManager();
    manager.getTransaction().begin();
    {
        Object t = manager.find(a, id);
        manager.remove(t);
    }

    manager.getTransaction().commit();
    manager.close();
    if(a.isInstance(Traza.class)){ // Aqui necesesito saber de que es intancia a clase a
        TrazaService.persistTraces();
    }
}
    
asked by Carlos Alberto Argote Quintero 03.07.2018 в 19:02
source

1 answer

0

with .getClass() you can know the kind of class the object has even when it has been "converted" to the most Generic object type Object however, I do not know if this applies to inherited classes

public static void main(String args[]) {
        Integer i=10;
        String  s="Foo";
        Object iObject= i;
        Object sObject= s;
        System.out.println(iObject.getClass().getName()); // Imprime 'java.lang.Integer'
        System.out.println(sObject.getClass().getName()); // Imprime 'java.lang.String'

//Comparar instancias por nombre de clase
        if( iObject.getClass().getName().equals(Integer.class.getName() )  ){
            System.out.println("El objeto es un Entero");
        }else{
            System.out.println("El objeto NO es un Entero");
        }
        if( i.getClass().getName().equals(Integer.class.getName() )  ){
            System.out.println("El objeto es un Entero");
        }else{
            System.out.println("El objeto NO es un Entero");
        }
    }
    
answered by 03.07.2018 в 20:17