jdk/src/java.base/share/classes/sun/reflect/Reflection.java
author zmajo
Fri, 03 Jul 2015 07:23:45 +0200
changeset 31671 362e0c0acece
parent 31074 0a91f3afaae7
child 34882 ce2a8ec851c1
permissions -rw-r--r--
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics Summary: Annotate possibly intrinsified methods with @HotSpotIntrinsicCandidate. Add checks omitted by intrinsics to the library code. Add CheckIntrinsics flags to check consistency of intrinsics. Reviewed-by: jrose, kvn, thartmann, vlivanov, abuckley, darcy, ascarpino, briangoetz, alanb, aph, dnsimon
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
20872
8e486b70dff8 8022718: Runtime accessibility checking: protected class, if extended, should be accessible from another package
drchase
parents: 16906
diff changeset
     2
 * Copyright (c) 2001, 2013, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 sun.reflect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 31074
diff changeset
    31
import jdk.internal.HotSpotIntrinsicCandidate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/** Common utility routines used by both java.lang and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    java.lang.reflect */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class Reflection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /** Used to filter out fields and methods from certain classes from public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        view, where they are sensitive or they may contain VM-internal objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        These Maps are updated very rarely. Rather than synchronize on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        each access, we use copy-on-write */
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    42
    private static volatile Map<Class<?>,String[]> fieldFilterMap;
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    43
    private static volatile Map<Class<?>,String[]> methodFilterMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static {
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    46
        Map<Class<?>,String[]> map = new HashMap<Class<?>,String[]>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        map.put(Reflection.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            new String[] {"fieldFilterMap", "methodFilterMap"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        map.put(System.class, new String[] {"security"});
31074
0a91f3afaae7 8081320: Backout JDK-8059340: ConstantPool::_resolved_references is missing in heap dump
vlivanov
parents: 31071
diff changeset
    50
        map.put(Class.class, new String[] {"classLoader"});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        fieldFilterMap = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    53
        methodFilterMap = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
    56
    /** Returns the class of the caller of the method calling this method,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
    57
        ignoring frames associated with java.lang.reflect.Method.invoke()
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
    58
        and its implementation. */
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
    59
    @CallerSensitive
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 31074
diff changeset
    60
    @HotSpotIntrinsicCandidate
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
    61
    public static native Class<?> getCallerClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
21284
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents: 20872
diff changeset
    63
    /**
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents: 20872
diff changeset
    64
     * @deprecated This method will be removed in JDK 9.
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents: 20872
diff changeset
    65
     * This method is a private JDK API and retained temporarily for
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents: 20872
diff changeset
    66
     * existing code to run until a replacement API is defined.
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents: 20872
diff changeset
    67
     */
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents: 20872
diff changeset
    68
    @Deprecated
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents: 20872
diff changeset
    69
    public static native Class<?> getCallerClass(int depth);
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents: 20872
diff changeset
    70
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /** Retrieves the access flags written to the class file. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        inner classes these flags may differ from those returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        Class.getModifiers(), which searches the InnerClasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        attribute to find the source-level access flags. This is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        instead of Class.getModifiers() for run-time access checks due
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        to compatibility reasons; see 4471811. Only the values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        low 13 bits (i.e., a mask of 0x1FFF) are guaranteed to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        valid. */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 31074
diff changeset
    79
    @HotSpotIntrinsicCandidate
20872
8e486b70dff8 8022718: Runtime accessibility checking: protected class, if extended, should be accessible from another package
drchase
parents: 16906
diff changeset
    80
    public static native int getClassAccessFlags(Class<?> c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /** A quick "fast-path" check to try to avoid getCallerClass()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        calls. */
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    84
    public static boolean quickCheckMemberAccess(Class<?> memberClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                                 int modifiers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return Modifier.isPublic(getClassAccessFlags(memberClass) & modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    90
    public static void ensureMemberAccess(Class<?> currentClass,
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    91
                                          Class<?> memberClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                          Object target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                          int modifiers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        throws IllegalAccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (currentClass == null || memberClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (!verifyMemberAccess(currentClass, memberClass, target, modifiers)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new IllegalAccessException("Class " + currentClass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                             " can not access a member of class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                             memberClass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                             " with modifiers \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                             Modifier.toString(modifiers) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                             "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   110
    public static boolean verifyMemberAccess(Class<?> currentClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                             // Declaring class of field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                             // or method
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   113
                                             Class<?> memberClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                             // May be NULL in case of statics
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   115
                                             Object   target,
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   116
                                             int      modifiers)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // Verify that currentClass can access a field, method, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // constructor of memberClass, where that member's access bits are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // "modifiers".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        boolean gotIsSameClassPackage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        boolean isSameClassPackage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (currentClass == memberClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            // Always succeeds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (!Modifier.isPublic(getClassAccessFlags(memberClass))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            isSameClassPackage = isSameClassPackage(currentClass, memberClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            gotIsSameClassPackage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (!isSameClassPackage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // At this point we know that currentClass can access memberClass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (Modifier.isPublic(modifiers)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        boolean successSoFar = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (Modifier.isProtected(modifiers)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            // See if currentClass is a subclass of memberClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (isSubclassOf(currentClass, memberClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                successSoFar = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (!successSoFar && !Modifier.isPrivate(modifiers)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (!gotIsSameClassPackage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                isSameClassPackage = isSameClassPackage(currentClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                                        memberClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                gotIsSameClassPackage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (isSameClassPackage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                successSoFar = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (!successSoFar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (Modifier.isProtected(modifiers)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // Additional test for protected members: JLS 6.6.2
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   171
            Class<?> targetClass = (target == null ? memberClass : target.getClass());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (targetClass != currentClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (!gotIsSameClassPackage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    isSameClassPackage = isSameClassPackage(currentClass, memberClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    gotIsSameClassPackage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (!isSameClassPackage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    if (!isSubclassOf(targetClass, currentClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   188
    private static boolean isSameClassPackage(Class<?> c1, Class<?> c2) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return isSameClassPackage(c1.getClassLoader(), c1.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                  c2.getClassLoader(), c2.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /** Returns true if two classes are in the same package; classloader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        and classname information is enough to determine a class's package */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private static boolean isSameClassPackage(ClassLoader loader1, String name1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                              ClassLoader loader2, String name2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (loader1 != loader2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            int lastDot1 = name1.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            int lastDot2 = name2.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if ((lastDot1 == -1) || (lastDot2 == -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                // One of the two doesn't have a package.  Only return true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                // if the other one also doesn't have a package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                return (lastDot1 == lastDot2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                int idx1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                int idx2 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                // Skip over '['s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                if (name1.charAt(idx1) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        idx1++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    } while (name1.charAt(idx1) == '[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    if (name1.charAt(idx1) != 'L') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        // Something is terribly wrong.  Shouldn't be here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        throw new InternalError("Illegal class name " + name1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (name2.charAt(idx2) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        idx2++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    } while (name2.charAt(idx2) == '[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    if (name2.charAt(idx2) != 'L') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        // Something is terribly wrong.  Shouldn't be here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        throw new InternalError("Illegal class name " + name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                // Check that package part is identical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                int length1 = lastDot1 - idx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                int length2 = lastDot2 - idx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                if (length1 != length2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                return name1.regionMatches(false, idx1, name2, idx2, length1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   243
    static boolean isSubclassOf(Class<?> queryClass,
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   244
                                Class<?> ofClass)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        while (queryClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if (queryClass == ofClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            queryClass = queryClass.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    // fieldNames must contain only interned Strings
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   256
    public static synchronized void registerFieldsToFilter(Class<?> containingClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                              String ... fieldNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        fieldFilterMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            registerFilter(fieldFilterMap, containingClass, fieldNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    // methodNames must contain only interned Strings
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   263
    public static synchronized void registerMethodsToFilter(Class<?> containingClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                              String ... methodNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        methodFilterMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            registerFilter(methodFilterMap, containingClass, methodNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   269
    private static Map<Class<?>,String[]> registerFilter(Map<Class<?>,String[]> map,
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   270
            Class<?> containingClass, String ... names) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (map.get(containingClass) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                            ("Filter already registered: " + containingClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   275
        map = new HashMap<Class<?>,String[]>(map);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        map.put(containingClass, names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   280
    public static Field[] filterFields(Class<?> containingClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                       Field[] fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (fieldFilterMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // Bootstrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return (Field[])filter(fields, fieldFilterMap.get(containingClass));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   289
    public static Method[] filterMethods(Class<?> containingClass, Method[] methods) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (methodFilterMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // Bootstrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return (Method[])filter(methods, methodFilterMap.get(containingClass));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private static Member[] filter(Member[] members, String[] filteredNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if ((filteredNames == null) || (members.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return members;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        int numNewMembers = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        for (Member member : members) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            boolean shouldSkip = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            for (String filteredName : filteredNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                if (member.getName() == filteredName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    shouldSkip = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (!shouldSkip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                ++numNewMembers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        Member[] newMembers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            (Member[])Array.newInstance(members[0].getClass(), numNewMembers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        int destIdx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        for (Member member : members) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            boolean shouldSkip = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            for (String filteredName : filteredNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                if (member.getName() == filteredName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    shouldSkip = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            if (!shouldSkip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                newMembers[destIdx++] = member;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return newMembers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   331
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   332
    /**
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   333
     * Tests if the given method is caller-sensitive and the declaring class
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   334
     * is defined by either the bootstrap class loader or extension class loader.
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   335
     */
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   336
    public static boolean isCallerSensitive(Method m) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   337
        final ClassLoader loader = m.getDeclaringClass().getClassLoader();
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   338
        if (sun.misc.VM.isSystemDomainLoader(loader) || isExtClassLoader(loader))  {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   339
            return m.isAnnotationPresent(CallerSensitive.class);
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   340
        }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   341
        return false;
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   342
    }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   343
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   344
    private static boolean isExtClassLoader(ClassLoader loader) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   345
        ClassLoader cl = ClassLoader.getSystemClassLoader();
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   346
        while (cl != null) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   347
            if (cl.getParent() == null && cl == loader) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   348
                return true;
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   349
            }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   350
            cl = cl.getParent();
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   351
        }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   352
        return false;
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   353
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
}