Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6460347

(proxy) Add convenience method to create proxies

    XMLWordPrintable

    Details

    • Subcomponent:
    • Understanding:
      Cause Known
    • CPU:
      x86
    • OS:
      linux, windows_xp

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              rmandalasunw Ranjith Mandala (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Imported:
                Indexed: