aboutsummaryrefslogtreecommitdiff
path: root/agent/src/share/classes/sun/jvm/hotspot/utilities/soql/JSJavaFactoryImpl.java
blob: bddde0f09d40bc6227c8e03738b06768bb5e269d (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
/*
 * Copyright 2004 Sun Microsystems, Inc.  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.
 *
 * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package sun.jvm.hotspot.utilities.soql;

import java.lang.ref.*;
import java.util.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.utilities.*;

public class JSJavaFactoryImpl implements JSJavaFactory {
   public JSJavaObject newJSJavaObject(Oop oop) {
      if (oop == null) return null;
      SoftReference sref = (SoftReference) om.get(oop);
      JSJavaObject res = (sref != null)? (JSJavaObject) sref.get() : null;
      if (res == null) {
         if (oop instanceof TypeArray) {
            res = new JSJavaTypeArray((TypeArray)oop, this);
         } else if (oop instanceof ObjArray) {
             res = new JSJavaObjArray((ObjArray)oop, this);
         } else if (oop instanceof Instance) {
            res = newJavaInstance((Instance) oop);
         } else if (oop instanceof Method) {
            res = new JSJavaMethod((Method) oop, this);
         }
      }
      if (res != null) {
         om.put(oop, new SoftReference(res));
      }
      return res;
   }

   public JSJavaKlass newJSJavaKlass(Klass klass) {
      JSJavaKlass res = null;
      if (klass instanceof InstanceKlass) {
          res = new JSJavaInstanceKlass((InstanceKlass) klass, this);
      } else if (klass instanceof ObjArrayKlass) {
          res = new JSJavaObjArrayKlass((ObjArrayKlass) klass, this);
      } else if (klass instanceof TypeArrayKlass) {
          res = new JSJavaTypeArrayKlass((TypeArrayKlass) klass, this);
      }
      if (res != null) {
         om.put(klass, new SoftReference(res));
      }
      return res;
   }

   public JSJavaField newJSJavaField(Field field) {
      if (field == null) return null;
      return new JSJavaField(field, this);
   }

   public JSJavaThread newJSJavaThread(JavaThread jthread) {
      if (jthread == null) return null;
      return new JSJavaThread(jthread, this);
   }

   public JSJavaFrame newJSJavaFrame(JavaVFrame jvf) {
      if (jvf == null) return null;
      return new JSJavaFrame(jvf, this);
   }

   public JSList newJSList(List list) {
      if (list == null) return null;
      return new JSList(list, this);
   }

   public JSMap newJSMap(Map map) {
      if (map == null) return null;
      return new JSMap(map, this);
   }

   public Object newJSJavaWrapper(Object item) {
      if (item == null) return null;
      if (item instanceof Oop) {
         return newJSJavaObject((Oop) item);
      } else if (item instanceof Field) {
         return newJSJavaField((Field) item);
      } else if (item instanceof JavaThread) {
         return newJSJavaThread((JavaThread) item);
      } else if (item instanceof JavaVFrame) {
         return newJSJavaFrame((JavaVFrame) item);
      } else if (item instanceof List) {
         return newJSList((List) item);
      } else if (item instanceof Map) {
         return newJSMap((Map) item);
      } else {
         // not-a-special-type, just return the input item
         return item;
      }
   }

   public JSJavaHeap newJSJavaHeap() {
      return new JSJavaHeap(this);
   }

   public JSJavaVM newJSJavaVM() {
      return new JSJavaVM(this);
   }

   // -- Internals only below this point
   private Symbol javaLangString() {
      if (javaLangString == null) {
         javaLangString = getSymbol("java/lang/String");
      }
      return javaLangString;
   }

   private Symbol javaLangThread() {
      if (javaLangThread == null) {
         javaLangThread = getSymbol("java/lang/Thread");
      }
      return javaLangThread;
   }

   private Symbol javaLangClass() {
      if (javaLangClass == null) {
         javaLangClass = getSymbol("java/lang/Class");
      }
      return javaLangClass;
   }

   private Symbol getSymbol(String str) {
      return VM.getVM().getSymbolTable().probe(str);
   }

   private JSJavaObject newJavaInstance(Instance instance) {
      // look for well-known classes
      Symbol className = instance.getKlass().getName();
      if (Assert.ASSERTS_ENABLED) {
         Assert.that(className != null, "Null class name");
      }
      JSJavaObject res = null;
      if (className.equals(javaLangString())) {
         res = new JSJavaString(instance, this);
      } else if (className.equals(javaLangThread())) {
         res = new JSJavaThread(instance, this);
      } else if (className.equals(javaLangClass())) {
         Klass reflectedType = OopUtilities.classOopToKlass(instance);
         if (reflectedType != null) {
             JSJavaKlass jk = newJSJavaKlass(reflectedType);
             // we don't support mirrors of VM internal Klasses
             if (jk == null) return null;
             res = new JSJavaClass(instance, jk, this);
         } else {
             // for primitive Classes, the reflected type is null
             return null;
         }
      } else {
         // not a well-known class. But the base class may be
         // one of the known classes.
         Klass kls = instance.getKlass().getSuper();
         while (kls != null) {
            className = kls.getName();
            // java.lang.Class and java.lang.String are final classes
            if (className.equals(javaLangThread())) {
               res = new JSJavaThread(instance, this);
               break;
            }
            kls = kls.getSuper();
         }
      }
      if (res == null) {
         res = new JSJavaInstance(instance, this);
      }
      return res;
   }

   // Map<Oop, SoftReference<JSJavaObject>>
   private Map om = new HashMap();
   private Symbol javaLangString;
   private Symbol javaLangThread;
   private Symbol javaLangClass;
}