field.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // field.h - Header file for fieldID instances. -*- c++ -*-
  2. /* Copyright (C) 1998, 1999, 2000, 2004, 2006 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. #ifndef __GCJ_FIELD_H__
  8. #define __GCJ_FIELD_H__
  9. #include <java/lang/Class.h>
  10. #include <java/lang/reflect/Field.h>
  11. #include <java/lang/reflect/Modifier.h>
  12. #include <gnu/gcj/RawData.h>
  13. #define _Jv_FIELD_UNRESOLVED_FLAG 0x8000
  14. struct _Jv_Field
  15. {
  16. struct _Jv_Utf8Const* name;
  17. /* The type of the field, if isResolved().
  18. If !isResolved(): The fields's signature as a (Utf8Const*). */
  19. jclass type;
  20. _Jv_ushort flags;
  21. _Jv_ushort bsize; /* not really needed ... */
  22. union {
  23. jint boffset; /* offset in bytes for instance field */
  24. char* addr; /* address of static field */
  25. jobject* object_addr; /* address of static object field... etc */
  26. jbyte* byte_addr;
  27. jshort* short_addr;
  28. jchar* char_addr;
  29. jint* int_addr;
  30. jlong* long_addr;
  31. jfloat* float_addr;
  32. jdouble* double_addr;
  33. } u;
  34. #ifdef __cplusplus
  35. jboolean isResolved ()
  36. { return ! (flags & _Jv_FIELD_UNRESOLVED_FLAG); }
  37. public:
  38. int getOffset () { return u.boffset; }
  39. jobject getObjectField (jobject obj)
  40. { return *(jobject *)((char *)obj + getOffset ()); }
  41. jfieldID getNextField () { return this + 1; }
  42. jboolean isRef ()
  43. {
  44. if (!isResolved ())
  45. {
  46. char first = ((_Jv_Utf8Const*)type)->first();
  47. return first == '[' || first == 'L';
  48. }
  49. else
  50. {
  51. return ! type->isPrimitive ();
  52. }
  53. }
  54. jclass getClass ()
  55. {
  56. // We can't use JvAssert here because it is not in a public
  57. // header.
  58. // JvAssert (isResolved ());
  59. return type;
  60. }
  61. // Need to mask off all unknown/internal flags before returning.
  62. int getModifiers()
  63. {
  64. return flags & java::lang::reflect::Modifier::ALL_FLAGS;
  65. }
  66. _Jv_Utf8Const * getNameUtf8Const (jclass) { return name; }
  67. #endif
  68. };
  69. #ifdef __cplusplus
  70. inline jbyte
  71. _Jv_GetStaticByteField (jclass, _Jv_Field* field)
  72. {
  73. return * (jbyte *) field->u.addr;
  74. }
  75. inline jshort
  76. _Jv_GetStaticShortField (jclass, _Jv_Field* field)
  77. {
  78. return * (jshort *) field->u.addr;
  79. }
  80. inline jint
  81. _Jv_GetStaticIntField (jclass, _Jv_Field* field)
  82. {
  83. return * (jint *) field->u.addr;
  84. }
  85. inline jlong
  86. _Jv_GetStaticLongField (jclass, _Jv_Field* field)
  87. {
  88. return * (jlong *) field->u.addr;
  89. }
  90. inline jobject
  91. _Jv_GetObjectField (jobject obj, _Jv_Field* field)
  92. {
  93. return field->getObjectField (obj);
  94. }
  95. inline jbyte
  96. _Jv_GetByteField (jobject obj, _Jv_Field* field)
  97. {
  98. return * (jbyte *) ((char*) obj + field->getOffset ());
  99. }
  100. inline jshort
  101. _Jv_GetShortField (jobject obj, _Jv_Field* field)
  102. {
  103. return * (jshort *) ((char*) obj + field->getOffset ());
  104. }
  105. inline jint
  106. _Jv_GetIntField (jobject obj, _Jv_Field* field)
  107. {
  108. return * (jint *) ((char*) obj + field->getOffset ());
  109. }
  110. inline jlong
  111. _Jv_GetLongField (jobject obj, _Jv_Field* field)
  112. {
  113. return * (jlong *) ((char*) obj + field->getOffset ());
  114. }
  115. extern inline jfieldID
  116. _Jv_FromReflectedField (java::lang::reflect::Field *field)
  117. {
  118. return (jfieldID) ((char *) field->declaringClass->fields + field->offset);
  119. }
  120. #ifdef __GCJ_CNI_H__
  121. extern inline jfieldID
  122. JvGetFirstInstanceField (jclass klass)
  123. {
  124. return &(klass->fields[klass->static_field_count]);
  125. }
  126. extern inline jint
  127. JvNumInstanceFields (jclass klass)
  128. {
  129. return klass->field_count - klass->static_field_count;
  130. }
  131. extern inline jfieldID
  132. JvGetFirstStaticField (jclass klass)
  133. {
  134. return &(klass->fields[0]);
  135. }
  136. extern inline jint
  137. JvNumStaticFields (jclass klass)
  138. {
  139. return klass->static_field_count;
  140. }
  141. extern inline jboolean
  142. JvFieldIsRef (jfieldID field)
  143. {
  144. return field->isRef () && field->type != &gnu::gcj::RawData::class$;
  145. }
  146. extern inline jobject
  147. JvGetObjectField (jobject obj, _Jv_Field* field)
  148. {
  149. return _Jv_GetObjectField (obj, field);
  150. }
  151. #endif /* defined (__GCJ_CNI_H__) */
  152. #endif /* __cplusplus */
  153. #endif /* __GCJ_FIELD_H */