How to determinate an object class with Class<?> clazz

Viewed 64

How can I determinate if entity and request are same type class of clazz (Example: if(entity.equals(clazz) && request.equals(clazz)) but not work. (In this case, i assigned ContattoCliente.class to clazz so that if is true (if (clazz == ContattoCliente.class))

Language Java version 11.

private Object convertMapMergedToEntity(Object entity, Object request, Class<?> clazz) {
        List<Object> resultList = new ArrayList<>();
        Object clienteMerged = new Object();
        ObjectMapper mapper = JsonMapper.builder().addModule(new JavaTimeModule()).build();
        if (clazz == ContattoCliente.class) {
            for (int i = 0; i < ((List<Object>) request).size(); i++) {
                Object contattoMerged = new Object();
                LinkedHashMap<Object, Object> entityMap = mapper.convertValue(((List<Object>) entity).get(i),
                        LinkedHashMap.class);
                LinkedHashMap<Object, Object> requestMap = mapper.convertValue(((List<Object>) request).get(i),
                        LinkedHashMap.class);

                contattoMerged = mapDifferenceMerged2(entityMap, requestMap, clazz);
                resultList.add(contattoMerged);
            }
            return resultList;
        } else if (clazz == Cliente.class) {
            LinkedHashMap<Object, Object> entityMap = mapper.convertValue(entity, LinkedHashMap.class);
            LinkedHashMap<Object, Object> requestMap = mapper.convertValue(request, LinkedHashMap.class);
            clienteMerged = mapDifferenceMerged2(entityMap, requestMap, clazz);
        }

        return clienteMerged;
    }


The problem is that if i compare them with Getclass () the condition will always be false, I have also tried everything that is after Dot Getclass ()
        System.out.println("entity  ->" + entity.getClass());
        System.out.println("request  -> " + request.getClass());
        System.out.println("clazz  ->" + clazz.getSimpleName());

Output:
entity -> class org.hibernate.collection.internal.PersistentBag
request -> class java.util.LinkedList
clazz -> ContattoCliente
3 Answers

I'm not sure if this is what you expected, but if you what to check whether entity and request are compatible with clazz, you can use Class.isInstance(Object), that's to say clazz.isInstance(entity) && clazz.isInstance(request).

I think that the method you're looking is isAssignableFrom. This method checks that a class is equal to, or subclass from the other.

So your code should look like this

if (ContattoCliente.class.isAssignableFrom(clazz)) {

I understood that you need to check whether entity and request are instances of the same class, and that you added the third parameter clazz because you previous attempts to check the "type equality" failed.

What about this:

if( nonNull( entity ) && nonNull( request ) && entity.getClass().equals( request.getClass() ) )
{
  …   
}

If the class for the arguments has to be that one specified by clazz, think about this construct:

private <T> Object convertMapMergedToEntity( T entity, T request, Class<T> clazz) 
{
  …
}

Here enforces the compiler that entity and request are of the type T that is specified by the clazz parameter; it does not require a runtime check any more.

If you want to have the runtime check by any means, try this (but add null checks for at least entity and request):

if( entity.getClass().equals( request.getClass() && entity.getClass().equals( clazz ) ) )
{
  …   
}
Related