jdk/test/java/lang/reflect/Proxy/Basic1.java
changeset 17497 e65d73b7ae54
parent 5506 202f599c92aa
child 23010 6dadb192ad81
equal deleted inserted replaced
17496:50d2fb6f323f 17497:e65d73b7ae54
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 4227192
    25  * @bug 4227192 4487672
    26  * @summary This is a basic functional test of the dynamic proxy API (part 1).
    26  * @summary This is a basic functional test of the dynamic proxy API (part 1).
    27  * @author Peter Jones
    27  * @author Peter Jones
    28  *
    28  *
    29  * @build Basic1
    29  * @build Basic1
    30  * @run main Basic1
    30  * @run main Basic1
    40 
    40 
    41         System.err.println(
    41         System.err.println(
    42             "\nBasic functional test of dynamic proxy API, part 1\n");
    42             "\nBasic functional test of dynamic proxy API, part 1\n");
    43 
    43 
    44         try {
    44         try {
    45             Class[] interfaces =
    45             Class<?>[] interfaces =
    46                 new Class[] { Runnable.class, Observer.class };
    46                 new Class<?>[] { Runnable.class, Observer.class };
    47 
    47 
    48             ClassLoader loader = ClassLoader.getSystemClassLoader();
    48             ClassLoader loader = ClassLoader.getSystemClassLoader();
    49 
    49 
    50             /*
    50             /*
    51              * Generate a proxy class.
    51              * Generate a proxy class.
    52              */
    52              */
    53             Class proxyClass = Proxy.getProxyClass(loader, interfaces);
    53             Class<?> proxyClass = Proxy.getProxyClass(loader, interfaces);
    54             System.err.println("+ generated proxy class: " + proxyClass);
    54             System.err.println("+ generated proxy class: " + proxyClass);
    55 
    55 
    56             /*
    56             /*
    57              * Verify that it is public, final, and not abstract.
    57              * Verify that it is public, final, and not abstract.
    58              */
    58              */
    70             }
    70             }
    71 
    71 
    72             /*
    72             /*
    73              * Verify that it is assignable to the proxy interfaces.
    73              * Verify that it is assignable to the proxy interfaces.
    74              */
    74              */
    75             for (int i = 0; i < interfaces.length; i++) {
    75             for (Class<?> intf : interfaces) {
    76                 if (!interfaces[i].isAssignableFrom(proxyClass)) {
    76                 if (!intf.isAssignableFrom(proxyClass)) {
    77                     throw new RuntimeException(
    77                     throw new RuntimeException(
    78                         "proxy class not assignable to proxy interface " +
    78                         "proxy class not assignable to proxy interface " +
    79                         interfaces[i].getName());
    79                         intf.getName());
    80                 }
    80                 }
    81             }
    81             }
    82 
    82 
    83             /*
    83             /*
    84              * Verify that it has the given permutation of interfaces.
    84              * Verify that it has the given permutation of interfaces.
    85              */
    85              */
    86             List l1 = Arrays.asList(interfaces);
    86             List<Class<?>> l1 = Arrays.asList(interfaces);
    87             List l2 = Arrays.asList(proxyClass.getInterfaces());
    87             List<Class<?>> l2 = Arrays.asList(proxyClass.getInterfaces());
    88             System.err.println("+ proxy class's interfaces: " + l2);
    88             System.err.println("+ proxy class's interfaces: " + l2);
    89             if (!l1.equals(l2)) {
    89             if (!l1.equals(l2)) {
    90                 throw new RuntimeException(
    90                 throw new RuntimeException(
    91                     "proxy class interfaces are " + l2 +
    91                     "proxy class interfaces are " + l2 +
    92                     " (expected " + l1 + ")");
    92                     " (expected " + l1 + ")");
   116 
   116 
   117             /*
   117             /*
   118              * Verify that it has a constructor that takes an
   118              * Verify that it has a constructor that takes an
   119              * InvocationHandler instance.
   119              * InvocationHandler instance.
   120              */
   120              */
   121             Constructor cons = proxyClass.getConstructor(
   121             Constructor<?> cons = proxyClass.getConstructor(InvocationHandler.class);
   122                 new Class[] { InvocationHandler.class });
   122 
       
   123             /*
       
   124              * Test constructor with null InvocationHandler
       
   125              */
       
   126             try {
       
   127                 cons.newInstance(new Object[] { null });
       
   128                 throw new RuntimeException("Expected NullPointerException thrown");
       
   129             } catch (InvocationTargetException e) {
       
   130                 Throwable t = e.getTargetException();
       
   131                 if (!(t instanceof NullPointerException)) {
       
   132                     throw t;
       
   133                 }
       
   134             }
   123 
   135 
   124             /*
   136             /*
   125              * Construct a proxy instance.
   137              * Construct a proxy instance.
   126              */
   138              */
   127             Handler handler = new Handler();
   139             Handler handler = new Handler();
   128             Object proxy = cons.newInstance(new Object[] { handler });
   140             Object proxy = cons.newInstance(handler);
   129             handler.currentProxy = proxy;
   141             handler.currentProxy = proxy;
   130 
   142 
   131             /*
   143             /*
   132              * Invoke a method on a proxy instance.
   144              * Invoke a method on a proxy instance.
   133              */
   145              */
   139                         handler.lastMethod + ")");
   151                         handler.lastMethod + ")");
   140             }
   152             }
   141 
   153 
   142             System.err.println("\nTEST PASSED");
   154             System.err.println("\nTEST PASSED");
   143 
   155 
   144         } catch (Exception e) {
   156         } catch (Throwable e) {
   145             System.err.println("\nTEST FAILED:");
   157             System.err.println("\nTEST FAILED:");
   146             e.printStackTrace();
   158             e.printStackTrace();
   147             throw new RuntimeException("TEST FAILED: " + e.toString());
   159             throw new RuntimeException("TEST FAILED: " + e.toString());
   148         }
   160         }
   149     }
   161     }