< prev index next >

src/java.base/share/classes/java/lang/invoke/MemberName.java

Print this page

   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     /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
 417     public boolean isBridge() {
 418         return allFlagsSet(IS_METHOD | BRIDGE);
 419     }
 420     /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
 421     public boolean isVarargs() {
 422         return allFlagsSet(VARARGS) && isInvocable();
 423     }
 424     /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
 425     public boolean isSynthetic() {
 426         return allFlagsSet(SYNTHETIC);
 427     }
 428 
 429     static final String CONSTRUCTOR_NAME = "<init>";  // the ever-popular











 430 
 431     // modifiers exported by the JVM:
 432     static final int RECOGNIZED_MODIFIERS = 0xFFFF;
 433 
 434     // private flags, not part of RECOGNIZED_MODIFIERS:
 435     static final int
 436             IS_METHOD        = MN_IS_METHOD,        // method (not constructor)
 437             IS_CONSTRUCTOR   = MN_IS_CONSTRUCTOR,   // constructor
 438             IS_FIELD         = MN_IS_FIELD,         // field
 439             IS_TYPE          = MN_IS_TYPE,          // nested type
 440             CALLER_SENSITIVE = MN_CALLER_SENSITIVE, // @CallerSensitive annotation detected
 441             TRUSTED_FINAL    = MN_TRUSTED_FINAL;    // trusted final field
 442 
 443     static final int ALL_ACCESS = Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED;
 444     static final int ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE;
 445     static final int IS_INVOCABLE = IS_METHOD | IS_CONSTRUCTOR;
 446 
 447     /** Utility method to query whether this member is a method or constructor. */
 448     public boolean isInvocable() {
 449         return anyFlagSet(IS_INVOCABLE);
 450     }
 451     /** Query whether this member is a method. */
 452     public boolean isMethod() {
 453         return allFlagsSet(IS_METHOD);
 454     }
 455     /** Query whether this member is a constructor. */
 456     public boolean isConstructor() {
 457         return allFlagsSet(IS_CONSTRUCTOR);
 458     }
 459     /** Query whether this member is a field. */
 460     public boolean isField() {
 461         return allFlagsSet(IS_FIELD);

 738     }
 739 
 740     // Construction from symbolic parts, for queries:
 741     /** Create a field or type name from the given components:
 742      *  Declaring class, name, type, reference kind.
 743      *  The declaring class may be supplied as null if this is to be a bare name and type.
 744      *  The resulting name will in an unresolved state.
 745      */
 746     public MemberName(Class<?> defClass, String name, Class<?> type, byte refKind) {
 747         init(defClass, name, type, flagsMods(IS_FIELD, 0, refKind));
 748         initResolved(false);
 749     }
 750     /** Create a method or constructor name from the given components:
 751      *  Declaring class, name, type, reference kind.
 752      *  It will be a constructor if and only if the name is {@code "<init>"}.
 753      *  The declaring class may be supplied as null if this is to be a bare name and type.
 754      *  The last argument is optional, a boolean which requests REF_invokeSpecial.
 755      *  The resulting name will in an unresolved state.
 756      */
 757     public MemberName(Class<?> defClass, String name, MethodType type, byte refKind) {
 758         int initFlags = (name != null && name.equals(CONSTRUCTOR_NAME) ? IS_CONSTRUCTOR : IS_METHOD);
 759         init(defClass, name, type, flagsMods(initFlags, 0, refKind));
 760         initResolved(false);
 761     }
 762     /** Create a method, constructor, or field name from the given components:
 763      *  Reference kind, declaring class, name, type.
 764      */
 765     public MemberName(byte refKind, Class<?> defClass, String name, Object type) {
 766         int kindFlags;
 767         if (MethodHandleNatives.refKindIsField(refKind)) {
 768             kindFlags = IS_FIELD;
 769             if (!(type instanceof Class))
 770                 throw newIllegalArgumentException("not a field type");
 771         } else if (MethodHandleNatives.refKindIsMethod(refKind)) {
 772             kindFlags = IS_METHOD;
 773             if (!(type instanceof MethodType))
 774                 throw newIllegalArgumentException("not a method type");
 775         } else if (refKind == REF_newInvokeSpecial) {
 776             kindFlags = IS_CONSTRUCTOR;
 777             if (!(type instanceof MethodType) ||
 778                 !CONSTRUCTOR_NAME.equals(name))

   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() {
 466         return anyFlagSet(IS_INVOCABLE);
 467     }
 468     /** Query whether this member is a method. */
 469     public boolean isMethod() {
 470         return allFlagsSet(IS_METHOD);
 471     }
 472     /** Query whether this member is a constructor. */
 473     public boolean isConstructor() {
 474         return allFlagsSet(IS_CONSTRUCTOR);
 475     }
 476     /** Query whether this member is a field. */
 477     public boolean isField() {
 478         return allFlagsSet(IS_FIELD);

 755     }
 756 
 757     // Construction from symbolic parts, for queries:
 758     /** Create a field or type name from the given components:
 759      *  Declaring class, name, type, reference kind.
 760      *  The declaring class may be supplied as null if this is to be a bare name and type.
 761      *  The resulting name will in an unresolved state.
 762      */
 763     public MemberName(Class<?> defClass, String name, Class<?> type, byte refKind) {
 764         init(defClass, name, type, flagsMods(IS_FIELD, 0, refKind));
 765         initResolved(false);
 766     }
 767     /** Create a method or constructor name from the given components:
 768      *  Declaring class, name, type, reference kind.
 769      *  It will be a constructor if and only if the name is {@code "<init>"}.
 770      *  The declaring class may be supplied as null if this is to be a bare name and type.
 771      *  The last argument is optional, a boolean which requests REF_invokeSpecial.
 772      *  The resulting name will in an unresolved state.
 773      */
 774     public MemberName(Class<?> defClass, String name, MethodType type, byte refKind) {
 775         int initFlags = CONSTRUCTOR_NAME.equals(name) ? IS_CONSTRUCTOR : IS_METHOD;
 776         init(defClass, name, type, flagsMods(initFlags, 0, refKind));
 777         initResolved(false);
 778     }
 779     /** Create a method, constructor, or field name from the given components:
 780      *  Reference kind, declaring class, name, type.
 781      */
 782     public MemberName(byte refKind, Class<?> defClass, String name, Object type) {
 783         int kindFlags;
 784         if (MethodHandleNatives.refKindIsField(refKind)) {
 785             kindFlags = IS_FIELD;
 786             if (!(type instanceof Class))
 787                 throw newIllegalArgumentException("not a field type");
 788         } else if (MethodHandleNatives.refKindIsMethod(refKind)) {
 789             kindFlags = IS_METHOD;
 790             if (!(type instanceof MethodType))
 791                 throw newIllegalArgumentException("not a method type");
 792         } else if (refKind == REF_newInvokeSpecial) {
 793             kindFlags = IS_CONSTRUCTOR;
 794             if (!(type instanceof MethodType) ||
 795                 !CONSTRUCTOR_NAME.equals(name))
< prev index next >