jdk/test/java/lang/reflect/AccessibleObject/ModuleSetAccessibleTest.java
author alanb
Fri, 10 Feb 2017 09:04:39 +0000
changeset 43712 5dfd0950317c
parent 42338 a60f280f803c
child 44545 83b611b88ac8
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: dfuchs, psandoz, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, alex.buckley@oracle.com, mark.reinhold@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @build ModuleSetAccessibleTest
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36656
diff changeset
    27
 * @modules java.base/java.lang:open
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36656
diff changeset
    28
 *          java.base/jdk.internal.misc:+open
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @run testng ModuleSetAccessibleTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 * @summary Test java.lang.reflect.AccessibleObject with modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.reflect.AccessibleObject;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.reflect.Constructor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.lang.reflect.Field;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.lang.reflect.InaccessibleObjectException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.lang.reflect.InvocationTargetException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.lang.reflect.Method;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.lang.reflect.Module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
36656
chegar
parents: 36511
diff changeset
    41
import jdk.internal.misc.Unsafe;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import org.testng.annotations.Test;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import static org.testng.Assert.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
@Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
public class ModuleSetAccessibleTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
     * Invoke a private constructor on a public class in an exported package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    public void testPrivateConstructorInExportedPackage() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        Constructor<?> ctor = Unsafe.class.getDeclaredConstructor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
            ctor.newInstance();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
            assertTrue(false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        } catch (IllegalAccessException expected) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        ctor.setAccessible(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        Unsafe unsafe = (Unsafe) ctor.newInstance();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
     * Invoke a private method on a public class in an exported package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    public void testPrivateMethodInExportedPackage() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        Method m = Unsafe.class.getDeclaredMethod("throwIllegalAccessError");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
            m.invoke(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            assertTrue(false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        } catch (IllegalAccessException expected) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        m.setAccessible(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
            m.invoke(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            assertTrue(false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        } catch (InvocationTargetException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            // thrown by throwIllegalAccessError
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            assertTrue(e.getCause() instanceof IllegalAccessError);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
     * Access a private field in a public class that is an exported package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    public void testPrivateFieldInExportedPackage() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
            f.get(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            assertTrue(false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        } catch (IllegalAccessException expected) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        f.setAccessible(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        Unsafe unsafe = (Unsafe) f.get(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
     * Invoke a public constructor on a public class in a non-exported package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    public void testPublicConstructorInNonExportedPackage() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        Class<?> clazz = Class.forName("sun.security.x509.X500Name");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        Constructor<?> ctor = clazz.getConstructor(String.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
            ctor.newInstance("cn=duke");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
            assertTrue(false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        } catch (IllegalAccessException expected) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
            ctor.setAccessible(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
            assertTrue(false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        } catch (InaccessibleObjectException expected) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        ctor.setAccessible(false); // should succeed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
     * Access a public field in a public class that in a non-exported package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    public void testPublicFieldInNonExportedPackage() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        Class<?> clazz = Class.forName("sun.security.x509.X500Name");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        Field f = clazz.getField("SERIALNUMBER_OID");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            f.get(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            assertTrue(false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        } catch (IllegalAccessException expected) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            f.setAccessible(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
            assertTrue(false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        } catch (InaccessibleObjectException expected) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        f.setAccessible(false); // should succeed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
     * Test that the Class constructor cannot be make accessible.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
    public void testJavaLangClass() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        // non-public constructor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        Constructor<?> ctor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
            = Class.class.getDeclaredConstructor(ClassLoader.class, Class.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        AccessibleObject[] ctors = { ctor };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            ctor.setAccessible(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
            assertTrue(false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        } catch (SecurityException expected) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
            AccessibleObject.setAccessible(ctors, true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
            assertTrue(false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        } catch (SecurityException expected) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        // should succeed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        ctor.setAccessible(false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        AccessibleObject.setAccessible(ctors, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
}