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;
|
2014-07-26 07:14:19 +04:00
|
|
|
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({
|
2014-07-29 14:43:12 +04:00
|
|
|
classInfo: self,
|
2014-07-14 02:32:58 +04:00
|
|
|
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-13 20:52:01 +04:00
|
|
|
});
|
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) {
|
2014-07-14 06:58:30 +04:00
|
|
|
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) {
|
2014-07-29 06:13:32 +04:00
|
|
|
method.code = new Uint8Array(a.info.code);
|
2014-07-14 02:32:58 +04:00
|
|
|
method.exception_table = a.info.exception_table;
|
|
|
|
method.max_locals = a.info.max_locals;
|
2014-07-13 20:28:19 +04:00
|
|
|
}
|
|
|
|
});
|
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);
|
2014-07-31 04:40:39 +04:00
|
|
|
if (c.outer_class_info_index)
|
|
|
|
classes.push(cp[cp[c.outer_class_info_index].name_index].bytes);
|
2014-07-14 02:32:58 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-18 22:31:05 +04:00
|
|
|
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;
|
2014-07-27 01:18:44 +04:00
|
|
|
if (ACCESS_FLAGS.isInterface(toClass.access_flags) && this.implementsInterface(toClass))
|
|
|
|
return true;
|
2014-07-20 00:18:45 +04:00
|
|
|
if (this.elementClass && toClass.elementClass)
|
|
|
|
return this.elementClass.isAssignableTo(toClass.elementClass);
|
2014-07-18 22:31:05 +04:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-20 00:18:45 +04:00
|
|
|
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;
|
2014-07-20 00:18:45 +04:00
|
|
|
this.elementClass = elementClass;
|
2014-07-14 04:02:57 +04:00
|
|
|
}
|
2014-07-18 10:11:45 +04:00
|
|
|
|
2014-07-27 05:20:54 +04:00
|
|
|
ArrayClass.prototype.methods = [];
|
|
|
|
|
2014-07-18 10:11:45 +04:00
|
|
|
ArrayClass.prototype.isArrayClass = true;
|
2014-07-20 00:18:45 +04:00
|
|
|
|
|
|
|
ArrayClass.prototype.implementsInterface = function(iface) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayClass.prototype.isAssignableTo = ClassInfo.prototype.isAssignableTo;
|
|
|
|
|
|
|
|
ArrayClass.prototype.getClassObject = ClassInfo.prototype.getClassObject;
|
2014-07-21 08:17:08 +04:00
|
|
|
|
|
|
|
ArrayClass.prototype.initialized = true;
|