sa-jdi

StringBuffer();
    if (args.length > 1) {
      buf.append(args[1].toString());
    } else {
      buf.append('.');
    }
    buf.append(File.separatorChar);
    buf.append(ik.getName().asString().replace('/', File.separatorChar));
    buf.append(".class");
    String fileName = buf.toString();
    File file = new File(fileName);
    try
    {
      int index = fileName.lastIndexOf(File.separatorChar);
      File dir = new File(fileName.substring(0, index));
      dir.mkdirs();
      FileOutputStream fos = new FileOutputStream(file);
      ClassWriter cw = new ClassWriter(ik, fos);
      cw.write();
      fos.close();
    }
    catch (IOException exp)
    {
      printError(exp.toString(), exp);
      return Boolean.FALSE;
    }
    return Boolean.TRUE;
  }
  
  public Object dumpHeap(Object[] args)
  {
    String fileName = "heap.bin";
    if (args.length > 0) {
      fileName = args[0].toString();
    }
    return new JMap().writeHeapHprofBin(fileName) ? Boolean.TRUE : Boolean.FALSE;
  }
  
  public void help(Object[] args)
  {
    println("Function/Variable        Description");
    println("=================        ===========");
    println("address(jobject)         returns the address of the Java object");
    println("classof(jobject)         returns the class object of the Java object");
    println("dumpClass(jclass,[dir])  writes .class for the given Java Class");
    println("dumpHeap([file])         writes heap in hprof binary format");
    println("help()                   prints this help message");
    println("identityHash(jobject)    returns the hashCode of the Java object");
    println("mirror(jobject)          returns a local mirror of the Java object");
    println("load([file1, file2,...]) loads JavaScript file(s). With no files, reads <stdin>");
    println("object(string)           converts a string address into Java object");
    println("owner(jobject)           returns the owner thread of this monitor or null");
    println("sizeof(jobject)          returns the size of Java object in bytes");
    println("staticof(jclass, field)  returns a static field of the given Java class");
    println("read([prompt])           reads a single line from standard input");
    println("quit()                   quits the interactive load call");
    println("jvm                      the target jvm that is being debugged");
  }
  
  public Object identityHash(Object[] args)
  {
    if (args.length != 1) {
      return UNDEFINED;
    }
    Object o = args[0];
    if ((o != null) && ((o instanceof JSJavaObject))) {
      return new Long(((JSJavaObject)o).getOop().identityHash());
    }
    return UNDEFINED;
  }
  
  public void load(Object[] args)
  {
    for (int i = 0; i < args.length; i++) {
      processSource(args[i].toString());
    }
  }
  
  public Object mirror(Object[] args)
  {
    Object o = args[0];
    if ((o != null) && ((o instanceof JSJavaObject)))
    {
      Oop oop = ((JSJavaObject)o).getOop();
      Object res = null;
      try
      {
        if ((oop instanceof InstanceKlass)) {
          res = getObjectReader().readClass((InstanceKlass)oop);
        } else {
          res = getObjectReader().readObject(oop);
        }
      }
      catch (Exception e)
      {
        if (debug) {
          e.printStackTrace(getErrorStream());
        }
      }
      return res != null ? res : UNDEFINED;
    }
    return UNDEFINED;
  }
  
  public Object owner(Object[] args)
  {
    Object o = args[0];
    if ((o != null) && ((o instanceof JSJavaObject))) {
      return getOwningThread((JSJavaObject)o);
    }
    return UNDEFINED;
  }
  
  public Object object(Object[] args)
  {
    Object o = args[0];
    if ((o != null) && ((o instanceof String)))
    {
      VM vm = VM.getVM();
      Address addr = vm.getDebugger().parseAddress((String)o);
      Oop oop = vm.getObjectHeap().newOop(addr.addOffsetToAsOopHandle(0L));
      return getJSJavaFactory().newJSJavaObject(oop);
    }
    return UNDEFINED;
  }
  
  public Object sizeof(Object[] args)
  {
    if (args.length != 1) {
      return UNDEFINED;
    }
    Object o = args[0];
    if ((o != null) && ((o instanceof JSJavaObject))) {
      return new Long(((JSJavaObject)o).getOop().getObjectSize());
    }
    return UNDEFINED;
  }
  
  public Object staticof(Object[] args)
  {
    Object classname = args[0];
    Object fieldname = args[1];
    if ((fieldname == null) || (classname == null) || (!(fieldname instanceof String))) {
      return UNDEFINED;
    }
    InstanceKlass ik = null;
    if ((classname instanceof JSJavaClass))
    {
      JSJavaClass jclass = (JSJavaClass)classname;
      JSJavaKlass jk = jclass.getJSJavaKlass();
      if ((jk != null) && ((jk instanceof JSJavaInstanceKlass))) {
        ik = ((JSJavaInstanceKlass)jk).getInstanceKlass();
      }
    }
    else if ((classname instanceof String))
    {
      ik = SystemDictionaryHelper.findInstanceKlass((String)classname);
    }
    else
    {
      return UNDEFINED;
    }
    if (ik == null) {
      return UNDEFINED;
    }
    JSJavaFactory factory = getJSJavaFactory();
    try
    {
      return ((JSJavaInstanceKlass)factory.newJSJavaKlass(ik)).getStaticFieldValue((String)fieldname);
    }
    catch (NoSuchFieldException e) {}
    return UNDEFINED;
  }
  
  public Object read(Object[] args)
  {
    BufferedReader in = getInputReader();
    if (in == null) {
      return null;
    }
    if (args.length > 0)
    {
      print(args[0].toString());
      print(":");
    }
    try
    {
      return in.readLine();
    }
    catch (IOException exp)
    {
      exp.printStackTrace();
      throw new RuntimeException(exp);
    }
  }
  
  public void quit(Object[] args)
  {
    quit();
  }
  
  public void writeln(Object[] args)
  {
    for (int i = 0; i < args.length; i++)
    {
      print(args[i].toString());
      print(" ");
    }
    println("");
  }
  
  public void write(Object[] args)
  {
    for (int i = 0; i < args.length; i++)
    {
      print(args[i].toString());
      print(" ");
    }
  }
  
  protected void start(boolean console)
  {
    ScriptContext context = engine.getContext();
    OutputStream out = getOutputStream();
    if (out != null) {
      context.setWriter(new PrintWriter(out));
    }
    OutputStream err = getErrorStream();
    if (err != null) {
      context.setErrorWriter(new PrintWriter(err));
    }
    loadInitFile();
    
    loadUserInitFile();
    
    JSJavaFactory fac = getJSJavaFactory();
    JSJavaVM jvm = fac != null ? fac.newJSJavaVM() : null;
    
    call("main", new Object[] { this, jvm });
    if (console) {
      processSource(null);
    }
  }
  
  protected JSJavaScriptEngine(boolean debug)
  {
    this.debug = debug;
    ScriptEngineManager manager = new ScriptEngineManager();
    engine = manager.getEngineByName("javascript");
    if (engine == null) {
      throw new RuntimeException("can't load JavaScript engine");
    }
    Method[] methods = getClass().getMethods();
    for (int i = 0; i < methods.length; i++)
    {
      Method m = methods[i];
      if (Modifier.isPublic(m.getModifiers()))
      {
        Class[] argTypes = m.getParameterTypes();
        if ((argTypes.length == 1) && (argTypes[0] == new Object[0].getClass())) {
          putFunction(this, m);
        }
      }
    }
  }
  
  protected JSJavaScriptEngine()
  {
    this(false);
  }
  
  protected abstract ObjectReader getObjectReader();
  
  protected abstract JSJavaFactory getJSJavaFactory();
  
  protected void printPrompt(String str)
  {
    System.err.print(str);
    System.err.flush();
  }
  
  protected void loadInitFile()
  {
    InputStream is = JSJavaScriptEngine.class.getResourceAsStream("sa.js");
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    evalReader(reader, "sa.js");
  }
  
  protected void loadUserInitFile()
  {
    File initFile = new File(getUserInitFileDir(), getUserInitFileName());
    if ((initFile.exists()) && (initFile.isFile())) {
      processSource(initFile.getAbsolutePath());
    }
  }
  
  protected String getUserInitFileDir()
  {
    return System.getProperty("user.home");
  }
  
  protected String getUserInitFileName()
  {
    return "jsdb.js";
  }
  
  protected BufferedReader getInputReader()
  {
    if (inReader == null) {
      inReader = new BufferedReader(new InputStreamReader(System.in));
    }
    return inReader;
  }
  
  protected PrintStream getOutputStream()
  {
    return System.out;
  }
  
  protected PrintStream getErrorStream()
  {
    return System.err;
  }
  
  protected void print(String name)
  {
    getOutputStream().print(name);
  }
  
  protected void println(String name)
  {
    getOutputStream().println(name);
  }
  
  protected void printError(String message)
  {
    printError(message, null);
  }
  
  protected void printError(String message, Exception exp)
  {
    getErrorStream().println(message);
    if ((exp != null) && (debug)) {
      exp.printStackTrace(getErrorStream());
    }
  }
  
  protected boolean isQuitting()
  {
    return quitting;
  }
  
  protected void quit()
  {
    quitting = true;
  }
  
  protected ScriptEngine getScriptEngine()
  {
    return engine;
  }
  
  private JSJavaThread getOwningThread(JSJavaObject jo)
  {
    Oop oop = jo.getOop();
    Mark mark = oop.getMark();
    ObjectMonitor mon = null;
    Address owner = null;
    JSJavaThread owningThread = null;
    if (!mark.hasMonitor())
    {
      if (mark.hasLocker()) {
        owner = mark.locker().getAddress();
      }
    }
    else
    {
      mon = mark.monitor();
      
      owner = mon.owner();
    }
    if (owner != null)
    {
      JSJavaFactory factory = getJSJavaFactory();
      owningThread = factory.newJSJavaThread(VM.getVM().getThreads().owningThreadFromMonitor(owner));
    }
    return owningThread;
  }
  
  private void processSource(String filename)
  {
    if (filename == null)
    {
      BufferedReader in = getInputReader();
      String sourceName = "<stdin>";
      int lineno = 0;
      boolean hitEOF = false;
      do
      {
        int startline = lineno;
        printPrompt("jsdb> ");
        Object source = read(EMPTY_ARRAY);
        if (source == null)
        {
          hitEOF = true;
          break;
        }
        lineno++;
        Object result = evalString(source.toString(), sourceName, startline);
        if (result != null) {
          printError(result.toString());
        }
      } while ((!isQuitting()) && 
      
        (!hitEOF));
    }
    else
    {
      Reader in = null;
      try
      {
        in = new BufferedReader(new FileReader(filename));
        evalReader(in, filename);
      }
      catch (FileNotFoundException ex)
      {
        println("File '" + filename + "' not found");
        throw new RuntimeException(ex);
      }
    }
  }
  
  protected Object evalString(String source, String filename, int lineNum)
  {
    try
    {
      engine.put("javax.script.filename", filename);
      return engine.eval(source);
    }
    catch (ScriptException sexp)
    {
      printError(sexp.toString(), sexp);
    }
    catch (Exception exp)
    {
      printError(exp.toString(), exp);
    }
    return null;
  }
  
  private Object evalReader(Reader in, String filename)
  {
    try
    {
      engine.put("javax.script.filename", filename);
      return engine.eval(in);
    }
    catch (ScriptException sexp)
    {
      System.err.println(sexp);
      printError(sexp.toString(), sexp);
    }
    finally
    {
      try
      {
        in.close();
      }
      catch (IOException ioe)
      {
        printError(ioe.toString(), ioe);
      }
    }
    return null;
  }
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.JSJavaScriptEngine
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

