jdk/src/java.base/share/classes/java/lang/Class.java
author jfranck
Wed, 25 Feb 2015 14:30:02 +0100
changeset 29113 4c3e0acf325e
parent 28519 299787d4cf96
child 30341 a026e34714ed
child 29986 97167d851fc4
permissions -rw-r--r--
8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod() Reviewed-by: ahgross, mchung, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25392
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 24867
diff changeset
     2
 * Copyright (c) 1994, 2014, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
16051
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
    28
import java.lang.reflect.AnnotatedElement;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.GenericArrayType;
17710
ce8517f5a2fe 8014836: Have GenericDeclaration extend AnnotatedElement
darcy
parents: 16906
diff changeset
    31
import java.lang.reflect.GenericDeclaration;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Member;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Field;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
    34
import java.lang.reflect.Executable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.reflect.Type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.TypeVariable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.reflect.InvocationTargetException;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
    41
import java.lang.reflect.AnnotatedType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.HashSet;
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
    51
import java.util.LinkedHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.HashMap;
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
    56
import java.util.Objects;
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25991
diff changeset
    57
import java.util.StringJoiner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.misc.Unsafe;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
    59
import sun.reflect.CallerSensitive;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.reflect.ConstantPool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import sun.reflect.Reflection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import sun.reflect.ReflectionFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import sun.reflect.generics.factory.CoreReflectionFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import sun.reflect.generics.factory.GenericsFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import sun.reflect.generics.repository.ClassRepository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import sun.reflect.generics.repository.MethodRepository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import sun.reflect.generics.repository.ConstructorRepository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import sun.reflect.generics.scope.ClassScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import java.lang.annotation.Annotation;
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 14014
diff changeset
    71
