For Weld on WildFly, do the following:
public boolean isProxy(Object obj) { try{ return Class.forName("org.jboss.weld.bean.proxy.ProxyObject").isInstance(obj); } catch (Exception e) { log.error("Unable to check if object is proxy", e); } return false; }
To extract the actual object instead of the proxy (I need to serialize it), I do this:
public Object getObject(Object obj) { Field f = null; boolean isAccessible = false; try { for(Field fi : Class.forName(handler).getDeclaredFields()) { if(fi.getName().equals(field)) { f = fi; isAccessible = f.isAccessible(); f.setAccessible(true); } } } catch (Exception e) { throw new RuntimeException(e); } if(f == null) { throw new RuntimeException(new NoSuchFieldException(String.format( "The required field '%s' not found in '%s'. " + "May be the code is obsolete for running on this application server.", field, method))); } else { try{ obj = f.get(getHandler(obj)); for(Method m : Class.forName(instance).getMethods()) { if(m.getName().equals(value)) { return m.invoke(obj); } } } catch (Exception e) { throw new RuntimeException(e); } finally { f.setAccessible(isAccessible); } throw new NoSuchMethodError(String.format( "The required method '%s' not found in '%s'. " + "May be the code is obsolete for running on this application server.", value, instance)); } }
Remember that this is the darkest magic, as far as possible, they have very low performance and can break during any WildFly update, if they change classes, methods for fields in it.
source share