pluotsorbet/classinfo.js

114 строки
3.7 KiB
JavaScript
Исходник Обычный вид История

2014-07-13 20:12:57 +04:00
/* -*- 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 ClassInfo = function(classBytes) {
2014-07-14 02:32:58 +04:00
var classImage = getClassImage(classBytes, this);
var cp = classImage.constant_pool;
this.className = cp[cp[classImage.this_class].name_index].bytes;
this.superClassName = classImage.super_class ? cp[cp[classImage.super_class].name_index].bytes : null;
this.access_flags = classImage.access_flags;
this.constant_pool = cp;
this.staticFields = {};
this.constructor = function () {
}
this.constructor.prototype.class = this;
2014-07-13 21:37:28 +04:00
2014-07-14 02:32:58 +04:00
var self = this;
2014-07-13 21:37:28 +04:00
2014-07-18 10:26:34 +04:00
this.interfaces = [];
classImage.interfaces.forEach(function(i) {
self.interfaces.push(cp[cp[i].name_index].bytes);
});
2014-07-14 02:32:58 +04:00
this.fields = [];
classImage.fields.forEach(function(f) {
self.fields.push({
access_flags: f.access_flags,
name: cp[f.name_index].bytes,
2014-07-20 02:53:58 +04:00
signature: cp[f.descriptor_index].bytes,
2014-07-14 02:32:58 +04:00
attributes: f.attributes
});
2014-07-14 02:32:58 +04:00
});
2014-07-13 21:37:28 +04:00
2014-07-14 02:32:58 +04:00
this.methods = [];
classImage.methods.forEach(function(m) {
var method = {};
2014-07-14 02:32:58 +04:00
method.classInfo = self;
method.access_flags = m.access_flags;
method.name = cp[m.name_index].bytes;
method.signature = cp[m.signature_index].bytes;
method.attributes = m.attributes;
method.attributes.forEach(function(a) {
if (a.info.type === ATTRIBUTE_TYPES.Code) {
method.code = Uint8Array(a.info.code);
method.exception_table = a.info.exception_table;
method.max_locals = a.info.max_locals;
}
});
2014-07-14 02:32:58 +04:00
self.methods.push(method);
});
2014-07-16 03:49:00 +04:00
var classes = this.classes = [];
2014-07-14 02:32:58 +04:00
classImage.attributes.forEach(function(a) {
if (a.info.type === ATTRIBUTE_TYPES.InnerClasses) {
a.info.classes.forEach(function(c) {
classes.push(cp[cp[c.inner_class_info_index].name_index].bytes);
classes.push(cp[cp[c.outer_class_info_index].name_index].bytes);
});
}
});
2014-07-13 20:12:57 +04:00
}
2014-07-14 04:02:57 +04:00
2014-07-18 10:44:06 +04:00
ClassInfo.prototype.implementsInterface = function(iface) {
2014-07-18 10:02:28 +04:00
var classInfo = this;
do {
2014-07-18 10:44:06 +04:00
var interfaces = classInfo.interfaces;
for (var n = 0; n < interfaces.length; ++n) {
if (interfaces[n] === iface)
return true;
}
2014-07-18 10:02:28 +04:00
classInfo = classInfo.superClass;
} while (classInfo);
return false;
}
ClassInfo.prototype.isAssignableTo = function(toClass) {
2014-07-21 04:10:12 +04:00
if (this === toClass || toClass === ClassInfo.java_lang_Object)
2014-07-18 10:44:06 +04:00
return true;
if (ACCESS_FLAGS.isInterface(toClass.access_flags) && this.implementsInterface(toClass))
return true;
if (this.elementClass && toClass.elementClass)
return this.elementClass.isAssignableTo(toClass.elementClass);
return this.superClass ? this.superClass.isAssignableTo(toClass) : false;
2014-07-18 10:44:06 +04:00
}
2014-07-18 10:02:28 +04:00
ClassInfo.prototype.getClassObject = function() {
var self = this;
return util.cache(this, "classObject", function () {
2014-07-21 04:15:09 +04:00
var classObject = CLASSES.newObject(CLASSES.java_lang_Class);
2014-07-18 10:02:28 +04:00
classObject.vmClass = self;
return classObject;
});
}
var ArrayClass = function(className, elementClass) {
2014-07-18 12:25:12 +04:00
this.className = className;
2014-07-14 04:02:57 +04:00
this.superClassName = "java/lang/Object";
this.access_flags = 0;
this.elementClass = elementClass;
2014-07-14 04:02:57 +04:00
}
2014-07-18 10:11:45 +04:00
ArrayClass.prototype.isArrayClass = true;
ArrayClass.prototype.implementsInterface = function(iface) {
return false;
}
ArrayClass.prototype.isAssignableTo = ClassInfo.prototype.isAssignableTo;
ArrayClass.prototype.getClassObject = ClassInfo.prototype.getClassObject;
ArrayClass.prototype.initialized = true;