import java.lang.reflect.Proxy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import sun.reflect.annotation.*;
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 14014
diff changeset
    73
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * Instances of the class {@code Class} represent classes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * interfaces in a running Java application.  An enum is a kind of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * class and an annotation is a kind of interface.  Every array also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * belongs to a class that is reflected as a {@code Class} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * that is shared by all arrays with the same element type and number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * of dimensions.  The primitive Java types ({@code boolean},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * {@code byte}, {@code char}, {@code short},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * {@code int}, {@code long}, {@code float}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * {@code double}), and the keyword {@code void} are also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * represented as {@code Class} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p> {@code Class} has no public constructor. Instead {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * objects are constructed automatically by the Java Virtual Machine as classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * are loaded and by calls to the {@code defineClass} method in the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <p> The following example uses a {@code Class} object to print the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * class name of an object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 21316
diff changeset
    95
 * <blockquote><pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *     void printClassName(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *         System.out.println("The class of " + obj +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *                            " is " + obj.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p> It is also possible to get the {@code Class} object for a named
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7803
diff changeset
   103
 * type (or for void) using a class literal.  See Section 15.8.2 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7803
diff changeset
   104
 * <cite>The Java&trade; Language Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 21316
diff changeset
   107
 * <blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * @param <T> the type of the class modeled by this {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * object.  For example, the type of {@code String.class} is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * Class<String>}.  Use {@code Class<?>} if the class being modeled is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   118
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 */
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   120
public final class Class<T> implements java.io.Serializable,
17710
ce8517f5a2fe 8014836: Have GenericDeclaration extend AnnotatedElement
darcy
parents: 16906
diff changeset
   121
                              GenericDeclaration,
ce8517f5a2fe 8014836: Have GenericDeclaration extend AnnotatedElement
darcy
parents: 16906
diff changeset
   122
                              Type,
ce8517f5a2fe 8014836: Have GenericDeclaration extend AnnotatedElement
darcy
parents: 16906
diff changeset
   123
                              AnnotatedElement {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static final int ANNOTATION= 0x00002000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static final int ENUM      = 0x00004000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static final int SYNTHETIC = 0x00001000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static native void registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /*
25392
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 24867
diff changeset
   134
     * Private constructor. Only the Java Virtual Machine creates Class objects.
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 24867
diff changeset
   135
     * This constructor is not used and prevents the default constructor being
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 24867
diff changeset
   136
     * generated.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
25517
f3c83ab83a16 8047737: Move array component mirror to instance of java/lang/Class
coleenp
parents: 25392
diff changeset
   138
    private Class(ClassLoader loader, Class<?> arrayComponentType) {
25392
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 24867
diff changeset
   139
        // Initialize final field for classLoader.  The initialization value of non-null
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 24867
diff changeset
   140
        // prevents future JIT optimizations from assuming this final field is null.
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 24867
diff changeset
   141
        classLoader = loader;
25517
f3c83ab83a16 8047737: Move array component mirror to instance of java/lang/Class
coleenp
parents: 25392
diff changeset
   142
        componentType = arrayComponentType;
25392
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 24867
diff changeset
   143
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Converts the object to a string. The string representation is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * string "class" or "interface", followed by a space, and then by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * fully qualified name of the class in the format returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * {@code getName}.  If this {@code Class} object represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * primitive type, this method returns the name of the primitive type.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * this {@code Class} object represents void this method returns
28519
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   152
     * "void". If this {@code Class} object represents an array type,
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   153
     * this method returns "class " followed by {@code getName}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return a string representation of this class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            + getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   162
    /**
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   163
     * Returns a string describing this {@code Class}, including
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   164
     * information about modifiers and type parameters.
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   165
     *
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   166
     * The string is formatted as a list of type modifiers, if any,
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   167
     * followed by the kind of type (empty string for primitive types
19031
6c9afe6dad33 8020810: Typo in javadoc for Class.toGenericString()
darcy
parents: 18827
diff changeset
   168
     * and {@code class}, {@code enum}, {@code interface}, or
6c9afe6dad33 8020810: Typo in javadoc for Class.toGenericString()
darcy
parents: 18827
diff changeset
   169
     * <code>&#64;</code>{@code interface}, as appropriate), followed
6c9afe6dad33 8020810: Typo in javadoc for Class.toGenericString()
darcy
parents: 18827
diff changeset
   170
     * by the type's name, followed by an angle-bracketed
6c9afe6dad33 8020810: Typo in javadoc for Class.toGenericString()
darcy
parents: 18827
diff changeset
   171
     * comma-separated list of the type's type parameters, if any.
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   172
     *
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   173
     * A space is used to separate modifiers from one another and to
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   174
     * separate any modifiers from the kind of type. The modifiers
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   175
     * occur in canonical order. If there are no type parameters, the
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   176
     * type parameter list is elided.
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   177
     *
28519
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   178
     * For an array type, the string starts with the type name,
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   179
     * followed by an angle-bracketed comma-separated list of the
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   180
     * type's type parameters, if any, followed by a sequence of
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   181
     * {@code []} characters, one set of brackets per dimension of
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   182
     * the array.
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   183
     *
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   184
     * <p>Note that since information about the runtime representation
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   185
     * of a type is being generated, modifiers not present on the
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   186
     * originating source code or illegal on the originating source
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   187
     * code may be present.
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   188
     *
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   189
     * @return a string describing this {@code Class}, including
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   190
     * information about modifiers and type parameters
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   191
     *
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   192
     * @since 1.8
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   193
     */
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   194
    public String toGenericString() {
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   195
        if (isPrimitive()) {
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   196
            return toString();
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   197
        } else {
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   198
            StringBuilder sb = new StringBuilder();
28519
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   199
            Class<?> component = this;
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   200
            int arrayDepth = 0;
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   201
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   202
            if (isArray()) {
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   203
                do {
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   204
                    arrayDepth++;
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   205
                    component = component.getComponentType();
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   206
                } while (component.isArray());
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   207
                sb.append(component.getName());
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   208
            } else {
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   209
                // Class modifiers are a superset of interface modifiers
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   210
                int modifiers = getModifiers() & Modifier.classModifiers();
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   211
                if (modifiers != 0) {
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   212
                    sb.append(Modifier.toString(modifiers));
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   213
                    sb.append(' ');
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   214
                }
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   215
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   216
                if (isAnnotation()) {
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   217
                    sb.append('@');
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   218
                }
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   219
                if (isInterface()) { // Note: all annotation types are interfaces
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   220
                    sb.append("interface");
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   221
                } else {
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   222
                    if (isEnum())
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   223
                        sb.append("enum");
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   224
                    else
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   225
                        sb.append("class");
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   226
                }
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   227
                sb.append(' ');
28519
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   228
                sb.append(getName());
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   229
            }
28519
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   230
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   231
            TypeVariable<?>[] typeparms = component.getTypeParameters();
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   232
            if (typeparms.length > 0) {
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   233
                boolean first = true;
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   234
                sb.append('<');
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   235
                for(TypeVariable<?> typeparm: typeparms) {
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   236
                    if (!first)
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   237
                        sb.append(',');
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   238
                    sb.append(typeparm.getTypeName());
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   239
                    first = false;
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   240
                }
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   241
                sb.append('>');
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   242
            }
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   243
28519
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   244
            for (int i = 0; i < arrayDepth; i++)
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   245
                sb.append("[]");
299787d4cf96 8060077: Class.toGenericString specification doesn't mention array types
darcy
parents: 28059
diff changeset
   246
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   247
            return sb.toString();
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   248
        }
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
   249
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Returns the {@code Class} object associated with the class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * interface with the given string name.  Invoking this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *  {@code Class.forName(className, true, currentLoader)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * where {@code currentLoader} denotes the defining class loader of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * the current class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <p> For example, the following code fragment returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * runtime {@code Class} descriptor for the class named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * {@code java.lang.Thread}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *   {@code Class t = Class.forName("java.lang.Thread")}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * A call to {@code forName("X")} causes the class named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * {@code X} to be initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param      className   the fully qualified name of the desired class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return     the {@code Class} object for the class with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *             specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @exception LinkageError if the linkage fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @exception ExceptionInInitializerError if the initialization provoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *            by this method fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @exception ClassNotFoundException if the class cannot be located
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
   282
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public static Class<?> forName(String className)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                throws ClassNotFoundException {
27072
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   285
        Class<?> caller = Reflection.getCallerClass();
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   286
        return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns the {@code Class} object associated with the class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * interface with the given string name, using the given class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Given the fully qualified name for a class or interface (in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * format returned by {@code getName}) this method attempts to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * locate, load, and link the class or interface.  The specified class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * loader is used to load the class or interface.  If the parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * {@code loader} is null, the class is loaded through the bootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * class loader.  The class is initialized only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * {@code initialize} parameter is {@code true} and if it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * not been initialized earlier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p> If {@code name} denotes a primitive type or void, an attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * will be made to locate a user-defined class in the unnamed package whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * name is {@code name}. Therefore, this method cannot be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * obtain any of the {@code Class} objects representing primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * types or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <p> If {@code name} denotes an array class, the component type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * the array class is loaded but not initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p> For example, in an instance method the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *  {@code Class.forName("Foo")}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *  {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Note that this method throws errors related to loading, linking or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Java Language Specification</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Note that this method does not check whether the requested class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * is accessible to its caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * <p> If the {@code loader} is {@code null}, and a security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * manager is present, and the caller's class loader is not null, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * method calls the security manager's {@code checkPermission} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * with a {@code RuntimePermission("getClassLoader")} permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * ensure it's ok to access the bootstrap class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param name       fully qualified name of the desired class
13589
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 11536
diff changeset
   336
     * @param initialize if {@code true} the class will be initialized.
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 11536
diff changeset
   337
     *                   See Section 12.4 of <em>The Java Language Specification</em>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param loader     class loader from which the class must be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @return           class object representing the desired class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @exception LinkageError if the linkage fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @exception ExceptionInInitializerError if the initialization provoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *            by this method fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @exception ClassNotFoundException if the class cannot be located by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *            the specified class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see       java.lang.Class#forName(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @see       java.lang.ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @since     1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
   351
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public static Class<?> forName(String name, boolean initialize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                   ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    {
27072
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   356
        Class<?> caller = null;
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   357
        SecurityManager sm = System.getSecurityManager();
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   358
        if (sm != null) {
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   359
            // Reflective call to get caller class is only needed if a security manager
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   360
            // is present.  Avoid the overhead of making this call otherwise.
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   361
            caller = Reflection.getCallerClass();
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   362
            if (sun.misc.VM.isSystemDomainLoader(loader)) {
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   363
                ClassLoader ccl = ClassLoader.getClassLoader(caller);
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 14014
diff changeset
   364
                if (!sun.misc.VM.isSystemDomainLoader(ccl)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    sm.checkPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        SecurityConstants.GET_CLASSLOADER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
27072
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   370
        return forName0(name, initialize, loader, caller);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
27072
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   373
    /** Called after security check for system loader access checks have been made. */
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
   374
    private static native Class<?> forName0(String name, boolean initialize,
27072
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   375
                                            ClassLoader loader,
ced3a76913f3 8015256: Better class accessibility
coleenp
parents: 26219
diff changeset
   376
                                            Class<?> caller)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        throws ClassNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Creates a new instance of the class represented by this {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * object.  The class is instantiated as if by a {@code new}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * expression with an empty argument list.  The class is initialized if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * has not already been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * <p>Note that this method propagates any exception thrown by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * nullary constructor, including a checked exception.  Use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * this method effectively bypasses the compile-time exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * checking that would otherwise be performed by the compiler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Constructor.newInstance} method avoids this problem by wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * any exception thrown by the constructor in a (checked) {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * java.lang.reflect.InvocationTargetException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   395
     * @return  a newly allocated instance of the class represented by this
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   396
     *          object.
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   397
     * @throws  IllegalAccessException  if the class or its nullary
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   398
     *          constructor is not accessible.
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   399
     * @throws  InstantiationException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   400
     *          if this {@code Class} represents an abstract class,
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   401
     *          an interface, an array class, a primitive type, or void;
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   402
     *          or if the class has no nullary constructor;
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   403
     *          or if the instantiation fails for some other reason.
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   404
     * @throws  ExceptionInInitializerError if the initialization
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   405
     *          provoked by this method fails.
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   406
     * @throws  SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   407
     *          If a security manager, <i>s</i>, is present and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   408
     *          the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   409
     *          ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   410
     *          invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   411
     *          s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
   412
     *          of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
   414
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public T newInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        throws InstantiationException, IllegalAccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (System.getSecurityManager() != null) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
   419
            checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        // NOTE: the following code may not be strictly correct under
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        // the current Java memory model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // Constructor lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (cachedConstructor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (this == Class.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                throw new IllegalAccessException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    "Can not call newInstance() on the Class for java.lang.Class"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            try {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
   433
                Class<?>[] empty = {};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                final Constructor<T> c = getConstructor0(empty, Member.DECLARED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                // Disable accessibility checks on the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                // since we have to do the security check here anyway
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                // (the stack depth is wrong for the Constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                // security check to work)
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   439
                java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   440
                    new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   441
                        public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                c.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                cachedConstructor = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            } catch (NoSuchMethodException e) {
10147
cd5c99f94305 6380161: (reflect) Exception from newInstance() not chained to cause.
darcy
parents: 10142
diff changeset
   448
                throw (InstantiationException)
cd5c99f94305 6380161: (reflect) Exception from newInstance() not chained to cause.
darcy
parents: 10142
diff changeset
   449
                    new InstantiationException(getName()).initCause(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        Constructor<T> tmpConstructor = cachedConstructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // Security check (same as in java.lang.reflect.Constructor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        int modifiers = tmpConstructor.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (!Reflection.quickCheckMemberAccess(this, modifiers)) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
   456
            Class<?> caller = Reflection.getCallerClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if (newInstanceCallerCache != caller) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                Reflection.ensureMemberAccess(caller, this, null, modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                newInstanceCallerCache = caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // Run constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            return tmpConstructor.newInstance((Object[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            Unsafe.getUnsafe().throwException(e.getTargetException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            // Not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    private volatile transient Constructor<T> cachedConstructor;
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
   472
    private volatile transient Class<?>       newInstanceCallerCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Determines if the specified {@code Object} is assignment-compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * with the object represented by this {@code Class}.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * the dynamic equivalent of the Java language {@code instanceof}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * operator. The method returns {@code true} if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * {@code Object} argument is non-null and can be cast to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * reference type represented by this {@code Class} object without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * raising a {@code ClassCastException.} It returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <p> Specifically, if this {@code Class} object represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * declared class, this method returns {@code true} if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * {@code Object} argument is an instance of the represented class (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * of any of its subclasses); it returns {@code false} otherwise. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * this {@code Class} object represents an array class, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * returns {@code true} if the specified {@code Object} argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * can be converted to an object of the array class by an identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * conversion or by a widening reference conversion; it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * {@code false} otherwise. If this {@code Class} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * represents an interface, this method returns {@code true} if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * class or any superclass of the specified {@code Object} argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * implements this interface; it returns {@code false} otherwise. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * this {@code Class} object represents a primitive type, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * returns {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @param   obj the object to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @return  true if {@code obj} is an instance of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   503
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public native boolean isInstance(Object obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Determines if the class or interface represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * {@code Class} object is either the same as, or is a superclass or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * superinterface of, the class or interface represented by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * {@code Class} parameter. It returns {@code true} if so;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * otherwise it returns {@code false}. If this {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * object represents a primitive type, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * {@code true} if the specified {@code Class} parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * exactly this {@code Class} object; otherwise it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * <p> Specifically, this method tests whether the type represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * specified {@code Class} parameter can be converted to the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * represented by this {@code Class} object via an identity conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * or via a widening reference conversion. See <em>The Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @param cls the {@code Class} object to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @return the {@code boolean} value indicating whether objects of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * type {@code cls} can be assigned to objects of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @exception NullPointerException if the specified Class parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *            null.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   530
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public native boolean isAssignableFrom(Class<?> cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Determines if the specified {@code Class} object represents an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * interface type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @return  {@code true} if this object represents an interface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *          {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public native boolean isInterface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * Determines if this {@code Class} object represents an array class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @return  {@code true} if this object represents an array class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *          {@code false} otherwise.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   550
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    public native boolean isArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Determines if the specified {@code Class} object represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * <p> There are nine predefined {@code Class} objects to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * the eight primitive types and void.  These are created by the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Virtual Machine, and have the same names as the primitive types that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * they represent, namely {@code boolean}, {@code byte},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * {@code char}, {@code short}, {@code int},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * {@code long}, {@code float}, and {@code double}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * <p> These objects may only be accessed via the following public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * final variables, and are the only {@code Class} objects for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * this method returns {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @return true if and only if this class represents a primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @see     java.lang.Boolean#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @see     java.lang.Character#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @see     java.lang.Byte#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @see     java.lang.Short#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @see     java.lang.Integer#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @see     java.lang.Long#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @see     java.lang.Float#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @see     java.lang.Double#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @see     java.lang.Void#TYPE
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   581
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public native boolean isPrimitive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * Returns true if this {@code Class} object represents an annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * type.  Note that if this method returns true, {@link #isInterface()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * would also return true, as all annotation types are also interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @return {@code true} if this class object represents an annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *      type; {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    public boolean isAnnotation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        return (getModifiers() & ANNOTATION) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * Returns {@code true} if this class is a synthetic class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * returns {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @return {@code true} if and only if this class is a synthetic class as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *         defined by the Java Language Specification.
14910
337380568515 8005097: Tie isSynthetic javadoc to the JLS
darcy
parents: 14676
diff changeset
   603
     * @jls 13.1 The Form of a Binary
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    public boolean isSynthetic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        return (getModifiers() & SYNTHETIC) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Returns the  name of the entity (class, interface, array class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * primitive type, or void) represented by this {@code Class} object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * as a {@code String}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * <p> If this class object represents a reference type that is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * array type then the binary name of the class is returned, as specified
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7803
diff changeset
   617
     * by
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7803
diff changeset
   618
     * <cite>The Java&trade; Language Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * <p> If this class object represents a primitive type or void, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * name returned is a {@code String} equal to the Java language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * keyword corresponding to the primitive type or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <p> If this class object represents a class of arrays, then the internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * form of the name consists of the name of the element type preceded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * one or more '{@code [}' characters representing the depth of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * nesting.  The encoding of element type names is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <blockquote><table summary="Element types and encodings">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <tr><th> Element Type <th> &nbsp;&nbsp;&nbsp; <th> Encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * <tr><td> boolean      <td> &nbsp;&nbsp;&nbsp; <td align=center> Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * <tr><td> byte         <td> &nbsp;&nbsp;&nbsp; <td align=center> B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * <tr><td> char         <td> &nbsp;&nbsp;&nbsp; <td align=center> C
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * <tr><td> class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *                       <td> &nbsp;&nbsp;&nbsp; <td align=center> L<i>classname</i>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <tr><td> double       <td> &nbsp;&nbsp;&nbsp; <td align=center> D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * <tr><td> float        <td> &nbsp;&nbsp;&nbsp; <td align=center> F
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * <tr><td> int          <td> &nbsp;&nbsp;&nbsp; <td align=center> I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * <tr><td> long         <td> &nbsp;&nbsp;&nbsp; <td align=center> J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * <tr><td> short        <td> &nbsp;&nbsp;&nbsp; <td align=center> S
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * <p> The class or interface name <i>classname</i> is the binary name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * the class specified above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * <p> Examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * String.class.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *     returns "java.lang.String"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * byte.class.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *     returns "byte"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * (new Object[3]).getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *     returns "[Ljava.lang.Object;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * (new int[3][4][5][6][7][8][9]).getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *     returns "[[[[[[[I"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @return  the name of the class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *          represented by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public String getName() {
3845
55b55f4c43d0 6881442: (reflect) Race condition in Class.getName()
martin
parents: 3219
diff changeset
   662
        String name = this.name;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (name == null)
3845
55b55f4c43d0 6881442: (reflect) Race condition in Class.getName()
martin
parents: 3219
diff changeset
   664
            this.name = name = getName0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    // cache the name to reduce the number of calls into the VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    private transient String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    private native String getName0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Returns the class loader for the class.  Some implementations may use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * null to represent the bootstrap class loader. This method will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * null in such implementations if this class was loaded by the bootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * <p> If a security manager is present, and the caller's class loader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * not null and the caller's class loader is not the same as or an ancestor of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * the class loader for the class whose class loader is requested, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * this method calls the security manager's {@code checkPermission}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * method with a {@code RuntimePermission("getClassLoader")}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * permission to ensure it's ok to access the class loader for the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * <p>If this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * represents a primitive type or void, null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @return  the class loader that loaded the class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *          represented by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *    if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *    {@code checkPermission} method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *    access to the class loader for the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @see java.lang.ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @see java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
   698
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public ClassLoader getClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        ClassLoader cl = getClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (cl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        if (sm != null) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
   705
            ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        return cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    // Package-private to allow ClassLoader access
25392
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 24867
diff changeset
   711
    ClassLoader getClassLoader0() { return classLoader; }
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 24867
diff changeset
   712
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 24867
diff changeset
   713
    // Initialized in JVM not by private constructor
27783
6317ec69db47 8065552: setAccessible(true) on fields of Class may throw a SecurityException
dfuchs
parents: 27730
diff changeset
   714
    // This field is filtered from reflection access, i.e. getDeclaredField
6317ec69db47 8065552: setAccessible(true) on fields of Class may throw a SecurityException
dfuchs
parents: 27730
diff changeset
   715
    // will throw NoSuchFieldException
25392
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 24867
diff changeset
   716
    private final ClassLoader classLoader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Returns an array of {@code TypeVariable} objects that represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * type variables declared by the generic declaration represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * {@code GenericDeclaration} object, in declaration order.  Returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * array of length 0 if the underlying generic declaration declares no type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @return an array of {@code TypeVariable} objects that represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *     the type variables declared by this generic declaration
3219
a348b237cbf8 6857803: Missing links to exceptions in javadoc for Class.getGeneric{Superclass, Interfaces}
darcy
parents: 2448
diff changeset
   727
     * @throws java.lang.reflect.GenericSignatureFormatError if the generic
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *     signature of this generic declaration does not conform to
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7803
diff changeset
   729
     *     the format specified in
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7803
diff changeset
   730
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
   733
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    public TypeVariable<Class<T>>[] getTypeParameters() {
18179
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   735
        ClassRepository info = getGenericInfo();
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   736
        if (info != null)
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   737
            return (TypeVariable<Class<T>>[])info.getTypeParameters();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        else
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
   739
            return (TypeVariable<Class<T>>[])new TypeVariable<?>[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    /**
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   744
     * Returns the {@code Class} representing the direct superclass of the
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   745
     * entity (class, interface, primitive type or void) represented by
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   746
     * this {@code Class}.  If this {@code Class} represents either the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * {@code Object} class, an interface, a primitive type, or void, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * null is returned.  If this object represents an array class then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * {@code Class} object representing the {@code Object} class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   752
     * @return the direct superclass of the class represented by this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    public native Class<? super T> getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * Returns the {@code Type} representing the direct superclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * the entity (class, interface, primitive type or void) represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * this {@code Class}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * <p>If the superclass is a parameterized type, the {@code Type}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * object returned must accurately reflect the actual type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * parameters used in the source code. The parameterized type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * representing the superclass is created if it had not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * created before. See the declaration of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * java.lang.reflect.ParameterizedType ParameterizedType} for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * semantics of the creation process for parameterized types.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * this {@code Class} represents either the {@code Object}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * class, an interface, a primitive type, or void, then null is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * returned.  If this object represents an array class then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * {@code Class} object representing the {@code Object} class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
3219
a348b237cbf8 6857803: Missing links to exceptions in javadoc for Class.getGeneric{Superclass, Interfaces}
darcy
parents: 2448
diff changeset
   775
     * @throws java.lang.reflect.GenericSignatureFormatError if the generic
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7803
diff changeset
   776
     *     class signature does not conform to the format specified in
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7803
diff changeset
   777
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @throws TypeNotPresentException if the generic superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *     refers to a non-existent type declaration
3219
a348b237cbf8 6857803: Missing links to exceptions in javadoc for Class.getGeneric{Superclass, Interfaces}
darcy
parents: 2448
diff changeset
   780
     * @throws java.lang.reflect.MalformedParameterizedTypeException if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *     generic superclass refers to a parameterized type that cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *     instantiated  for any reason
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   783
     * @return the direct superclass of the class represented by this object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    public Type getGenericSuperclass() {
18179
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   787
        ClassRepository info = getGenericInfo();
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   788
        if (info == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            return getSuperclass();
18179
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   790
        }
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   791
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   792
        // Historical irregularity:
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   793
        // Generic signature marks interfaces with superclass = Object
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   794
        // but this API returns null for interfaces
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   795
        if (isInterface()) {
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   796
            return null;
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   797
        }
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   798
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   799
        return info.getSuperclass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * Gets the package for this class.  The class loader of this class is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * to find the package.  If the class was loaded by the bootstrap class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * loader the set of packages loaded from CLASSPATH is searched to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * package of the class. Null is returned if no package object was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * by the class loader of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * <p> Packages have attributes for versions and specifications only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * information was defined in the manifests that accompany the classes, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * if the class loader created the package instance with the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * from the manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * @return the package of the class, or null if no package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *         information is available from the archive or codebase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    public Package getPackage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        return Package.getPackage(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    /**
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   823
     * Returns the interfaces directly implemented by the class or interface
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * represented by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   826
     * <p>If this object represents a class, the return value is an array
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   827
     * containing objects representing all interfaces directly implemented by
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   828
     * the class.  The order of the interface objects in the array corresponds
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   829
     * to the order of the interface names in the {@code implements} clause of
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   830
     * the declaration of the class represented by this object.  For example,
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   831
     * given the declaration:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * suppose the value of {@code s} is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * {@code Shimmer}; the value of the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * {@code s.getClass().getInterfaces()[0]}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * is the {@code Class} object that represents interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * {@code FloorWax}; and the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * {@code s.getClass().getInterfaces()[1]}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * is the {@code Class} object that represents interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * {@code DessertTopping}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   848
     * <p>If this object represents an interface, the array contains objects
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   849
     * representing all interfaces directly extended by the interface.  The
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   850
     * order of the interface objects in the array corresponds to the order of
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   851
     * the interface names in the {@code extends} clause of the declaration of
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   852
     * the interface represented by this object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   854
     * <p>If this object represents a class or interface that implements no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * interfaces, the method returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   857
     * <p>If this object represents a primitive type or void, the method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   860
     * <p>If this {@code Class} object represents an array type, the
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
   861
     * interfaces {@code Cloneable} and {@code java.io.Serializable} are
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
   862
     * returned in that order.
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
   863
     *
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   864
     * @return an array of interfaces directly implemented by this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
18179
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   866
    public Class<?>[] getInterfaces() {
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   867
        ReflectionData<T> rd = reflectionData();
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   868
        if (rd == null) {
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   869
            // no cloning required
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   870
            return getInterfaces0();
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   871
        } else {
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   872
            Class<?>[] interfaces = rd.interfaces;
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   873
            if (interfaces == null) {
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   874
                interfaces = getInterfaces0();
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   875
                rd.interfaces = interfaces;
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   876
            }
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   877
            // defensively copy before handing over to user code
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   878
            return interfaces.clone();
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   879
        }
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   880
    }
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   881
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   882
    private native Class<?>[] getInterfaces0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * Returns the {@code Type}s representing the interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * directly implemented by the class or interface represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * <p>If a superinterface is a parameterized type, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * {@code Type} object returned for it must accurately reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * the actual type parameters used in the source code. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * parameterized type representing each superinterface is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * if it had not been created before. See the declaration of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * {@link java.lang.reflect.ParameterizedType ParameterizedType}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * for the semantics of the creation process for parameterized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   898
     * <p>If this object represents a class, the return value is an array
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   899
     * containing objects representing all interfaces directly implemented by
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   900
     * the class.  The order of the interface objects in the array corresponds
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   901
     * to the order of the interface names in the {@code implements} clause of
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   902
     * the declaration of the class represented by this object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   904
     * <p>If this object represents an interface, the array contains objects
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   905
     * representing all interfaces directly extended by the interface.  The
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   906
     * order of the interface objects in the array corresponds to the order of
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   907
     * the interface names in the {@code extends} clause of the declaration of
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   908
     * the interface represented by this object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   910
     * <p>If this object represents a class or interface that implements no
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   911
     * interfaces, the method returns an array of length 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   913
     * <p>If this object represents a primitive type or void, the method
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   914
     * returns an array of length 0.
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   915
     *
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   916
     * <p>If this {@code Class} object represents an array type, the
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   917
     * interfaces {@code Cloneable} and {@code java.io.Serializable} are
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   918
     * returned in that order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *
3219
a348b237cbf8 6857803: Missing links to exceptions in javadoc for Class.getGeneric{Superclass, Interfaces}
darcy
parents: 2448
diff changeset
   920
     * @throws java.lang.reflect.GenericSignatureFormatError
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *     if the generic class signature does not conform to the format
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7803
diff changeset
   922
     *     specified in
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7803
diff changeset
   923
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * @throws TypeNotPresentException if any of the generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *     superinterfaces refers to a non-existent type declaration
3219
a348b237cbf8 6857803: Missing links to exceptions in javadoc for Class.getGeneric{Superclass, Interfaces}
darcy
parents: 2448
diff changeset
   926
     * @throws java.lang.reflect.MalformedParameterizedTypeException
a348b237cbf8 6857803: Missing links to exceptions in javadoc for Class.getGeneric{Superclass, Interfaces}
darcy
parents: 2448
diff changeset
   927
     *     if any of the generic superinterfaces refer to a parameterized
a348b237cbf8 6857803: Missing links to exceptions in javadoc for Class.getGeneric{Superclass, Interfaces}
darcy
parents: 2448
diff changeset
   928
     *     type that cannot be instantiated for any reason
27730
e739f0725baa 8062773: Clarifications for Class specification
martin
parents: 27719
diff changeset
   929
     * @return an array of interfaces directly implemented by this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    public Type[] getGenericInterfaces() {
18179
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   933
        ClassRepository info = getGenericInfo();
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
   934
        return (info == null) ?  getInterfaces() : info.getSuperInterfaces();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Returns the {@code Class} representing the component type of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * array.  If this class does not represent an array class this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * @return the {@code Class} representing the component type of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * class if this class is an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @see     java.lang.reflect.Array
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   946
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     */
25517
f3c83ab83a16 8047737: Move array component mirror to instance of java/lang/Class
coleenp
parents: 25392
diff changeset
   948
    public Class<?> getComponentType() {
f3c83ab83a16 8047737: Move array component mirror to instance of java/lang/Class
coleenp
parents: 25392
diff changeset
   949
        // Only return for array types. Storage may be reused for Class for instance types.
f3c83ab83a16 8047737: Move array component mirror to instance of java/lang/Class
coleenp
parents: 25392
diff changeset
   950
        if (isArray()) {
f3c83ab83a16 8047737: Move array component mirror to instance of java/lang/Class
coleenp
parents: 25392
diff changeset
   951
            return componentType;
f3c83ab83a16 8047737: Move array component mirror to instance of java/lang/Class
coleenp
parents: 25392
diff changeset
   952
        } else {
f3c83ab83a16 8047737: Move array component mirror to instance of java/lang/Class
coleenp
parents: 25392
diff changeset
   953
            return null;
f3c83ab83a16 8047737: Move array component mirror to instance of java/lang/Class
coleenp
parents: 25392
diff changeset
   954
        }
f3c83ab83a16 8047737: Move array component mirror to instance of java/lang/Class
coleenp
parents: 25392
diff changeset
   955
    }
f3c83ab83a16 8047737: Move array component mirror to instance of java/lang/Class
coleenp
parents: 25392
diff changeset
   956
f3c83ab83a16 8047737: Move array component mirror to instance of java/lang/Class
coleenp
parents: 25392
diff changeset
   957
    private final Class<?> componentType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Returns the Java language modifiers for this class or interface, encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * in an integer. The modifiers consist of the Java Virtual Machine's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * constants for {@code public}, {@code protected},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * {@code private}, {@code final}, {@code static},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * {@code abstract} and {@code interface}; they should be decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * using the methods of class {@code Modifier}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * <p> If the underlying class is an array class, then its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * {@code public}, {@code private} and {@code protected}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * modifiers are the same as those of its component type.  If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * {@code Class} represents a primitive type or void, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * {@code public} modifier is always {@code true}, and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * {@code protected} and {@code private} modifiers are always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * {@code false}. If this object represents an array class, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * primitive type or void, then its {@code final} modifier is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * {@code true} and its interface modifier is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * {@code false}. The values of its other modifiers are not determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * by this specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * <p> The modifier encodings are defined in <em>The Java Virtual Machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * Specification</em>, table 4.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @return the {@code int} representing the modifiers for this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * @see     java.lang.reflect.Modifier
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   985
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    public native int getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * Gets the signers of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @return  the signers of this class, or null if there are no signers.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *          particular, this method returns null if this object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *          a primitive type or void.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   996
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    public native Object[] getSigners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * Set the signers of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    native void setSigners(Object[] signers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * If this {@code Class} object represents a local or anonymous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * class within a method, returns a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * java.lang.reflect.Method Method} object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * immediately enclosing method of the underlying class. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * {@code null} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * In particular, this method returns {@code null} if the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * class is a local or anonymous class immediately enclosed by a type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * declaration, instance initializer or static initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @return the immediately enclosing method of the underlying class, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *     that class is a local or anonymous class; otherwise {@code null}.
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1020
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1021
     * @throws SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1022
     *         If a security manager, <i>s</i>, is present and any of the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1023
     *         following conditions is met:
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1024
     *
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1025
     *         <ul>
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1026
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1027
     *         <li> the caller's class loader is not the same as the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1028
     *         class loader of the enclosing class and invocation of
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1029
     *         {@link SecurityManager#checkPermission
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1030
     *         s.checkPermission} method with
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1031
     *         {@code RuntimePermission("accessDeclaredMembers")}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1032
     *         denies access to the methods within the enclosing class
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1033
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1034
     *         <li> the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1035
     *         ancestor of the class loader for the enclosing class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1036
     *         invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1037
     *         s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1038
     *         of the enclosing class
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1039
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1040
     *         </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     */
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1043
    @CallerSensitive
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1044
    public Method getEnclosingMethod() throws SecurityException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        if (enclosingInfo == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            if (!enclosingInfo.isMethod())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                                                              getFactory());
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  1055
            Class<?>   returnType       = toClass(typeInfo.getReturnType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            Type []    parameterTypes   = typeInfo.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            // Convert Types to Classes; returned types *should*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            // be class objects since the methodDescriptor's used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            // don't have generics information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            for(int i = 0; i < parameterClasses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                parameterClasses[i] = toClass(parameterTypes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1065
            // Perform access check
29113
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1066
            final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1067
            enclosingCandidate.checkMemberAccess(Member.DECLARED,
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1068
                                                 Reflection.getCallerClass(), true);
29113
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1069
            // Client is ok to access declared methods but j.l.Class might not be.
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1070
            Method[] candidates = AccessController.doPrivileged(
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1071
                    new PrivilegedAction<Method[]>() {
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1072
                        @Override
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1073
                        public Method[] run() {
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1074
                            return enclosingCandidate.getDeclaredMethods();
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1075
                        }
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1076
                    });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
             * Loop over all declared methods; match method name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
             * number of and type of parameters, *and* return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
             * type.  Matching return type is also necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
             * because of covariant returns, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
             */
29113
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1083
            for(Method m: candidates) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                if (m.getName().equals(enclosingInfo.getName()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                    Class<?>[] candidateParamClasses = m.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    if (candidateParamClasses.length == parameterClasses.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                        boolean matches = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                        for(int i = 0; i < candidateParamClasses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                            if (!candidateParamClasses[i].equals(parameterClasses[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                                matches = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                        if (matches) { // finally, check return type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                            if (m.getReturnType().equals(returnType) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                                return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            throw new InternalError("Enclosing method not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    private native Object[] getEnclosingMethod0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    private EnclosingMethodInfo getEnclosingMethodInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        Object[] enclosingInfo = getEnclosingMethod0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        if (enclosingInfo == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            return new EnclosingMethodInfo(enclosingInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    private final static class EnclosingMethodInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        private Class<?> enclosingClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        private String descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        private EnclosingMethodInfo(Object[] enclosingInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            if (enclosingInfo.length != 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                throw new InternalError("Malformed enclosing method information");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                // The array is expected to have three elements:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                // the immediately enclosing class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                enclosingClass = (Class<?>) enclosingInfo[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                assert(enclosingClass != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                // the immediately enclosing method or constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                // name (can be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                name            = (String)   enclosingInfo[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                // the immediately enclosing method or constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                // descriptor (null iff name is).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                descriptor      = (String)   enclosingInfo[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                assert((name != null && descriptor != null) || name == descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            } catch (ClassCastException cce) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10147
diff changeset
  1142
                throw new InternalError("Invalid type in enclosing method information", cce);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        boolean isPartial() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            return enclosingClass == null || name == null || descriptor == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        boolean isConstructor() { return !isPartial() && "<init>".equals(name); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        boolean isMethod() { return !isPartial() && !isConstructor() && !"<clinit>".equals(name); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        Class<?> getEnclosingClass() { return enclosingClass; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        String getName() { return name; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        String getDescriptor() { return descriptor; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  1162
    private static Class<?> toClass(Type o) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        if (o instanceof GenericArrayType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                                     0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                .getClass();
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  1167
        return (Class<?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * If this {@code Class} object represents a local or anonymous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * class within a constructor, returns a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * java.lang.reflect.Constructor Constructor} object representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * the immediately enclosing constructor of the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * class. Returns {@code null} otherwise.  In particular, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * method returns {@code null} if the underlying class is a local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * or anonymous class immediately enclosed by a type declaration,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * instance initializer or static initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * @return the immediately enclosing constructor of the underlying class, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     *     that class is a local or anonymous class; otherwise {@code null}.
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1182
     * @throws SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1183
     *         If a security manager, <i>s</i>, is present and any of the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1184
     *         following conditions is met:
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1185
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1186
     *         <ul>
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1187
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1188
     *         <li> the caller's class loader is not the same as the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1189
     *         class loader of the enclosing class and invocation of
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1190
     *         {@link SecurityManager#checkPermission
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1191
     *         s.checkPermission} method with
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1192
     *         {@code RuntimePermission("accessDeclaredMembers")}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1193
     *         denies access to the constructors within the enclosing class
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1194
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1195
     *         <li> the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1196
     *         ancestor of the class loader for the enclosing class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1197
     *         invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1198
     *         s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1199
     *         of the enclosing class
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1200
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1201
     *         </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     */
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1204
    @CallerSensitive
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1205
    public Constructor<?> getEnclosingConstructor() throws SecurityException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        if (enclosingInfo == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            if (!enclosingInfo.isConstructor())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                                                                        getFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            Type []    parameterTypes   = typeInfo.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            // Convert Types to Classes; returned types *should*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            // be class objects since the methodDescriptor's used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            // don't have generics information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            for(int i = 0; i < parameterClasses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                parameterClasses[i] = toClass(parameterTypes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1225
            // Perform access check
29113
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1226
            final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1227
            enclosingCandidate.checkMemberAccess(Member.DECLARED,
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1228
                                                 Reflection.getCallerClass(), true);
29113
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1229
            // Client is ok to access declared methods but j.l.Class might not be.
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1230
            Constructor<?>[] candidates = AccessController.doPrivileged(
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1231
                    new PrivilegedAction<Constructor<?>[]>() {
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1232
                        @Override
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1233
                        public Constructor<?>[] run() {
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1234
                            return enclosingCandidate.getDeclaredConstructors();
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1235
                        }
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1236
                    });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
             * Loop over all declared constructors; match number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
             * of and type of parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
             */
29113
4c3e0acf325e 8014678: Spurious AccessControlException thrown in java.lang.Class.getEnclosingMethod()
jfranck
parents: 28519
diff changeset
  1241
            for(Constructor<?> c: candidates) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                Class<?>[] candidateParamClasses = c.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                if (candidateParamClasses.length == parameterClasses.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                    boolean matches = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                    for(int i = 0; i < candidateParamClasses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                        if (!candidateParamClasses[i].equals(parameterClasses[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                            matches = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                    if (matches)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            throw new InternalError("Enclosing constructor not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * If the class or interface represented by this {@code Class} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * is a member of another class, returns the {@code Class} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * representing the class in which it was declared.  This method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * null if this class or interface is not a member of any other class.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * this {@code Class} object represents an array class, a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * type, or void,then this method returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * @return the declaring class for this class
20816
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1271
     * @throws SecurityException
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1272
     *         If a security manager, <i>s</i>, is present and the caller's
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1273
     *         class loader is not the same as or an ancestor of the class
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1274
     *         loader for the declaring class and invocation of {@link
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1275
     *         SecurityManager#checkPackageAccess s.checkPackageAccess()}
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1276
     *         denies access to the package of the declaring class
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1277
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     */
20816
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1279
    @CallerSensitive
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1280
    public Class<?> getDeclaringClass() throws SecurityException {
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1281
        final Class<?> candidate = getDeclaringClass0();
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1282
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1283
        if (candidate != null)
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1284
            candidate.checkPackageAccess(
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1285
                    ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1286
        return candidate;
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1287
    }
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1288
b44a353664b8 8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
jfranck
parents: 18546
diff changeset
  1289
    private native Class<?> getDeclaringClass0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * Returns the immediately enclosing class of the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * class.  If the underlying class is a top level class this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * method returns {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * @return the immediately enclosing class of the underlying class
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1297
     * @exception  SecurityException
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1298
     *             If a security manager, <i>s</i>, is present and the caller's
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1299
     *             class loader is not the same as or an ancestor of the class
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1300
     *             loader for the enclosing class and invocation of {@link
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1301
     *             SecurityManager#checkPackageAccess s.checkPackageAccess()}
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1302
     *             denies access to the package of the enclosing class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     */
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1305
    @CallerSensitive
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1306
    public Class<?> getEnclosingClass() throws SecurityException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        // There are five kinds of classes (or interfaces):
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        // a) Top level classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        // b) Nested classes (static member classes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        // c) Inner classes (non-static member classes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        // d) Local classes (named classes declared within a method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        // e) Anonymous classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        // JVM Spec 4.8.6: A class must have an EnclosingMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        // attribute if and only if it is a local class or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        // anonymous class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1319
        Class<?> enclosingCandidate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        if (enclosingInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            // This is a top level or a nested class or an inner class (a, b, or c)
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1323
            enclosingCandidate = getDeclaringClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            Class<?> enclosingClass = enclosingInfo.getEnclosingClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            // This is a local class or an anonymous class (d or e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            if (enclosingClass == this || enclosingClass == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                throw new InternalError("Malformed enclosing method information");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            else
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1330
                enclosingCandidate = enclosingClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        }
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1332
18263
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  1333
        if (enclosingCandidate != null)
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  1334
            enclosingCandidate.checkPackageAccess(
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  1335
                    ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
18234
996e8c4c858d 8007812: (reflect) Class.getEnclosingMethod problematic for some classes
jfranck
parents: 16906
diff changeset
  1336
        return enclosingCandidate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * Returns the simple name of the underlying class as given in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * source code. Returns an empty string if the underlying class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * anonymous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * <p>The simple name of an array is the simple name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * component type with "[]" appended.  In particular the simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * name of an array whose component type is anonymous is "[]".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * @return the simple name of the underlying class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    public String getSimpleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        if (isArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            return getComponentType().getSimpleName()+"[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        String simpleName = getSimpleBinaryName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        if (simpleName == null) { // top level class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            simpleName = getName();
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 22581
diff changeset
  1358
            return simpleName.substring(simpleName.lastIndexOf('.')+1); // strip the package name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        // According to JLS3 "Binary Compatibility" (13.1) the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        // name of non-package classes (not top level) is the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        // name of the immediately enclosing class followed by a '$' followed by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        // (for nested and inner classes): the simple name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        // (for local classes): 1 or more digits followed by the simple name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        // (for anonymous classes): 1 or more digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        // Since getSimpleBinaryName() will strip the binary name of
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 25517
diff changeset
  1368
        // the immediately enclosing class, we are now looking at a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        // string that matches the regular expression "\$[0-9]*"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        // followed by a simple name (considering the simple of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        // anonymous class to be the empty string).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        // Remove leading "\$[0-9]*" from the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        int length = simpleName.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        if (length < 1 || simpleName.charAt(0) != '$')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            throw new InternalError("Malformed class name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        int index = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        while (index < length && isAsciiDigit(simpleName.charAt(index)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        // Eventually, this is the empty string iff this is an anonymous class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        return simpleName.substring(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    /**
16743
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1385
     * Return an informative string for the name of this type.
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1386
     *
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1387
     * @return an informative string for the name of this type
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1388
     * @since 1.8
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1389
     */
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1390
    public String getTypeName() {
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1391
        if (isArray()) {
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1392
            try {
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1393
                Class<?> cl = this;
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1394
                int dimensions = 0;
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1395
                while (cl.isArray()) {
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1396
                    dimensions++;
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1397
                    cl = cl.getComponentType();
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1398
                }
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1399
                StringBuilder sb = new StringBuilder();
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1400
                sb.append(cl.getName());
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1401
                for (int i = 0; i < dimensions; i++) {
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1402
                    sb.append("[]");
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1403
                }
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1404
                return sb.toString();
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1405
            } catch (Throwable e) { /*FALLTHRU*/ }
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1406
        }
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1407
        return getName();
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1408
    }
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1409
b0b34102bb4c 6298888: Add toGenericString to j.l.Class and getTypeName to j.l.reflect.Type
darcy
parents: 16126
diff changeset
  1410
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * Character.isDigit answers {@code true} to some non-ascii
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * digits.  This one does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    private static boolean isAsciiDigit(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        return '0' <= c && c <= '9';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * Returns the canonical name of the underlying class as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * defined by the Java Language Specification.  Returns null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * the underlying class does not have a canonical name (i.e., if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * it is a local or anonymous class or an array whose component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * type does not have a canonical name).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * @return the canonical name of the underlying class if it exists, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * {@code null} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    public String getCanonicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        if (isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            String canonicalName = getComponentType().getCanonicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            if (canonicalName != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                return canonicalName + "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        if (isLocalOrAnonymousClass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        Class<?> enclosingClass = getEnclosingClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        if (enclosingClass == null) { // top level class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            return getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            String enclosingName = enclosingClass.getCanonicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            if (enclosingName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            return enclosingName + "." + getSimpleName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * Returns {@code true} if and only if the underlying class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * is an anonymous class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * @return {@code true} if and only if this class is an anonymous class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    public boolean isAnonymousClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        return "".equals(getSimpleName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * Returns {@code true} if and only if the underlying class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * is a local class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * @return {@code true} if and only if this class is a local class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    public boolean isLocalClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        return isLocalOrAnonymousClass() && !isAnonymousClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * Returns {@code true} if and only if the underlying class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * is a member class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * @return {@code true} if and only if this class is a member class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    public boolean isMemberClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        return getSimpleBinaryName() != null && !isLocalOrAnonymousClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * Returns the "simple binary name" of the underlying class, i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * the binary name without the leading enclosing class name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * Returns {@code null} if the underlying class is a top level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    private String getSimpleBinaryName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        Class<?> enclosingClass = getEnclosingClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        if (enclosingClass == null) // top level class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        // Otherwise, strip the enclosing class' name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            return getName().substring(enclosingClass.getName().length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        } catch (IndexOutOfBoundsException ex) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10147
diff changeset
  1496
            throw new InternalError("Malformed class name", ex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * Returns {@code true} if this is a local class or an anonymous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * class.  Returns {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    private boolean isLocalOrAnonymousClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        // JVM Spec 4.8.6: A class must have an EnclosingMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        // attribute if and only if it is a local class or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        // anonymous class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        return getEnclosingMethodInfo() != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * Returns an array containing {@code Class} objects representing all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * the public classes and interfaces that are members of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * represented by this {@code Class} object.  This includes public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * class and interface members inherited from superclasses and public class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * and interface members declared by the class.  This method returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * array of length 0 if this {@code Class} object has no public member
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * classes or interfaces.  This method also returns an array of length 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * this {@code Class} object represents a primitive type, an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * class, or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * @return the array of {@code Class} objects representing the public
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1523
     *         members of this class
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1524
     * @throws SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1525
     *         If a security manager, <i>s</i>, is present and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1526
     *         the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1527
     *         ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1528
     *         invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1529
     *         s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1530
     *         of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1532
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1534
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    public Class<?>[] getClasses() {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1536
        checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        // Privileged so this implementation can look at DECLARED classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        // something the caller might not have privilege to do.  The code here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        // is allowed to look at DECLARED classes because (1) it does not hand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        // out anything other than public members and (2) public member access
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        // has already been ok'd by the SecurityManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1544
        return java.security.AccessController.doPrivileged(
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  1545
            new java.security.PrivilegedAction<Class<?>[]>() {
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
  1546
                public Class<?>[] run() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  1547
                    List<Class<?>> list = new ArrayList<>();
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  1548
                    Class<?> currentClass = Class.this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                    while (currentClass != null) {
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  1550
                        for (Class<?> m : currentClass.getDeclaredClasses()) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  1551
                            if (Modifier.isPublic(m.getModifiers())) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  1552
                                list.add(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                        currentClass = currentClass.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                    }
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
  1557
                    return list.toArray(new Class<?>[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * Returns an array containing {@code Field} objects reflecting all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * the accessible public fields of the class or interface represented by
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1566
     * this {@code Class} object.
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1567
     *
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 27783
diff changeset
  1568
     * <p> If this {@code Class} object represents a class or interface with
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1569
     * no accessible public fields, then this method returns an array of length
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1570
     * 0.
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1571
     *
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1572
     * <p> If this {@code Class} object represents a class, then this method
27719
d8e33060989c 8063147: Class.getFields spec should state that fields are inherited from superinterfaces
martin
parents: 27072
diff changeset
  1573
     * returns the public fields of the class and of all its superclasses and
d8e33060989c 8063147: Class.getFields spec should state that fields are inherited from superinterfaces
martin
parents: 27072
diff changeset
  1574
     * superinterfaces.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     *
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1576
     * <p> If this {@code Class} object represents an interface, then this
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1577
     * method returns the fields of the interface and of all its
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1578
     * superinterfaces.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     *
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1580
     * <p> If this {@code Class} object represents an array type, a primitive
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1581
     * type, or void, then this method returns an array of length 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     *
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1583
     * <p> The elements in the returned array are not sorted and are not in any
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1584
     * particular order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * @return the array of {@code Field} objects representing the
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1587
     *         public fields
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1588
     * @throws SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1589
     *         If a security manager, <i>s</i>, is present and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1590
     *         the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1591
     *         ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1592
     *         invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1593
     *         s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1594
     *         of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1596
     * @since 1.1
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1597
     * @jls 8.2 Class Members
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1598
     * @jls 8.3 Field Declarations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1600
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    public Field[] getFields() throws SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1602
        checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        return copyFields(privateGetPublicFields(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    /**
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1608
     * Returns an array containing {@code Method} objects reflecting all the
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1609
     * public methods of the class or interface represented by this {@code
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1610
     * Class} object, including those declared by the class or interface and
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1611
     * those inherited from superclasses and superinterfaces.
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1612
     *
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1613
     * <p> If this {@code Class} object represents a type that has multiple
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1614
     * public methods with the same name and parameter types, but different
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1615
     * return types, then the returned array has a {@code Method} object for
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1616
     * each such method.
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1617
     *
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1618
     * <p> If this {@code Class} object represents a type with a class
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1619
     * initialization method {@code <clinit>}, then the returned array does
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1620
     * <em>not</em> have a corresponding {@code Method} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     *
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1622
     * <p> If this {@code Class} object represents an array type, then the
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1623
     * returned array has a {@code Method} object for each of the public
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1624
     * methods inherited by the array type from {@code Object}. It does not
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1625
     * contain a {@code Method} object for {@code clone()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     *
21970
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1627
     * <p> If this {@code Class} object represents an interface then the
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1628
     * returned array does not contain any implicitly declared methods from
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1629
     * {@code Object}. Therefore, if no methods are explicitly declared in
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1630
     * this interface or any of its superinterfaces then the returned array
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1631
     * has length 0. (Note that a {@code Class} object which represents a class
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1632
     * always has public methods, inherited from {@code Object}.)
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1633
     *
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1634
     * <p> If this {@code Class} object represents a primitive type or void,
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1635
     * then the returned array has length 0.
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1636
     *
21314
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  1637
     * <p> Static methods declared in superinterfaces of the class or interface
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  1638
     * represented by this {@code Class} object are not considered members of
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  1639
     * the class or interface.
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  1640
     *
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1641
     * <p> The elements in the returned array are not sorted and are not in any
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1642
     * particular order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * @return the array of {@code Method} objects representing the
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1645
     *         public methods of this class
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1646
     * @throws SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1647
     *         If a security manager, <i>s</i>, is present and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1648
     *         the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1649
     *         ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1650
     *         invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1651
     *         s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1652
     *         of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     *
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1654
     * @jls 8.2 Class Members
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1655
     * @jls 8.4 Method Declarations
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1656
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1658
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    public Method[] getMethods() throws SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1660
        checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        return copyMethods(privateGetPublicMethods());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * Returns an array containing {@code Constructor} objects reflecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * all the public constructors of the class represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * {@code Class} object.  An array of length 0 is returned if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * class has no public constructors, or if the class is an array class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * if the class reflects a primitive type or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * Note that while this method returns an array of {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * Constructor<T>} objects (that is an array of constructors from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * this class), the return type of this method is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * might be expected.  This less informative return type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * necessary since after being returned from this method, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * array could be modified to hold {@code Constructor} objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     * different classes, which would violate the type guarantees of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     * {@code Constructor<T>[]}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
     * @return the array of {@code Constructor} objects representing the
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1683
     *         public constructors of this class
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1684
     * @throws SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1685
     *         If a security manager, <i>s</i>, is present and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1686
     *         the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1687
     *         ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1688
     *         invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1689
     *         s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1690
     *         of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1692
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1694
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
    public Constructor<?>[] getConstructors() throws SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1696
        checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        return copyConstructors(privateGetDeclaredConstructors(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    /**
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1702
     * Returns a {@code Field} object that reflects the specified public member
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1703
     * field of the class or interface represented by this {@code Class}
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1704
     * object. The {@code name} parameter is a {@code String} specifying the
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1705
     * simple name of the desired field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * <p> The field to be reflected is determined by the algorithm that
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1708
     * follows.  Let C be the class or interface represented by this object:
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1709
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * <OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * <LI> If C declares a public field with the name specified, that is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     *      field to be reflected.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * <LI> If no field was found in step 1 above, this algorithm is applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     *      recursively to each direct superinterface of C. The direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     *      superinterfaces are searched in the order they were declared.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * <LI> If no field was found in steps 1 and 2 above, and C has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     *      superclass S, then this algorithm is invoked recursively upon S.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     *      If C has no superclass, then a {@code NoSuchFieldException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     *      is thrown.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * </OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     *
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1722
     * <p> If this {@code Class} object represents an array type, then this
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1723
     * method does not find the {@code length} field of the array type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * @param name the field name
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1726
     * @return the {@code Field} object of this class specified by
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1727
     *         {@code name}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1728
     * @throws NoSuchFieldException if a field with the specified name is
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1729
     *         not found.
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1730
     * @throws NullPointerException if {@code name} is {@code null}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1731
     * @throws SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1732
     *         If a security manager, <i>s</i>, is present and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1733
     *         the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1734
     *         ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1735
     *         invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1736
     *         s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1737
     *         of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1739
     * @since 1.1
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1740
     * @jls 8.2 Class Members
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1741
     * @jls 8.3 Field Declarations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1743
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
    public Field getField(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        throws NoSuchFieldException, SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1746
        checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        Field field = getField0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        if (field == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            throw new NoSuchFieldException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        return field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * Returns a {@code Method} object that reflects the specified public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * member method of the class or interface represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * {@code Class} object. The {@code name} parameter is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * {@code String} specifying the simple name of the desired method. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * {@code parameterTypes} parameter is an array of {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * objects that identify the method's formal parameter types, in declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * order. If {@code parameterTypes} is {@code null}, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * treated as if it were an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     *
21970
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1765
     * <p> If the {@code name} is "{@code <init>}" or "{@code <clinit>}" a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * {@code NoSuchMethodException} is raised. Otherwise, the method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * be reflected is determined by the algorithm that follows.  Let C be the
21970
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1768
     * class or interface represented by this object:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * <OL>
21970
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1770
     * <LI> C is searched for a <I>matching method</I>, as defined below. If a
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1771
     *      matching method is found, it is reflected.</LI>
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1772
     * <LI> If no matching method is found by step 1 then:
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1773
     *   <OL TYPE="a">
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1774
     *   <LI> If C is a class other than {@code Object}, then this algorithm is
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1775
     *        invoked recursively on the superclass of C.</LI>
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1776
     *   <LI> If C is the class {@code Object}, or if C is an interface, then
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1777
     *        the superinterfaces of C (if any) are searched for a matching
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1778
     *        method. If any such method is found, it is reflected.</LI>
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1779
     *   </OL></LI>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * </OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     *
21970
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1782
     * <p> To find a matching method in a class or interface C:&nbsp; If C
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1783
     * declares exactly one public method with the specified name and exactly
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1784
     * the same formal parameter types, that is the method reflected. If more
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1785
     * than one such method is found in C, and one of these methods has a
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1786
     * return type that is more specific than any of the others, that method is
fb9a728c4b77 8029117: (reflect) clarify javadoc for getMethod(...) and getMethods()
jfranck
parents: 21358
diff changeset
  1787
     * reflected; otherwise one of the methods is chosen arbitrarily.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * <p>Note that there may be more than one matching method in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * class because while the Java language forbids a class to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * declare multiple methods with the same signature but different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * return types, the Java virtual machine does not.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * increased flexibility in the virtual machine can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * implement various language features.  For example, covariant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * returns can be implemented with {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * java.lang.reflect.Method#isBridge bridge methods}; the bridge
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * method and the method being overridden would have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * signature but different return types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     *
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1800
     * <p> If this {@code Class} object represents an array type, then this
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1801
     * method does not find the {@code clone()} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     *
21314
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  1803
     * <p> Static methods declared in superinterfaces of the class or interface
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  1804
     * represented by this {@code Class} object are not considered members of
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  1805
     * the class or interface.
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  1806
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     * @param name the name of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * @param parameterTypes the list of parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * @return the {@code Method} object that matches the specified
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1810
     *         {@code name} and {@code parameterTypes}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1811
     * @throws NoSuchMethodException if a matching method is not found
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1812
     *         or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1813
     * @throws NullPointerException if {@code name} is {@code null}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1814
     * @throws SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1815
     *         If a security manager, <i>s</i>, is present and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1816
     *         the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1817
     *         ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1818
     *         invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1819
     *         s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1820
     *         of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     *
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1822
     * @jls 8.2 Class Members
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1823
     * @jls 8.4 Method Declarations
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1824
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1826
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
    public Method getMethod(String name, Class<?>... parameterTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        throws NoSuchMethodException, SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1829
        checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
21314
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  1830
        Method method = getMethod0(name, parameterTypes, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * Returns a {@code Constructor} object that reflects the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * public constructor of the class represented by this {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * object. The {@code parameterTypes} parameter is an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * {@code Class} objects that identify the constructor's formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * parameter types, in declared order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * If this {@code Class} object represents an inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * declared in a non-static context, the formal parameter types
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * include the explicit enclosing instance as the first parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * <p> The constructor to reflect is the public constructor of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * represented by this {@code Class} object whose formal parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * types match those specified by {@code parameterTypes}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * @param parameterTypes the parameter array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * @return the {@code Constructor} object of the public constructor that
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1855
     *         matches the specified {@code parameterTypes}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1856
     * @throws NoSuchMethodException if a matching method is not found.
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1857
     * @throws SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1858
     *         If a security manager, <i>s</i>, is present and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1859
     *         the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1860
     *         ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1861
     *         invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1862
     *         s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1863
     *         of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1865
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1867
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
    public Constructor<T> getConstructor(Class<?>... parameterTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        throws NoSuchMethodException, SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1870
        checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        return getConstructor0(parameterTypes, Member.PUBLIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * Returns an array of {@code Class} objects reflecting all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * classes and interfaces declared as members of the class represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * this {@code Class} object. This includes public, protected, default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * (package) access, and private classes and interfaces declared by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * class, but excludes inherited classes and interfaces.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * returns an array of length 0 if the class declares no classes or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * interfaces as members, or if this {@code Class} object represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * primitive type, an array class, or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * @return the array of {@code Class} objects representing all the
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1886
     *         declared members of this class
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1887
     * @throws SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1888
     *         If a security manager, <i>s</i>, is present and any of the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1889
     *         following conditions is met:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1891
     *         <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1893
     *         <li> the caller's class loader is not the same as the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1894
     *         class loader of this class and invocation of
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1895
     *         {@link SecurityManager#checkPermission
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1896
     *         s.checkPermission} method with
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1897
     *         {@code RuntimePermission("accessDeclaredMembers")}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1898
     *         denies access to the declared classes within this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1900
     *         <li> the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1901
     *         ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1902
     *         invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1903
     *         s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1904
     *         of this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1906
     *         </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1908
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1910
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
    public Class<?>[] getDeclaredClasses() throws SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1912
        checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        return getDeclaredClasses0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     * Returns an array of {@code Field} objects reflecting all the fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     * declared by the class or interface represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     * {@code Class} object. This includes public, protected, default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     * (package) access, and private fields, but excludes inherited fields.
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1922
     *
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1923
     * <p> If this {@code Class} object represents a class or interface with no
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1924
     * declared fields, then this method returns an array of length 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     *
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1926
     * <p> If this {@code Class} object represents an array type, a primitive
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1927
     * type, or void, then this method returns an array of length 0.
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1928
     *
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1929
     * <p> The elements in the returned array are not sorted and are not in any
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1930
     * particular order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1932
     * @return  the array of {@code Field} objects representing all the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1933
     *          declared fields of this class
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1934
     * @throws  SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1935
     *          If a security manager, <i>s</i>, is present and any of the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1936
     *          following conditions is met:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1938
     *          <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1940
     *          <li> the caller's class loader is not the same as the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1941
     *          class loader of this class and invocation of
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1942
     *          {@link SecurityManager#checkPermission
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1943
     *          s.checkPermission} method with
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1944
     *          {@code RuntimePermission("accessDeclaredMembers")}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1945
     *          denies access to the declared fields within this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1947
     *          <li> the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1948
     *          ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1949
     *          invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1950
     *          s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1951
     *          of this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1953
     *          </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1955
     * @since 1.1
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1956
     * @jls 8.2 Class Members
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  1957
     * @jls 8.3 Field Declarations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1959
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
    public Field[] getDeclaredFields() throws SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  1961
        checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        return copyFields(privateGetDeclaredFields(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
    /**
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1967
     *
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1968
     * Returns an array containing {@code Method} objects reflecting all the
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1969
     * declared methods of the class or interface represented by this {@code
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1970
     * Class} object, including public, protected, default (package)
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1971
     * access, and private methods, but excluding inherited methods.
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1972
     *
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1973
     * <p> If this {@code Class} object represents a type that has multiple
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1974
     * declared methods with the same name and parameter types, but different
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1975
     * return types, then the returned array has a {@code Method} object for
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1976
     * each such method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     *
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1978
     * <p> If this {@code Class} object represents a type that has a class
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1979
     * initialization method {@code <clinit>}, then the returned array does
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1980
     * <em>not</em> have a corresponding {@code Method} object.
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1981
     *
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1982
     * <p> If this {@code Class} object represents a class or interface with no
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1983
     * declared methods, then the returned array has length 0.
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1984
     *
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1985
     * <p> If this {@code Class} object represents an array type, a primitive
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1986
     * type, or void, then the returned array has length 0.
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1987
     *
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1988
     * <p> The elements in the returned array are not sorted and are not in any
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  1989
     * particular order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1991
     * @return  the array of {@code Method} objects representing all the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1992
     *          declared methods of this class
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1993
     * @throws  SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1994
     *          If a security manager, <i>s</i>, is present and any of the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1995
     *          following conditions is met:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1997
     *          <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  1999
     *          <li> the caller's class loader is not the same as the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2000
     *          class loader of this class and invocation of
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2001
     *          {@link SecurityManager#checkPermission
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2002
     *          s.checkPermission} method with
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2003
     *          {@code RuntimePermission("accessDeclaredMembers")}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2004
     *          denies access to the declared methods within this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2006
     *          <li> the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2007
     *          ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2008
     *          invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2009
     *          s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2010
     *          of this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2012
     *          </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     *
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  2014
     * @jls 8.2 Class Members
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  2015
     * @jls 8.4 Method Declarations
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  2016
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2018
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
    public Method[] getDeclaredMethods() throws SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2020
        checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        return copyMethods(privateGetDeclaredMethods(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     * Returns an array of {@code Constructor} objects reflecting all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * constructors declared by the class represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * {@code Class} object. These are public, protected, default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     * (package) access, and private constructors.  The elements in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     * returned are not sorted and are not in any particular order.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * class has a default constructor, it is included in the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * This method returns an array of length 0 if this {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * object represents an interface, a primitive type, an array class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * <p> See <em>The Java Language Specification</em>, section 8.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2038
     * @return  the array of {@code Constructor} objects representing all the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2039
     *          declared constructors of this class
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2040
     * @throws  SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2041
     *          If a security manager, <i>s</i>, is present and any of the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2042
     *          following conditions is met:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2044
     *          <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2046
     *          <li> the caller's class loader is not the same as the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2047
     *          class loader of this class and invocation of
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2048
     *          {@link SecurityManager#checkPermission
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2049
     *          s.checkPermission} method with
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2050
     *          {@code RuntimePermission("accessDeclaredMembers")}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2051
     *          denies access to the declared constructors within this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2053
     *          <li> the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2054
     *          ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2055
     *          invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2056
     *          s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2057
     *          of this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2059
     *          </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  2061
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2063
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
    public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2065
        checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        return copyConstructors(privateGetDeclaredConstructors(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * Returns a {@code Field} object that reflects the specified declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * field of the class or interface represented by this {@code Class}
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  2073
     * object. The {@code name} parameter is a {@code String} that specifies
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  2074
     * the simple name of the desired field.
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  2075
     *
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  2076
     * <p> If this {@code Class} object represents an array type, then this
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  2077
     * method does not find the {@code length} field of the array type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     * @param name the name of the field
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2080
     * @return  the {@code Field} object for the specified field in this
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2081
     *          class
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2082
     * @throws  NoSuchFieldException if a field with the specified name is
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2083
     *          not found.
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2084
     * @throws  NullPointerException if {@code name} is {@code null}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2085
     * @throws  SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2086
     *          If a security manager, <i>s</i>, is present and any of the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2087
     *          following conditions is met:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2089
     *          <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2091
     *          <li> the caller's class loader is not the same as the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2092
     *          class loader of this class and invocation of
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2093
     *          {@link SecurityManager#checkPermission
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2094
     *          s.checkPermission} method with
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2095
     *          {@code RuntimePermission("accessDeclaredMembers")}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2096
     *          denies access to the declared field
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2098
     *          <li> the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2099
     *          ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2100
     *          invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2101
     *          s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2102
     *          of this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2104
     *          </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  2106
     * @since 1.1
19819
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  2107
     * @jls 8.2 Class Members
a2cde5f9a3d6 5047859: (reflect) Class.getField can't find String[].length
jfranck
parents: 19588
diff changeset
  2108
     * @jls 8.3 Field Declarations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2110
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
    public Field getDeclaredField(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        throws NoSuchFieldException, SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2113
        checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        Field field = searchFields(privateGetDeclaredFields(false), name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        if (field == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            throw new NoSuchFieldException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        return field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     * Returns a {@code Method} object that reflects the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * declared method of the class or interface represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * {@code Class} object. The {@code name} parameter is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     * {@code String} that specifies the simple name of the desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * method, and the {@code parameterTypes} parameter is an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * {@code Class} objects that identify the method's formal parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * types, in declared order.  If more than one method with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * parameter types is declared in a class, and one of these methods has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     * return type that is more specific than any of the others, that method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * returned; otherwise one of the methods is chosen arbitrarily.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     * name is "&lt;init&gt;"or "&lt;clinit&gt;" a {@code NoSuchMethodException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     * is raised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     *
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  2136
     * <p> If this {@code Class} object represents an array type, then this
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  2137
     * method does not find the {@code clone()} method.
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  2138
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     * @param name the name of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     * @param parameterTypes the parameter array
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2141
     * @return  the {@code Method} object for the method of this class
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2142
     *          matching the specified name and parameters
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2143
     * @throws  NoSuchMethodException if a matching method is not found.
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2144
     * @throws  NullPointerException if {@code name} is {@code null}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2145
     * @throws  SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2146
     *          If a security manager, <i>s</i>, is present and any of the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2147
     *          following conditions is met:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2149
     *          <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2151
     *          <li> the caller's class loader is not the same as the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2152
     *          class loader of this class and invocation of
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2153
     *          {@link SecurityManager#checkPermission
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2154
     *          s.checkPermission} method with
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2155
     *          {@code RuntimePermission("accessDeclaredMembers")}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2156
     *          denies access to the declared method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2158
     *          <li> the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2159
     *          ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2160
     *          invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2161
     *          s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2162
     *          of this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2164
     *          </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     *
19834
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  2166
     * @jls 8.2 Class Members
c43d94c72c41 4987375: (reflect) Class.get{Declared}Method{s} does not return clone() for array types
jfranck
parents: 19819
diff changeset
  2167
     * @jls 8.4 Method Declarations
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  2168
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2170
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
    public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
        throws NoSuchMethodException, SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2173
        checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
            throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     * Returns a {@code Constructor} object that reflects the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     * constructor of the class or interface represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     * {@code Class} object.  The {@code parameterTypes} parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     * an array of {@code Class} objects that identify the constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     * formal parameter types, in declared order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     * If this {@code Class} object represents an inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     * declared in a non-static context, the formal parameter types
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     * include the explicit enclosing instance as the first parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     * @param parameterTypes the parameter array
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2194
     * @return  The {@code Constructor} object for the constructor with the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2195
     *          specified parameter list
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2196
     * @throws  NoSuchMethodException if a matching method is not found.
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2197
     * @throws  SecurityException
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2198
     *          If a security manager, <i>s</i>, is present and any of the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2199
     *          following conditions is met:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2201
     *          <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2203
     *          <li> the caller's class loader is not the same as the
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2204
     *          class loader of this class and invocation of
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2205
     *          {@link SecurityManager#checkPermission
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2206
     *          s.checkPermission} method with
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2207
     *          {@code RuntimePermission("accessDeclaredMembers")}
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2208
     *          denies access to the declared constructor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2210
     *          <li> the caller's class loader is not the same as or an
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2211
     *          ancestor of the class loader for the current class and
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2212
     *          invocation of {@link SecurityManager#checkPackageAccess
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2213
     *          s.checkPackageAccess()} denies access to the package
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2214
     *          of this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     *
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2216
     *          </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  2218
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2220
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
    public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
        throws NoSuchMethodException, SecurityException {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2223
        checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        return getConstructor0(parameterTypes, Member.DECLARED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
     * Finds a resource with a given name.  The rules for searching resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
     * associated with a given class are implemented by the defining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
     * {@linkplain ClassLoader class loader} of the class.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
     * delegates to this object's class loader.  If this object was loaded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     * the bootstrap class loader, the method delegates to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
     * ClassLoader#getSystemResourceAsStream}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     * <p> Before delegation, an absolute resource name is constructed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     * given resource name using this algorithm:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     * <li> If the {@code name} begins with a {@code '/'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
     * portion of the {@code name} following the {@code '/'}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
     * <li> Otherwise, the absolute name is of the following form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     *   {@code modified_package_name/name}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     * <p> Where the {@code modified_package_name} is the package name of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     * object with {@code '/'} substituted for {@code '.'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
     * (<tt>'&#92;u002e'</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     * @param  name name of the desired resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     * @return      A {@link java.io.InputStream} object or {@code null} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     *              no resource with this name is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     * @throws  NullPointerException If {@code name} is {@code null}
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  2260
     * @since  1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     public InputStream getResourceAsStream(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        name = resolveName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
        ClassLoader cl = getClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        if (cl==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            // A system class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            return ClassLoader.getSystemResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        return cl.getResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
     * Finds a resource with a given name.  The rules for searching resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
     * associated with a given class are implemented by the defining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
     * {@linkplain ClassLoader class loader} of the class.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     * delegates to this object's class loader.  If this object was loaded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
     * the bootstrap class loader, the method delegates to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
     * ClassLoader#getSystemResource}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     * <p> Before delegation, an absolute resource name is constructed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     * given resource name using this algorithm:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
     * <li> If the {@code name} begins with a {@code '/'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
     * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
     * portion of the {@code name} following the {@code '/'}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
     * <li> Otherwise, the absolute name is of the following form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
     *   {@code modified_package_name/name}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
     * <p> Where the {@code modified_package_name} is the package name of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
     * object with {@code '/'} substituted for {@code '.'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
     * (<tt>'&#92;u002e'</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     * @param  name name of the desired resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     * @return      A  {@link java.net.URL} object or {@code null} if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     *              resource with this name is found
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  2304
     * @since  1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
    public java.net.URL getResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        name = resolveName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        ClassLoader cl = getClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
        if (cl==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
            // A system class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
            return ClassLoader.getSystemResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
        return cl.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    /** protection domain returned when the internal domain is null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
    private static java.security.ProtectionDomain allPermDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * Returns the {@code ProtectionDomain} of this class.  If there is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     * security manager installed, this method first calls the security
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
     * manager's {@code checkPermission} method with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
     * {@code RuntimePermission("getProtectionDomain")} permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
     * ensure it's ok to get the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
     * {@code ProtectionDomain}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     * @return the ProtectionDomain of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     *        {@code checkPermission} method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
     *        getting the ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
     * @see java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
     * @see java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
    public java.security.ProtectionDomain getProtectionDomain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
            sm.checkPermission(SecurityConstants.GET_PD_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
        java.security.ProtectionDomain pd = getProtectionDomain0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
        if (pd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            if (allPermDomain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                java.security.Permissions perms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                    new java.security.Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                perms.add(SecurityConstants.ALL_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                allPermDomain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                    new java.security.ProtectionDomain(null, perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            pd = allPermDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        return pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * Returns the ProtectionDomain of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
    private native java.security.ProtectionDomain getProtectionDomain0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     * Return the Virtual Machine's Class object for the named
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     * primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
     */
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
  2371
    static native Class<?> getPrimitiveClass(String name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     * Check if client is allowed to access members.  If access is denied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     * throw a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     *
18263
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2377
     * This method also enforces package access.
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2378
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     * <p> Default policy: allow all clients access with normal Java access
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
     * control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2382
    private void checkMemberAccess(int which, Class<?> caller, boolean checkProxyInterfaces) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2383
        final SecurityManager s = System.getSecurityManager();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        if (s != null) {
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2385
            /* Default policy allows access to all {@link Member#PUBLIC} members,
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2386
             * as well as access to classes that have the same class loader as the caller.
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2387
             * In all other cases, it requires RuntimePermission("accessDeclaredMembers")
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2388
             * permission.
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2389
             */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2390
            final ClassLoader ccl = ClassLoader.getClassLoader(caller);
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2391
            final ClassLoader cl = getClassLoader0();
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2392
            if (which != Member.PUBLIC) {
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2393
                if (ccl != cl) {
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 18546
diff changeset
  2394
                    s.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2395
                }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2396
            }
18263
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2397
            this.checkPackageAccess(ccl, checkProxyInterfaces);
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2398
        }
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2399
    }
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2400
18263
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2401
    /*
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2402
     * Checks if a client loaded in ClassLoader ccl is allowed to access this
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2403
     * class under the current package access policy. If access is denied,
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2404
     * throw a SecurityException.
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2405
     */
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2406
    private void checkPackageAccess(final ClassLoader ccl, boolean checkProxyInterfaces) {
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2407
        final SecurityManager s = System.getSecurityManager();
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2408
        if (s != null) {
69df685432b7 8011139: (reflect) Revise checking in getEnclosingClass
jfranck
parents: 18253
diff changeset
  2409
            final ClassLoader cl = getClassLoader0();
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16743
diff changeset
  2410
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 14014
diff changeset
  2411
            if (ReflectUtil.needsPackageAccessCheck(ccl, cl)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                String name = this.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
                int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
                if (i != -1) {
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 14014
diff changeset
  2415
                    // skip the package access check on a proxy class in default proxy package
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 14014
diff changeset
  2416
                    String pkg = name.substring(0, i);
18244
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 18234
diff changeset
  2417
                    if (!Proxy.isProxyClass(this) || ReflectUtil.isNonPublicProxyClass(this)) {
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 14014
diff changeset
  2418
                        s.checkPackageAccess(pkg);
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 14014
diff changeset
  2419
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
            }
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 14014
diff changeset
  2422
            // check package access on the proxy interfaces
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 14014
diff changeset
  2423
            if (checkProxyInterfaces && Proxy.isProxyClass(this)) {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 14014
diff changeset
  2424
                ReflectUtil.checkProxyPackageAccess(ccl, this.getInterfaces());
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 14014
diff changeset
  2425
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     * Add a package name prefix if the name is not absolute Remove leading "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     * if name is absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
    private String resolveName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        if (!name.startsWith("/")) {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  2438
            Class<?> c = this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
            while (c.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
                c = c.getComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
            String baseName = c.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
            int index = baseName.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
            if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
                name = baseName.substring(0, index).replace('.', '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                    +"/"+name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
            name = name.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
    /**
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2455
     * Atomic operations support.
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2456
     */
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2457
    private static class Atomic {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2458
        // initialize Unsafe machinery here, since we need to call Class.class instance method
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2459
        // and have to avoid calling it in the static initializer of the Class class...
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2460
        private static final Unsafe unsafe = Unsafe.getUnsafe();
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2461
        // offset of Class.reflectionData instance field
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2462
        private static final long reflectionDataOffset;
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2463
        // offset of Class.annotationType instance field
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2464
        private static final long annotationTypeOffset;
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  2465
        // offset of Class.annotationData instance field
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  2466
        private static final long annotationDataOffset;
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2467
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2468
        static {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2469
            Field[] fields = Class.class.getDeclaredFields0(false); // bypass caches
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2470
            reflectionDataOffset = objectFieldOffset(fields, "reflectionData");
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2471
            annotationTypeOffset = objectFieldOffset(fields, "annotationType");
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  2472
            annotationDataOffset = objectFieldOffset(fields, "annotationData");
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2473
        }
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2474
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2475
        private static long objectFieldOffset(Field[] fields, String fieldName) {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2476
            Field field = searchFields(fields, fieldName);
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2477
            if (field == null) {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2478
                throw new Error("No " + fieldName + " field found in java.lang.Class");
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2479
            }
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2480
            return unsafe.objectFieldOffset(field);
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2481
        }
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2482
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2483
        static <T> boolean casReflectionData(Class<?> clazz,
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2484
                                             SoftReference<ReflectionData<T>> oldData,
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2485
                                             SoftReference<ReflectionData<T>> newData) {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2486
            return unsafe.compareAndSwapObject(clazz, reflectionDataOffset, oldData, newData);
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2487
        }
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2488
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2489
        static <T> boolean casAnnotationType(Class<?> clazz,
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2490
                                             AnnotationType oldType,
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2491
                                             AnnotationType newType) {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2492
            return unsafe.compareAndSwapObject(clazz, annotationTypeOffset, oldType, newType);
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2493
        }
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  2494
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  2495
        static <T> boolean casAnnotationData(Class<?> clazz,
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  2496
                                             AnnotationData oldData,
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  2497
                                             AnnotationData newData) {
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  2498
            return unsafe.compareAndSwapObject(clazz, annotationDataOffset, oldData, newData);
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  2499
        }
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2500
    }
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2501
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2502
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     * Reflection support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
    // Caches for certain reflective results
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
    private static boolean useCaches = true;
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2508
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2509
    // reflection data that might get invalidated when JVM TI RedefineClasses() is called
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  2510
    private static class ReflectionData<T> {
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2511
        volatile Field[] declaredFields;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2512
        volatile Field[] publicFields;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2513
        volatile Method[] declaredMethods;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2514
        volatile Method[] publicMethods;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2515
        volatile Constructor<T>[] declaredConstructors;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2516
        volatile Constructor<T>[] publicConstructors;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2517
        // Intermediate results for getFields and getMethods
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2518
        volatile Field[] declaredPublicFields;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2519
        volatile Method[] declaredPublicMethods;
18179
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2520
        volatile Class<?>[] interfaces;
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2521
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2522
        // Value of classRedefinedCount when we created this ReflectionData instance
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2523
        final int redefinedCount;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2524
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2525
        ReflectionData(int redefinedCount) {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2526
            this.redefinedCount = redefinedCount;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2527
        }
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2528
    }
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2529
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2530
    private volatile transient SoftReference<ReflectionData<T>> reflectionData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
    // Incremented by the VM on each call to JVM TI RedefineClasses()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
    // that redefines this class or a superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
    private volatile transient int classRedefinedCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2536
    // Lazily create and cache ReflectionData
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2537
    private ReflectionData<T> reflectionData() {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2538
        SoftReference<ReflectionData<T>> reflectionData = this.reflectionData;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2539
        int classRedefinedCount = this.classRedefinedCount;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2540
        ReflectionData<T> rd;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2541
        if (useCaches &&
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2542
            reflectionData != null &&
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2543
            (rd = reflectionData.get()) != null &&
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2544
            rd.redefinedCount == classRedefinedCount) {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2545
            return rd;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2546
        }
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2547
        // else no SoftReference or cleared SoftReference or stale ReflectionData
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2548
        // -> create and replace new instance
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2549
        return newReflectionData(reflectionData, classRedefinedCount);
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2550
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2552
    private ReflectionData<T> newReflectionData(SoftReference<ReflectionData<T>> oldReflectionData,
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2553
                                                int classRedefinedCount) {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2554
        if (!useCaches) return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2556
        while (true) {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2557
            ReflectionData<T> rd = new ReflectionData<>(classRedefinedCount);
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2558
            // try to CAS it...
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2559
            if (Atomic.casReflectionData(this, oldReflectionData, new SoftReference<>(rd))) {
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2560
                return rd;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2561
            }
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2562
            // else retry
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2563
            oldReflectionData = this.reflectionData;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2564
            classRedefinedCount = this.classRedefinedCount;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2565
            if (oldReflectionData != null &&
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2566
                (rd = oldReflectionData.get()) != null &&
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2567
                rd.redefinedCount == classRedefinedCount) {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2568
                return rd;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2569
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
    // Generic signature handling
18179
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2574
    private native String getGenericSignature0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
    // Generic info repository; lazily initialized
18179
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2577
    private volatile transient ClassRepository genericInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
    // accessor for factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
    private GenericsFactory getFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        // create scope and factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        return CoreReflectionFactory.make(this, ClassScope.make(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
18179
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2585
    // accessor for generic info repository;
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2586
    // generic info is lazily initialized
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
    private ClassRepository getGenericInfo() {
18179
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2588
        ClassRepository genericInfo = this.genericInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
        if (genericInfo == null) {
18179
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2590
            String signature = getGenericSignature0();
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2591
            if (signature == null) {
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2592
                genericInfo = ClassRepository.NONE;
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2593
            } else {
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2594
                genericInfo = ClassRepository.make(signature, getFactory());
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2595
            }
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2596
            this.genericInfo = genericInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
        }
18179
9b6ad451b521 8016236: Class.getGenericInterfaces performance improvement
shade
parents: 18139
diff changeset
  2598
        return (genericInfo != ClassRepository.NONE) ? genericInfo : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
    // Annotations handling
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  2602
    native byte[] getRawAnnotations();
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  2603
    // Since 1.8
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  2604
    native byte[] getRawTypeAnnotations();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  2605
    static byte[] getExecutableTypeAnnotationBytes(Executable ex) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  2606
        return getReflectionFactory().getExecutableTypeAnnotationBytes(ex);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  2607
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
    native ConstantPool getConstantPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
    // java.lang.reflect.Field handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
    // Returns an array of "root" fields. These Field objects must NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
    // be propagated to the outside world, but must instead be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
    // via ReflectionFactory.copyField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
    private Field[] privateGetDeclaredFields(boolean publicOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        checkInitted();
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2622
        Field[] res;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2623
        ReflectionData<T> rd = reflectionData();
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2624
        if (rd != null) {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2625
            res = publicOnly ? rd.declaredPublicFields : rd.declaredFields;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
            if (res != null) return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
        // No cached value available; request value from VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
        res = Reflection.filterFields(this, getDeclaredFields0(publicOnly));
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2630
        if (rd != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
            if (publicOnly) {
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2632
                rd.declaredPublicFields = res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
            } else {
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2634
                rd.declaredFields = res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
    // Returns an array of "root" fields. These Field objects must NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
    // be propagated to the outside world, but must instead be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
    // via ReflectionFactory.copyField.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2643
    private Field[] privateGetPublicFields(Set<Class<?>> traversedInterfaces) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
        checkInitted();
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2645
        Field[] res;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2646
        ReflectionData<T> rd = reflectionData();
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2647
        if (rd != null) {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2648
            res = rd.publicFields;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
            if (res != null) return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
        // No cached value available; compute value recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
        // Traverse in correct order for getField().
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  2654
        List<Field> fields = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
        if (traversedInterfaces == null) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  2656
            traversedInterfaces = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
        // Local fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
        Field[] tmp = privateGetDeclaredFields(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
        addAll(fields, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
        // Direct superinterfaces, recursively
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2664
        for (Class<?> c : getInterfaces()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
            if (!traversedInterfaces.contains(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
                traversedInterfaces.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
                addAll(fields, c.privateGetPublicFields(traversedInterfaces));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
        // Direct superclass, recursively
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
        if (!isInterface()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2673
            Class<?> c = getSuperclass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
                addAll(fields, c.privateGetPublicFields(traversedInterfaces));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
        res = new Field[fields.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
        fields.toArray(res);
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2681
        if (rd != null) {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2682
            rd.publicFields = res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2687
    private static void addAll(Collection<Field> c, Field[] o) {
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  2688
        for (Field f : o) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  2689
            c.add(f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
    // java.lang.reflect.Constructor handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
    // Returns an array of "root" constructors. These Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
    // objects must NOT be propagated to the outside world, but must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
    // instead be copied via ReflectionFactory.copyConstructor.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2703
    private Constructor<T>[] privateGetDeclaredConstructors(boolean publicOnly) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
        checkInitted();
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2705
        Constructor<T>[] res;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2706
        ReflectionData<T> rd = reflectionData();
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2707
        if (rd != null) {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2708
            res = publicOnly ? rd.publicConstructors : rd.declaredConstructors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
            if (res != null) return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
        // No cached value available; request value from VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
        if (isInterface()) {
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
  2713
            @SuppressWarnings("unchecked")
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
  2714
            Constructor<T>[] temporaryRes = (Constructor<T>[]) new Constructor<?>[0];
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
  2715
            res = temporaryRes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
            res = getDeclaredConstructors0(publicOnly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
        }
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2719
        if (rd != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
            if (publicOnly) {
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2721
                rd.publicConstructors = res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
            } else {
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2723
                rd.declaredConstructors = res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
    // java.lang.reflect.Method handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
    // Returns an array of "root" methods. These Method objects must NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
    // be propagated to the outside world, but must instead be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
    // via ReflectionFactory.copyMethod.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
    private Method[] privateGetDeclaredMethods(boolean publicOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
        checkInitted();
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2740
        Method[] res;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2741
        ReflectionData<T> rd = reflectionData();
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2742
        if (rd != null) {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2743
            res = publicOnly ? rd.declaredPublicMethods : rd.declaredMethods;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
            if (res != null) return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
        // No cached value available; request value from VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
        res = Reflection.filterMethods(this, getDeclaredMethods0(publicOnly));
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2748
        if (rd != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
            if (publicOnly) {
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2750
                rd.declaredPublicMethods = res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
            } else {
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2752
                rd.declaredMethods = res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
    static class MethodArray {
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2759
        // Don't add or remove methods except by add() or remove() calls.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
        private Method[] methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
        private int length;
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2762
        private int defaults;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
        MethodArray() {
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2765
            this(20);
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2766
        }
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2767
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2768
        MethodArray(int initialSize) {
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2769
            if (initialSize < 2)
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2770
                throw new IllegalArgumentException("Size should be 2 or more");
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2771
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2772
            methods = new Method[initialSize];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
            length = 0;
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2774
            defaults = 0;
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2775
        }
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2776
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2777
        boolean hasDefaults() {
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2778
            return defaults != 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
        void add(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
            if (length == methods.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
                methods = Arrays.copyOf(methods, 2 * methods.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
            methods[length++] = m;
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2786
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2787
            if (m != null && m.isDefault())
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2788
                defaults++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
        void addAll(Method[] ma) {
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  2792
            for (Method m : ma) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  2793
                add(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
        void addAll(MethodArray ma) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
            for (int i = 0; i < ma.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                add(ma.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
        void addIfNotPresent(Method newMethod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
            for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                Method m = methods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                if (m == newMethod || (m != null && m.equals(newMethod))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
            add(newMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
        void addAllIfNotPresent(MethodArray newMethods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
            for (int i = 0; i < newMethods.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                Method m = newMethods.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                if (m != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                    addIfNotPresent(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2822
        /* Add Methods declared in an interface to this MethodArray.
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2823
         * Static methods declared in interfaces are not inherited.
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2824
         */
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2825
        void addInterfaceMethods(Method[] methods) {
21314
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  2826
            for (Method candidate : methods) {
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  2827
                if (!Modifier.isStatic(candidate.getModifiers())) {
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  2828
                    add(candidate);
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  2829
                }
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  2830
            }
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  2831
        }
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  2832
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
        int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
            return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
        Method get(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
            return methods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2841
        Method getFirst() {
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2842
            for (Method m : methods)
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2843
                if (m != null)
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2844
                    return m;
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2845
            return null;
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2846
        }
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2847
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2848
        void removeByNameAndDescriptor(Method toRemove) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
            for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
                Method m = methods[i];
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2851
                if (m != null && matchesNameAndDescriptor(m, toRemove)) {
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2852
                    remove(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2857
        private void remove(int i) {
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2858
            if (methods[i] != null && methods[i].isDefault())
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2859
                defaults--;
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2860
            methods[i] = null;
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2861
        }
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2862
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2863
        private boolean matchesNameAndDescriptor(Method m1, Method m2) {
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2864
            return m1.getReturnType() == m2.getReturnType() &&
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2865
                   m1.getName() == m2.getName() && // name is guaranteed to be interned
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2866
                   arrayContentsEq(m1.getParameterTypes(),
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2867
                           m2.getParameterTypes());
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2868
        }
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2869
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
        void compactAndTrim() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            int newPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
            // Get rid of null slots
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
            for (int pos = 0; pos < length; pos++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
                Method m = methods[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
                if (m != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
                    if (pos != newPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                        methods[newPos] = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
                    newPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
            if (newPos != methods.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
                methods = Arrays.copyOf(methods, newPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2887
        /* Removes all Methods from this MethodArray that have a more specific
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2888
         * default Method in this MethodArray.
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2889
         *
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2890
         * Users of MethodArray are responsible for pruning Methods that have
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2891
         * a more specific <em>concrete</em> Method.
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2892
         */
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2893
        void removeLessSpecifics() {
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2894
            if (!hasDefaults())
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2895
                return;
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2896
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2897
            for (int i = 0; i < length; i++) {
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2898
                Method m = get(i);
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2899
                if  (m == null || !m.isDefault())
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2900
                    continue;
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2901
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2902
                for (int j  = 0; j < length; j++) {
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2903
                    if (i == j)
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2904
                        continue;
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2905
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2906
                    Method candidate = get(j);
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2907
                    if (candidate == null)
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2908
                        continue;
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2909
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2910
                    if (!matchesNameAndDescriptor(m, candidate))
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2911
                        continue;
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2912
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2913
                    if (hasMoreSpecificClass(m, candidate))
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2914
                        remove(j);
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2915
                }
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2916
            }
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2917
        }
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2918
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
        Method[] getArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
            return methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
        }
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2922
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2923
        // Returns true if m1 is more specific than m2
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2924
        static boolean hasMoreSpecificClass(Method m1, Method m2) {
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2925
            Class<?> m1Class = m1.getDeclaringClass();
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2926
            Class<?> m2Class = m2.getDeclaringClass();
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2927
            return m1Class != m2Class && m2Class.isAssignableFrom(m1Class);
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2928
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
    // Returns an array of "root" methods. These Method objects must NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
    // be propagated to the outside world, but must instead be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
    // via ReflectionFactory.copyMethod.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
    private Method[] privateGetPublicMethods() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
        checkInitted();
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2937
        Method[] res;
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2938
        ReflectionData<T> rd = reflectionData();
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2939
        if (rd != null) {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2940
            res = rd.publicMethods;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
            if (res != null) return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
        // No cached value available; compute value recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
        // Start by fetching public declared methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
        MethodArray methods = new MethodArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
        {
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2948
            Method[] tmp = privateGetDeclaredMethods(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
            methods.addAll(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
        // Now recur over superclass and direct superinterfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
        // Go over superinterfaces first so we can more easily filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
        // out concrete implementations inherited from superclasses at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
        // the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
        MethodArray inheritedMethods = new MethodArray();
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  2956
        for (Class<?> i : getInterfaces()) {
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2957
            inheritedMethods.addInterfaceMethods(i.privateGetPublicMethods());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
        if (!isInterface()) {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  2960
            Class<?> c = getSuperclass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
                MethodArray supers = new MethodArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
                supers.addAll(c.privateGetPublicMethods());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
                // Filter out concrete implementations of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
                // interface methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
                for (int i = 0; i < supers.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
                    Method m = supers.get(i);
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2968
                    if (m != null &&
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2969
                            !Modifier.isAbstract(m.getModifiers()) &&
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2970
                            !m.isDefault()) {
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2971
                        inheritedMethods.removeByNameAndDescriptor(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
                // Insert superclass's inherited methods before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                // superinterfaces' to satisfy getMethod's search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                // order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                supers.addAll(inheritedMethods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
                inheritedMethods = supers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
        // Filter out all local methods from inherited ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
        for (int i = 0; i < methods.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
            Method m = methods.get(i);
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2984
            inheritedMethods.removeByNameAndDescriptor(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
        methods.addAllIfNotPresent(inheritedMethods);
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  2987
        methods.removeLessSpecifics();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
        methods.compactAndTrim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
        res = methods.getArray();
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2990
        if (rd != null) {
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  2991
            rd.publicMethods = res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
    // Helpers for fetchers of one field, method, or constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  3001
    private static Field searchFields(Field[] fields, String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
        String internedName = name.intern();
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  3003
        for (Field field : fields) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  3004
            if (field.getName() == internedName) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  3005
                return getReflectionFactory().copyField(field);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
    private Field getField0(String name) throws NoSuchFieldException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
        // Note: the intent is that the search algorithm this routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
        // uses be equivalent to the ordering imposed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
        // privateGetPublicFields(). It fetches only the declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
        // public fields for each class, however, to reduce the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
        // of Field objects which have to be created for the common
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
        // case where the field being requested is declared in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
        // class which is being queried.
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  3019
        Field res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
        // Search declared public fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
        if ((res = searchFields(privateGetDeclaredFields(true), name)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
        // Direct superinterfaces, recursively
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  3025
        Class<?>[] interfaces = getInterfaces();
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  3026
        for (Class<?> c : interfaces) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
            if ((res = c.getField0(name)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
        // Direct superclass, recursively
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
        if (!isInterface()) {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  3033
            Class<?> c = getSuperclass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
                if ((res = c.getField0(name)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
                    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
    private static Method searchMethods(Method[] methods,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
                                        String name,
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  3045
                                        Class<?>[] parameterTypes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
        Method res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
        String internedName = name.intern();
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 21970
diff changeset
  3049
        for (Method m : methods) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
            if (m.getName() == internedName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
                && arrayContentsEq(parameterTypes, m.getParameterTypes())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
                && (res == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
                    || res.getReturnType().isAssignableFrom(m.getReturnType())))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
                res = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
        return (res == null ? res : getReflectionFactory().copyMethod(res));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
21314
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  3060
    private Method getMethod0(String name, Class<?>[] parameterTypes, boolean includeStaticMethods) {
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3061
        MethodArray interfaceCandidates = new MethodArray(2);
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3062
        Method res =  privateGetMethodRecursive(name, parameterTypes, includeStaticMethods, interfaceCandidates);
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3063
        if (res != null)
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3064
            return res;
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3065
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3066
        // Not found on class or superclass directly
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3067
        interfaceCandidates.removeLessSpecifics();
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3068
        return interfaceCandidates.getFirst(); // may be null
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3069
    }
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3070
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3071
    private Method privateGetMethodRecursive(String name,
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3072
            Class<?>[] parameterTypes,
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3073
            boolean includeStaticMethods,
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3074
            MethodArray allInterfaceCandidates) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
        // Note: the intent is that the search algorithm this routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
        // uses be equivalent to the ordering imposed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
        // privateGetPublicMethods(). It fetches only the declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
        // public methods for each class, however, to reduce the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
        // number of Method objects which have to be created for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
        // common case where the method being requested is declared in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
        // the class which is being queried.
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3082
        //
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3083
        // Due to default methods, unless a method is found on a superclass,
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3084
        // methods declared in any superinterface needs to be considered.
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3085
        // Collect all candidates declared in superinterfaces in {@code
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3086
        // allInterfaceCandidates} and select the most specific if no match on
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3087
        // a superclass is found.
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3088
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3089
        // Must _not_ return root methods
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  3090
        Method res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
        // Search declared public methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
        if ((res = searchMethods(privateGetDeclaredMethods(true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
                                 name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
                                 parameterTypes)) != null) {
21314
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  3095
            if (includeStaticMethods || !Modifier.isStatic(res.getModifiers()))
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  3096
                return res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        // Search superclass's methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
        if (!isInterface()) {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  3100
            Class<? super T> c = getSuperclass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
            if (c != null) {
21314
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  3102
                if ((res = c.getMethod0(name, parameterTypes, true)) != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
                    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
        // Search superinterfaces' methods
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  3108
        Class<?>[] interfaces = getInterfaces();
21314
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  3109
        for (Class<?> c : interfaces)
1a616b8bdb31 8009411: (reflect) Class.getMethods should not include static methods from interfaces
jfranck
parents: 20853
diff changeset
  3110
            if ((res = c.getMethod0(name, parameterTypes, false)) != null)
24867
c3514175b6cd 8029674: (reflect) getMethods returns default methods that are not members of the class
jfranck
parents: 24865
diff changeset
  3111
                allInterfaceCandidates.add(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
        // Not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  3116
    private Constructor<T> getConstructor0(Class<?>[] parameterTypes,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
                                        int which) throws NoSuchMethodException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3119
        Constructor<T>[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC));
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3120
        for (Constructor<T> constructor : constructors) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
            if (arrayContentsEq(parameterTypes,
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3122
                                constructor.getParameterTypes())) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3123
                return getReflectionFactory().copyConstructor(constructor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
        throw new NoSuchMethodException(getName() + ".<init>" + argumentTypesToString(parameterTypes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
    // Other helpers and base implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
    private static boolean arrayContentsEq(Object[] a1, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
        if (a1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
            return a2 == null || a2.length == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
        if (a2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
            return a1.length == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
        if (a1.length != a2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
        for (int i = 0; i < a1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
            if (a1[i] != a2[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
    private static Field[] copyFields(Field[] arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
        Field[] out = new Field[arg.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
        ReflectionFactory fact = getReflectionFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
        for (int i = 0; i < arg.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
            out[i] = fact.copyField(arg[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
    private static Method[] copyMethods(Method[] arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
        Method[] out = new Method[arg.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
        ReflectionFactory fact = getReflectionFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
        for (int i = 0; i < arg.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
            out[i] = fact.copyMethod(arg[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3173
    private static <U> Constructor<U>[] copyConstructors(Constructor<U>[] arg) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3174
        Constructor<U>[] out = arg.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
        ReflectionFactory fact = getReflectionFactory();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3176
        for (int i = 0; i < out.length; i++) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3177
            out[i] = fact.copyConstructor(out[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
    private native Field[]       getDeclaredFields0(boolean publicOnly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
    private native Method[]      getDeclaredMethods0(boolean publicOnly);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3184
    private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  3185
    private native Class<?>[]   getDeclaredClasses0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  3187
    private static String        argumentTypesToString(Class<?>[] argTypes) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25991
diff changeset
  3188
        StringJoiner sj = new StringJoiner(", ", "(", ")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
        if (argTypes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
            for (int i = 0; i < argTypes.length; i++) {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  3191
                Class<?> c = argTypes[i];
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25991
diff changeset
  3192
                sj.add((c == null) ? "null" : c.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
        }
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25991
diff changeset
  3195
        return sj.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
    /** use serialVersionUID from JDK 1.1 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
    private static final long serialVersionUID = 3206093459760846163L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
     * Class Class is special cased within the Serialization Stream Protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
     * A Class instance is written initially into an ObjectOutputStream in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
     * following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
     *      {@code TC_CLASS} ClassDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
     *      A ClassDescriptor is a special cased serialization of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
     *      a {@code java.io.ObjectStreamClass} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
     * A new handle is generated for the initial time the class descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
     * is written into the stream. Future references to the class descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
     * are written as references to the initial class descriptor instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
     * @see java.io.ObjectStreamClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
    private static final ObjectStreamField[] serialPersistentFields =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
        new ObjectStreamField[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
     * Returns the assertion status that would be assigned to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
     * class if it were to be initialized at the time this method is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
     * If this class has had its assertion status set, the most recent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
     * setting will be returned; otherwise, if any package default assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
     * status pertains to this class, the most recent setting for the most
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
     * specific pertinent package default assertion status is returned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
     * otherwise, if this class is not a system class (i.e., it has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
     * class loader) its class loader's default assertion status is returned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
     * otherwise, the system class default assertion status is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
     * Few programmers will have any need for this method; it is provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
     * for the benefit of the JRE itself.  (It allows a class to determine at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
     * the time that it is initialized whether assertions should be enabled.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
     * Note that this method is not guaranteed to return the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
     * assertion status that was (or will be) associated with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
     * class when it was (or will be) initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
     * @return the desired assertion status of the specified class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
     * @see    java.lang.ClassLoader#setClassAssertionStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
     * @see    java.lang.ClassLoader#setPackageAssertionStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
     * @see    java.lang.ClassLoader#setDefaultAssertionStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
    public boolean desiredAssertionStatus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
        ClassLoader loader = getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
        // If the loader is null this is a system class, so ask the VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
        if (loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
            return desiredAssertionStatus0(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  3252
        // If the classloader has been initialized with the assertion
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  3253
        // directives, ask it. Otherwise, ask the VM.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  3254
        synchronized(loader.assertionLock) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  3255
            if (loader.classAssertionStatus != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  3256
                return loader.desiredAssertionStatus(getName());
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  3257
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
        }
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  3259
        return desiredAssertionStatus0(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
    // Retrieves the desired assertion status of this class from the VM
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  3263
    private static native boolean desiredAssertionStatus0(Class<?> clazz);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
     * Returns true if and only if this class was declared as an enum in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
     * source code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
     * @return true if and only if this class was declared as an enum in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
     *     source code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
    public boolean isEnum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
        // An enum must both directly extend java.lang.Enum and have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
        // the ENUM bit set; classes for specialized enum constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
        // don't do the former.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
        return (this.getModifiers() & ENUM) != 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
        this.getSuperclass() == java.lang.Enum.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
    // Fetches the factory for reflective objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
    private static ReflectionFactory getReflectionFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
        if (reflectionFactory == null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3284
            reflectionFactory =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
                java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
                    (new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
        return reflectionFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
    private static ReflectionFactory reflectionFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
    // To be able to query system properties as soon as they're available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
    private static boolean initted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
    private static void checkInitted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
        if (initted) return;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3296
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3297
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
                    // Tests to ensure the system properties table is fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
                    // initialized. This is needed because reflection code is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
                    // called very early in the initialization process (before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
                    // command-line arguments have been parsed and therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
                    // these user-settable properties installed.) We assume that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
                    // if System.out is non-null then the System class has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
                    // fully initialized and that the bulk of the startup code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
                    // has been run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
                    if (System.out == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
                        // java.lang.System not yet fully initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
10142
43c81972df7b 7073296: Executable.equalParamTypes() incorrectly returns true when the number of params differs.
mduigou
parents: 9275
diff changeset
  3312
                    // Doesn't use Boolean.getBoolean to avoid class init.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
                    String val =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
                        System.getProperty("sun.reflect.noCaches");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
                    if (val != null && val.equals("true")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
                        useCaches = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
                    initted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
     * Returns the elements of this enum class or null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
     * Class object does not represent an enum type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
     * @return an array containing the values comprising the enum class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
     *     represented by this Class object in the order they're
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
     *     declared, or null if this Class object does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
     *     represent an enum type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
    public T[] getEnumConstants() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
        T[] values = getEnumConstantsShared();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
        return (values != null) ? values.clone() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
     * Returns the elements of this enum class or null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
     * Class object does not represent an enum type;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3343
     * identical to getEnumConstants except that the result is
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3344
     * uncloned, cached, and shared by all callers.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
    T[] getEnumConstantsShared() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
        if (enumConstants == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
            if (!isEnum()) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
                final Method values = getMethod("values");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3351
                java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3352
                    new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3353
                        public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
                                values.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
                        });
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
  3358
                @SuppressWarnings("unchecked")
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
  3359
                T[] temporaryConstants = (T[])values.invoke(null);
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
  3360
                enumConstants = temporaryConstants;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
            // These can happen when users concoct enum-like classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
            // that don't comply with the enum spec.
10147
cd5c99f94305 6380161: (reflect) Exception from newInstance() not chained to cause.
darcy
parents: 10142
diff changeset
  3364
            catch (InvocationTargetException | NoSuchMethodException |
cd5c99f94305 6380161: (reflect) Exception from newInstance() not chained to cause.
darcy
parents: 10142
diff changeset
  3365
                   IllegalAccessException ex) { return null; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
        return enumConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
    private volatile transient T[] enumConstants = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
     * Returns a map from simple name to enum constant.  This package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
     * method is used internally by Enum to implement
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13589
diff changeset
  3374
     * {@code public static <T extends Enum<T>> T valueOf(Class<T>, String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
     * efficiently.  Note that the map is returned by this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
     * created lazily on first use.  Typically it won't ever get created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
    Map<String, T> enumConstantDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
        if (enumConstantDirectory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
            T[] universe = getEnumConstantsShared();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
            if (universe == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
                    getName() + " is not an enum type");
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  3384
            Map<String, T> m = new HashMap<>(2 * universe.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
            for (T constant : universe)
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 3845
diff changeset
  3386
                m.put(((Enum<?>)constant).name(), constant);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
            enumConstantDirectory = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
        return enumConstantDirectory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
    private volatile transient Map<String, T> enumConstantDirectory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
     * Casts an object to the class or interface represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
     * by this {@code Class} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
     * @param obj the object to be cast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
     * @return the object after casting, or null if obj is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
     * @throws ClassCastException if the object is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
     * null and is not assignable to the type T.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
     */
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
  3405
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
    public T cast(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
        if (obj != null && !isInstance(obj))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
            throw new ClassCastException(cannotCastMsg(obj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
        return (T) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
    private String cannotCastMsg(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
        return "Cannot cast " + obj.getClass().getName() + " to " + getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
     * Casts this {@code Class} object to represent a subclass of the class
11016
e2665f4ac6d2 7110111: Minor Java SE javadoc & Constructor clean up
lancea
parents: 10419
diff changeset
  3418
     * represented by the specified class object.  Checks that the cast
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
     * is valid, and throws a {@code ClassCastException} if it is not.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
     * this method succeeds, it always returns a reference to this class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
     * <p>This method is useful when a client needs to "narrow" the type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
     * a {@code Class} object to pass it to an API that restricts the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
     * {@code Class} objects that it is willing to accept.  A cast would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
     * generate a compile-time warning, as the correctness of the cast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
     * could not be checked at runtime (because generic types are implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
     * by erasure).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18276
diff changeset
  3429
     * @param <U> the type to cast this class object to
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18276
diff changeset
  3430
     * @param clazz the class of the type to cast this class object to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
     * @return this {@code Class} object, cast to represent a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
     *    the specified class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
     * @throws ClassCastException if this {@code Class} object does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
     *    represent a subclass of the specified class (here "subclass" includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
     *    the class itself).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
     */
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11016
diff changeset
  3438
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
    public <U> Class<? extends U> asSubclass(Class<U> clazz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
        if (clazz.isAssignableFrom(this))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
            return (Class<? extends U>) this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
            throw new ClassCastException(this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
     */
15511
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 15510
diff changeset
  3450
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
    public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3452
        Objects.requireNonNull(annotationClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3454
        return (A) annotationData().annotations.get(annotationClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3457
    /**
16051
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
  3458
     * {@inheritDoc}
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
  3459
     * @throws NullPointerException {@inheritDoc}
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
  3460
     * @since 1.5
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
  3461
     */
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
  3462
    @Override
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
  3463
    public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
17710
ce8517f5a2fe 8014836: Have GenericDeclaration extend AnnotatedElement
darcy
parents: 16906
diff changeset
  3464
        return GenericDeclaration.super.isAnnotationPresent(annotationClass);
16051
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
  3465
    }
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
  3466
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
  3467
    /**
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3468
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3469
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3470
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
  3471
    @Override
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
  3472
    public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3473
        Objects.requireNonNull(annotationClass);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3474
21316
ca0a7cd228c9 8004912: Repeating annotations - getAnnotationsByType(Class<T>) is not working as expected for few inheritance scenarios
alundblad
parents: 21314
diff changeset
  3475
        AnnotationData annotationData = annotationData();
ca0a7cd228c9 8004912: Repeating annotations - getAnnotationsByType(Class<T>) is not working as expected for few inheritance scenarios
alundblad
parents: 21314
diff changeset
  3476
        return AnnotationSupport.getAssociatedAnnotations(annotationData.declaredAnnotations,
21358
d41ff832d4f6 8027170: Annotations declared on super-super-class should be overridden by super-class.
alundblad
parents: 21334
diff changeset
  3477
                                                          this,
21316
ca0a7cd228c9 8004912: Repeating annotations - getAnnotationsByType(Class<T>) is not working as expected for few inheritance scenarios
alundblad
parents: 21314
diff changeset
  3478
                                                          annotationClass);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3479
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
    public Annotation[] getAnnotations() {
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3485
        return AnnotationParser.toArray(annotationData().annotations);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3486
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3487
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3488
    /**
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3489
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3490
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3491
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
  3492
    @Override
15511
8f45487ac694 8005712: Simplify support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 15510
diff changeset
  3493
    @SuppressWarnings("unchecked")
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3494
    public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) {
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3495
        Objects.requireNonNull(annotationClass);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3496
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3497
        return (A) annotationData().declaredAnnotations.get(annotationClass);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3498
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3499
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3500
    /**
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3501
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3502
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3503
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
  3504
    @Override
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
  3505
    public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationClass) {
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3506
        Objects.requireNonNull(annotationClass);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
  3507
21316
ca0a7cd228c9 8004912: Repeating annotations - getAnnotationsByType(Class<T>) is not working as expected for few inheritance scenarios
alundblad
parents: 21314
diff changeset
  3508
        return AnnotationSupport.getDirectlyAndIndirectlyPresent(annotationData().declaredAnnotations,
ca0a7cd228c9 8004912: Repeating annotations - getAnnotationsByType(Class<T>) is not working as expected for few inheritance scenarios
alundblad
parents: 21314
diff changeset
  3509
                                                                 annotationClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
    public Annotation[] getDeclaredAnnotations()  {
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3516
        return AnnotationParser.toArray(annotationData().declaredAnnotations);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3519
    // annotation data that might get invalidated when JVM TI RedefineClasses() is called
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3520
    private static class AnnotationData {
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3521
        final Map<Class<? extends Annotation>, Annotation> annotations;
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3522
        final Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  3523
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3524
        // Value of classRedefinedCount when we created this AnnotationData instance
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3525
        final int redefinedCount;
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3526
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3527
        AnnotationData(Map<Class<? extends Annotation>, Annotation> annotations,
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3528
                       Map<Class<? extends Annotation>, Annotation> declaredAnnotations,
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3529
                       int redefinedCount) {
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3530
            this.annotations = annotations;
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3531
            this.declaredAnnotations = declaredAnnotations;
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3532
            this.redefinedCount = redefinedCount;
15259
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  3533
        }
33fec5f9630b 8005232: (JEP-149) Class Instance size reduction
dholmes
parents: 14910
diff changeset
  3534
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3536
    // Annotations cache
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3537
    @SuppressWarnings("UnusedDeclaration")
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3538
    private volatile transient AnnotationData annotationData;
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3539
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3540
    private AnnotationData annotationData() {
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3541
        while (true) { // retry loop
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3542
            AnnotationData annotationData = this.annotationData;
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3543
            int classRedefinedCount = this.classRedefinedCount;
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3544
            if (annotationData != null &&
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3545
                annotationData.redefinedCount == classRedefinedCount) {
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3546
                return annotationData;
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3547
            }
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3548
            // null or stale annotationData -> optimistically create new instance
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3549
            AnnotationData newAnnotationData = createAnnotationData(classRedefinedCount);
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3550
            // try to install it
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3551
            if (Atomic.casAnnotationData(this, annotationData, newAnnotationData)) {
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3552
                // successfully installed new AnnotationData
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3553
                return newAnnotationData;
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3554
            }
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3555
        }
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3556
    }
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3557
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3558
    private AnnotationData createAnnotationData(int classRedefinedCount) {
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3559
        Map<Class<? extends Annotation>, Annotation> declaredAnnotations =
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3560
            AnnotationParser.parseAnnotations(getRawAnnotations(), getConstantPool(), this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
        Class<?> superClass = getSuperclass();
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3562
        Map<Class<? extends Annotation>, Annotation> annotations = null;
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3563
        if (superClass != null) {
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3564
            Map<Class<? extends Annotation>, Annotation> superAnnotations =
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3565
                superClass.annotationData().annotations;
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3566
            for (Map.Entry<Class<? extends Annotation>, Annotation> e : superAnnotations.entrySet()) {
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3567
                Class<? extends Annotation> annotationClass = e.getKey();
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3568
                if (AnnotationType.getInstance(annotationClass).isInherited()) {
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3569
                    if (annotations == null) { // lazy construction
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3570
                        annotations = new LinkedHashMap<>((Math.max(
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3571
                                declaredAnnotations.size(),
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3572
                                Math.min(12, declaredAnnotations.size() + superAnnotations.size())
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3573
                            ) * 4 + 2) / 3
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3574
                        );
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3575
                    }
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3576
                    annotations.put(annotationClass, e.getValue());
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3577
                }
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3578
            }
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3579
        }
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3580
        if (annotations == null) {
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3581
            // no inherited annotations -> share the Map with declaredAnnotations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
            annotations = declaredAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
        } else {
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3584
            // at least one inherited annotation -> declared may override inherited
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
            annotations.putAll(declaredAnnotations);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
        }
20180
0dedfb3744f2 8011940: java.lang.Class.getAnnotations() always enters synchronized method
plevart
parents: 19834
diff changeset
  3587
        return new AnnotationData(annotations, declaredAnnotations, classRedefinedCount);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
    // Annotation types cache their internal (AnnotationType) form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  3592
    @SuppressWarnings("UnusedDeclaration")
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  3593
    private volatile transient AnnotationType annotationType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  3595
    boolean casAnnotationType(AnnotationType oldType, AnnotationType newType) {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents: 18766
diff changeset
  3596
        return Atomic.casAnnotationType(this, oldType, newType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
    AnnotationType getAnnotationType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
        return annotationType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
    }
11536
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11275
diff changeset
  3602
21358
d41ff832d4f6 8027170: Annotations declared on super-super-class should be overridden by super-class.
alundblad
parents: 21334
diff changeset
  3603
    Map<Class<? extends Annotation>, Annotation> getDeclaredAnnotationMap() {
d41ff832d4f6 8027170: Annotations declared on super-super-class should be overridden by super-class.
alundblad
parents: 21334
diff changeset
  3604
        return annotationData().declaredAnnotations;
d41ff832d4f6 8027170: Annotations declared on super-super-class should be overridden by super-class.
alundblad
parents: 21334
diff changeset
  3605
    }
d41ff832d4f6 8027170: Annotations declared on super-super-class should be overridden by super-class.
alundblad
parents: 21334
diff changeset
  3606
11536
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11275
diff changeset
  3607
    /* Backing store of user-defined values pertaining to this class.
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11275
diff changeset
  3608
     * Maintained by the ClassValue class.
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11275
diff changeset
  3609
     */
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11275
diff changeset
  3610
    transient ClassValue.ClassValueMap classValueMap;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3611
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3612
    /**
20481
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3613
     * Returns an {@code AnnotatedType} object that represents the use of a
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3614
     * type to specify the superclass of the entity represented by this {@code
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3615
     * Class} object. (The <em>use</em> of type Foo to specify the superclass
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3616
     * in '...  extends Foo' is distinct from the <em>declaration</em> of type
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3617
     * Foo.)
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3618
     *
20481
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3619
     * <p> If this {@code Class} object represents a type whose declaration
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3620
     * does not explicitly indicate an annotated superclass, then the return
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3621
     * value is an {@code AnnotatedType} object representing an element with no
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3622
     * annotations.
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3623
     *
20481
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3624
     * <p> If this {@code Class} represents either the {@code Object} class, an
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3625
     * interface type, an array type, a primitive type, or void, the return
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3626
     * value is {@code null}.
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3627
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18276
diff changeset
  3628
     * @return an object representing the superclass
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3629
     * @since 1.8
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3630
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3631
    public AnnotatedType getAnnotatedSuperclass() {
19588
5f7c7cdd74e9 8022343: j.l.Class.getAnnotatedSuperclass() doesn't return null in some cases
jfranck
parents: 19031
diff changeset
  3632
        if (this == Object.class ||
5f7c7cdd74e9 8022343: j.l.Class.getAnnotatedSuperclass() doesn't return null in some cases
jfranck
parents: 19031
diff changeset
  3633
                isInterface() ||
5f7c7cdd74e9 8022343: j.l.Class.getAnnotatedSuperclass() doesn't return null in some cases
jfranck
parents: 19031
diff changeset
  3634
                isArray() ||
5f7c7cdd74e9 8022343: j.l.Class.getAnnotatedSuperclass() doesn't return null in some cases
jfranck
parents: 19031
diff changeset
  3635
                isPrimitive() ||
5f7c7cdd74e9 8022343: j.l.Class.getAnnotatedSuperclass() doesn't return null in some cases
jfranck
parents: 19031
diff changeset
  3636
                this == Void.TYPE) {
5f7c7cdd74e9 8022343: j.l.Class.getAnnotatedSuperclass() doesn't return null in some cases
jfranck
parents: 19031
diff changeset
  3637
            return null;
5f7c7cdd74e9 8022343: j.l.Class.getAnnotatedSuperclass() doesn't return null in some cases
jfranck
parents: 19031
diff changeset
  3638
        }
5f7c7cdd74e9 8022343: j.l.Class.getAnnotatedSuperclass() doesn't return null in some cases
jfranck
parents: 19031
diff changeset
  3639
5f7c7cdd74e9 8022343: j.l.Class.getAnnotatedSuperclass() doesn't return null in some cases
jfranck
parents: 19031
diff changeset
  3640
        return TypeAnnotationParser.buildAnnotatedSuperclass(getRawTypeAnnotations(), getConstantPool(), this);
5f7c7cdd74e9 8022343: j.l.Class.getAnnotatedSuperclass() doesn't return null in some cases
jfranck
parents: 19031
diff changeset
  3641
    }
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3642
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3643
    /**
20481
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3644
     * Returns an array of {@code AnnotatedType} objects that represent the use
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3645
     * of types to specify superinterfaces of the entity represented by this
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3646
     * {@code Class} object. (The <em>use</em> of type Foo to specify a
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3647
     * superinterface in '... implements Foo' is distinct from the
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3648
     * <em>declaration</em> of type Foo.)
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3649
     *
20481
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3650
     * <p> If this {@code Class} object represents a class, the return value is
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3651
     * an array containing objects representing the uses of interface types to
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3652
     * specify interfaces implemented by the class. The order of the objects in
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3653
     * the array corresponds to the order of the interface types used in the
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3654
     * 'implements' clause of the declaration of this {@code Class} object.
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3655
     *
20481
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3656
     * <p> If this {@code Class} object represents an interface, the return
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3657
     * value is an array containing objects representing the uses of interface
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3658
     * types to specify interfaces directly extended by the interface. The
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3659
     * order of the objects in the array corresponds to the order of the
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3660
     * interface types used in the 'extends' clause of the declaration of this
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3661
     * {@code Class} object.
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3662
     *
20481
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3663
     * <p> If this {@code Class} object represents a class or interface whose
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3664
     * declaration does not explicitly indicate any annotated superinterfaces,
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3665
     * the return value is an array of length 0.
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3666
     *
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3667
     * <p> If this {@code Class} object represents either the {@code Object}
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 20180
diff changeset
  3668
     * class, an array type, a primitive type, or void, the return value is an
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3669
     * array of length 0.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3670
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18276
diff changeset
  3671
     * @return an array representing the superinterfaces
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3672
     * @since 1.8
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3673
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3674
    public AnnotatedType[] getAnnotatedInterfaces() {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3675
         return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3676
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 15259
diff changeset
  3677
}