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

(proxy) Proxy constructor should check for null argument

XMLWordPrintable

    • b91
    • generic
    • generic
    • Verified



      Name: bsC130419 Date: 08/02/2001


      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)

      The specification for java.lang.reflect.Proxy.newProxyInstance requires a
      NullPointerException if the InvocationHandler argument is null. However, this
      same restriction does not currently apply to proxy instances constructed by
      reflection. I propose that the Proxy constructor be modified to throw a
      NullPointerException if parameter h is null and isProxyInstance() returns true,
      so that it is impossible to create a proxy instance without a handler, rather
      than the current problem of failing at an undetermined point later on when the
      handler is first used.

      This class shows the difference:
      import java.lang.reflect.*;
      class Foo {
        public static void main(String[] args) {
          Object o = null;
          try {
            o = Proxy.newProxyInstance(Foo.class.getClassLoader(),
                                       new Class[0], null);
          } catch (NullPointerException e) {
            System.out.println("newProxyInstance OK");
          }
          try {
            o = Proxy.getProxyClass(Foo.class.getClassLoader(),
                                    new Class[0]).
                  getConstructor(new Class[] {InvocationHandler.class}).
                  newInstance(new Object[1]);
            System.out.println("Oops, created proxy instance without handler");
          } catch (NullPointerException e) {
            System.out.println("Proxy constructor OK");
          } catch (Exception e) {
            System.out.println("shouldn't get here");
          }
          if (o != null)
            try {
              System.out.println(o);
            } catch (NullPointerException e) {
              System.out.println("Delayed failure is BAD");
              e.printStackTrace();
            }
        }
      }

      Implementation wise, all you need do is change Proxy(InvocationHandler h),
      adding a check:
      if (h == null && isProxyInstance(this)) throw new NullPointerException();
      (Review ID: 129305)
      ======================================================================

            mchung Mandy Chung (Inactive)
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: