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