forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModifier.java
507 lines (446 loc) · 17.7 KB
/
Modifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang.reflect;
import jdk.internal.reflect.ReflectionFactory;
import java.security.AccessController;
import java.util.StringJoiner;
/**
* The Modifier class provides {@code static} methods and
* constants to decode class and member access modifiers. The sets of
* modifiers are represented as integers with distinct bit positions
* representing different modifiers. The values for the constants
* representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of
* <cite>The Java™ Virtual Machine Specification</cite>.
*
* @see Class#getModifiers()
* @see Member#getModifiers()
*
* @author Nakul Saraiya
* @author Kenneth Russell
* @since 1.1
*/
// 类/接口/构造器/方法/字段上的修饰符
public class Modifier {
/**
* The {@code int} value representing the {@code public} modifier.
*/
public static final int PUBLIC = 0x00000001; // public
/**
* The {@code int} value representing the {@code private} modifier.
*/
public static final int PRIVATE = 0x00000002; // private
/**
* The {@code int} value representing the {@code protected} modifier.
*/
public static final int PROTECTED = 0x00000004; // protected
/**
* The {@code int} value representing the {@code static} modifier.
*/
public static final int STATIC = 0x00000008; // static
/**
* The {@code int} value representing the {@code final} modifier.
*/
public static final int FINAL = 0x00000010; // final
/**
* The {@code int} value representing the {@code synchronized} modifier.
*/
public static final int SYNCHRONIZED = 0x00000020; // synchronized
/**
* The {@code int} value representing the {@code volatile} modifier.
*/
public static final int VOLATILE = 0x00000040; // volatile
/**
* The {@code int} value representing the {@code transient} modifier.
*/
public static final int TRANSIENT = 0x00000080; // transient
/**
* The {@code int} value representing the {@code native} modifier.
*/
public static final int NATIVE = 0x00000100; // native
/**
* The {@code int} value representing the {@code interface} modifier.
*/
public static final int INTERFACE = 0x00000200; // interface
/**
* The {@code int} value representing the {@code abstract} modifier.
*/
public static final int ABSTRACT = 0x00000400; // abstract
/**
* The {@code int} value representing the {@code strictfp} modifier.
*/
public static final int STRICT = 0x00000800; // strictfp
// Bits not (yet) exposed in the public API either because they
// have different meanings for fields and methods and there is no
// way to distinguish between the two in this class, or because
// they are not Java programming language keywords
static final int BRIDGE = 0x00000040;
static final int VARARGS = 0x00000080;
static final int SYNTHETIC = 0x00001000;
static final int ANNOTATION = 0x00002000;
static final int ENUM = 0x00004000;
static final int MANDATED = 0x00008000;
/* Bootstrapping protocol between java.lang and java.lang.reflect packages */
static {
ReflectionFactory factory = AccessController.doPrivileged(new ReflectionFactory.GetReflectionFactoryAction());
factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess());
}
/*▼ 判断是否存在某种修饰符 ███████████████████████████████████████████████████████████████┓ */
/**
* Return {@code true} if the integer argument includes the
* {@code public} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code public} modifier; {@code false} otherwise.
*/
public static boolean isPublic(int mod) {
return (mod & PUBLIC) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code private} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code private} modifier; {@code false} otherwise.
*/
public static boolean isPrivate(int mod) {
return (mod & PRIVATE) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code protected} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code protected} modifier; {@code false} otherwise.
*/
public static boolean isProtected(int mod) {
return (mod & PROTECTED) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code static} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code static} modifier; {@code false} otherwise.
*/
public static boolean isStatic(int mod) {
return (mod & STATIC) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code final} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code final} modifier; {@code false} otherwise.
*/
public static boolean isFinal(int mod) {
return (mod & FINAL) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code synchronized} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code synchronized} modifier; {@code false} otherwise.
*/
public static boolean isSynchronized(int mod) {
return (mod & SYNCHRONIZED) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code volatile} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code volatile} modifier; {@code false} otherwise.
*/
public static boolean isVolatile(int mod) {
return (mod & VOLATILE) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code transient} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code transient} modifier; {@code false} otherwise.
*/
public static boolean isTransient(int mod) {
return (mod & TRANSIENT) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code native} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code native} modifier; {@code false} otherwise.
*/
public static boolean isNative(int mod) {
return (mod & NATIVE) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code interface} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code interface} modifier; {@code false} otherwise.
*/
public static boolean isInterface(int mod) {
return (mod & INTERFACE) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code abstract} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code abstract} modifier; {@code false} otherwise.
*/
public static boolean isAbstract(int mod) {
return (mod & ABSTRACT) != 0;
}
/**
* Return {@code true} if the integer argument includes the
* {@code strictfp} modifier, {@code false} otherwise.
*
* @param mod a set of modifiers
* @return {@code true} if {@code mod} includes the
* {@code strictfp} modifier; {@code false} otherwise.
*/
public static boolean isStrict(int mod) {
return (mod & STRICT) != 0;
}
static boolean isSynthetic(int mod) {
return (mod & SYNTHETIC) != 0;
}
static boolean isMandated(int mod) {
return (mod & MANDATED) != 0;
}
/*▲ 判断是否存在某种修饰符 ███████████████████████████████████████████████████████████████┛ */
// Note on the FOO_MODIFIERS fields and fooModifiers() methods:
// the sets of modifiers are not guaranteed to be constants
// across time and Java SE releases. Therefore, it would not be
// appropriate to expose an external interface to this information
// that would allow the values to be treated as Java-level
// constants since the values could be constant folded and updates
// to the sets of modifiers missed. Thus, the fooModifiers()
// methods return an unchanging values for a given release, but a
// value that can potentially change over time.
/**
* The Java source modifiers that can be applied to a class.
* @jls 8.1.1 Class Modifiers
*/
// 类修饰符
private static final int CLASS_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
Modifier.STRICT;
/**
* The Java source modifiers that can be applied to an interface.
* @jls 9.1.1 Interface Modifiers
*/
// 接口修饰符
private static final int INTERFACE_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT;
/**
* The Java source modifiers that can be applied to a constructor.
* @jls 8.8.3 Constructor Modifiers
*/
// 构造器修饰符
private static final int CONSTRUCTOR_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
/**
* The Java source modifiers that can be applied to a method.
* @jls8.4.3 Method Modifiers
*/
// 方法修饰符
private static final int METHOD_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT;
/**
* The Java source modifiers that can be applied to a field.
* @jls 8.3.1 Field Modifiers
*/
// 字段修饰符
private static final int FIELD_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT |
Modifier.VOLATILE;
/**
* The Java source modifiers that can be applied to a method or constructor parameter.
* @jls 8.4.1 Formal Parameters
*/
// 形参修饰符
private static final int PARAMETER_MODIFIERS =
Modifier.FINAL;
/**
*
*/
// 访问控制修饰符
static final int ACCESS_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a class.
*
* @return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a class.
*
* @jls 8.1.1 Class Modifiers
* @since 1.7
*/
// 返回类修饰符
public static int classModifiers() {
return CLASS_MODIFIERS;
}
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to an interface.
*
* @return an {@code int} value OR-ing together the source language
* modifiers that can be applied to an interface.
*
* @jls 9.1.1 Interface Modifiers
* @since 1.7
*/
// 返回接口修饰符
public static int interfaceModifiers() {
return INTERFACE_MODIFIERS;
}
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a constructor.
*
* @return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a constructor.
*
* @jls 8.8.3 Constructor Modifiers
* @since 1.7
*/
// 返回构造器修饰符
public static int constructorModifiers() {
return CONSTRUCTOR_MODIFIERS;
}
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a method.
*
* @return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a method.
*
* @jls 8.4.3 Method Modifiers
* @since 1.7
*/
// 返回方法修饰符
public static int methodModifiers() {
return METHOD_MODIFIERS;
}
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a field.
*
* @return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a field.
*
* @jls 8.3.1 Field Modifiers
* @since 1.7
*/
// 返回字段修饰符
public static int fieldModifiers() {
return FIELD_MODIFIERS;
}
/**
* Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a parameter.
*
* @return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a parameter.
*
* @jls 8.4.1 Formal Parameters
* @since 1.8
*/
// 返回形参修饰符
public static int parameterModifiers() {
return PARAMETER_MODIFIERS;
}
/**
* Return a string describing the access modifier flags in
* the specified modifier. For example:
* <blockquote><pre>
* public final synchronized strictfp
* </pre></blockquote>
* The modifier names are returned in an order consistent with the
* suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
* <cite>The Java™ Language Specification</cite>.
* The full modifier ordering used by this method is:
* <blockquote> {@code
* public protected private abstract static final transient
* volatile synchronized native strictfp
* interface } </blockquote>
* The {@code interface} modifier discussed in this class is
* not a true modifier in the Java language and it appears after
* all other modifiers listed by this method. This method may
* return a string of modifiers that are not valid modifiers of a
* Java entity; in other words, no checking is done on the
* possible validity of the combination of modifiers represented
* by the input.
*
* Note that to perform such checking for a known kind of entity,
* such as a constructor or method, first AND the argument of
* {@code toString} with the appropriate mask from a method like
* {@link #constructorModifiers} or {@link #methodModifiers}.
*
* @param mod a set of modifiers
* @return a string representation of the set of modifiers
* represented by {@code mod}
*/
// 字符串化
public static String toString(int mod) {
StringJoiner sj = new StringJoiner(" ");
if ((mod & PUBLIC) != 0) sj.add("public");
if ((mod & PROTECTED) != 0) sj.add("protected");
if ((mod & PRIVATE) != 0) sj.add("private");
/* Canonical order */
if ((mod & ABSTRACT) != 0) sj.add("abstract");
if ((mod & STATIC) != 0) sj.add("static");
if ((mod & FINAL) != 0) sj.add("final");
if ((mod & TRANSIENT) != 0) sj.add("transient");
if ((mod & VOLATILE) != 0) sj.add("volatile");
if ((mod & SYNCHRONIZED) != 0) sj.add("synchronized");
if ((mod & NATIVE) != 0) sj.add("native");
if ((mod & STRICT) != 0) sj.add("strictfp");
if ((mod & INTERFACE) != 0) sj.add("interface");
return sj.toString();
}
}