public class Class {
public int accessFlags;
public String name;
public String superClassName;
public String[] interfaceNames;
public RunTimeConstantPool runTimeConstantPool;
public Field[] fields;
public Method[] methods;
public ClassLoader loader;
public Class superClass;
public Class[] interfaces;
public int instanceSlotCount;
public int staticSlotCount;
public Slots staticVars;
public Class(ClassFile classFile) {
this.accessFlags = classFile.accessFlags();
this.name = classFile.className();
this.superClassName = classFile.superClassName();
this.interfaceNames = classFile.interfaceNames();
this.runTimeConstantPool = new RunTimeConstantPool(this, classFile.constantPool());
this.fields = new Field().newFields(this, classFile.fields());
this.methods = new Method().newMethods(this, classFile.methods());
}
public boolean isPublic() {
return 0 != (this.accessFlags & AccessFlags.ACC_PUBLIC);
}
public boolean isFinal() {
return 0 != (this.accessFlags & AccessFlags.ACC_FINAL);
}
public boolean isSuper() {
return 0 != (this.accessFlags & AccessFlags.ACC_SUPER);
}
public boolean isInterface() {
return 0 != (this.accessFlags & AccessFlags.ACC_INTERFACE);
}
public boolean isAbstract() {
return 0 != (this.accessFlags & AccessFlags.ACC_ABSTRACT);
}
public boolean isSynthetic() {
return 0 != (this.accessFlags & AccessFlags.ACC_SYNTHETIC);
}
public boolean isAnnotation() {
return 0 != (this.accessFlags & AccessFlags.ACC_ANNOTATION);
}
public boolean isEnum() {
return 0 != (this.accessFlags & AccessFlags.ACC_ENUM);
}
public RunTimeConstantPool constantPool() {
return this.runTimeConstantPool;
}
public Slots staticVars() {
return this.staticVars;
}
public boolean isAccessibleTo(Class other) {
return this.isPublic() || this.getPackageName().equals(other.getPackageName());
}
public String getPackageName() {
int i = this.name.lastIndexOf("/");
if (i >= 0) return this.name;
return "";
}
public Method getMainMethod() {
return this.getStaticMethod("main", "([Ljava/lang/String;)V");
}
private Method getStaticMethod(String name, String descriptor) {
for (Method method : this.methods) {
if (method.name.equals(name) && method.descriptor.equals(descriptor)) {
return method;
}
}
return null;
}
public Object newObject() {
return new Object(this);
}
public boolean isAssignableFrom(Class other) {
if (this == other) return true;
if (!other.isInterface()) {
return this.isSubClassOf(other);
} else {
return this.isImplements(other);
}
}
public boolean isSubClassOf(Class other) {
for (Class c = this.superClass; c != null; c = c.superClass) {
if (c == other) {
return true;
}
}
return false;
}
private boolean isImplements(Class other) {
for (Class c = this; c != null; c = c.superClass) {
for (Class clazz : c.interfaces) {
if (clazz == other || clazz.isSubInterfaceOf(other)) {
return true;
}
}
}
return false;
}
public boolean isSubInterfaceOf(Class iface) {
for (Class superInterface : this.interfaces) {
if (superInterface == iface || superInterface.isSubInterfaceOf(iface)) {
return true;
}
}
return false;
}
}
ClassMember.java
public class ClassMember {
public int accessFlags;
public String name;
public String descriptor;
public Class clazz;
public void copyMemberInfo(MemberInfo memberInfo) {
this.accessFlags = memberInfo.accessFlags();
this.name = memberInfo.name();
this.descriptor = memberInfo.descriptor();
}
public boolean isPublic() {
return 0 != (this.accessFlags & AccessFlags.ACC_PUBLIC);
}
public boolean isPrivate() {
return 0 != (this.accessFlags & AccessFlags.ACC_PRIVATE);
}
public boolean isProtected() {
return 0 != (this.accessFlags & AccessFlags.ACC_PROTECTED);
}
public boolean isStatic() {
return 0 != (this.accessFlags & AccessFlags.ACC_STATIC);
}
public boolean isFinal() {
return 0 != (this.accessFlags & AccessFlags.ACC_FINAL);
}
public boolean isSynthetic() {
return 0 != (this.accessFlags & AccessFlags.ACC_SYNTHETIC);
}
public String name() {
return this.name;
}
public String descriptor() {
return this.descriptor;
}
public Class clazz() {
return this.clazz;
}
public boolean isAccessibleTo(Class d) {
if (this.isPublic()) {
return true;
}
Class c = this.clazz;
if (this.isProtected()) {
return d == c || c.getPackageName().equals(d.getPackageName());
}
if (!this.isPrivate()) {
return c.getPackageName().equals(d.getPackageName());
}
return d == c;
}
}
Field.java
package org.itstack.demo.jvm.rtda.heap.methodarea;
import org.itstack.demo.jvm.classfile.MemberInfo;
import org.itstack.demo.jvm.classfile.attributes.impl.ConstantValueAttribute;
import org.itstack.demo.jvm.rtda.heap.constantpool.AccessFlags;
public class Field extends ClassMember {
public int constValueIndex;
public int slotId;
public Field[] newFields(Class clazz, MemberInfo[] cfFields) {
Field[] fields = new Field[cfFields.length];
for (int i = 0; i < cfFields.length; i++) {
fields[i] = new Field();
fields[i].clazz = clazz;
fields[i].copyMemberInfo(cfFields[i]);
fields[i].copyAttributes(cfFields[i]);
}
return fields;
}
public void copyAttributes(MemberInfo cfField) {
ConstantValueAttribute valAttr = cfField.ConstantValueAttribute();
if (null != valAttr) {
this.constValueIndex = valAttr.constantValueIdx();
}
}
public boolean isVolatile() {
return 0 != (this.accessFlags & AccessFlags.ACC_VOLATILE);
}
public boolean isTransient() {
return 0 != (this.accessFlags & AccessFlags.ACC_TRANSIENT);
}
public boolean isEnum() {
return 0 != (this.accessFlags & AccessFlags.ACC_ENUM);
}
public int constValueIndex() {
return this.constValueIndex;
}
public int slotId() {
return this.slotId;
}
public boolean isLongOrDouble() {
return this.descriptor.equals("J") || this.descriptor.equals("D");
}
}
Method.java
package org.itstack.demo.jvm.rtda.heap.methodarea;
import org.itstack.demo.jvm.classfile.MemberInfo;
import org.itstack.demo.jvm.classfile.attributes.impl.CodeAttribute;
import org.itstack.demo.jvm.rtda.heap.constantpool.AccessFlags;
public class Method extends ClassMember {
public int maxStack;
public int maxLocals;
public byte[] code;
public Method[] newMethods(Class clazz, MemberInfo[] cfMethods) {
Method[] methods = new Method[cfMethods.length];
for (int i = 0; i < cfMethods.length; i++) {
methods[i] = new Method();
methods[i].clazz = clazz;
methods[i].copyMemberInfo(cfMethods[i]);
methods[i].copyAttributes(cfMethods[i]);
}
return methods;
}
public void copyAttributes(MemberInfo cfMethod) {
CodeAttribute codeAttr = cfMethod.codeAttribute();
if (null != codeAttr) {
this.maxStack = codeAttr.maxStack();
this.maxLocals = codeAttr.maxLocals();
this.code = codeAttr.data();
}
}
public boolean isSynchronized() {
return 0 != (this.accessFlags & AccessFlags.ACC_SYNCHRONIZED);
}
public boolean isBridge() {
return 0 != (this.accessFlags & AccessFlags.ACC_BRIDGE);
}
public boolean isVarargs() {
return 0 != (this.accessFlags & AccessFlags.ACC_VARARGS);
}
public boolean isNative() {
return 0 != (this.accessFlags & AccessFlags.ACC_NATIVE);
}
public boolean isAbstract() {
return 0 != (this.accessFlags & AccessFlags.ACC_ABSTRACT);
}
public boolean isStrict() {
return 0 != (this.accessFlags & AccessFlags.ACC_STRICT);
}
public int maxStack() {
return this.maxStack;
}
public int maxLocals() {
return this.maxLocals;
}
public byte[] code() {
return this.code;
}
}
Object.java
package org.itstack.demo.jvm.rtda.heap.methodarea;
public class Object {
Class clazz;
Slots fields;
public Object(Class clazz){
this.clazz = clazz;
this.fields = new Slots(clazz.instanceSlotCount);
}
public Class clazz(){
return this.clazz;
}
public Slots fields(){
return this.fields;
}
public boolean isInstanceOf(Class clazz){
return clazz.isAssignableFrom(this.clazz);
}
}
Slots.java
package org.itstack.demo.jvm.rtda.heap.methodarea;
import org.itstack.demo.jvm.rtda.Slot;
public class Slots {
private Slot[] slots;
public Slots(int slotCount) {
if (slotCount > 0) {
slots = new Slot[slotCount];
for (int i = 0; i < slotCount; i++) {
slots[i] = new Slot();
}
}
}
public void setInt(int idx, int val) {
this.slots[idx].num = val;
}
public int getInt(int idx) {
return this.slots[idx].num;
}
public void setFloat(int idx, float val) {
this.slots[idx].num = (int) val;
}
public float getFloat(int idx) {
return this.slots[idx].num;
}
public void setLong(int idx, long val) {
this.slots[idx].num = (int) val;
this.slots[idx + 1].num = (int) (val >> 32);
}
public long getLong(int idx) {
int low = this.slots[idx].num;
int high = this.slots[idx + 1].num;
return (long) high << 32 | (long) low;
}
public void setDouble(int idx, double val) {
this.setLong(idx, (long) val);
}
public Double getDouble(int idx) {
return (double) this.getLong(idx);
}
public void setRef(int idx, Object ref) {
this.slots[idx].ref = ref;
}
public Object getRef(int idx){
return this.slots[idx].ref;
}
}