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