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.
207 // Get a snapshot of type which doesn't get changed by racing threads.
208 final Object type = this.type;
209 if (type instanceof Class<?> cl) {
210 return cl;
211 }
212 }
213
214 // type is not a Class yet. Convert it thread-safely.
215 synchronized (this) {
216 if (type instanceof String sig) {
217 MethodType mtype = MethodType.fromDescriptor("()"+sig, getClassLoader());
218 Class<?> res = mtype.returnType();
219 type = res;
220 }
221 // Make sure type is a Class for racing threads.
222 assert type instanceof Class<?> : "bad field type " + type;
223 }
224 return (Class<?>) type;
225 }
226
227 /** Utility method to produce either the method type or field type of this member. */
228 public Object getType() {
229 return (isInvocable() ? getMethodType() : getFieldType());
230 }
231
232 /** Return the modifier flags of this member.
233 * @see java.lang.reflect.Modifier
234 */
235 public int getModifiers() {
236 return (flags & RECOGNIZED_MODIFIERS);
237 }
238
239 /** Return the reference kind of this member, or zero if none.
240 */
241 public byte getReferenceKind() {
242 return (byte) ((flags >>> MN_REFERENCE_KIND_SHIFT) & MN_REFERENCE_KIND_MASK);
243 }
244 private boolean referenceKindIsConsistent() {
245 byte refKind = getReferenceKind();
246 if (refKind == REF_NONE) return isType();
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))
|
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.value.CheckedType;
29 import jdk.internal.value.NormalCheckedType;
30 import jdk.internal.value.NullRestrictedCheckedType;
31 import sun.invoke.util.VerifyAccess;
32
33 import java.lang.reflect.Constructor;
34 import java.lang.reflect.Field;
35 import java.lang.reflect.Member;
36 import java.lang.reflect.Method;
37 import java.lang.reflect.Modifier;
38 import java.util.Objects;
39
40 import static java.lang.invoke.MethodHandleNatives.Constants.*;
41 import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
42 import static java.lang.invoke.MethodHandleStatics.newInternalError;
43
44 /**
45 * A {@code MemberName} is a compact symbolic datum which fully characterizes
46 * a method or field reference.
47 * A member name refers to a field, method, constructor, or member type.
48 * Every member name has a simple name (a string) and a type (either a Class or MethodType).
49 * A member name may also have a non-null declaring class, or it may be simply
50 * a naked name/type pair.
210 // Get a snapshot of type which doesn't get changed by racing threads.
211 final Object type = this.type;
212 if (type instanceof Class<?> cl) {
213 return cl;
214 }
215 }
216
217 // type is not a Class yet. Convert it thread-safely.
218 synchronized (this) {
219 if (type instanceof String sig) {
220 MethodType mtype = MethodType.fromDescriptor("()"+sig, getClassLoader());
221 Class<?> res = mtype.returnType();
222 type = res;
223 }
224 // Make sure type is a Class for racing threads.
225 assert type instanceof Class<?> : "bad field type " + type;
226 }
227 return (Class<?>) type;
228 }
229
230 /**
231 * Return {@code CheckedType} representing the type of this member.
232 */
233 public CheckedType getCheckedFieldType() {
234 return isNullRestricted() ? NullRestrictedCheckedType.of(getFieldType())
235 : NormalCheckedType.of(getFieldType());
236 }
237
238 /** Utility method to produce either the method type or field type of this member. */
239 public Object getType() {
240 return (isInvocable() ? getMethodType() : getFieldType());
241 }
242
243 /** Return the modifier flags of this member.
244 * @see java.lang.reflect.Modifier
245 */
246 public int getModifiers() {
247 return (flags & RECOGNIZED_MODIFIERS);
248 }
249
250 /** Return the reference kind of this member, or zero if none.
251 */
252 public byte getReferenceKind() {
253 return (byte) ((flags >>> MN_REFERENCE_KIND_SHIFT) & MN_REFERENCE_KIND_MASK);
254 }
255 private boolean referenceKindIsConsistent() {
256 byte refKind = getReferenceKind();
257 if (refKind == REF_NONE) return isType();
402 }
403 /** Utility method to query whether this member or its defining class is final. */
404 public boolean canBeStaticallyBound() {
405 return Modifier.isFinal(flags | clazz.getModifiers());
406 }
407 /** Utility method to query the modifier flags of this member. */
408 public boolean isVolatile() {
409 return Modifier.isVolatile(flags);
410 }
411 /** Utility method to query the modifier flags of this member. */
412 public boolean isAbstract() {
413 return Modifier.isAbstract(flags);
414 }
415 /** Utility method to query the modifier flags of this member. */
416 public boolean isNative() {
417 return Modifier.isNative(flags);
418 }
419 // let the rest (native, volatile, transient, etc.) be tested via Modifier.isFoo
420
421 // unofficial modifier flags, used by HotSpot:
422 static final int BRIDGE = 0x00000040;
423 static final int VARARGS = 0x00000080;
424 static final int SYNTHETIC = 0x00001000;
425 static final int ANNOTATION = 0x00002000;
426 static final int ENUM = 0x00004000;
427
428 /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
429 public boolean isBridge() {
430 return allFlagsSet(IS_METHOD | BRIDGE);
431 }
432 /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
433 public boolean isVarargs() {
434 return allFlagsSet(VARARGS) && isInvocable();
435 }
436 /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
437 public boolean isSynthetic() {
438 return allFlagsSet(SYNTHETIC);
439 }
440
441 /** Query whether this member is a flat field */
442 public boolean isFlat() { return (flags & MN_FLAT_FIELD) == MN_FLAT_FIELD; }
443
444 /** Query whether this member is a null-restricted field */
445 public boolean isNullRestricted() { return (flags & MN_NULL_RESTRICTED) == MN_NULL_RESTRICTED; }
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() {
467 return anyFlagSet(IS_INVOCABLE);
468 }
469 /** Query whether this member is a method. */
470 public boolean isMethod() {
471 return allFlagsSet(IS_METHOD);
472 }
473 /** Query whether this member is a constructor. */
474 public boolean isConstructor() {
475 return allFlagsSet(IS_CONSTRUCTOR);
476 }
477 /** Query whether this member is a field. */
478 public boolean isField() {
479 return allFlagsSet(IS_FIELD);
756 }
757
758 // Construction from symbolic parts, for queries:
759 /** Create a field or type name from the given components:
760 * Declaring class, name, type, reference kind.
761 * The declaring class may be supplied as null if this is to be a bare name and type.
762 * The resulting name will in an unresolved state.
763 */
764 public MemberName(Class<?> defClass, String name, Class<?> type, byte refKind) {
765 init(defClass, name, type, flagsMods(IS_FIELD, 0, refKind));
766 initResolved(false);
767 }
768 /** Create a method or constructor name from the given components:
769 * Declaring class, name, type, reference kind.
770 * It will be a constructor if and only if the name is {@code "<init>"}.
771 * The declaring class may be supplied as null if this is to be a bare name and type.
772 * The last argument is optional, a boolean which requests REF_invokeSpecial.
773 * The resulting name will in an unresolved state.
774 */
775 public MemberName(Class<?> defClass, String name, MethodType type, byte refKind) {
776 int initFlags = CONSTRUCTOR_NAME.equals(name) ? IS_CONSTRUCTOR : IS_METHOD;
777 init(defClass, name, type, flagsMods(initFlags, 0, refKind));
778 initResolved(false);
779 }
780 /** Create a method, constructor, or field name from the given components:
781 * Reference kind, declaring class, name, type.
782 */
783 public MemberName(byte refKind, Class<?> defClass, String name, Object type) {
784 int kindFlags;
785 if (MethodHandleNatives.refKindIsField(refKind)) {
786 kindFlags = IS_FIELD;
787 if (!(type instanceof Class))
788 throw newIllegalArgumentException("not a field type");
789 } else if (MethodHandleNatives.refKindIsMethod(refKind)) {
790 kindFlags = IS_METHOD;
791 if (!(type instanceof MethodType))
792 throw newIllegalArgumentException("not a method type");
793 } else if (refKind == REF_newInvokeSpecial) {
794 kindFlags = IS_CONSTRUCTOR;
795 if (!(type instanceof MethodType) ||
796 !CONSTRUCTOR_NAME.equals(name))
|