-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
5.0, 6
-
Cause Known
-
x86
-
linux, windows_xp
A DESCRIPTION OF THE REQUEST :
I suggest adding the following method to java.lang.reflect.Proxy:
/**
* Returns an instance of a proxy class for the specified interface
* that dispatches method invocations to the specified invocation
* handler. This method is equivalent to:
* <pre>
* (T)Proxy.newProxyInstance(ifc.getClassLoader(), new Class[] { ifc }, handler);
* </pre>
*
* @param ifc the interface for the proxy class to implement
* @param h the invocation handler to dispatch method invocations to
* @return a proxy instance with the specified invocation handler of a
* proxy class that is defined by the specified interface's class loader
* and that implements the specified interface
* @throws IllegalArgumentException if any of the restrictions on the
* parameters that may be passed to <code>getProxyClass</code>
* are violated
* @throws NullPointerException if the <code>interface</code>
* argument or any of its elements is <code>null</code>, or
* if the invocation handler, <code>h</code>, is
* <code>null</code>
*/
public static <T> T newProxyInstance(Class<T> ifc, InvocationHandler invocationHandler) {
return ifc.cast(Proxy.newProxyInstance(ifc.getClassLoader(), new Class[] { ifc }, invocationHandler));
}
JUSTIFICATION :
Most invocations of the Proxy.newProxyInstace method
a) specify only one interface
b) specify that interface's classloader as target classloader for the proxy class.
c) want the result to be statically of the type of the passed interface
Having to create a class array and a cast looks ulgier than it needs to. With only one interface argument, the result is garantueed to implement that interface, so that makes for a good return type.
I suggest adding the following method to java.lang.reflect.Proxy:
/**
* Returns an instance of a proxy class for the specified interface
* that dispatches method invocations to the specified invocation
* handler. This method is equivalent to:
* <pre>
* (T)Proxy.newProxyInstance(ifc.getClassLoader(), new Class[] { ifc }, handler);
* </pre>
*
* @param ifc the interface for the proxy class to implement
* @param h the invocation handler to dispatch method invocations to
* @return a proxy instance with the specified invocation handler of a
* proxy class that is defined by the specified interface's class loader
* and that implements the specified interface
* @throws IllegalArgumentException if any of the restrictions on the
* parameters that may be passed to <code>getProxyClass</code>
* are violated
* @throws NullPointerException if the <code>interface</code>
* argument or any of its elements is <code>null</code>, or
* if the invocation handler, <code>h</code>, is
* <code>null</code>
*/
public static <T> T newProxyInstance(Class<T> ifc, InvocationHandler invocationHandler) {
return ifc.cast(Proxy.newProxyInstance(ifc.getClassLoader(), new Class[] { ifc }, invocationHandler));
}
JUSTIFICATION :
Most invocations of the Proxy.newProxyInstace method
a) specify only one interface
b) specify that interface's classloader as target classloader for the proxy class.
c) want the result to be statically of the type of the passed interface
Having to create a class array and a cast looks ulgier than it needs to. With only one interface argument, the result is garantueed to implement that interface, so that makes for a good return type.
- duplicates
-
JDK-6543130 (proxy) Suggest more type-safe overload of Proxy.newProxyInstance
-
- Closed
-