简体   繁体   中英

Are java.lang.reflect.Proxy instances treated specially for finalization?

When I create an instance of an interface using java.lang.reflect.Proxy.newInstance(...) calls to finalize on that object are not being passed to the invocationHandler. Can anyone point me to where this behaviour is documented?

private Method lastInvokedMethod = null;

@Test
public void finalize_methods_seem_to_disappear_on_proxies() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    final Method lengthMethod = CharSequence.class.getDeclaredMethod("length");
    final Method finalizeMethod = Object.class.getDeclaredMethod("finalize");
    final Method equalsMethod = Object.class.getDeclaredMethod("equals", new Class[] {Object.class});

    InvocationHandler handler = new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            lastInvokedMethod = method;
            if (method.equals(lengthMethod))
                return 42;
            else if (method.equals(equalsMethod))
                return true;
            else
                return null;
        }
    };
    CharSequence proxy = (CharSequence) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[]{CharSequence.class}, handler);

    // check that invocationHandler is working via reflection
    lastInvokedMethod = null;
    assertEquals(42, invokeMethod(proxy, lengthMethod));
    assertEquals(lengthMethod, lastInvokedMethod);

    // check that other methods defined on Object are delegated
    lastInvokedMethod = null;
    assertEquals(true, invokeMethod(proxy, equalsMethod, "banana"));
    assertEquals(equalsMethod, lastInvokedMethod);

    // check that we can invoke finalize through reflection
    Object finalizableObject = new Object() {
        protected void finalize() throws Throwable {
            lastInvokedMethod = finalizeMethod;
            super.finalize();
        }
    };
    lastInvokedMethod = null;
    invokeMethod(finalizableObject, finalizeMethod);
    assertEquals(finalizeMethod, lastInvokedMethod);

    // Finally - a call to finalize is not delegated
    lastInvokedMethod = null;
    invokeMethod(proxy, finalizeMethod);
    assertNull(lastInvokedMethod);
}

private Object invokeMethod(Object object, Method method, Object... args) throws IllegalAccessException, InvocationTargetException {
    method.setAccessible(true);
    return method.invoke(object, args);
}

java.lang.reflect.Proxy API

•An invocation of the hashCode, equals, or toString methods declared in java.lang.Object on a proxy instance will be encoded and dispatched to the invocation handler's invoke method in the same manner as interface method invocations are encoded and dispatched, as described above. The declaring class of the Method object passed to invoke will be java.lang.Object. Other public methods of a proxy instance inherited from java.lang.Object are not overridden by a proxy class, so invocations of those methods behave like they do for instances of java.lang.Object .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM