summaryrefslogtreecommitdiff
path: root/libjava/gnu/gcj/util/Debug.java
blob: d19ab694d803142b330656e75a323d5678d94c54 (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
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
/*  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.  */

/* Utility methods that allow an object to be converted to a textual
   representation on an OutputStream.  The intention here is that this
   class be used for debugging, so we provide information about all
   fields, public or otherwise. */

package gnu.gcj.util;

import java.lang.reflect.*;
import java.io.*;
import java.util.*;

class Debug 
{
  private final PrintStream p;
  private final int maxdepth;
  private final int maxArrayLength;
  private final boolean printStaticFields;
  private int depth; 

  Debug(PrintStream writer, int maxdepth, int maxArrayLength, boolean printStaticFields)
  {
    p = writer;
    this.maxdepth = maxdepth;
    this.maxArrayLength = maxArrayLength;
    this.printStaticFields = printStaticFields;
  }

  Debug(PrintStream writer)
  {
    this(writer, 0, 10, false);
  }

  Debug(int maxdepth, boolean printStaticFields)
  {
    this(new PrintStream
	 (new FileOutputStream(FileDescriptor.err), true), 
	 maxdepth, 
	 maxdepth > 0 ? 1000 : 10, printStaticFields);
  }

  Debug(int maxdepth)
  {
    this(maxdepth, false);
  }

  Debug()
  {
    this(0, false);
  }
  
  private final void indent()
  {
    for (int i = 0; i < depth; i++)
      p.print("  ");
  }

  private final java.util.IdentityHashMap h = 
  new java.util.IdentityHashMap();

  private static native Field[] getDeclaredFields(Class c);
  private static native Object getField(Object o, Field f);
  private static native long getAddr(Object o);

  // Return an array containing all the fields of a class and its
  // superclasses.
  private Field[] internalGetFields(Class c)
  {
    HashSet set = new HashSet();
    set.addAll(Arrays.asList(getDeclaredFields(c)));
    Class[] interfaces = c.getInterfaces();
    for (int i = 0; i < interfaces.length; i++)
      set.addAll(Arrays.asList(internalGetFields(interfaces[i])));
    Class superClass = c.getSuperclass();
    if (superClass != null)
      set.addAll(Arrays.asList(internalGetFields(superClass)));
    return (Field[])set.toArray(new Field[set.size()]);
  }

  // FIXME: We could just use getClass() here, but this is a
  // workaround for a C++ bug that is causing getClass() to be
  // miscompiled.
  static private Class getItsClass(Object O)
  {
    return O.getClass();
  }

  // Print a reasonably readable textual representation of an object
  // on our OutputStream.  Objects are only printed once, no matter
  // how many references point to them.
  private void print(Object O)
  {
    int savedDepth = depth;
    h.put(O, O);
    try
      {
	Class C = getItsClass(O);
	p.print(C.getName() + "@");
	p.println(Long.toHexString(getAddr(O)));

	if (C.isArray())
	  {
	    indent(); p.println("{");
	    depth++;
	    indent();
	    C = C.getComponentType();

	    int len = Array.getLength(O);
	    for (int i = 0; i < len; i++)
	      {
		Object thing = Array.get(O, i);
		print0(thing, C);
		p.print(", ");
		if (i > maxArrayLength)
		  {
		    p.print("...");
		    break;
		  }
	      }
	    depth--;
	    p.println();
	    indent(); p.print("}");
	    return;
	  }

	indent(); p.println("{");
	depth++;
	if (C == java.lang.Class.class)
	  {
	    indent();
	    p.println ("class = " + O.toString() + ",");
	  }
	else if (C == java.lang.reflect.Field.class)
	  {
	    indent();
	    p.println ("<field> = \"" + O.toString() + "\",");
	  }
	else if (C == java.lang.String.class)
	  {
	    indent();
	    p.println ("<string> = \"" + O.toString() + "\",");
	  }
	Field[] f = internalGetFields(C);
	for (int i = 0; i < f.length; i++)
	  {
	    Class type = f[i].getType();
	    boolean isStatic = (f[i].getModifiers() & Modifier.STATIC) != 0;
 
	    if (isStatic && ! printStaticFields)
	      continue;

	    indent();
	    if (isStatic)
	      p.print("static ");
	    p.print(type.getName() +" " +f[i].getName() + " = ");
	    Object thing = getField(O, f[i]);
	    print0(thing, type);
	    p.println(",");
	  }
	depth--;
	indent(); p.print("}");
      }
    catch (Throwable t)
      {
	p.print("error: 0x" + Long.toHexString(getAddr(O)) + ";");
	depth = savedDepth;
      }
  }

  private void print0(Object thing, Class C)
  {
    try
      {
	if (thing == null)
	  {
	    p.print("null");
	    return;
	  }
	else if (C == gnu.gcj.RawData.class || 
		 C == gnu.gcj.RawDataManaged.class)
	  {
	  }
	else if (C.isPrimitive())
	  {
	    if (getItsClass(thing) == Character.class)				
	      p.print("'" + thing + "'");
	    else
	      p.print(thing);
	    return;
	  }
	else if (getItsClass(thing) == String.class)
	  {			  
	    p.print("\"" + thing + "\"");
	    return;
	  }
	else if (depth < maxdepth && h.get(thing) == null)
	  {
	    depth++;
	    print(thing);
	    depth--;
	    return;
	  }
      }
    catch (Throwable t)
      {
      }
    
    // The default action: just print the address.
    p.print("0x"+ Long.toHexString(getAddr(thing)));    
  }

  // Print the textual representation of an object on System.err.
  public void write(Object O)
  {
    depth = 0;
    print(O);
    p.flush();
  }
}