lundi 23 mars 2015

invokeExact on an instance created with entityClass.newInstance() causes java.lang.invoke.WrongMethodTypeException?

I have an issue with invokeExact method when passing it an instance created with newInstance method.


My method looks something like this



public <T extends BaseEntity> T getEntity(Class<T> entityClass) {

T entity;

try {
entity = entityClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}

for (Field field : map.get(entityClass).keySet()) {
if (get(field.getName()) != null) {
try {
MethodHandle methodHandle = map.get(entityClass).get(field);
methodHandle.invokeExact(entity, (String) get(field.getName()));
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}

return entity;
}


The method is invoked like this



resource.getEntity(DemoEntity.class);


end that results in



Caused by: java.lang.invoke.WrongMethodTypeException: **expected (DemoEntity,String)void but found (BaseEntity,String)void**
at java.lang.invoke.Invokers.newWrongMethodTypeException(Invokers.java:349) ~[na:1.7.0_45]
at java.lang.invoke.Invokers.checkExactType(Invokers.java:360) ~[na:1.7.0_45]
at com.me.api.entity.request.DemoEntityRequest.getEntity(DemoEntityRequest.java:49) ~[classes/:na]
... 27 common frames omitted


What is causing the problem here, why invoke method sees T entity as its super class type and not as a DemoEntity?






Aucun commentaire:

Enregistrer un commentaire