-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
DynamicHub.java
2200 lines (1850 loc) 路 82.5 KB
/
DynamicHub.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2012, 2024, 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 com.oracle.svm.core.hub;
import static com.oracle.svm.core.MissingRegistrationUtils.throwMissingRegistrationErrors;
import static com.oracle.svm.core.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE;
import static com.oracle.svm.core.annotate.TargetElement.CONSTRUCTOR_NAME;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.ALL_CLASSES_FLAG;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.ALL_CONSTRUCTORS_FLAG;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.ALL_DECLARED_CLASSES_FLAG;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.ALL_DECLARED_CONSTRUCTORS_FLAG;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.ALL_DECLARED_FIELDS_FLAG;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.ALL_DECLARED_METHODS_FLAG;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.ALL_FIELDS_FLAG;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.ALL_METHODS_FLAG;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.ALL_NEST_MEMBERS_FLAG;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.ALL_PERMITTED_SUBCLASSES_FLAG;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.ALL_RECORD_COMPONENTS_FLAG;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.ALL_SIGNERS_FLAG;
import static com.oracle.svm.core.code.RuntimeMetadataDecoderImpl.CLASS_ACCESS_FLAGS_MASK;
import static com.oracle.svm.core.reflect.RuntimeMetadataDecoder.NO_DATA;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;
import java.lang.constant.Constable;
import java.lang.constant.ConstantDesc;
import java.lang.invoke.TypeDescriptor;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Field;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.RecordComponent;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.net.URL;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;
import org.graalvm.nativeimage.AnnotationAccess;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.c.function.CFunctionPointer;
import org.graalvm.nativeimage.impl.InternalPlatform;
import com.oracle.svm.core.BuildPhaseProvider.AfterCompilation;
import com.oracle.svm.core.BuildPhaseProvider.AfterHostedUniverse;
import com.oracle.svm.core.BuildPhaseProvider.CompileQueueFinished;
import com.oracle.svm.core.RuntimeAssertionsSupport;
import com.oracle.svm.core.SubstrateUtil;
import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.Delete;
import com.oracle.svm.core.annotate.InjectAccessors;
import com.oracle.svm.core.annotate.KeepOriginal;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.annotate.TargetElement;
import com.oracle.svm.core.classinitialization.ClassInitializationInfo;
import com.oracle.svm.core.classinitialization.EnsureClassInitializedNode;
import com.oracle.svm.core.config.ConfigurationValues;
import com.oracle.svm.core.config.ObjectLayout;
import com.oracle.svm.core.heap.UnknownObjectField;
import com.oracle.svm.core.heap.UnknownPrimitiveField;
import com.oracle.svm.core.jdk.JDK21OrEarlier;
import com.oracle.svm.core.jdk.JDK22OrLater;
import com.oracle.svm.core.jdk.JDK23OrLater;
import com.oracle.svm.core.jdk.Resources;
import com.oracle.svm.core.meta.SharedType;
import com.oracle.svm.core.reflect.MissingReflectionRegistrationUtils;
import com.oracle.svm.core.reflect.RuntimeMetadataDecoder;
import com.oracle.svm.core.reflect.RuntimeMetadataDecoder.ConstructorDescriptor;
import com.oracle.svm.core.reflect.RuntimeMetadataDecoder.FieldDescriptor;
import com.oracle.svm.core.reflect.RuntimeMetadataDecoder.MethodDescriptor;
import com.oracle.svm.core.reflect.fieldaccessor.UnsafeFieldAccessorFactory;
import com.oracle.svm.core.reflect.target.Target_jdk_internal_reflect_ConstantPool;
import com.oracle.svm.core.util.LazyFinalReference;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.util.ReflectionUtil;
import com.oracle.svm.util.ReflectionUtil.ReflectionUtilError;
import jdk.graal.compiler.api.directives.GraalDirectives;
import jdk.graal.compiler.api.replacements.Fold;
import jdk.graal.compiler.core.common.NumUtil;
import jdk.graal.compiler.core.common.SuppressFBWarnings;
import jdk.graal.compiler.replacements.ReplacementsUtil;
import jdk.internal.access.JavaLangReflectAccess;
import jdk.internal.misc.Unsafe;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.CallerSensitiveAdapter;
import jdk.internal.reflect.FieldAccessor;
import jdk.internal.reflect.Reflection;
import jdk.internal.reflect.ReflectionFactory;
import sun.reflect.annotation.AnnotationType;
import sun.reflect.generics.factory.GenericsFactory;
import sun.reflect.generics.repository.ClassRepository;
/**
* Instantiations of this class have a special layout. See {@code DynamicHubLayout} for a
* description of how the object is arranged.
*/
@Substitute
@TargetClass(java.lang.Class.class)
@SuppressWarnings({"static-method", "serial"})
@SuppressFBWarnings(value = "Se", justification = "DynamicHub must implement Serializable for compatibility with java.lang.Class, not because of actual serialization")
public final class DynamicHub implements AnnotatedElement, java.lang.reflect.Type, GenericDeclaration, Serializable, TypeDescriptor.OfField<DynamicHub>, Constable {
@Substitute //
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
@Platforms(Platform.HOSTED_ONLY.class) //
private final Class<?> hostedJavaClass;
/**
* The name of the class this hub is representing, as defined in {@link Class#getName()}.
*
* Even though the field is only assigned in the constructor, it cannot be final: The
* substitution system does not allow a final field when the target class has a field with the
* same name. And using a different field name fails for various other reasons that are too
* complicated to fix. Therefore, we ensure early constant folding using an invocation plugin
* for the getName() method.
*/
private String name;
/**
* Used to quickly determine in which category a certain hub falls (e.g., instance or array).
* The returned category does not necessarily match the {@link LayoutEncoding}, see
* {@link Hybrid} objects for more details.
*/
private final int hubType;
/**
* Used to quickly determine if this class is a subclass of {@link Reference}.
*/
private final byte referenceType;
/**
* Encoding of the object or array size. Decode using {@link LayoutEncoding}.
*/
@UnknownPrimitiveField(availability = AfterHostedUniverse.class)//
private int layoutEncoding;
/**
* Unique id number for this type, used for fast type checks and type casts.
*/
@UnknownPrimitiveField(availability = AfterHostedUniverse.class)//
private int typeID;
// region closed-world only fields
/**
* In our current version, type checks are accomplished by performing a range check on a value
* from an array. The slot to read from the checked type is determined by
* {@link #getTypeCheckSlot()} and the check passes if {@link #getTypeCheckStart()} <= value <
* ({@link #getTypeCheckStart()} + {@link #getTypeCheckRange()}).
*/
@UnknownPrimitiveField(availability = AfterHostedUniverse.class)//
private short typeCheckStart;
/**
* The number of ids which are in valid range for a type check.
*/
@UnknownPrimitiveField(availability = AfterHostedUniverse.class)//
private short typeCheckRange;
/**
* The value slot within the type id slot array to read when comparing against this type.
*/
@UnknownPrimitiveField(availability = AfterHostedUniverse.class)//
private short typeCheckSlot;
/**
* Array containing this type's type check id information. During a type check, a requested
* column of this array is read to determine if this value fits within the range of ids which
* match the assignee's type.
*/
@UnknownObjectField(availability = AfterHostedUniverse.class)//
private short[] closedTypeWorldTypeCheckSlots;
// endregion closed-world only fields
// region open-world only fields
/**
* This stores the depth of the type in the inheritance hierarchy. If the type is an interface
* then the typeIDDepth is -1.
*/
@UnknownPrimitiveField(availability = AfterHostedUniverse.class)//
private int typeIDDepth;
@UnknownPrimitiveField(availability = AfterHostedUniverse.class)//
private int numClassTypes;
@UnknownPrimitiveField(availability = AfterHostedUniverse.class)//
private int numInterfaceTypes;
/**
* Array containing this type's type check id information. During a type check, these slots are
* searched for a matching typeID.
*/
@UnknownObjectField(availability = AfterHostedUniverse.class)//
private int[] openTypeWorldTypeCheckSlots;
// endregion open-world only fields
/**
* The offset of the synthetic field which stores whatever is used for monitorEnter/monitorExit
* by an instance of this class. If 0, then instances of this class are locked using a side
* table.
*/
@UnknownPrimitiveField(availability = AfterHostedUniverse.class)//
private char monitorOffset;
@UnknownPrimitiveField(availability = AfterHostedUniverse.class)//
private char identityHashOffset;
/**
* Bit-set for various boolean flags, to reduce size of instances. It is important that this
* field is final, so that various methods are constant folded for constant classes already
* before the static analysis.
*/
private final short flags;
/** Is this a primitive type. */
private static final int IS_PRIMITIVE_FLAG_BIT = 0;
/** Is this an interface. */
private static final int IS_INTERFACE_FLAG_BIT = 1;
/** Is this a Hidden Class. */
private static final int IS_HIDDEN_FLAG_BIT = 2;
/** Is this a Record Class. */
private static final int IS_RECORD_FLAG_BIT = 3;
/** Holds assertionStatus determined by {@link RuntimeAssertionsSupport}. */
private static final int ASSERTION_STATUS_FLAG_BIT = 4;
/**
* Class/superclass/implemented interfaces has default methods. Necessary metadata for class
* initialization, but even for classes/interfaces that are already initialized during image
* generation, so it cannot be a field in {@link ClassInitializationInfo}.
*/
private static final int HAS_DEFAULT_METHODS_FLAG_BIT = 5;
/**
* Directly declares default methods. Necessary metadata for class initialization, but even for
* interfaces that are already initialized during image generation, so it cannot be a field in
* {@link ClassInitializationInfo}.
*/
private static final int DECLARES_DEFAULT_METHODS_FLAG_BIT = 6;
/** Is this a Sealed Class. */
private static final int IS_SEALED_FLAG_BIT = 7;
/** Is this a VM-internal class that should be hidden from stack traces. */
private static final int IS_VM_INTERNAL_FLAG_BIT = 8;
/**
* Is this a lambda form hidden class that should be hidden from stack traces in some
* circumstances.
*/
private static final int IS_LAMBDA_FORM_HIDDEN_BIT = 9;
/**
* Indicates this Class was linked during build time. Accessing an unlinked class during run
* time will throw an error.
*/
private static final int IS_LINKED_BIT = 10;
/**
* Indicates whether the class is a proxy class according to
* {@link java.lang.reflect.Proxy#isProxyClass}.
*/
private static final int IS_PROXY_CLASS_BIT = 11;
/**
* Similar to {@link #flags}, but non-final because {@link #setSharedData} sets the value.
*/
@UnknownPrimitiveField(availability = AfterHostedUniverse.class)//
private byte additionalFlags;
/** Indicates whether the type has been discovered as instantiated by the static analysis. */
private static final int IS_INSTANTIATED_BIT = 0;
/** Can this class be instantiated as an instance. */
private static final int CAN_INSTANTIATE_AS_INSTANCE_BIT = 1;
private static final int IS_REGISTERED_FOR_SERIALIZATION = 2;
/**
* The {@link Modifier modifiers} of this class.
*/
private final int modifiers;
/**
* The hub for the superclass, or null if an interface or primitive type.
*
* @see Class#getSuperclass()
*/
private final DynamicHub superHub;
/**
* The hub for the component type of an array, or null if this hub is not an array hub.
*
* @see Class#getComponentType()
*/
@Substitute //
private final DynamicHub componentType;
/**
* The hub for an array of this type, or null if the array type has been determined as
* uninstantiated by the static analysis.
*/
private DynamicHub arrayHub;
/**
* The class that declares this class, as returned by {@code Class.getDeclaringClass0} or an
* exception that happened at image-build time.
*/
private final Object declaringClass;
/**
* The interfaces that this class implements. Either null (no interfaces), a {@link DynamicHub}
* (one interface), or a {@link DynamicHub}[] array (more than one interface).
*/
private Object interfacesEncoding;
/**
* Reference to a list of enum values for subclasses of {@link Enum}; null otherwise.
*/
private Object enumConstantsReference;
/**
* Reference map information for this hub. The byte[] array encoding data is available via
* {@link DynamicHubSupport#getReferenceMapEncoding()}.
*/
@UnknownPrimitiveField(availability = AfterHostedUniverse.class)//
private int referenceMapIndex;
/**
* Back link to the SubstrateType used by the substrate meta access. Only used for the subset of
* types for which a SubstrateType exists.
*/
@UnknownObjectField(fullyQualifiedTypes = "com.oracle.svm.graal.meta.SubstrateType")//
private SharedType metaType;
/**
* Source file name if known; null otherwise.
*/
private final String sourceFileName;
/**
* Metadata for running class initializers at run time. Refers to a singleton marker object for
* classes/interfaces already initialized during image generation, i.e., this field is never
* null at run time.
*/
private ClassInitializationInfo classInitializationInfo;
@UnknownObjectField(availability = AfterHostedUniverse.class)//
private CFunctionPointer[] vtable;
/** Field used for module information access at run-time. */
private Module module;
/** The class that serves as the host for the nest. All nestmates have the same host. */
private final Class<?> nestHost;
/** The simple binary name of this class, as returned by {@code Class.getSimpleBinaryName0}. */
private final String simpleBinaryName;
private final DynamicHubCompanion companion;
private final String signature;
@Substitute //
@InjectAccessors(ClassLoaderAccessors.class) //
private ClassLoader classLoader;
@Substitute //
@InjectAccessors(ReflectionDataAccessors.class) //
private SoftReference<Target_java_lang_Class_ReflectionData<?>> reflectionData;
@Substitute //
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset) //
private int classRedefinedCount;
@Substitute //
@InjectAccessors(AnnotationDataAccessors.class) //
private Target_java_lang_Class_AnnotationData annotationData;
@Substitute //
@InjectAccessors(AnnotationTypeAccessors.class) //
private AnnotationType annotationType;
// This field has a fixed value 3206093459760846163L in java.lang.Class
@Substitute private static final long serialVersionUID = 3206093459760846163L;
@Substitute //
@InjectAccessors(CachedConstructorAccessors.class) //
private Constructor<?> cachedConstructor;
@UnknownObjectField(canBeNull = true, availability = AfterCompilation.class) private DynamicHubMetadata hubMetadata;
@UnknownObjectField(canBeNull = true, availability = AfterCompilation.class) private ReflectionMetadata reflectionMetadata;
@Platforms(Platform.HOSTED_ONLY.class)
public DynamicHub(Class<?> hostedJavaClass, String name, int hubType, ReferenceType referenceType, DynamicHub superType, DynamicHub componentHub, String sourceFileName, int modifiers,
ClassLoader classLoader, boolean isHidden, boolean isRecord, Class<?> nestHost, boolean assertionStatus, boolean hasDefaultMethods, boolean declaresDefaultMethods,
boolean isSealed, boolean isVMInternal, boolean isLambdaFormHidden, boolean isLinked, String simpleBinaryName, Object declaringClass, String signature, boolean isProxyClass) {
this.hostedJavaClass = hostedJavaClass;
this.module = hostedJavaClass.getModule();
this.name = name;
this.hubType = hubType;
this.referenceType = referenceType.getValue();
this.superHub = superType;
this.componentType = componentHub;
this.sourceFileName = sourceFileName;
this.modifiers = modifiers;
this.nestHost = nestHost;
this.simpleBinaryName = simpleBinaryName;
this.declaringClass = declaringClass;
this.signature = signature;
this.flags = NumUtil.safeToUShort(makeFlag(IS_PRIMITIVE_FLAG_BIT, hostedJavaClass.isPrimitive()) |
makeFlag(IS_INTERFACE_FLAG_BIT, hostedJavaClass.isInterface()) |
makeFlag(IS_HIDDEN_FLAG_BIT, isHidden) |
makeFlag(IS_RECORD_FLAG_BIT, isRecord) |
makeFlag(ASSERTION_STATUS_FLAG_BIT, assertionStatus) |
makeFlag(HAS_DEFAULT_METHODS_FLAG_BIT, hasDefaultMethods) |
makeFlag(DECLARES_DEFAULT_METHODS_FLAG_BIT, declaresDefaultMethods) |
makeFlag(IS_SEALED_FLAG_BIT, isSealed) |
makeFlag(IS_VM_INTERNAL_FLAG_BIT, isVMInternal) |
makeFlag(IS_LAMBDA_FORM_HIDDEN_BIT, isLambdaFormHidden) |
makeFlag(IS_LINKED_BIT, isLinked) |
makeFlag(IS_PROXY_CLASS_BIT, isProxyClass));
this.companion = new DynamicHubCompanion(hostedJavaClass, classLoader);
}
@Platforms(Platform.HOSTED_ONLY.class)
private static int makeFlag(int flagBit, boolean value) {
int flagMask = 1 << flagBit;
return value ? flagMask : 0;
}
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
private static boolean isFlagSet(byte flags, int flagBit) {
int flagMask = 1 << flagBit;
return (flags & flagMask) != 0;
}
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
private static boolean isFlagSet(short flags, int flagBit) {
int flagMask = 1 << flagBit;
return (flags & flagMask) != 0;
}
@Platforms(Platform.HOSTED_ONLY.class)
public void setClassInitializationInfo(ClassInitializationInfo classInitializationInfo) {
this.classInitializationInfo = classInitializationInfo;
}
@Platforms(Platform.HOSTED_ONLY.class)
public void setSharedData(int layoutEncoding, int monitorOffset, int identityHashOffset, long referenceMapIndex,
boolean isInstantiated, boolean canInstantiateAsInstance, boolean isRegisteredForSerialization) {
assert !(!isInstantiated && canInstantiateAsInstance);
VMError.guarantee(monitorOffset == (char) monitorOffset, "Class %s has an invalid monitor field offset. Most likely, its objects are larger than supported.", name);
VMError.guarantee(identityHashOffset == (char) identityHashOffset, "Class %s has an invalid identity hash code field offset. Most likely, its objects are larger than supported.", name);
this.layoutEncoding = layoutEncoding;
this.monitorOffset = (char) monitorOffset;
this.identityHashOffset = (char) identityHashOffset;
if ((int) referenceMapIndex != referenceMapIndex) {
throw VMError.shouldNotReachHere("Reference map index not within integer range, need to switch field from int to long");
}
this.referenceMapIndex = (int) referenceMapIndex;
this.additionalFlags = NumUtil.safeToUByte(makeFlag(IS_INSTANTIATED_BIT, isInstantiated) |
makeFlag(CAN_INSTANTIATE_AS_INSTANCE_BIT, canInstantiateAsInstance) |
makeFlag(IS_REGISTERED_FOR_SERIALIZATION, isRegisteredForSerialization));
}
@Platforms(Platform.HOSTED_ONLY.class)
public void setClosedTypeWorldData(CFunctionPointer[] vtable, int typeID, short typeCheckStart, short typeCheckRange, short typeCheckSlot, short[] typeCheckSlots) {
assert this.vtable == null : "Initialization must be called only once";
this.typeID = typeID;
this.typeCheckStart = typeCheckStart;
this.typeCheckRange = typeCheckRange;
this.typeCheckSlot = typeCheckSlot;
this.closedTypeWorldTypeCheckSlots = typeCheckSlots;
this.vtable = vtable;
}
@Platforms(Platform.HOSTED_ONLY.class)
public void setOpenTypeWorldData(CFunctionPointer[] vtable, int typeID,
int typeCheckDepth, int numClassTypes, int numInterfaceTypes, int[] typeCheckSlots) {
assert this.vtable == null : "Initialization must be called only once";
this.typeID = typeID;
this.typeIDDepth = typeCheckDepth;
this.numClassTypes = numClassTypes;
this.numInterfaceTypes = numInterfaceTypes;
this.openTypeWorldTypeCheckSlots = typeCheckSlots;
this.vtable = vtable;
}
@Platforms(Platform.HOSTED_ONLY.class)
public void setArrayHub(DynamicHub arrayHub) {
assert (this.arrayHub == null || this.arrayHub == arrayHub) && arrayHub != null;
assert arrayHub.getComponentHub() == this;
this.arrayHub = arrayHub;
}
@Platforms(Platform.HOSTED_ONLY.class)
public void setInterfacesEncoding(Object interfacesEncoding) {
this.interfacesEncoding = interfacesEncoding;
}
@Platforms(Platform.HOSTED_ONLY.class)
public Object getInterfacesEncoding() {
return interfacesEncoding;
}
@Platforms(Platform.HOSTED_ONLY.class)
public boolean shouldInitEnumConstants() {
return enumConstantsReference == null;
}
@Platforms(Platform.HOSTED_ONLY.class)
public void initEnumConstants(Enum<?>[] enumConstants) {
/* Enum is eagerly initialized, so no need for `LazyFinalReference`. */
enumConstantsReference = enumConstants;
}
@Platforms(Platform.HOSTED_ONLY.class)
public void initEnumConstantsAtRuntime(Class<?> enumClass) {
/* Adapted from `Class.getEnumConstantsShared`. */
try {
Method values = ReflectionUtil.lookupMethod(enumClass, "values");
enumConstantsReference = new LazyFinalReference<>(() -> initEnumConstantsAtRuntime(values));
} catch (ReflectionUtilError e) {
/*
* This can happen when users concoct enum-like classes that don't comply with the enum
* spec.
*/
enumConstantsReference = null;
} catch (NoClassDefFoundError e) {
/*
* This can happen when an enum references a missing class. So, in order to match the
* JVM behaviour, we rethrow the error at runtime.
*/
String message = e.getMessage();
enumConstantsReference = new LazyFinalReference<>(() -> throwNoClassDefFoundErrorAtRuntime(message));
}
}
@Platforms(Platform.HOSTED_ONLY.class)
public String getSignature() {
return signature;
}
@Platforms(Platform.HOSTED_ONLY.class)
public void setHubMetadata(int enclosingMethodInfoIndex, int annotationsIndex, int typeAnnotationsIndex, int classesEncodingIndex, int permittedSubclassesEncodingIndex,
int nestMembersEncodingIndex, int signersEncodingIndex) {
this.hubMetadata = new DynamicHubMetadata(enclosingMethodInfoIndex, annotationsIndex, typeAnnotationsIndex, classesEncodingIndex, permittedSubclassesEncodingIndex, nestMembersEncodingIndex,
signersEncodingIndex);
}
@Platforms(Platform.HOSTED_ONLY.class)
public void setReflectionMetadata(int fieldsEncodingIndex, int methodsEncodingIndex, int constructorsEncodingIndex, int recordComponentsEncodingIndex, int classFlags) {
this.reflectionMetadata = new ReflectionMetadata(fieldsEncodingIndex, methodsEncodingIndex, constructorsEncodingIndex, recordComponentsEncodingIndex, classFlags);
}
private void checkClassFlag(int mask, String methodName) {
if (throwMissingRegistrationErrors() && !isClassFlagSet(mask)) {
MissingReflectionRegistrationUtils.forBulkQuery(DynamicHub.toClass(this), methodName);
}
}
private boolean isClassFlagSet(int mask) {
return (reflectionMetadata != null && (reflectionMetadata.classFlags & mask) != 0);
}
/** Executed at runtime. */
private static Object initEnumConstantsAtRuntime(Method values) {
try {
return values.invoke(null);
} catch (InvocationTargetException | IllegalAccessException e) {
/*
* These can happen when users concoct enum-like classes that don't comply with the enum
* spec.
*/
return null;
}
}
/** Executed at runtime. */
private static Object throwNoClassDefFoundErrorAtRuntime(String message) {
throw new NoClassDefFoundError(message);
}
@Platforms(Platform.HOSTED_ONLY.class)
public void setMetaType(SharedType metaType) {
this.metaType = metaType;
}
public boolean hasDefaultMethods() {
return isFlagSet(flags, HAS_DEFAULT_METHODS_FLAG_BIT);
}
public boolean declaresDefaultMethods() {
return isFlagSet(flags, DECLARES_DEFAULT_METHODS_FLAG_BIT);
}
public ClassInitializationInfo getClassInitializationInfo() {
return classInitializationInfo;
}
public boolean isInitialized() {
return classInitializationInfo.isInitialized();
}
public void ensureInitialized() {
EnsureClassInitializedNode.ensureClassInitialized(toClass(this));
}
public SharedType getMetaType() {
return metaType;
}
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public String getSourceFileName() {
return sourceFileName;
}
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public int getLayoutEncoding() {
return layoutEncoding;
}
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public int getTypeID() {
return typeID;
}
public short getTypeCheckSlot() {
return typeCheckSlot;
}
public short getTypeCheckStart() {
return typeCheckStart;
}
public short getTypeCheckRange() {
return typeCheckRange;
}
public int getTypeIDDepth() {
return typeIDDepth;
}
public int getNumClassTypes() {
return numClassTypes;
}
public int getNumInterfaceTypes() {
return numInterfaceTypes;
}
public int[] getOpenTypeWorldTypeCheckSlots() {
return openTypeWorldTypeCheckSlots;
}
public int getMonitorOffset() {
return monitorOffset;
}
/**
* If possible, use {@link LayoutEncoding#getIdentityHashOffset(Object)} instead. If the hash
* code field is optional, note that this method may return an offset that is outside the bounds
* of a newly allocated object.
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public int getIdentityHashOffset() {
ObjectLayout ol = ConfigurationValues.getObjectLayout();
if (ol.isIdentityHashFieldInObjectHeader()) { // enable elimination of our field
return ol.getObjectHeaderIdentityHashOffset();
}
int result = identityHashOffset;
if (GraalDirectives.inIntrinsic()) {
ReplacementsUtil.dynamicAssert(result > 0, "must have an identity hash field");
} else {
assert result > 0 : "must have an identity hash field";
}
return result;
}
public DynamicHub getSuperHub() {
return superHub;
}
public DynamicHub getComponentHub() {
return componentType;
}
public DynamicHub getArrayHub() {
return arrayHub;
}
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public int getReferenceMapIndex() {
return referenceMapIndex;
}
public boolean isInstantiated() {
return isFlagSet(additionalFlags, IS_INSTANTIATED_BIT);
}
public boolean canInstantiateAsInstance() {
return isFlagSet(additionalFlags, CAN_INSTANTIATE_AS_INSTANCE_BIT);
}
public boolean isProxyClass() {
return isFlagSet(flags, IS_PROXY_CLASS_BIT);
}
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static DynamicHub fromClass(Class<?> clazz) {
return SubstrateUtil.cast(clazz, DynamicHub.class);
}
public DynamicHubCompanion getCompanion() {
return companion;
}
/*
* Note that this method must be a static method and not an instance method, otherwise null
* values cannot be converted.
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static Class<?> toClass(DynamicHub hub) {
return SubstrateUtil.cast(hub, Class.class);
}
/**
* Returns the {@link Class} object that represents the type during image generation.
*/
@Platforms(Platform.HOSTED_ONLY.class)
public Class<?> getHostedJavaClass() {
return hostedJavaClass;
}
@Substitute
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public String getName() {
return name;
}
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public int getHubType() {
return hubType;
}
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public boolean isInstanceClass() {
return HubType.isInstance(hubType);
}
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public boolean isPodInstanceClass() {
return HubType.isPodInstance(hubType);
}
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public boolean isReferenceInstanceClass() {
return HubType.isReferenceInstance(hubType);
}
@Substitute
@Override
public boolean isArray() {
throw VMError.shouldNotReachHere("Intrinsified in StandardGraphBuilderPlugins.");
}
public boolean hubIsArray() {
return HubType.isArray(hubType);
}
@Substitute
public boolean isInterface() {
return isFlagSet(flags, IS_INTERFACE_FLAG_BIT);
}
@Substitute
@Override
public boolean isPrimitive() {
return isFlagSet(flags, IS_PRIMITIVE_FLAG_BIT);
}
@Substitute
public int getModifiers() {
return modifiers;
}
public int getClassAccessFlags() {
return reflectionMetadata != null ? (reflectionMetadata.classFlags & CLASS_ACCESS_FLAGS_MASK) : modifiers;
}
@Substitute
private DynamicHub getComponentType() {
return componentType;
}
@Substitute
private DynamicHub getSuperclass() {
return superHub;
}
@Substitute
private boolean isInstance(@SuppressWarnings("unused") Object obj) {
throw VMError.shouldNotReachHere("Intrinsified in StandardGraphBuilderPlugins.");
}
@Substitute
private Object cast(@SuppressWarnings("unused") Object obj) {
throw VMError.shouldNotReachHere("Intrinsified in StandardGraphBuilderPlugins.");
}
@Substitute
private boolean isAssignableFrom(@SuppressWarnings("unused") Class<?> cls) {
throw VMError.shouldNotReachHere("Intrinsified in StandardGraphBuilderPlugins.");
}
@Substitute
private boolean isAnnotation() {
/*
* We do not do the check "this.getModifiers() & ANNOTATION) != 0" because we do not have
* the full modifier bits.
*/
return isInterface() && getInterfaces().length == 1 && DynamicHub.toClass(getInterfaces()[0]) == Annotation.class;
}
@Substitute
private boolean isEnum() {
/*
* We do not do the check "this.getModifiers() & ENUM) != 0" because we do not have the full
* modifier bits.
*/
return toClass(getSuperclass()) == java.lang.Enum.class;
}
@KeepOriginal
private native Object[] getEnumConstants();
@Substitute
public Object[] getEnumConstantsShared() {
if (enumConstantsReference instanceof LazyFinalReference) {
return (Object[]) ((LazyFinalReference<?>) enumConstantsReference).get();
}
return (Object[]) enumConstantsReference;
}
@KeepOriginal
public native URL getResource(String resourceName);
@Substitute
public InputStream getResourceAsStream(String resourceName) {
String resolvedName = resolveName(resourceName);
return Resources.singleton().createInputStream(module, resolvedName);
}
@KeepOriginal
private native String resolveName(String resourceName);
@KeepOriginal
private native boolean isOpenToCaller(String resourceName, Class<?> caller);
@Substitute
public ClassLoader getClassLoader() {
return companion.getClassLoader();
}
@KeepOriginal
private native ClassLoader getClassLoader0();
public boolean isLoaded() {
return companion.hasClassLoader();
}
void setClassLoaderAtRuntime(ClassLoader loader) {
companion.setClassLoader(loader);
}
@KeepOriginal
private native String getSimpleName();
@KeepOriginal //
private native String getSimpleName0();
@KeepOriginal
private native String getCanonicalName();
@KeepOriginal
private native String getCanonicalName0();
@KeepOriginal
@Override
public native String getTypeName();
@KeepOriginal
private native String getSimpleBinaryName();
@KeepOriginal
private native <U> Class<? extends U> asSubclass(Class<U> clazz);
@KeepOriginal
private native boolean isAnonymousClass();
@KeepOriginal
@TargetElement(onlyWith = JDK21OrEarlier.class)
private native boolean isUnnamedClass();
@Substitute
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public boolean isHidden() {
return isFlagSet(flags, IS_HIDDEN_FLAG_BIT);
}
@Substitute
public boolean isRecord() {
return isFlagSet(flags, IS_RECORD_FLAG_BIT);
}
@Substitute
public boolean isSealed() {
return isFlagSet(flags, IS_SEALED_FLAG_BIT);
}
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public boolean isVMInternal() {
return isFlagSet(flags, IS_VM_INTERNAL_FLAG_BIT);
}
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public boolean isLambdaFormHidden() {
return isFlagSet(flags, IS_LAMBDA_FORM_HIDDEN_BIT);
}
public boolean isLinked() {
return isFlagSet(flags, IS_LINKED_BIT);
}
public boolean isRegisteredForSerialization() {
return isFlagSet(additionalFlags, IS_REGISTERED_FOR_SERIALIZATION);
}
@KeepOriginal
private native boolean isLocalClass();
@KeepOriginal
private native boolean isMemberClass();
@KeepOriginal
private native boolean isLocalOrAnonymousClass();
@KeepOriginal
private native Class<?> getEnclosingClass();
@KeepOriginal