jdk/test/java/lang/reflect/Proxy/Basic1.java
author mchung
Thu, 16 May 2013 15:08:24 -0700
changeset 17497 e65d73b7ae54
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
4487672: (proxy) Proxy constructor should check for null argument Reviewed-by: alanb, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
17497
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
    25
 * @bug 4227192 4487672
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary This is a basic functional test of the dynamic proxy API (part 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @build Basic1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main Basic1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class Basic1 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            "\nBasic functional test of dynamic proxy API, part 1\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        try {
17497
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
    45
            Class<?>[] interfaces =
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
    46
                new Class<?>[] { Runnable.class, Observer.class };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            ClassLoader loader = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
             * Generate a proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
             */
17497
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
    53
            Class<?> proxyClass = Proxy.getProxyClass(loader, interfaces);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            System.err.println("+ generated proxy class: " + proxyClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
             * Verify that it is public, final, and not abstract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            int flags = proxyClass.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                "+ proxy class's modifiers: " + Modifier.toString(flags));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            if (!Modifier.isPublic(flags)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                throw new RuntimeException("proxy class in not public");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (!Modifier.isFinal(flags)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                throw new RuntimeException("proxy class in not final");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            if (Modifier.isAbstract(flags)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                throw new RuntimeException("proxy class in abstract");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
             * Verify that it is assignable to the proxy interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
             */
17497
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
    75
            for (Class<?> intf : interfaces) {
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
    76
                if (!intf.isAssignableFrom(proxyClass)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        "proxy class not assignable to proxy interface " +
17497
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
    79
                        intf.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
             * Verify that it has the given permutation of interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
             */
17497
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
    86
            List<Class<?>> l1 = Arrays.asList(interfaces);
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
    87
            List<Class<?>> l2 = Arrays.asList(proxyClass.getInterfaces());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            System.err.println("+ proxy class's interfaces: " + l2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (!l1.equals(l2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    "proxy class interfaces are " + l2 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    " (expected " + l1 + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
             * Verify that system agress that it is a proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (Proxy.isProxyClass(Object.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    "Proxy.isProxyClass returned true for java.lang.Object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (!Proxy.isProxyClass(proxyClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    "Proxy.isProxyClass returned false for proxy class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
             * Verify that its protection domain is the bootstrap domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            ProtectionDomain pd = proxyClass.getProtectionDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            System.err.println("+ proxy class's protextion domain: " + pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (!pd.implies(new AllPermission())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    "proxy class does not have AllPermission");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
             * Verify that it has a constructor that takes an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
             * InvocationHandler instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
             */
17497
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   121
            Constructor<?> cons = proxyClass.getConstructor(InvocationHandler.class);
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   122
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   123
            /*
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   124
             * Test constructor with null InvocationHandler
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   125
             */
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   126
            try {
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   127
                cons.newInstance(new Object[] { null });
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   128
                throw new RuntimeException("Expected NullPointerException thrown");
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   129
            } catch (InvocationTargetException e) {
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   130
                Throwable t = e.getTargetException();
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   131
                if (!(t instanceof NullPointerException)) {
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   132
                    throw t;
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   133
                }
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   134
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
             * Construct a proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            Handler handler = new Handler();
17497
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   140
            Object proxy = cons.newInstance(handler);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            handler.currentProxy = proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
             * Invoke a method on a proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            Method m = Runnable.class.getMethod("run", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            ((Runnable) proxy).run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (!handler.lastMethod.equals(m)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    "proxy method invocation failure (lastMethod = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        handler.lastMethod + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            System.err.println("\nTEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
17497
e65d73b7ae54 4487672: (proxy) Proxy constructor should check for null argument
mchung
parents: 5506
diff changeset
   156
        } catch (Throwable e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            System.err.println("\nTEST FAILED:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new RuntimeException("TEST FAILED: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static class Handler implements InvocationHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        Object currentProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        Method lastMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        public Object invoke(Object proxy, Method method, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (proxy != currentProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    "wrong proxy instance passed to invoke method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            lastMethod = method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}