import sun.jvm.hotspot.oops.Instance;
import sun.jvm.hotspot.oops.Oop;
import sun.jvm.hotspot.oops.OopUtilities;

public class JSJavaString
  extends JSJavaInstance
{
  public JSJavaString(Instance instance, JSJavaFactory fac)
  {
    super(instance, fac);
  }
  
  public String toString()
  {
    StringBuffer buf = new StringBuffer();
    buf.append("String (address=");
    buf.append(getOop().getHandle());
    buf.append(", value=");
    buf.append("'");
    buf.append(getString());
    buf.append('\'');
    buf.append(')');
    return buf.toString();
  }
  
  protected Object getFieldValue(String name)
  {
    if (name.equals("stringValue")) {
      return getString();
    }
    return super.getFieldValue(name);
  }
  
  protected String[] getFieldNames()
  {
    String[] fields = super.getFieldNames();
    String[] res = new String[fields.length + 1];
    System.arraycopy(fields, 0, res, 0, fields.length);
    res[fields.length] = "stringValue";
    return res;
  }
  
  protected boolean hasField(String name)
  {
    if (name.equals("stringValue")) {
      return true;
    }
    return super.hasField(name);
  }
  
  private String getString()
  {
    return OopUtilities.stringOopToString(getOop());
  }
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.JSJavaString
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import sun.jvm.hotspot.debugger.OopHandle;
import sun.jvm.hotspot.oops.Instance;
import sun.jvm.hotspot.oops.ObjectHeap;
import sun.jvm.hotspot.oops.Oop;
import sun.jvm.hotspot.oops.OopUtilities;
import sun.jvm.hotspot.runtime.JavaThread;
import sun.jvm.hotspot.runtime.JavaVFrame;
import sun.jvm.hotspot.runtime.MonitorInfo;
import sun.jvm.hotspot.runtime.ObjectMonitor;
import sun.jvm.hotspot.runtime.VM;

public class JSJavaThread
  extends JSJavaInstance
{
  private JavaThread jthread;
  private JSList framesCache;
  private JSList monitorsCache;
  
  public JSJavaThread(Instance threadOop, JSJavaFactory fac)
  {
    super(threadOop, fac);
    
    jthread = OopUtilities.threadOopGetJavaThread(threadOop);
  }
  
  public JSJavaThread(JavaThread jt, JSJavaFactory fac)
  {
    super((Instance)jt.getThreadObj(), fac);
    jthread = jt;
  }
  
  public String toString()
  {
    String name = getName();
    StringBuffer buf = new StringBuffer();
    buf.append("Thread (address=");
    buf.append(getOop().getHandle());
    buf.append(", name=");
    if (name != null) {
      buf.append(name);
    } else {
      buf.append("<unnamed>");
    }
    buf.append(')');
    return buf.toString();
  }
  
  protected Object getFieldValue(String name)
  {
    if (name.equals("name")) {
      return getName();
    }
    if (name.equals("frames")) {
      return getFrames();
    }
    if (name.equals("monitors")) {
      return getOwnedMonitors();
    }
    return super.getFieldValue(name);
  }
  
  protected String[] getFieldNames()
  {
    String[] flds = super.getFieldNames();
    String[] res = new String[flds.length + 2];
    System.arraycopy(flds, 0, res, 0, flds.length);
    res[flds.length] = "frames";
    res[(flds.length + 1)] = "monitors";
    return res;
  }
  
  protected boolean hasField(String name)
  {
    if ((name.equals("frames")) || (name.equals("monitors"))) {
      return true;
    }
    return super.hasField(name);
  }
  
  private String getName()
  {
    return OopUtilities.threadOopGetName(getOop());
  }
  
  private synchronized JSList getFrames()
  {
    if (framesCache == null)
    {
      List list = new ArrayList(0);
      if (jthread != null)
      {
        JavaVFrame jvf = jthread.getLastJavaVFrameDbg();
        while (jvf != null)
        {
          list.add(jvf);
          jvf = jvf.javaSender();
        }
      }
      framesCache = factory.newJSList(list);
    }
    return framesCache;
  }
  
  private synchronized JSList getOwnedMonitors()
  {
    if (monitorsCache == null)
    {
      List ownedMonitors = new ArrayList(0);
      ObjectHeap heap;
      Iterator loItr;
      if (jthread != null)
      {
        List lockedObjects = new ArrayList();
        
        ObjectMonitor waitingMonitor = jthread.getCurrentWaitingMonitor();
        OopHandle waitingObj = null;
        if (waitingMonitor != null) {
          waitingObj = waitingMonitor.object();
        }
        ObjectMonitor pendingMonitor = jthread.getCurrentPendingMonitor();
        OopHandle pendingObj = null;
        if (pendingMonitor != null) {
          pendingObj = pendingMonitor.object();
        }
        JavaVFrame frame = jthread.getLastJavaVFrameDbg();
        while (frame != null)
        {
          List frameMonitors = frame.getMonitors();
          for (Iterator miItr = frameMonitors.iterator(); miItr.hasNext();)
          {
            MonitorInfo mi = (MonitorInfo)miItr.next();
            if ((!mi.eliminated()) || (!frame.isCompiledFrame()))
            {
              OopHandle obj = mi.owner();
              if ((obj != null) && 
              
                (!obj.equals(waitingObj)) && 
                
                (!obj.equals(pendingObj)))
              {
                boolean found = false;
                for (Iterator loItr = lockedObjects.iterator(); loItr.hasNext();) {
                  if (obj.equals(loItr.next())) {
                    found = true;
                  }
                }
                if (!found) {
                  lockedObjects.add(obj);
                }
              }
            }
          }
          frame = frame.javaSender();
        }
        heap = VM.getVM().getObjectHeap();
        for (loItr = lockedObjects.iterator(); loItr.hasNext();) {
          ownedMonitors.add(heap.newOop((OopHandle)loItr.next()));
        }
      }
      monitorsCache = factory.newJSList(ownedMonitors);
    }
    return monitorsCache;
  }
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.JSJavaThread
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

import sun.jvm.hotspot.oops.TypeArray;

public class JSJavaTypeArray
  extends JSJavaArray
{
  public JSJavaTypeArray(TypeArray array, JSJavaFactory fac)
  {
    super(array, fac);
  }
  
  public final TypeArray getTypeArray()
  {
    return (TypeArray)getArray();
  }
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.JSJavaTypeArray
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

import sun.jvm.hotspot.oops.Array;
import sun.jvm.hotspot.oops.TypeArray;
import sun.jvm.hotspot.oops.TypeArrayKlass;
import sun.jvm.hotspot.utilities.Assert;

public class JSJavaTypeArrayKlass
  extends JSJavaArrayKlass
{
  public JSJavaTypeArrayKlass(TypeArrayKlass kls, JSJavaFactory fac)
  {
    super(kls, fac);
  }
  
  public final TypeArrayKlass getTypeArrayKlass()
  {
    return (TypeArrayKlass)getArrayKlass();
  }
  
  public String getName()
  {
    int type = getTypeArrayKlass().getElementType();
    switch (type)
    {
    case 4: 
      return "boolean[]";
    case 5: 
      return "char[]";
    case 6: 
      return "float[]";
    case 7: 
      return "double[]";
    case 8: 
      return "byte[]";
    case 9: 
      return "short[]";
    case 10: 
      return "int[]";
    case 11: 
      return "long[]";
    }
    if (Assert.ASSERTS_ENABLED) {
      Assert.that(false, "Unknown primitive array type");
    }
    return null;
  }
  
  public Object getFieldValue(int index, Array array)
  {
    TypeArray typeArr = (TypeArray)array;
    int type = getTypeArrayKlass().getElementType();
    switch (type)
    {
    case 4: 
      return Boolean.valueOf(typeArr.getBooleanAt(index));
    case 5: 
      return new String(new char[] { typeArr.getCharAt(index) });
    case 6: 
      return new Float(typeArr.getFloatAt(index));
    case 7: 
      return new Double(typeArr.getDoubleAt(index));
    case 8: 
      return new Byte(typeArr.getByteAt(index));
    case 9: 
      return new Short(typeArr.getShortAt(index));
    case 10: 
      return new Integer(typeArr.getIntAt(index));
    case 11: 
      return new Long(typeArr.getLongAt(index));
    }
    if (Assert.ASSERTS_ENABLED) {
      Assert.that(false, "Unknown primitive array type");
    }
    return null;
  }
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.JSJavaTypeArrayKlass
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import sun.jvm.hotspot.runtime.JavaThread;
import sun.jvm.hotspot.runtime.Threads;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.runtime.VM.Flag;

public class JSJavaVM
  extends DefaultScriptObject
{
  private static final int FIELD_ADDRESS_SIZE = 0;
  private static final int FIELD_BUILD_INFO = 1;
  private static final int FIELD_CPU = 2;
  private static final int FIELD_FLAGS = 3;
  private static final int FIELD_HEAP = 4;
  private static final int FIELD_OS = 5;
  private static final int FIELD_SYS_PROPS = 6;
  private static final int FIELD_THREADS = 7;
  private static final int FIELD_TYPE = 8;
  private static final int FIELD_VERSION = 9;
  private static final int FIELD_CLASS_PATH = 10;
  private static final int FIELD_BOOT_CLASS_PATH = 11;
  private static final int FIELD_USER_DIR = 12;
  private static final int FIELD_UNDEFINED = -1;
  
  public JSJavaVM(JSJavaFactory factory)
  {
    this.factory = factory;
    vm = VM.getVM();
  }
  
  public Object get(String name)
  {
    int fieldID = getFieldID(name);
    switch (fieldID)
    {
    case 0: 
      return new Long(getVMBit());
    case 1: 
      return vm.getVMInternalInfo();
    case 2: 
      return vm.getCPU();
    case 3: 
      return getFlags();
    case 4: 
      return getHeap();
    case 5: 
      return vm.getOS();
    case 6: 
      return getSysProps();
    case 7: 
      return getThreads();
    case 8: 
      return getType();
    case 9: 
      return vm.getVMRelease();
    case 10: 
      return getClassPath();
    case 11: 
      return getBootClassPath();
    case 12: 
      return getUserDir();
    }
    return super.get(name);
  }
  
  public Object[] getIds()
  {
    Object[] superIds = super.getIds();
    Object[] tmp = fields.keySet().toArray();
    Object[] res = new Object[superIds.length + tmp.length];
    System.arraycopy(tmp, 0, res, 0, tmp.length);
    System.arraycopy(superIds, 0, res, tmp.length, superIds.length);
    return res;
  }
  
  public boolean has(String name)
  {
    if (getFieldID(name) != -1) {
      return true;
    }
    return super.has(name);
  }
  
  public void put(String name, Object value)
  {
    if (getFieldID(name) == -1) {
      super.put(name, value);
    }
  }
  
  public String toString()
  {
    StringBuffer buf = new StringBuffer();
    buf.append("Java Hotspot ");
    buf.append(getType());
    buf.append(' ');
    buf.append(getVMBit());
    buf.append(" bit VM (build ");
    buf.append(vm.getVMRelease());
    buf.append(")");
    return buf.toString();
  }
  
  private static Map fields = new HashMap();
  private JSMap flagsCache;
  private JSJavaHeap heapCache;
  private JSMap sysPropsCache;
  private JSList threadsCache;
  private final JSJavaFactory factory;
  private final VM vm;
  
  private static void addField(String name, int fieldId)
  {
    fields.put(name, new Integer(fieldId));
  }
  
  private static int getFieldID(String name)
  {
    Integer res = (Integer)fields.get(name);
    return res != null ? res.intValue() : -1;
  }
  
  static
  {
    addField("addressSize", 0);
    addField("buildInfo", 1);
    addField("cpu", 2);
    addField("flags", 3);
    addField("heap", 4);
    addField("os", 5);
    addField("sysProps", 6);
    addField("threads", 7);
    addField("type", 8);
    addField("version", 9);
    addField("classPath", 10);
    addField("bootClassPath", 11);
    addField("userDir", 12);
  }
  
  private long getVMBit()
  {
    return vm.getAddressSize() * 8L;
  }
  
  private synchronized JSMap getFlags()
  {
    if (flagsCache == null)
    {
      VM.Flag[] flags = vm.getCommandLineFlags();
      Map map = new HashMap();
      if (flags != null) {
        for (int f = 0; f < flags.length; f++)
        {
          VM.Flag flag = flags[f];
          map.put(flag.getName(), flag.getValue());
        }
      }
      flagsCache = factory.newJSMap(map);
    }
    return flagsCache;
  }
  
  private synchronized JSJavaHeap getHeap()
  {
    if (heapCache == null) {
      heapCache = factory.newJSJavaHeap();
    }
    return heapCache;
  }
  
  private synchronized JSMap getSysProps()
  {
    if (sysPropsCache == null)
    {
      Properties props = vm.getSystemProperties();
      Map map = new HashMap();
      if (props != null)
      {
        Enumeration e = props.propertyNames();
        while (e.hasMoreElements())
        {
          String key = (String)e.nextElement();
          map.put(key, props.getProperty(key));
        }
      }
      sysPropsCache = factory.newJSMap(map);
    }
    return sysPropsCache;
  }
  
  private synchronized JSList getThreads()
  {
    if (threadsCache == null)
    {
      List threads = new ArrayList(0);
      threadsCache = factory.newJSList(threads);
      JavaThread jthread = vm.getThreads().first();
      while (jthread != null)
      {
        threads.add(jthread);
        jthread = jthread.next();
      }
    }
    return threadsCache;
  }
  
  private String getType()
  {
    if (vm.isClientCompiler()) {
      return "Client";
    }
    if (vm.isServerCompiler()) {
      return "Server";
    }
    return "Core";
  }
  
  private String getClassPath()
  {
    return vm.getSystemProperty("java.class.path");
  }
  
  private String getBootClassPath()
  {
    return vm.getSystemProperty("sun.boot.class.path");
  }
  
  private String getUserDir()
  {
    return vm.getSystemProperty("user.dir");
  }
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.JSJavaVM
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

import java.util.Iterator;
import java.util.List;

public class JSList
  extends DefaultScriptObject
{
  private final List list;
  private final JSJavaFactory factory;
  
  public JSList(List list, JSJavaFactory fac)
  {
    this.list = list;
    factory = fac;
  }
  
  public Object get(String name)
  {
    if (name.equals("length")) {
      return new Integer(list.size());
    }
    return super.get(name);
  }
  
  public Object get(int index)
  {
    if (isInRange(index))
    {
      Object item = list.get(index);
      return wrapObject(item);
    }
    return super.get(index);
  }
  
  public Object[] getIds()
  {
    Object[] superIds = super.getIds();
    int size = list.size();
    Object[] res = new Object[superIds.length + size];
    for (int i = 0; i < size; i++) {
      res[i] = new Integer(i);
    }
    System.arraycopy(superIds, 0, res, size, superIds.length);
    return res;
  }
  
  public boolean has(String name)
  {
    if (name.equals("length")) {
      return true;
    }
    return super.has(name);
  }
  
  public boolean has(int index)
  {
    if (isInRange(index)) {
      return true;
    }
    return super.has(index);
  }
  
  public void put(String name, Object value)
  {
    if (!name.equals("length")) {
      super.put(name, value);
    }
  }
  
  public void put(int index, Object value)
  {
    if (!isInRange(index)) {
      super.put(index, value);
    }
  }
  
  public String toString()
  {
    StringBuffer buf = new StringBuffer();
    buf.append('[');
    for (Iterator itr = list.iterator(); itr.hasNext();)
    {
      buf.append(wrapObject(itr.next()));
      if (itr.hasNext()) {
        buf.append(", ");
      }
    }
    buf.append(']');
    return buf.toString();
  }
  
  private boolean isInRange(int index)
  {
    return (index >= 0) && (index < list.size());
  }
  
  private Object wrapObject(Object obj)
  {
    return factory.newJSJavaWrapper(obj);
  }
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.JSList
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class JSMap
  extends DefaultScriptObject
{
  private final Map map;
  private final JSJavaFactory factory;
  
  public JSMap(Map map, JSJavaFactory fac)
  {
    this.map = map;
    factory = fac;
  }
  
  public Object get(String name)
  {
    if (map.containsKey(name)) {
      return wrapObject(map.get(name));
    }
    return super.get(name);
  }
  
  public Object[] getIds()
  {
    Object[] superIds = super.getIds();
    Object[] tmp = map.keySet().toArray();
    Object[] res = new Object[superIds.length + tmp.length];
    System.arraycopy(tmp, 0, res, 0, tmp.length);
    System.arraycopy(superIds, 0, res, tmp.length, superIds.length);
    return res;
  }
  
  public boolean has(String name)
  {
    if (map.containsKey(name)) {
      return true;
    }
    return super.has(name);
  }
  
  public void put(String name, Object value)
  {
    if (!map.containsKey(name)) {
      super.put(name, value);
    }
  }
  
  public String toString()
  {
    StringBuffer buf = new StringBuffer();
    Set keys = map.keySet();
    buf.append('{');
    for (Iterator itr = keys.iterator(); itr.hasNext();)
    {
      Object key = itr.next();
      buf.append(key);
      buf.append('=');
      buf.append(wrapObject(map.get(key)));
      if (itr.hasNext()) {
        buf.append(", ");
      }
    }
    buf.append('}');
    return buf.toString();
  }
  
  private Object wrapObject(Object obj)
  {
    return factory.newJSJavaWrapper(obj);
  }
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.JSMap
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.script.Invocable;

public class MapScriptObject
  implements ScriptObject
{
  private Map map;
  
  public MapScriptObject()
  {
    this(new HashMap());
  }
  
  public MapScriptObject(Map map)
  {
    this.map = Collections.synchronizedMap(map);
  }
  
  public Object[] getIds()
  {
    return map.keySet().toArray();
  }
  
  public Object get(String name)
  {
    if (has(name)) {
      return map.get(name);
    }
    return UNDEFINED;
  }
  
  public Object get(int index)
  {
    if (has(index))
    {
      Object key = Integer.valueOf(index);
      return map.get(key);
    }
    return UNDEFINED;
  }
  
  public void put(String name, Object value)
  {
    map.put(name, value);
  }
  
  public void put(int index, Object value)
  {
    map.put(Integer.valueOf(index), value);
  }
  
  public boolean has(String name)
  {
    return map.containsKey(name);
  }
  
  public boolean has(int index)
  {
    return map.containsKey(Integer.valueOf(index));
  }
  
  public boolean delete(String name)
  {
    if (map.containsKey(name))
    {
      map.remove(name);
      return true;
    }
    return false;
  }
  
  public boolean delete(int index)
  {
    Object key = Integer.valueOf(index);
    if (map.containsKey(key))
    {
      map.remove(key);
      return true;
    }
    return false;
  }
  
  protected void putFunction(Object target, Method method)
  {
    putFunction(target, method, true);
  }
  
  protected void putFunction(Object target, Method method, boolean wrapArgs)
  {
    map.put(method.getName(), new MethodCallable(target, method, wrapArgs));
  }
  
  protected void putFunction(Object target, String name, Invocable invocable)
  {
    map.put(name, new InvocableCallable(target, name, invocable));
  }
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.MapScriptObject
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

import java.lang.reflect.Method;
import javax.script.ScriptException;

public class MethodCallable
  implements Callable
{
  private Object target;
  private Method method;
  private boolean wrapArgs;
  
  public MethodCallable(Object target, Method method, boolean wrapArgs)
  {
    this.method = method;
    this.target = target;
    this.wrapArgs = wrapArgs;
  }
  
  public MethodCallable(Object target, Method method)
  {
    this(target, method, true);
  }
  
  public Object call(Object[] args)
    throws ScriptException
  {
    try
    {
      if (wrapArgs) {
        return method.invoke(target, new Object[] { args });
      }
      return method.invoke(target, args);
    }
    catch (RuntimeException re)
    {
      throw re;
    }
    catch (Exception exp)
    {
      throw new ScriptException(exp);
    }
  }
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.MethodCallable
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

public abstract interface ObjectVisitor
{
  public abstract void visit(Object paramObject);
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.ObjectVisitor
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

public abstract interface ScriptObject
{
  public static final Object UNDEFINED = new Object();
  public static final Object[] EMPTY_ARRAY = new Object[0];
  
  public abstract Object[] getIds();
  
  public abstract Object get(String paramString);
  
  public abstract Object get(int paramInt);
  
  public abstract void put(String paramString, Object paramObject);
  
  public abstract void put(int paramInt, Object paramObject);
  
  public abstract boolean has(String paramString);
  
  public abstract boolean has(int paramInt);
  
  public abstract boolean delete(String paramString);
  
  public abstract boolean delete(int paramInt);
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.ScriptObject
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

import javax.script.ScriptEngine;
import sun.jvm.hotspot.oops.HeapVisitor;
import sun.jvm.hotspot.oops.Oop;

final class SOQLEngine$1
  implements HeapVisitor
{
  boolean filterExists;
  private final SOQLEngine this$0;
  
  SOQLEngine$1(SOQLEngine paramSOQLEngine, ObjectVisitor paramObjectVisitor) {}
  
  public void prologue(long usedSize)
  {
    filterExists = (this$0.getScriptEngine().get("filter") != null);
  }
  
  public boolean doObj(Oop obj)
  {
    SOQLEngine.access$000(this$0, obj, val$visitor, filterExists);
    return false;
  }
  
  public void epilogue() {}
}

/* Location:
 * Qualified Name:     sun.jvm.hotspot.utilities.soql.SOQLEngine.1
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package sun.jvm.hotspot.utilities.soql;

import java.io.PrintStream;
import java.util.StringTokenizer;
import javax.script.ScriptEngine;
import sun.jvm.hotspot.oops.HeapVisitor;
import sun.jvm.hotspot.oops.InstanceKlass;
import sun.jvm.hotspot.oops.ObjectHeap;
import sun.jvm.hotspot.oops.Oop;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.utilities.ObjectReader;
import sun.jvm.hotspot.utilities.SystemDictionaryHelper;

public class SOQLEngine
  extends JSJavaScriptEngine
{
  public static synchronized SOQLEngine getEngine()
  {
    if (soleInstance == null) {
      soleInstance = new SOQLEngine();
    }
    return soleInstance;
  }
  
  public synchronized void executeQuery(String query, ObjectVisitor visitor)
    throws SOQLException
  {
    debugPrint("query : " + query);
    StringTokenizer st = new StringTokenizer(query);
    if (st.hasMoreTokens())
    {
      String first = st.nextToken();
      if (!first.equals("select")) {
        throw new SOQLException("query syntax error: no 'select' clause");
      }
    }
    else
    {
      throw new SOQLException("query syntax error: no 'select' clause");
    }
    int selectStart = query.indexOf("select");
    int fromStart = query.indexOf("from");
    
    String selectExpr = null;
    String className = null;
    boolean isInstanceOf = false;
    String whereExpr = null;
    String identifier = null;
    if (fromStart != -1)
    {
      selectExpr = query.substring(selectStart + "select".length(), fromStart);
      st = new StringTokenizer(query.substring(fromStart + "from".length()));
      if (st.hasMoreTokens())
      {
        String tmp = st.nextToken();
        if (tmp.equals("instanceof"))
        {
          isInstanceOf = true;
          if (!st.hasMoreTokens()) {
            throw new SOQLException("no class name after 'instanceof'");
          }
          className = st.nextToken();
        }
        else
        {
          className = tmp;
        }
      }
      else
      {
        throw new SOQLException("query syntax error: class name must follow 'from'");
      }
      if (st.hasMoreTokens())
      {
        identifier = st.nextToken();
        if (identifier.equals("where")) {
          throw new SOQLException("query syntax error: identifier should follow class name");
        }
        if (st.hasMoreTokens())
        {
          String tmp = st.nextToken();
          if (!tmp.equals("where")) {
            throw new SOQLException("query syntax error: 'where' clause expected after 'from' clause");
          }
          int whereEnd = query.lastIndexOf("where") + 5;
          whereExpr = query.substring(whereEnd);
        }
      }
      else
      {
        throw new SOQLException("query syntax error: identifier should follow class name");
      }
    }
    else
    {
      selectExpr = query.substring(selectStart + "select".length(), query.length());
    }
    executeQuery(new SOQLQuery(selectExpr, isInstanceOf, className, identifier, whereExpr), visitor);
  }
  
  private void executeQuery(SOQLQuery q, ObjectVisitor visitor)
    throws SOQLException
  {
    InstanceKlass kls = null;
    if (className != null)
    {
      kls = SystemDictionaryHelper.findInstanceKlass(className);
      if (kls == null) {
        throw new SOQLException(className + " is not found!");
      }
    }
    StringBuffer buf = new StringBuffer();
    buf.append("function result(");
    if (identifier != null) {
      buf.append(identifier);
    }
    buf.append(") { return ");
    buf.append(selectExpr.replace('\n', ' '));
    buf.append("; }");
    
    String selectCode = buf.toString();
    debugPrint(selectCode);
    String whereCode = null;
    if (whereExpr != null)
    {
      buf = new StringBuffer();
      buf.append("function filter(");
      buf.append(identifier);
      buf.append(") { return ");
      buf.append(whereExpr.replace('\n', ' '));
      buf.append("; }");
      whereCode = buf.toString();
      debugPrint(whereCode);
    }
    else
    {
      whereCode = "filter = null;";
    }
    beginQuery();
    
    evalString(selectCode, "", 1);
    evalString(whereCode, "", 1);
    if (className != null) {
      try
      {
        iterateOops(kls, visitor, isInstanceOf);
      }
      finally
      {
        endQuery();
      }
    } else {
      try
      {
        Object select = call("result", new Object[0]);
        visitor.visit(select);
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
  }
  
  private void dispatchObject(Oop oop, ObjectVisitor visitor, boolean filterExists)
  {
    JSJavaObject jsObj = factory.newJSJavaObject(oop);
    Object[] args = { jsObj };
    boolean b = true;
    try
    {
      if (filterExists)
      {
        Object res = call("filter", args);
        if ((res instanceof Boolean)) {
          b = ((Boolean)res).booleanValue();
        } else if ((res instanceof Number)) {
          b = ((Number)res).intValue() != 0;
        } else {
          b = res != null;
        }
      }
      if
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

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd