javaClasses.hpp revision 3959:4daebd4cc1dd
1117395Skan/*
2169689Skan * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
390075Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
490075Sobrien *
590075Sobrien * This code is free software; you can redistribute it and/or modify it
690075Sobrien * under the terms of the GNU General Public License version 2 only, as
790075Sobrien * published by the Free Software Foundation.
890075Sobrien *
990075Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1090075Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1190075Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1290075Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1390075Sobrien * accompanied this code).
1490075Sobrien *
1590075Sobrien * You should have received a copy of the GNU General Public License version
1690075Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1790075Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18169689Skan *
19169689Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2090075Sobrien * or visit www.oracle.com if you need additional information or have any
2190075Sobrien * questions.
2290075Sobrien *
2390075Sobrien */
24169689Skan
25169689Skan#ifndef SHARE_VM_CLASSFILE_JAVACLASSES_HPP
26169689Skan#define SHARE_VM_CLASSFILE_JAVACLASSES_HPP
2790075Sobrien
2890075Sobrien#include "classfile/systemDictionary.hpp"
29#include "jvmtifiles/jvmti.h"
30#include "oops/oop.hpp"
31#include "runtime/os.hpp"
32#include "utilities/utf8.hpp"
33
34// Interface for manipulating the basic Java classes.
35//
36// All dependencies on layout of actual Java classes should be kept here.
37// If the layout of any of the classes above changes the offsets must be adjusted.
38//
39// For most classes we hardwire the offsets for performance reasons. In certain
40// cases (e.g. java.security.AccessControlContext) we compute the offsets at
41// startup since the layout here differs between JDK1.2 and JDK1.3.
42//
43// Note that fields (static and non-static) are arranged with oops before non-oops
44// on a per class basis. The offsets below have to reflect this ordering.
45//
46// When editing the layouts please update the check_offset verification code
47// correspondingly. The names in the enums must be identical to the actual field
48// names in order for the verification code to work.
49
50
51// Interface to java.lang.String objects
52
53class java_lang_String : AllStatic {
54 private:
55  static int value_offset;
56  static int offset_offset;
57  static int count_offset;
58  static int hash_offset;
59
60  static bool initialized;
61
62  static Handle basic_create(int length, TRAPS);
63
64  static void set_value( oop string, typeArrayOop buffer) {
65    assert(initialized, "Must be initialized");
66    string->obj_field_put(value_offset,  (oop)buffer);
67  }
68  static void set_offset(oop string, int offset) {
69    assert(initialized, "Must be initialized");
70    if (offset_offset > 0) {
71      string->int_field_put(offset_offset, offset);
72    }
73  }
74  static void set_count( oop string, int count) {
75    assert(initialized, "Must be initialized");
76    if (count_offset > 0) {
77      string->int_field_put(count_offset,  count);
78    }
79  }
80
81 public:
82  static void compute_offsets();
83
84  // Instance creation
85  static Handle create_from_unicode(jchar* unicode, int len, TRAPS);
86  static oop    create_oop_from_unicode(jchar* unicode, int len, TRAPS);
87  static Handle create_from_str(const char* utf8_str, TRAPS);
88  static oop    create_oop_from_str(const char* utf8_str, TRAPS);
89  static Handle create_from_symbol(Symbol* symbol, TRAPS);
90  static Handle create_from_platform_dependent_str(const char* str, TRAPS);
91  static Handle char_converter(Handle java_string, jchar from_char, jchar to_char, TRAPS);
92
93  static bool has_offset_field()  {
94    assert(initialized, "Must be initialized");
95    return (offset_offset > 0);
96  }
97
98  static bool has_count_field()  {
99    assert(initialized, "Must be initialized");
100    return (count_offset > 0);
101  }
102
103  static bool has_hash_field()  {
104    assert(initialized, "Must be initialized");
105    return (hash_offset > 0);
106  }
107
108  static int value_offset_in_bytes()  {
109    assert(initialized && (value_offset > 0), "Must be initialized");
110    return value_offset;
111  }
112  static int count_offset_in_bytes()  {
113    assert(initialized && (count_offset > 0), "Must be initialized");
114    return count_offset;
115  }
116  static int offset_offset_in_bytes() {
117    assert(initialized && (offset_offset > 0), "Must be initialized");
118    return offset_offset;
119  }
120  static int hash_offset_in_bytes()   {
121    assert(initialized && (hash_offset > 0), "Must be initialized");
122    return hash_offset;
123  }
124
125  // Accessors
126  static typeArrayOop value(oop java_string) {
127    assert(initialized && (value_offset > 0), "Must be initialized");
128    assert(is_instance(java_string), "must be java_string");
129    return (typeArrayOop) java_string->obj_field(value_offset);
130  }
131  static int offset(oop java_string) {
132    assert(initialized, "Must be initialized");
133    assert(is_instance(java_string), "must be java_string");
134    if (offset_offset > 0) {
135      return java_string->int_field(offset_offset);
136    } else {
137      return 0;
138    }
139  }
140  static int length(oop java_string) {
141    assert(initialized, "Must be initialized");
142    assert(is_instance(java_string), "must be java_string");
143    if (count_offset > 0) {
144      return java_string->int_field(count_offset);
145    } else {
146      return ((typeArrayOop)java_string->obj_field(value_offset))->length();
147    }
148  }
149  static int utf8_length(oop java_string);
150
151  // String converters
152  static char*  as_utf8_string(oop java_string);
153  static char*  as_utf8_string(oop java_string, char* buf, int buflen);
154  static char*  as_utf8_string(oop java_string, int start, int len);
155  static char*  as_platform_dependent_str(Handle java_string, TRAPS);
156  static jchar* as_unicode_string(oop java_string, int& length);
157  // produce an ascii string with all other values quoted using \u####
158  static char*  as_quoted_ascii(oop java_string);
159
160  // Compute the hash value for a java.lang.String object which would
161  // contain the characters passed in.
162  //
163  // As the hash value used by the String object itself, in
164  // String.hashCode().  This value is normally calculated in Java code
165  // in the String.hashCode method(), but is precomputed for String
166  // objects in the shared archive file.
167  // hash P(31) from Kernighan & Ritchie
168  //
169  // For this reason, THIS ALGORITHM MUST MATCH String.hashCode().
170  template <typename T> static unsigned int hash_code(T* s, int len) {
171    unsigned int h = 0;
172    while (len-- > 0) {
173      h = 31*h + (unsigned int) *s;
174      s++;
175    }
176    return h;
177  }
178  static unsigned int hash_code(oop java_string);
179
180  // This is the string hash code used by the StringTable, which may be
181  // the same as String.hashCode or an alternate hash code.
182  static unsigned int hash_string(oop java_string);
183
184  static bool equals(oop java_string, jchar* chars, int len);
185
186  // Conversion between '.' and '/' formats
187  static Handle externalize_classname(Handle java_string, TRAPS) { return char_converter(java_string, '/', '.', THREAD); }
188  static Handle internalize_classname(Handle java_string, TRAPS) { return char_converter(java_string, '.', '/', THREAD); }
189
190  // Conversion
191  static Symbol* as_symbol(Handle java_string, TRAPS);
192  static Symbol* as_symbol_or_null(oop java_string);
193
194  // Testers
195  static bool is_instance(oop obj) {
196    return obj != NULL && obj->klass() == SystemDictionary::String_klass();
197  }
198
199  // Debugging
200  static void print(Handle java_string, outputStream* st);
201  friend class JavaClasses;
202};
203
204
205// Interface to java.lang.Class objects
206
207#define CLASS_INJECTED_FIELDS(macro)                                       \
208  macro(java_lang_Class, klass,                  intptr_signature,  false) \
209  macro(java_lang_Class, resolved_constructor,   intptr_signature,  false) \
210  macro(java_lang_Class, array_klass,            intptr_signature,  false) \
211  macro(java_lang_Class, oop_size,               int_signature,     false) \
212  macro(java_lang_Class, static_oop_field_count, int_signature,     false)
213
214class java_lang_Class : AllStatic {
215  friend class VMStructs;
216
217 private:
218  // The fake offsets are added by the class loader when java.lang.Class is loaded
219
220  static int _klass_offset;
221  static int _resolved_constructor_offset;
222  static int _array_klass_offset;
223
224  static int _oop_size_offset;
225  static int _static_oop_field_count_offset;
226
227  static bool offsets_computed;
228  static int classRedefinedCount_offset;
229  static GrowableArray<Klass*>* _fixup_mirror_list;
230
231 public:
232  static void compute_offsets();
233
234  // Instance creation
235  static oop  create_mirror(KlassHandle k, TRAPS);
236  static void fixup_mirror(KlassHandle k, TRAPS);
237  static oop  create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS);
238  // Conversion
239  static Klass* as_Klass(oop java_class);
240  static void set_klass(oop java_class, Klass* klass);
241  static BasicType as_BasicType(oop java_class, Klass** reference_klass = NULL);
242  static BasicType as_BasicType(oop java_class, KlassHandle* reference_klass) {
243    Klass* refk_oop = NULL;
244    BasicType result = as_BasicType(java_class, &refk_oop);
245    (*reference_klass) = KlassHandle(refk_oop);
246    return result;
247  }
248  static Symbol* as_signature(oop java_class, bool intern_if_not_found, TRAPS);
249  static void print_signature(oop java_class, outputStream *st);
250  // Testing
251  static bool is_instance(oop obj) {
252    return obj != NULL && obj->klass() == SystemDictionary::Class_klass();
253  }
254  static bool is_primitive(oop java_class);
255  static BasicType primitive_type(oop java_class);
256  static oop primitive_mirror(BasicType t);
257  // JVM_NewInstance support
258  static Method* resolved_constructor(oop java_class);
259  static void set_resolved_constructor(oop java_class, Method* constructor);
260  // JVM_NewArray support
261  static Klass* array_klass(oop java_class);
262  static void set_array_klass(oop java_class, Klass* klass);
263  // compiler support for class operations
264  static int klass_offset_in_bytes()                { return _klass_offset; }
265  static int resolved_constructor_offset_in_bytes() { return _resolved_constructor_offset; }
266  static int array_klass_offset_in_bytes()          { return _array_klass_offset; }
267  // Support for classRedefinedCount field
268  static int classRedefinedCount(oop the_class_mirror);
269  static void set_classRedefinedCount(oop the_class_mirror, int value);
270
271  static int oop_size(oop java_class);
272  static void set_oop_size(oop java_class, int size);
273  static int static_oop_field_count(oop java_class);
274  static void set_static_oop_field_count(oop java_class, int size);
275
276  static GrowableArray<Klass*>* fixup_mirror_list() {
277    return _fixup_mirror_list;
278  }
279  static void set_fixup_mirror_list(GrowableArray<Klass*>* v) {
280    _fixup_mirror_list = v;
281  }
282  // Debugging
283  friend class JavaClasses;
284  friend class InstanceKlass;   // verification code accesses offsets
285  friend class ClassFileParser; // access to number_of_fake_fields
286};
287
288// Interface to java.lang.Thread objects
289
290class java_lang_Thread : AllStatic {
291 private:
292  // Note that for this class the layout changed between JDK1.2 and JDK1.3,
293  // so we compute the offsets at startup rather than hard-wiring them.
294  static int _name_offset;
295  static int _group_offset;
296  static int _contextClassLoader_offset;
297  static int _inheritedAccessControlContext_offset;
298  static int _priority_offset;
299  static int _eetop_offset;
300  static int _daemon_offset;
301  static int _stillborn_offset;
302  static int _stackSize_offset;
303  static int _tid_offset;
304  static int _thread_status_offset;
305  static int _park_blocker_offset;
306  static int _park_event_offset ;
307
308  static void compute_offsets();
309
310 public:
311  // Instance creation
312  static oop create();
313  // Returns the JavaThread associated with the thread obj
314  static JavaThread* thread(oop java_thread);
315  // Set JavaThread for instance
316  static void set_thread(oop java_thread, JavaThread* thread);
317  // Name
318  static typeArrayOop name(oop java_thread);
319  static void set_name(oop java_thread, typeArrayOop name);
320  // Priority
321  static ThreadPriority priority(oop java_thread);
322  static void set_priority(oop java_thread, ThreadPriority priority);
323  // Thread group
324  static oop  threadGroup(oop java_thread);
325  // Stillborn
326  static bool is_stillborn(oop java_thread);
327  static void set_stillborn(oop java_thread);
328  // Alive (NOTE: this is not really a field, but provides the correct
329  // definition without doing a Java call)
330  static bool is_alive(oop java_thread);
331  // Daemon
332  static bool is_daemon(oop java_thread);
333  static void set_daemon(oop java_thread);
334  // Context ClassLoader
335  static oop context_class_loader(oop java_thread);
336  // Control context
337  static oop inherited_access_control_context(oop java_thread);
338  // Stack size hint
339  static jlong stackSize(oop java_thread);
340  // Thread ID
341  static jlong thread_id(oop java_thread);
342
343  // Blocker object responsible for thread parking
344  static oop park_blocker(oop java_thread);
345
346  // Pointer to type-stable park handler, encoded as jlong.
347  // Should be set when apparently null
348  // For details, see unsafe.cpp Unsafe_Unpark
349  static jlong park_event(oop java_thread);
350  static bool set_park_event(oop java_thread, jlong ptr);
351
352  // Java Thread Status for JVMTI and M&M use.
353  // This thread status info is saved in threadStatus field of
354  // java.lang.Thread java class.
355  enum ThreadStatus {
356    NEW                      = 0,
357    RUNNABLE                 = JVMTI_THREAD_STATE_ALIVE +          // runnable / running
358                               JVMTI_THREAD_STATE_RUNNABLE,
359    SLEEPING                 = JVMTI_THREAD_STATE_ALIVE +          // Thread.sleep()
360                               JVMTI_THREAD_STATE_WAITING +
361                               JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT +
362                               JVMTI_THREAD_STATE_SLEEPING,
363    IN_OBJECT_WAIT           = JVMTI_THREAD_STATE_ALIVE +          // Object.wait()
364                               JVMTI_THREAD_STATE_WAITING +
365                               JVMTI_THREAD_STATE_WAITING_INDEFINITELY +
366                               JVMTI_THREAD_STATE_IN_OBJECT_WAIT,
367    IN_OBJECT_WAIT_TIMED     = JVMTI_THREAD_STATE_ALIVE +          // Object.wait(long)
368                               JVMTI_THREAD_STATE_WAITING +
369                               JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT +
370                               JVMTI_THREAD_STATE_IN_OBJECT_WAIT,
371    PARKED                   = JVMTI_THREAD_STATE_ALIVE +          // LockSupport.park()
372                               JVMTI_THREAD_STATE_WAITING +
373                               JVMTI_THREAD_STATE_WAITING_INDEFINITELY +
374                               JVMTI_THREAD_STATE_PARKED,
375    PARKED_TIMED             = JVMTI_THREAD_STATE_ALIVE +          // LockSupport.park(long)
376                               JVMTI_THREAD_STATE_WAITING +
377                               JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT +
378                               JVMTI_THREAD_STATE_PARKED,
379    BLOCKED_ON_MONITOR_ENTER = JVMTI_THREAD_STATE_ALIVE +          // (re-)entering a synchronization block
380                               JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER,
381    TERMINATED               = JVMTI_THREAD_STATE_TERMINATED
382  };
383  // Write thread status info to threadStatus field of java.lang.Thread.
384  static void set_thread_status(oop java_thread_oop, ThreadStatus status);
385  // Read thread status info from threadStatus field of java.lang.Thread.
386  static ThreadStatus get_thread_status(oop java_thread_oop);
387
388  static const char*  thread_status_name(oop java_thread_oop);
389
390  // Debugging
391  friend class JavaClasses;
392};
393
394// Interface to java.lang.ThreadGroup objects
395
396class java_lang_ThreadGroup : AllStatic {
397 private:
398  static int _parent_offset;
399  static int _name_offset;
400  static int _threads_offset;
401  static int _groups_offset;
402  static int _maxPriority_offset;
403  static int _destroyed_offset;
404  static int _daemon_offset;
405  static int _vmAllowSuspension_offset;
406  static int _nthreads_offset;
407  static int _ngroups_offset;
408
409  static void compute_offsets();
410
411 public:
412  // parent ThreadGroup
413  static oop  parent(oop java_thread_group);
414  // name
415  static typeArrayOop name(oop java_thread_group);
416  // ("name as oop" accessor is not necessary)
417  // Number of threads in group
418  static int nthreads(oop java_thread_group);
419  // threads
420  static objArrayOop threads(oop java_thread_group);
421  // Number of threads in group
422  static int ngroups(oop java_thread_group);
423  // groups
424  static objArrayOop groups(oop java_thread_group);
425  // maxPriority in group
426  static ThreadPriority maxPriority(oop java_thread_group);
427  // Destroyed
428  static bool is_destroyed(oop java_thread_group);
429  // Daemon
430  static bool is_daemon(oop java_thread_group);
431  // vmAllowSuspension
432  static bool is_vmAllowSuspension(oop java_thread_group);
433  // Debugging
434  friend class JavaClasses;
435};
436
437
438
439// Interface to java.lang.Throwable objects
440
441class java_lang_Throwable: AllStatic {
442  friend class BacktraceBuilder;
443
444 private:
445  // Offsets
446  enum {
447    hc_backtrace_offset     =  0,
448    hc_detailMessage_offset =  1,
449    hc_cause_offset         =  2,  // New since 1.4
450    hc_stackTrace_offset    =  3   // New since 1.4
451  };
452  enum {
453      hc_static_unassigned_stacktrace_offset = 0  // New since 1.7
454  };
455  // Trace constants
456  enum {
457    trace_methods_offset = 0,
458    trace_bcis_offset    = 1,
459    trace_mirrors_offset = 2,
460    trace_next_offset    = 3,
461    trace_size           = 4,
462    trace_chunk_size     = 32
463  };
464
465  static int backtrace_offset;
466  static int detailMessage_offset;
467  static int cause_offset;
468  static int stackTrace_offset;
469  static int static_unassigned_stacktrace_offset;
470
471  // Printing
472  static char* print_stack_element_to_buffer(Method* method, int bci);
473  static void print_to_stream(Handle stream, const char* str);
474  // StackTrace (programmatic access, new since 1.4)
475  static void clear_stacktrace(oop throwable);
476  // No stack trace available
477  static const char* no_stack_trace_message();
478  // Stacktrace (post JDK 1.7.0 to allow immutability protocol to be followed)
479  static void set_stacktrace(oop throwable, oop st_element_array);
480  static oop unassigned_stacktrace();
481
482 public:
483  // Backtrace
484  static oop backtrace(oop throwable);
485  static void set_backtrace(oop throwable, oop value);
486  // Needed by JVMTI to filter out this internal field.
487  static int get_backtrace_offset() { return backtrace_offset;}
488  static int get_detailMessage_offset() { return detailMessage_offset;}
489  // Message
490  static oop message(oop throwable);
491  static oop message(Handle throwable);
492  static void set_message(oop throwable, oop value);
493  // Print stack trace stored in exception by call-back to Java
494  // Note: this is no longer used in Merlin, but we still suppport
495  // it for compatibility.
496  static void print_stack_trace(oop throwable, oop print_stream);
497  static void print_stack_element(Handle stream, Method* method, int bci);
498  static void print_stack_element(outputStream *st, Method* method, int bci);
499  static void print_stack_usage(Handle stream);
500
501  // Allocate space for backtrace (created but stack trace not filled in)
502  static void allocate_backtrace(Handle throwable, TRAPS);
503  // Fill in current stack trace for throwable with preallocated backtrace (no GC)
504  static void fill_in_stack_trace_of_preallocated_backtrace(Handle throwable);
505  // Fill in current stack trace, can cause GC
506  static void fill_in_stack_trace(Handle throwable, methodHandle method, TRAPS);
507  static void fill_in_stack_trace(Handle throwable, methodHandle method = methodHandle());
508  // Programmatic access to stack trace
509  static oop  get_stack_trace_element(oop throwable, int index, TRAPS);
510  static int  get_stack_trace_depth(oop throwable, TRAPS);
511  // Printing
512  static void print(oop throwable, outputStream* st);
513  static void print(Handle throwable, outputStream* st);
514  static void print_stack_trace(oop throwable, outputStream* st);
515  // Debugging
516  friend class JavaClasses;
517};
518
519
520// Interface to java.lang.reflect.AccessibleObject objects
521
522class java_lang_reflect_AccessibleObject: AllStatic {
523 private:
524  // Note that to reduce dependencies on the JDK we compute these
525  // offsets at run-time.
526  static int override_offset;
527
528  static void compute_offsets();
529
530 public:
531  // Accessors
532  static jboolean override(oop reflect);
533  static void set_override(oop reflect, jboolean value);
534
535  // Debugging
536  friend class JavaClasses;
537};
538
539
540// Interface to java.lang.reflect.Method objects
541
542class java_lang_reflect_Method : public java_lang_reflect_AccessibleObject {
543 private:
544  // Note that to reduce dependencies on the JDK we compute these
545  // offsets at run-time.
546  static int clazz_offset;
547  static int name_offset;
548  static int returnType_offset;
549  static int parameterTypes_offset;
550  static int exceptionTypes_offset;
551  static int slot_offset;
552  static int modifiers_offset;
553  static int signature_offset;
554  static int annotations_offset;
555  static int parameter_annotations_offset;
556  static int annotation_default_offset;
557  static int type_annotations_offset;
558
559  static void compute_offsets();
560
561 public:
562  // Allocation
563  static Handle create(TRAPS);
564
565  // Accessors
566  static oop clazz(oop reflect);
567  static void set_clazz(oop reflect, oop value);
568
569  static oop name(oop method);
570  static void set_name(oop method, oop value);
571
572  static oop return_type(oop method);
573  static void set_return_type(oop method, oop value);
574
575  static oop parameter_types(oop method);
576  static void set_parameter_types(oop method, oop value);
577
578  static oop exception_types(oop method);
579  static void set_exception_types(oop method, oop value);
580
581  static int slot(oop reflect);
582  static void set_slot(oop reflect, int value);
583
584  static int modifiers(oop method);
585  static void set_modifiers(oop method, int value);
586
587  static bool has_signature_field();
588  static oop signature(oop method);
589  static void set_signature(oop method, oop value);
590
591  static bool has_annotations_field();
592  static oop annotations(oop method);
593  static void set_annotations(oop method, oop value);
594
595  static bool has_parameter_annotations_field();
596  static oop parameter_annotations(oop method);
597  static void set_parameter_annotations(oop method, oop value);
598
599  static bool has_annotation_default_field();
600  static oop annotation_default(oop method);
601  static void set_annotation_default(oop method, oop value);
602
603  static bool has_type_annotations_field();
604  static oop type_annotations(oop method);
605  static void set_type_annotations(oop method, oop value);
606
607  // Debugging
608  friend class JavaClasses;
609};
610
611
612// Interface to java.lang.reflect.Constructor objects
613
614class java_lang_reflect_Constructor : public java_lang_reflect_AccessibleObject {
615 private:
616  // Note that to reduce dependencies on the JDK we compute these
617  // offsets at run-time.
618  static int clazz_offset;
619  static int parameterTypes_offset;
620  static int exceptionTypes_offset;
621  static int slot_offset;
622  static int modifiers_offset;
623  static int signature_offset;
624  static int annotations_offset;
625  static int parameter_annotations_offset;
626  static int type_annotations_offset;
627
628  static void compute_offsets();
629
630 public:
631  // Allocation
632  static Handle create(TRAPS);
633
634  // Accessors
635  static oop clazz(oop reflect);
636  static void set_clazz(oop reflect, oop value);
637
638  static oop parameter_types(oop constructor);
639  static void set_parameter_types(oop constructor, oop value);
640
641  static oop exception_types(oop constructor);
642  static void set_exception_types(oop constructor, oop value);
643
644  static int slot(oop reflect);
645  static void set_slot(oop reflect, int value);
646
647  static int modifiers(oop constructor);
648  static void set_modifiers(oop constructor, int value);
649
650  static bool has_signature_field();
651  static oop signature(oop constructor);
652  static void set_signature(oop constructor, oop value);
653
654  static bool has_annotations_field();
655  static oop annotations(oop constructor);
656  static void set_annotations(oop constructor, oop value);
657
658  static bool has_parameter_annotations_field();
659  static oop parameter_annotations(oop method);
660  static void set_parameter_annotations(oop method, oop value);
661
662  static bool has_type_annotations_field();
663  static oop type_annotations(oop constructor);
664  static void set_type_annotations(oop constructor, oop value);
665
666  // Debugging
667  friend class JavaClasses;
668};
669
670
671// Interface to java.lang.reflect.Field objects
672
673class java_lang_reflect_Field : public java_lang_reflect_AccessibleObject {
674 private:
675  // Note that to reduce dependencies on the JDK we compute these
676  // offsets at run-time.
677  static int clazz_offset;
678  static int name_offset;
679  static int type_offset;
680  static int slot_offset;
681  static int modifiers_offset;
682  static int signature_offset;
683  static int annotations_offset;
684  static int type_annotations_offset;
685
686  static void compute_offsets();
687
688 public:
689  // Allocation
690  static Handle create(TRAPS);
691
692  // Accessors
693  static oop clazz(oop reflect);
694  static void set_clazz(oop reflect, oop value);
695
696  static oop name(oop field);
697  static void set_name(oop field, oop value);
698
699  static oop type(oop field);
700  static void set_type(oop field, oop value);
701
702  static int slot(oop reflect);
703  static void set_slot(oop reflect, int value);
704
705  static int modifiers(oop field);
706  static void set_modifiers(oop field, int value);
707
708  static bool has_signature_field();
709  static oop signature(oop constructor);
710  static void set_signature(oop constructor, oop value);
711
712  static bool has_annotations_field();
713  static oop annotations(oop constructor);
714  static void set_annotations(oop constructor, oop value);
715
716  static bool has_parameter_annotations_field();
717  static oop parameter_annotations(oop method);
718  static void set_parameter_annotations(oop method, oop value);
719
720  static bool has_annotation_default_field();
721  static oop annotation_default(oop method);
722  static void set_annotation_default(oop method, oop value);
723
724  static bool has_type_annotations_field();
725  static oop type_annotations(oop field);
726  static void set_type_annotations(oop field, oop value);
727
728  // Debugging
729  friend class JavaClasses;
730};
731
732// Interface to sun.reflect.ConstantPool objects
733class sun_reflect_ConstantPool {
734 private:
735  // Note that to reduce dependencies on the JDK we compute these
736  // offsets at run-time.
737  static int _oop_offset;
738
739  static void compute_offsets();
740
741 public:
742  // Allocation
743  static Handle create(TRAPS);
744
745  // Accessors
746  static void set_cp(oop reflect, ConstantPool* value);
747  static int oop_offset() {
748    return _oop_offset;
749  }
750
751  static ConstantPool* get_cp(oop reflect);
752
753  // Debugging
754  friend class JavaClasses;
755};
756
757// Interface to sun.reflect.UnsafeStaticFieldAccessorImpl objects
758class sun_reflect_UnsafeStaticFieldAccessorImpl {
759 private:
760  static int _base_offset;
761  static void compute_offsets();
762
763 public:
764  static int base_offset() {
765    return _base_offset;
766  }
767
768  // Debugging
769  friend class JavaClasses;
770};
771
772// Interface to java.lang primitive type boxing objects:
773//  - java.lang.Boolean
774//  - java.lang.Character
775//  - java.lang.Float
776//  - java.lang.Double
777//  - java.lang.Byte
778//  - java.lang.Short
779//  - java.lang.Integer
780//  - java.lang.Long
781
782// This could be separated out into 8 individual classes.
783
784class java_lang_boxing_object: AllStatic {
785 private:
786  enum {
787   hc_value_offset = 0
788  };
789  static int value_offset;
790  static int long_value_offset;
791
792  static oop initialize_and_allocate(BasicType type, TRAPS);
793 public:
794  // Allocation. Returns a boxed value, or NULL for invalid type.
795  static oop create(BasicType type, jvalue* value, TRAPS);
796  // Accessors. Returns the basic type being boxed, or T_ILLEGAL for invalid oop.
797  static BasicType get_value(oop box, jvalue* value);
798  static BasicType set_value(oop box, jvalue* value);
799  static BasicType basic_type(oop box);
800  static bool is_instance(oop box)                 { return basic_type(box) != T_ILLEGAL; }
801  static bool is_instance(oop box, BasicType type) { return basic_type(box) == type; }
802  static void print(oop box, outputStream* st)     { jvalue value;  print(get_value(box, &value), &value, st); }
803  static void print(BasicType type, jvalue* value, outputStream* st);
804
805  static int value_offset_in_bytes(BasicType type) {
806    return ( type == T_LONG || type == T_DOUBLE ) ? long_value_offset :
807                                                    value_offset;
808  }
809
810  // Debugging
811  friend class JavaClasses;
812};
813
814
815
816// Interface to java.lang.ref.Reference objects
817
818class java_lang_ref_Reference: AllStatic {
819 public:
820  enum {
821   hc_referent_offset   = 0,
822   hc_queue_offset      = 1,
823   hc_next_offset       = 2,
824   hc_discovered_offset = 3  // Is not last, see SoftRefs.
825  };
826  enum {
827   hc_static_lock_offset    = 0,
828   hc_static_pending_offset = 1
829  };
830
831  static int referent_offset;
832  static int queue_offset;
833  static int next_offset;
834  static int discovered_offset;
835  static int static_lock_offset;
836  static int static_pending_offset;
837  static int number_of_fake_oop_fields;
838
839  // Accessors
840  static oop referent(oop ref) {
841    return ref->obj_field(referent_offset);
842  }
843  static void set_referent(oop ref, oop value) {
844    ref->obj_field_put(referent_offset, value);
845  }
846  static void set_referent_raw(oop ref, oop value) {
847    ref->obj_field_put_raw(referent_offset, value);
848  }
849  static HeapWord* referent_addr(oop ref) {
850    return ref->obj_field_addr<HeapWord>(referent_offset);
851  }
852  static oop next(oop ref) {
853    return ref->obj_field(next_offset);
854  }
855  static void set_next(oop ref, oop value) {
856    ref->obj_field_put(next_offset, value);
857  }
858  static void set_next_raw(oop ref, oop value) {
859    ref->obj_field_put_raw(next_offset, value);
860  }
861  static HeapWord* next_addr(oop ref) {
862    return ref->obj_field_addr<HeapWord>(next_offset);
863  }
864  static oop discovered(oop ref) {
865    return ref->obj_field(discovered_offset);
866  }
867  static void set_discovered(oop ref, oop value) {
868    ref->obj_field_put(discovered_offset, value);
869  }
870  static void set_discovered_raw(oop ref, oop value) {
871    ref->obj_field_put_raw(discovered_offset, value);
872  }
873  static HeapWord* discovered_addr(oop ref) {
874    return ref->obj_field_addr<HeapWord>(discovered_offset);
875  }
876  // Accessors for statics
877  static oop  pending_list_lock();
878  static oop  pending_list();
879
880  static HeapWord*  pending_list_lock_addr();
881  static HeapWord*  pending_list_addr();
882};
883
884
885// Interface to java.lang.ref.SoftReference objects
886
887class java_lang_ref_SoftReference: public java_lang_ref_Reference {
888 public:
889  enum {
890   // The timestamp is a long field and may need to be adjusted for alignment.
891   hc_timestamp_offset  = hc_discovered_offset + 1
892  };
893  enum {
894   hc_static_clock_offset = 0
895  };
896
897  static int timestamp_offset;
898  static int static_clock_offset;
899
900  // Accessors
901  static jlong timestamp(oop ref);
902
903  // Accessors for statics
904  static jlong clock();
905  static void set_clock(jlong value);
906};
907
908
909// Interface to java.lang.invoke.MethodHandle objects
910
911class MethodHandleEntry;
912
913class java_lang_invoke_MethodHandle: AllStatic {
914  friend class JavaClasses;
915
916 private:
917  static int _type_offset;               // the MethodType of this MH
918  static int _form_offset;               // the LambdaForm of this MH
919
920  static void compute_offsets();
921
922 public:
923  // Accessors
924  static oop            type(oop mh);
925  static void       set_type(oop mh, oop mtype);
926
927  static oop            form(oop mh);
928  static void       set_form(oop mh, oop lform);
929
930  // Testers
931  static bool is_subclass(Klass* klass) {
932    return klass->is_subclass_of(SystemDictionary::MethodHandle_klass());
933  }
934  static bool is_instance(oop obj) {
935    return obj != NULL && is_subclass(obj->klass());
936  }
937
938  // Accessors for code generation:
939  static int type_offset_in_bytes()             { return _type_offset; }
940  static int form_offset_in_bytes()             { return _form_offset; }
941};
942
943// Interface to java.lang.invoke.LambdaForm objects
944// (These are a private interface for managing adapter code generation.)
945
946class java_lang_invoke_LambdaForm: AllStatic {
947  friend class JavaClasses;
948
949 private:
950  static int _vmentry_offset;  // type is MemberName
951
952  static void compute_offsets();
953
954 public:
955  // Accessors
956  static oop            vmentry(oop lform);
957  static void       set_vmentry(oop lform, oop invoker);
958
959  // Testers
960  static bool is_subclass(Klass* klass) {
961    return SystemDictionary::LambdaForm_klass() != NULL &&
962      klass->is_subclass_of(SystemDictionary::LambdaForm_klass());
963  }
964  static bool is_instance(oop obj) {
965    return obj != NULL && is_subclass(obj->klass());
966  }
967
968  // Accessors for code generation:
969  static int vmentry_offset_in_bytes()          { return _vmentry_offset; }
970};
971
972
973// Interface to java.lang.invoke.MemberName objects
974// (These are a private interface for Java code to query the class hierarchy.)
975
976#define MEMBERNAME_INJECTED_FIELDS(macro)                               \
977  macro(java_lang_invoke_MemberName, vmloader, object_signature, false) \
978  macro(java_lang_invoke_MemberName, vmindex,  intptr_signature, false) \
979  macro(java_lang_invoke_MemberName, vmtarget, intptr_signature, false)
980
981class java_lang_invoke_MemberName: AllStatic {
982  friend class JavaClasses;
983
984 private:
985  // From java.lang.invoke.MemberName:
986  //    private Class<?>   clazz;       // class in which the method is defined
987  //    private String     name;        // may be null if not yet materialized
988  //    private Object     type;        // may be null if not yet materialized
989  //    private int        flags;       // modifier bits; see reflect.Modifier
990  //    private intptr     vmtarget;    // VM-specific target value
991  //    private intptr_t   vmindex;     // member index within class or interface
992  static int _clazz_offset;
993  static int _name_offset;
994  static int _type_offset;
995  static int _flags_offset;
996  static int _vmtarget_offset;
997  static int _vmloader_offset;
998  static int _vmindex_offset;
999
1000  static void compute_offsets();
1001
1002 public:
1003  // Accessors
1004  static oop            clazz(oop mname);
1005  static void       set_clazz(oop mname, oop clazz);
1006
1007  static oop            type(oop mname);
1008  static void       set_type(oop mname, oop type);
1009
1010  static oop            name(oop mname);
1011  static void       set_name(oop mname, oop name);
1012
1013  static int            flags(oop mname);
1014  static void       set_flags(oop mname, int flags);
1015
1016  static Metadata*      vmtarget(oop mname);
1017  static void       set_vmtarget(oop mname, Metadata* target);
1018
1019  static intptr_t       vmindex(oop mname);
1020  static void       set_vmindex(oop mname, intptr_t index);
1021
1022  // Testers
1023  static bool is_subclass(Klass* klass) {
1024    return klass->is_subclass_of(SystemDictionary::MemberName_klass());
1025  }
1026  static bool is_instance(oop obj) {
1027    return obj != NULL && is_subclass(obj->klass());
1028  }
1029
1030  // Relevant integer codes (keep these in synch. with MethodHandleNatives.Constants):
1031  enum {
1032    MN_IS_METHOD           = 0x00010000, // method (not constructor)
1033    MN_IS_CONSTRUCTOR      = 0x00020000, // constructor
1034    MN_IS_FIELD            = 0x00040000, // field
1035    MN_IS_TYPE             = 0x00080000, // nested type
1036    MN_REFERENCE_KIND_SHIFT = 24, // refKind
1037    MN_REFERENCE_KIND_MASK = 0x0F000000 >> MN_REFERENCE_KIND_SHIFT,
1038    // The SEARCH_* bits are not for MN.flags but for the matchFlags argument of MHN.getMembers:
1039    MN_SEARCH_SUPERCLASSES = 0x00100000, // walk super classes
1040    MN_SEARCH_INTERFACES   = 0x00200000  // walk implemented interfaces
1041  };
1042
1043  // Accessors for code generation:
1044  static int clazz_offset_in_bytes()            { return _clazz_offset; }
1045  static int type_offset_in_bytes()             { return _type_offset; }
1046  static int name_offset_in_bytes()             { return _name_offset; }
1047  static int flags_offset_in_bytes()            { return _flags_offset; }
1048  static int vmtarget_offset_in_bytes()         { return _vmtarget_offset; }
1049  static int vmindex_offset_in_bytes()          { return _vmindex_offset; }
1050};
1051
1052
1053// Interface to java.lang.invoke.MethodType objects
1054
1055class java_lang_invoke_MethodType: AllStatic {
1056  friend class JavaClasses;
1057
1058 private:
1059  static int _rtype_offset;
1060  static int _ptypes_offset;
1061
1062  static void compute_offsets();
1063
1064 public:
1065  // Accessors
1066  static oop            rtype(oop mt);
1067  static objArrayOop    ptypes(oop mt);
1068
1069  static oop            ptype(oop mt, int index);
1070  static int            ptype_count(oop mt);
1071
1072  static int            ptype_slot_count(oop mt);  // extra counts for long/double
1073  static int            rtype_slot_count(oop mt);  // extra counts for long/double
1074
1075  static Symbol*        as_signature(oop mt, bool intern_if_not_found, TRAPS);
1076  static void           print_signature(oop mt, outputStream* st);
1077
1078  static bool is_instance(oop obj) {
1079    return obj != NULL && obj->klass() == SystemDictionary::MethodType_klass();
1080  }
1081
1082  static bool equals(oop mt1, oop mt2);
1083
1084  // Accessors for code generation:
1085  static int rtype_offset_in_bytes()            { return _rtype_offset; }
1086  static int ptypes_offset_in_bytes()           { return _ptypes_offset; }
1087};
1088
1089
1090// Interface to java.lang.invoke.CallSite objects
1091
1092class java_lang_invoke_CallSite: AllStatic {
1093  friend class JavaClasses;
1094
1095private:
1096  static int _target_offset;
1097
1098  static void compute_offsets();
1099
1100public:
1101  // Accessors
1102  static oop              target(         oop site)             { return site->obj_field(             _target_offset);         }
1103  static void         set_target(         oop site, oop target) {        site->obj_field_put(         _target_offset, target); }
1104
1105  static volatile oop     target_volatile(oop site)             { return site->obj_field_volatile(    _target_offset);         }
1106  static void         set_target_volatile(oop site, oop target) {        site->obj_field_put_volatile(_target_offset, target); }
1107
1108  // Testers
1109  static bool is_subclass(Klass* klass) {
1110    return klass->is_subclass_of(SystemDictionary::CallSite_klass());
1111  }
1112  static bool is_instance(oop obj) {
1113    return obj != NULL && is_subclass(obj->klass());
1114  }
1115
1116  // Accessors for code generation:
1117  static int target_offset_in_bytes()           { return _target_offset; }
1118};
1119
1120
1121// Interface to java.security.AccessControlContext objects
1122
1123class java_security_AccessControlContext: AllStatic {
1124 private:
1125  // Note that for this class the layout changed between JDK1.2 and JDK1.3,
1126  // so we compute the offsets at startup rather than hard-wiring them.
1127  static int _context_offset;
1128  static int _privilegedContext_offset;
1129  static int _isPrivileged_offset;
1130
1131  static void compute_offsets();
1132 public:
1133  static oop create(objArrayHandle context, bool isPrivileged, Handle privileged_context, TRAPS);
1134
1135  // Debugging/initialization
1136  friend class JavaClasses;
1137};
1138
1139
1140// Interface to java.lang.ClassLoader objects
1141
1142#define CLASSLOADER_INJECTED_FIELDS(macro)                            \
1143  macro(java_lang_ClassLoader, loader_data,  intptr_signature, false)
1144
1145class java_lang_ClassLoader : AllStatic {
1146 private:
1147  // The fake offsets are added by the class loader when java.lang.Class is loaded
1148  enum {
1149   hc_parent_offset = 0
1150  };
1151  static int _loader_data_offset;
1152  static bool offsets_computed;
1153  static int parent_offset;
1154  static int parallelCapable_offset;
1155
1156 public:
1157  static void compute_offsets();
1158
1159  static ClassLoaderData** loader_data_addr(oop loader);
1160  static ClassLoaderData* loader_data(oop loader);
1161
1162  static oop parent(oop loader);
1163  static bool isAncestor(oop loader, oop cl);
1164
1165  // Support for parallelCapable field
1166  static bool parallelCapable(oop the_class_mirror);
1167
1168  static bool is_trusted_loader(oop loader);
1169
1170  // Fix for 4474172
1171  static oop  non_reflection_class_loader(oop loader);
1172
1173  // Testers
1174  static bool is_subclass(Klass* klass) {
1175    return klass->is_subclass_of(SystemDictionary::ClassLoader_klass());
1176  }
1177  static bool is_instance(oop obj) {
1178    return obj != NULL && is_subclass(obj->klass());
1179  }
1180
1181  // Debugging
1182  friend class JavaClasses;
1183  friend class ClassFileParser; // access to number_of_fake_fields
1184};
1185
1186
1187// Interface to java.lang.System objects
1188
1189class java_lang_System : AllStatic {
1190 private:
1191  enum {
1192   hc_static_in_offset  = 0,
1193   hc_static_out_offset = 1,
1194   hc_static_err_offset = 2
1195  };
1196
1197  static int  static_in_offset;
1198  static int static_out_offset;
1199  static int static_err_offset;
1200
1201 public:
1202  static int  in_offset_in_bytes();
1203  static int out_offset_in_bytes();
1204  static int err_offset_in_bytes();
1205
1206  // Debugging
1207  friend class JavaClasses;
1208};
1209
1210
1211// Interface to java.lang.StackTraceElement objects
1212
1213class java_lang_StackTraceElement: AllStatic {
1214 private:
1215  enum {
1216    hc_declaringClass_offset  = 0,
1217    hc_methodName_offset = 1,
1218    hc_fileName_offset   = 2,
1219    hc_lineNumber_offset = 3
1220  };
1221
1222  static int declaringClass_offset;
1223  static int methodName_offset;
1224  static int fileName_offset;
1225  static int lineNumber_offset;
1226
1227 public:
1228  // Setters
1229  static void set_declaringClass(oop element, oop value);
1230  static void set_methodName(oop element, oop value);
1231  static void set_fileName(oop element, oop value);
1232  static void set_lineNumber(oop element, int value);
1233
1234  // Create an instance of StackTraceElement
1235  static oop create(methodHandle m, int bci, TRAPS);
1236
1237  // Debugging
1238  friend class JavaClasses;
1239};
1240
1241
1242// Interface to java.lang.AssertionStatusDirectives objects
1243
1244class java_lang_AssertionStatusDirectives: AllStatic {
1245 private:
1246  enum {
1247    hc_classes_offset,
1248    hc_classEnabled_offset,
1249    hc_packages_offset,
1250    hc_packageEnabled_offset,
1251    hc_deflt_offset
1252  };
1253
1254  static int classes_offset;
1255  static int classEnabled_offset;
1256  static int packages_offset;
1257  static int packageEnabled_offset;
1258  static int deflt_offset;
1259
1260 public:
1261  // Setters
1262  static void set_classes(oop obj, oop val);
1263  static void set_classEnabled(oop obj, oop val);
1264  static void set_packages(oop obj, oop val);
1265  static void set_packageEnabled(oop obj, oop val);
1266  static void set_deflt(oop obj, bool val);
1267  // Debugging
1268  friend class JavaClasses;
1269};
1270
1271
1272class java_nio_Buffer: AllStatic {
1273 private:
1274  static int _limit_offset;
1275
1276 public:
1277  static int  limit_offset();
1278  static void compute_offsets();
1279};
1280
1281class java_util_concurrent_locks_AbstractOwnableSynchronizer : AllStatic {
1282 private:
1283  static int  _owner_offset;
1284 public:
1285  static void initialize(TRAPS);
1286  static oop  get_owner_threadObj(oop obj);
1287};
1288
1289// Use to declare fields that need to be injected into Java classes
1290// for the JVM to use.  The name_index and signature_index are
1291// declared in vmSymbols.  The may_be_java flag is used to declare
1292// fields that might already exist in Java but should be injected if
1293// they don't.  Otherwise the field is unconditionally injected and
1294// the JVM uses the injected one.  This is to ensure that name
1295// collisions don't occur.  In general may_be_java should be false
1296// unless there's a good reason.
1297
1298class InjectedField {
1299 public:
1300  const SystemDictionary::WKID klass_id;
1301  const vmSymbols::SID name_index;
1302  const vmSymbols::SID signature_index;
1303  const bool           may_be_java;
1304
1305
1306  Klass* klass() const    { return SystemDictionary::well_known_klass(klass_id); }
1307  Symbol* name() const      { return lookup_symbol(name_index); }
1308  Symbol* signature() const { return lookup_symbol(signature_index); }
1309
1310  int compute_offset();
1311
1312  // Find the Symbol for this index
1313  static Symbol* lookup_symbol(int symbol_index) {
1314    return vmSymbols::symbol_at((vmSymbols::SID)symbol_index);
1315  }
1316};
1317
1318#define DECLARE_INJECTED_FIELD_ENUM(klass, name, signature, may_be_java) \
1319  klass##_##name##_enum,
1320
1321#define ALL_INJECTED_FIELDS(macro)          \
1322  CLASS_INJECTED_FIELDS(macro)              \
1323  CLASSLOADER_INJECTED_FIELDS(macro)        \
1324  MEMBERNAME_INJECTED_FIELDS(macro)
1325
1326// Interface to hard-coded offset checking
1327
1328class JavaClasses : AllStatic {
1329 private:
1330
1331  static InjectedField _injected_fields[];
1332
1333  static bool check_offset(const char *klass_name, int offset, const char *field_name, const char* field_sig) PRODUCT_RETURN0;
1334  static bool check_static_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) PRODUCT_RETURN0;
1335  static bool check_constant(const char *klass_name, int constant, const char *field_name, const char* field_sig) PRODUCT_RETURN0;
1336
1337 public:
1338  enum InjectedFieldID {
1339    ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD_ENUM)
1340    MAX_enum
1341  };
1342
1343  static int compute_injected_offset(InjectedFieldID id);
1344
1345  static void compute_hard_coded_offsets();
1346  static void compute_offsets();
1347  static void check_offsets() PRODUCT_RETURN;
1348
1349  static InjectedField* get_injected(Symbol* class_name, int* field_count);
1350};
1351
1352#undef DECLARE_INJECTED_FIELD_ENUM
1353
1354#endif // SHARE_VM_CLASSFILE_JAVACLASSES_HPP
1355