/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* vim: set shiftwidth=4 tabstop=4 autoindent cindent expandtab: */ 'use strict'; var Classes = function() { if (this instanceof Classes) { this.classfiles = []; this.mainclass = []; this.classes = {}; } else { return new Classes(); } } Classes.prototype.addPath = function(name, data) { if (name.substr(-4) === ".jar") { data = new ZipFile(data); } this.classfiles[name] = data; } Classes.prototype.loadFile = function(fileName) { var classfiles = this.classfiles; var data = classfiles[fileName]; if (data) return data; Object.keys(classfiles).every(function (name) { if (name.substr(-4) !== ".jar") return true; var zip = classfiles[name]; if (fileName in zip.directory) { var bytes = zip.read(fileName); data = bytes.buffer.slice(0, bytes.length); } return !data; }); classfiles[fileName] = data; return data; } Classes.prototype.loadClassBytes = function(bytes) { var classInfo = new ClassInfo(bytes); this.classes[classInfo.className] = classInfo; return classInfo; } Classes.prototype.loadClassFile = function(fileName) { LOG.debug("loading " + fileName + " ..."); var bytes = this.loadFile(fileName); if (!bytes) return null; var classInfo = this.loadClassBytes(bytes); var classes = classInfo.classes; for (var i=0; i", "()V"); if (!clinit) return; LOG.debug("call " + className + ". ..."); caller.invoke(clinit); } Classes.prototype.getClass = function(caller, className, initialize) { var classInfo = this.classes[className]; if (classInfo) return classInfo; if (!!(classInfo = this.loadClassFile(className + ".class"))) { if (initialize) { classInfo.staticFields = {}; this.initClass(caller, className); } return classInfo; } throw new Error(util.format("Implementation of the %s class is not found.", className)); }; Classes.prototype.getStaticField = function(caller, className, fieldName) { return this.getClass(caller, className, true).staticFields[fieldName]; } Classes.prototype.setStaticField = function(caller, className, fieldName, value) { this.getClass(caller, className, true).staticFields[fieldName] = value; } Classes.prototype.getMethod = function(caller, className, methodName, signature, staticFlag, inheritFlag) { console.log(className, methodName, signature); // Only force initialization when accessing a static method. var classInfo = this.getClass(caller, className, staticFlag); var methods = classInfo.methods; for (var i=0; i