1 /*
2 * Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25
26 package java.lang.invoke;
27
28 import sun.invoke.util.VerifyAccess;
29
30 import java.lang.reflect.Constructor;
31 import java.lang.reflect.Field;
32 import java.lang.reflect.Member;
33 import java.lang.reflect.Method;
34 import java.lang.reflect.Modifier;
35 import java.util.Objects;
36
37 import static java.lang.invoke.MethodHandleNatives.Constants.*;
38 import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
39 import static java.lang.invoke.MethodHandleStatics.newInternalError;
40
41 /**
42 * A {@code MemberName} is a compact symbolic datum which fully characterizes
43 * a method or field reference.
44 * A member name refers to a field, method, constructor, or member type.
45 * Every member name has a simple name (a string) and a type (either a Class or MethodType).
46 * A member name may also have a non-null declaring class, or it may be simply
47 * a naked name/type pair.
48 * A member name may also have non-zero modifier flags.
49 * Finally, a member name may be either resolved or unresolved.
371
372 /** Utility method to query the modifier flags of this member. */
373 public boolean isStatic() {
374 return Modifier.isStatic(flags);
375 }
376 /** Utility method to query the modifier flags of this member. */
377 public boolean isPublic() {
378 return Modifier.isPublic(flags);
379 }
380 /** Utility method to query the modifier flags of this member. */
381 public boolean isPrivate() {
382 return Modifier.isPrivate(flags);
383 }
384 /** Utility method to query the modifier flags of this member. */
385 public boolean isProtected() {
386 return Modifier.isProtected(flags);
387 }
388 /** Utility method to query the modifier flags of this member. */
389 public boolean isFinal() {
390 return Modifier.isFinal(flags);
391 }
392 /** Utility method to query whether this member or its defining class is final. */
393 public boolean canBeStaticallyBound() {
394 return Modifier.isFinal(flags | clazz.getModifiers());
395 }
396 /** Utility method to query the modifier flags of this member. */
397 public boolean isVolatile() {
398 return Modifier.isVolatile(flags);
399 }
400 /** Utility method to query the modifier flags of this member. */
401 public boolean isAbstract() {
402 return Modifier.isAbstract(flags);
403 }
404 /** Utility method to query the modifier flags of this member. */
405 public boolean isNative() {
406 return Modifier.isNative(flags);
407 }
408 // let the rest (native, volatile, transient, etc.) be tested via Modifier.isFoo
409
410 // unofficial modifier flags, used by HotSpot:
411 static final int BRIDGE = 0x00000040;
412 static final int VARARGS = 0x00000080;
413 static final int SYNTHETIC = 0x00001000;
414 static final int ANNOTATION = 0x00002000;
415 static final int ENUM = 0x00004000;
416
417 /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
418 public boolean isBridge() {
419 return allFlagsSet(IS_METHOD | BRIDGE);
420 }
421 /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
422 public boolean isVarargs() {
423 return allFlagsSet(VARARGS) && isInvocable();
424 }
425 /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
426 public boolean isSynthetic() {
427 return allFlagsSet(SYNTHETIC);
428 }
429
430 static final String CONSTRUCTOR_NAME = "<init>";
431
432 // modifiers exported by the JVM:
433 static final int RECOGNIZED_MODIFIERS = 0xFFFF;
434
435 // private flags, not part of RECOGNIZED_MODIFIERS:
436 static final int
437 IS_METHOD = MN_IS_METHOD, // method (not constructor)
438 IS_CONSTRUCTOR = MN_IS_CONSTRUCTOR, // constructor
439 IS_FIELD = MN_IS_FIELD, // field
440 IS_TYPE = MN_IS_TYPE, // nested type
441 CALLER_SENSITIVE = MN_CALLER_SENSITIVE, // @CallerSensitive annotation detected
442 TRUSTED_FINAL = MN_TRUSTED_FINAL; // trusted final field
443
444 static final int ALL_ACCESS = Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED;
445 static final int ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE;
446 static final int IS_INVOCABLE = IS_METHOD | IS_CONSTRUCTOR;
447
448 /** Utility method to query whether this member is a method or constructor. */
449 public boolean isInvocable() {
|
1 /*
2 * Copyright (c) 2008, 2026, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25
26 package java.lang.invoke;
27
28 import sun.invoke.util.VerifyAccess;
29
30 import java.lang.classfile.ClassFile;
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.Field;
33 import java.lang.reflect.Member;
34 import java.lang.reflect.Method;
35 import java.lang.reflect.Modifier;
36 import java.util.Objects;
37
38 import static java.lang.invoke.MethodHandleNatives.Constants.*;
39 import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
40 import static java.lang.invoke.MethodHandleStatics.newInternalError;
41
42 /**
43 * A {@code MemberName} is a compact symbolic datum which fully characterizes
44 * a method or field reference.
45 * A member name refers to a field, method, constructor, or member type.
46 * Every member name has a simple name (a string) and a type (either a Class or MethodType).
47 * A member name may also have a non-null declaring class, or it may be simply
48 * a naked name/type pair.
49 * A member name may also have non-zero modifier flags.
50 * Finally, a member name may be either resolved or unresolved.
372
373 /** Utility method to query the modifier flags of this member. */
374 public boolean isStatic() {
375 return Modifier.isStatic(flags);
376 }
377 /** Utility method to query the modifier flags of this member. */
378 public boolean isPublic() {
379 return Modifier.isPublic(flags);
380 }
381 /** Utility method to query the modifier flags of this member. */
382 public boolean isPrivate() {
383 return Modifier.isPrivate(flags);
384 }
385 /** Utility method to query the modifier flags of this member. */
386 public boolean isProtected() {
387 return Modifier.isProtected(flags);
388 }
389 /** Utility method to query the modifier flags of this member. */
390 public boolean isFinal() {
391 return Modifier.isFinal(flags);
392 }
393 /** Utility method to query the ACC_STRICT_INIT flag of this member. */
394 public boolean isStrictInit() {
395 return (flags & ClassFile.ACC_STRICT_INIT) != 0;
396 }
397 /** Utility method to query whether this member or its defining class is final. */
398 public boolean canBeStaticallyBound() {
399 return Modifier.isFinal(flags | clazz.getModifiers());
400 }
401 /** Utility method to query the modifier flags of this member. */
402 public boolean isVolatile() {
403 return Modifier.isVolatile(flags);
404 }
405 /** Utility method to query the modifier flags of this member. */
406 public boolean isAbstract() {
407 return Modifier.isAbstract(flags);
408 }
409 /** Utility method to query the modifier flags of this member. */
410 public boolean isNative() {
411 return Modifier.isNative(flags);
412 }
413 // let the rest (native, volatile, transient, etc.) be tested via Modifier.isFoo
414
415 // unofficial modifier flags, used by HotSpot:
416 static final int BRIDGE = 0x00000040;
417 static final int VARARGS = 0x00000080;
418 static final int SYNTHETIC = 0x00001000;
419 static final int ANNOTATION = 0x00002000;
420 static final int ENUM = 0x00004000;
421
422 /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
423 public boolean isBridge() {
424 return allFlagsSet(IS_METHOD | BRIDGE);
425 }
426 /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
427 public boolean isVarargs() {
428 return allFlagsSet(VARARGS) && isInvocable();
429 }
430 /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
431 public boolean isSynthetic() {
432 return allFlagsSet(SYNTHETIC);
433 }
434
435 /** Query whether this member is a flat field */
436 public boolean isFlat() { return getLayout() != 0; }
437
438 /** Query whether this member is a null-restricted field */
439 public boolean isNullRestricted() { return (flags & MN_NULL_RESTRICTED) == MN_NULL_RESTRICTED; }
440
441 /**
442 * VM-internal layout code for this field, 0 if this field is not flat.
443 */
444 public int getLayout() { return (flags >>> MN_LAYOUT_SHIFT) & MN_LAYOUT_MASK; }
445
446 static final String CONSTRUCTOR_NAME = "<init>";
447
448 // modifiers exported by the JVM:
449 static final int RECOGNIZED_MODIFIERS = 0xFFFF;
450
451 // private flags, not part of RECOGNIZED_MODIFIERS:
452 static final int
453 IS_METHOD = MN_IS_METHOD, // method (not constructor)
454 IS_CONSTRUCTOR = MN_IS_CONSTRUCTOR, // constructor
455 IS_FIELD = MN_IS_FIELD, // field
456 IS_TYPE = MN_IS_TYPE, // nested type
457 CALLER_SENSITIVE = MN_CALLER_SENSITIVE, // @CallerSensitive annotation detected
458 TRUSTED_FINAL = MN_TRUSTED_FINAL; // trusted final field
459
460 static final int ALL_ACCESS = Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED;
461 static final int ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE;
462 static final int IS_INVOCABLE = IS_METHOD | IS_CONSTRUCTOR;
463
464 /** Utility method to query whether this member is a method or constructor. */
465 public boolean isInvocable() {
|