src/java.base/share/classes/java/lang/reflect/ReflectAccess.java
author mchung
Tue, 23 Jul 2019 20:03:03 -0700
changeset 57503 042dfb697624
parent 52020 4c247dde38ed
permissions -rw-r--r--
8219774: Reexamine the initialization of LangReflectAccess shared secret at AccessibleObject::<clinit> Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57503
042dfb697624 8219774: Reexamine the initialization of LangReflectAccess shared secret at AccessibleObject::<clinit>
mchung
parents: 52020
diff changeset
     2
 * Copyright (c) 2001, 2019, 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang.reflect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 32268
diff changeset
    28
import jdk.internal.reflect.MethodAccessor;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 32268
diff changeset
    29
import jdk.internal.reflect.ConstructorAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/** Package-private class implementing the
57503
042dfb697624 8219774: Reexamine the initialization of LangReflectAccess shared secret at AccessibleObject::<clinit>
mchung
parents: 52020
diff changeset
    32
    jdk.internal.access.JavaLangReflectAccess interface, allowing the java.lang
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    package to instantiate objects in this package. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
57503
042dfb697624 8219774: Reexamine the initialization of LangReflectAccess shared secret at AccessibleObject::<clinit>
mchung
parents: 52020
diff changeset
    35
class ReflectAccess implements jdk.internal.access.JavaLangReflectAccess {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public <T> Constructor<T> newConstructor(Class<T> declaringClass,
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 2
diff changeset
    37
                                             Class<?>[] parameterTypes,
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 2
diff changeset
    38
                                             Class<?>[] checkedExceptions,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
                                             int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
                                             int slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                                             String signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                                             byte[] annotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                                             byte[] parameterAnnotations)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    45
        return new Constructor<>(declaringClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                  parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                                  checkedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                                  modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                  slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                                  signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                  annotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                  parameterAnnotations);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public MethodAccessor getMethodAccessor(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return m.getMethodAccessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public void setMethodAccessor(Method m, MethodAccessor accessor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        m.setMethodAccessor(accessor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 2
diff changeset
    63
    public ConstructorAccessor getConstructorAccessor(Constructor<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return c.getConstructorAccessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 2
diff changeset
    67
    public void setConstructorAccessor(Constructor<?> c,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                       ConstructorAccessor accessor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        c.setConstructorAccessor(accessor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 2
diff changeset
    73
    public int getConstructorSlot(Constructor<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return c.getSlot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 2
diff changeset
    77
    public String getConstructorSignature(Constructor<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return c.getSignature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 2
diff changeset
    81
    public byte[] getConstructorAnnotations(Constructor<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return c.getRawAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 2
diff changeset
    85
    public byte[] getConstructorParameterAnnotations(Constructor<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return c.getRawParameterAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 9035
diff changeset
    89
    public byte[] getExecutableTypeAnnotationBytes(Executable ex) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 9035
diff changeset
    90
        return ex.getTypeAnnotationBytes();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 9035
diff changeset
    91
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 9035
diff changeset
    92
42997
5f83f2054eca 8172190: Re-apply the fix for bugs 8062389, 8029459, 8061950
plevart
parents: 42948
diff changeset
    93
    public Class<?>[] getExecutableSharedParameterTypes(Executable ex) {
5f83f2054eca 8172190: Re-apply the fix for bugs 8062389, 8029459, 8061950
plevart
parents: 42948
diff changeset
    94
        return ex.getSharedParameterTypes();
5f83f2054eca 8172190: Re-apply the fix for bugs 8062389, 8029459, 8061950
plevart
parents: 42948
diff changeset
    95
    }
5f83f2054eca 8172190: Re-apply the fix for bugs 8062389, 8029459, 8061950
plevart
parents: 42948
diff changeset
    96
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // Copying routines, needed to quickly fabricate new Field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // Method, and Constructor objects from templates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public Method      copyMethod(Method arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return arg.copy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
32268
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 25859
diff changeset
   104
    public Method      leafCopyMethod(Method arg) {
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 25859
diff changeset
   105
        return arg.leafCopy();
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 25859
diff changeset
   106
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public Field       copyField(Field arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return arg.copy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public <T> Constructor<T> copyConstructor(Constructor<T> arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return arg.copy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
50091
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 47216
diff changeset
   115
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 47216
diff changeset
   116
    @SuppressWarnings("unchecked")
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 47216
diff changeset
   117
    public <T extends AccessibleObject> T getRoot(T obj) {
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 47216
diff changeset
   118
        return (T) obj.getRoot();
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 47216
diff changeset
   119
    }
52020
4c247dde38ed 8206240: java.lang.Class.newInstance() is causing caller to leak
mchung
parents: 50091
diff changeset
   120
4c247dde38ed 8206240: java.lang.Class.newInstance() is causing caller to leak
mchung
parents: 50091
diff changeset
   121
    public <T> T newInstance(Constructor<T> ctor, Object[] args, Class<?> caller)
4c247dde38ed 8206240: java.lang.Class.newInstance() is causing caller to leak
mchung
parents: 50091
diff changeset
   122
        throws IllegalAccessException, InstantiationException, InvocationTargetException
4c247dde38ed 8206240: java.lang.Class.newInstance() is causing caller to leak
mchung
parents: 50091
diff changeset
   123
    {
4c247dde38ed 8206240: java.lang.Class.newInstance() is causing caller to leak
mchung
parents: 50091
diff changeset
   124
        return ctor.newInstanceWithCaller(args, true, caller);
4c247dde38ed 8206240: java.lang.Class.newInstance() is causing caller to leak
mchung
parents: 50091
diff changeset
   125
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}