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

(proxy) Better Exception handling in a Proxy when Invocation Handler return null

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 1.4.1
    • core-libs
    • Fix Understood
    • x86
    • windows_2000



      Name: rmT116609 Date: 02/04/2003


      FULL PRODUCT VERSION :
      java version "1.4.1"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
      Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)

      FULL OPERATING SYSTEM VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      If an Invocation Handler in a java.lang.reflect.Proxy
      returns null and the Interface returns a basic datatype you
      will receive a:
         java.lang.NullPointerException
         at $Proxy53.getLong(Unknown Source)
         ...

      This exception does not explain the problem at all and
      could lead to a difficult search for a solution.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create an interface where a method returns a basic
      datatype
      2. Implement an Invocation handler that return null for
      this method
      3. Invoke the method through the interface with the proxy
      as implementation

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Currently you get this exception:

         java.lang.NullPointerException
         at $Proxy53.getLong(Unknown Source)
         ...

      I would like that either the Proxy returns the default
      value of the given basic data type like 0 for a Number etc.
      or throws a more appropriate exception.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      $ java ProxyTest
      Exception in thread "main" java.lang.NullPointerException
              at $Proxy0.getLong(Unknown Source)
              at ProxyTest.main(ProxyTest.java:19)

      ---------- BEGIN SOURCE ----------
      import java.lang.reflect.InvocationHandler;
      import java.lang.reflect.Method;
      import java.lang.reflect.Proxy;

      public class ProxyTest {
          public static void main( String[] pArguments ) {
              InvocationHandler lHandler = new InvocationHandler() {
                  public Object invoke( Object proxy, Method method, Object[] args ) {
                      return null;
                  }
              };
              Class[] lInterfaces = new Class[ 1 ];
              lInterfaces[ 0 ] = Test.class;
              Test lTest = (Test) Proxy.newProxyInstance(
                  Thread.currentThread().getContextClassLoader(),
                  lInterfaces,
                  lHandler
              );
              lTest.getLong( 1L );
          }
          
          public interface Test {
              public long getLong( long value );
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Invocation Handler has to make sure that for a basic datatype never a "null" is returned.
      (Review ID: 179179)
      ======================================================================

            peterjones Peter Jones (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: