@SuppressWarnings("unchecked")
public <T extends Entity> T createEntityProxy(EntitySpec<T> spec, T entity) {
ClassLoader classloader = (spec.getImplementation() != null ? spec.getImplementation() : spec.getType()).getClassLoader();
MutableSet.Builder<Class<?>> builder = MutableSet.<Class<?>>builder()
.add(EntityProxy.class, Entity.class, EntityLocal.class, EntityInternal.class);
if (spec.getType().isInterface()) {
builder.add(spec.getType());
} else {
log.warn("EntitySpec declared in terms of concrete type "+spec.getType()+"; should be supplied in terms of interface");
}
builder.addAll(spec.getAdditionalInterfaces());
Set<Class<?>> interfaces = builder.build();
return (T) java.lang.reflect.Proxy.newProxyInstance(
classloader,
interfaces.toArray(new Class[interfaces.size()]),
new EntityProxyImpl(entity));
}