summaryrefslogtreecommitdiff
path: root/libjava/gnu/gcj/util/natDebug.cc
blob: e2631b14a745941031005fce495406a7a6fc95c8 (plain)
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
// natDebug -- C++ side of Debug

/* Copyright (C) 2004  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

#include <config.h>
#include <stddef.h>
#include <gcj/cni.h>
#include <gcj/field.h>
#include <gcj/javaprims.h>
#include <java/lang/reflect/Field.h>
#include <java/lang/Class.h>
#include <java/lang/Byte.h>
#include <java/lang/Short.h>
#include <java/lang/Integer.h>
#include <java/lang/Long.h>
#include <java/lang/Float.h>
#include <java/lang/Double.h>
#include <java/lang/Boolean.h>
#include <java/lang/Character.h>
#include <java/lang/IllegalArgumentException.h>

#include <gnu/gcj/util/Debug.h>

jlong 
gnu::gcj::util::Debug::getAddr (::java::lang::Object *o)
{
  return (jlong)(size_t)o;
}

JArray< ::java::lang::reflect::Field *> *
gnu::gcj::util::Debug::getDeclaredFields (::java::lang::Class *c)
{
  return c->getDeclaredFields (false);
}

static void *
getField (::java::lang::Object *obj, 
	  ::java::lang::reflect::Field *field)
{
  using namespace java::lang::reflect;
  
  jfieldID fld = _Jv_FromReflectedField (field);
  _Jv_ushort flags = fld->getModifiers();

  if (flags & Modifier::STATIC)
    {
      jclass fldClass = field->getDeclaringClass ();
      JvInitClass(fldClass);
      return (void*) fld->u.addr;
    }
  else
    {
      return (void*) ((char*) obj + fld->getOffset ());
    }
}

::java::lang::Object *
gnu::gcj::util::Debug::getField (::java::lang::Object *o, 
				   ::java::lang::reflect::Field *field)
{
  void *addr = ::getField (o, field);

  jclass type = field->getType();
  if (! type->isPrimitive ())
    return * (jobject*) addr;
  if (type == JvPrimClass (double))
    return new ::java::lang::Double (* (jdouble*) addr);
  if (type == JvPrimClass (float))
    return new ::java::lang::Float (* (jfloat*) addr);
  if (type == JvPrimClass (long))
    return new ::java::lang::Long (* (jlong*) addr);
  if (type == JvPrimClass (int))
    return new ::java::lang::Integer (* (jint*) addr);
  if (type == JvPrimClass (short))
    return new ::java::lang::Short (* (jshort*) addr);
  if (type == JvPrimClass (byte))
    return new ::java::lang::Byte (* (jbyte*) addr);
  if (type == JvPrimClass (char))
    return new ::java::lang::Character (* (jchar*) addr);
  if (type == JvPrimClass (boolean))
    {
      _Jv_InitClass (&::java::lang::Boolean::class$);
      if (* (jboolean*) addr)
	return ::java::lang::Boolean::TRUE;
      else
	return ::java::lang::Boolean::FALSE;
    }
  throw new ::java::lang::IllegalArgumentException;
}

/* A simple method of printing an object that can be called from a
   debugger.  */
extern "C"
void
_Jv_Debug (void *p)
{
  (new ::gnu::gcj::util::Debug ())->write ((jobject)p);
}

extern "C"
void
_Jv_DeepDebug (void *p, int depth)
{
  (new ::gnu::gcj::util::Debug (depth))->write ((jobject)p);
}

extern "C"
void
_Jv_StaticDeepDebug (void *p, int depth)
{
  (new ::gnu::gcj::util::Debug (depth, true))->write ((jobject)p);
}