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