Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting instance variables of the proxied objects in the InvocationHandler
    primarykey
    data
    text
    <p>In the Proxy object (the object implementing <code>java.lang.reflect.InvocationHandler</code>), I am trying to set an instance variable in the proxied object.</p> <p>Like the following:</p> <pre><code>public class ServiceProxy implements InvocationHandler { private final Object proxiedObject; private ServiceProxy(final Object object) { this.proxiedObject = object; } public static Object newInstance(final Object object) { return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), new ServiceProxy(object)); } public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { Object result = null; MyObject mo = new MyObject(); // Is the following safe when the proxiedObject is being acceessed by multiple threads? final Field sessionField = this.proxiedObject.getClass().getSuperclass().getDeclaredField("mo"); sessionField.setAccessible(true); sessionField.set(this.object, mo); result = method.invoke(this.proxiedObject, args); return result; } } </code></pre> <p>Is this safe?</p> <p><strong>EDIT</strong>:</p> <p>Actual code:</p> <pre><code>Object result = null; Session session = HibernateUtil.getSessionFactory().getCurrentSession() // Is the following save when the proxiedObject is being acceessed by multiple threads? final Field sessionField = this.proxiedObject.getClass().getSuperclass().getDeclaredField("session"); sessionField.setAccessible(true); sessionField.set(this.object, session); result = method.invoke(this.proxiedObject, args); return result; </code></pre> <p><strong>Edit2</strong>: The proxied object is being called from GWT client that calls multiple methods of the same proxied object at the same time. When this happens, I got the session instance field (of proxied class) to be closed and opened in unexpected manner.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload