/** * Constructs a class file including no members. * * @param isInterface * true if this is an interface. false if this is a class. * @param classname * a fully-qualified class name * @param superclass * a fully-qualified super class name or null. */ public ClassFile(boolean isInterface, String classname, String superclass) { major = MAJOR_VERSION; minor = 0; // JDK 1.3 or later constPool = new ConstPool(classname); thisClass = constPool.getThisClassInfo(); if (isInterface) accessFlags = AccessFlag.INTERFACE | AccessFlag.ABSTRACT; else accessFlags = AccessFlag.SUPER; initSuperclass(superclass); interfaces = null; fields = new ArrayList(); methods = new ArrayList(); thisclassname = classname; attributes = new ArrayList(); attributes.add(new SourceFileAttribute(constPool, getSourcefileName(thisclassname))); }
public static int addMethod(ConstPool cPool, CtMethod method) { return cPool.addMethodrefInfo( cPool.getThisClassInfo(), method.getName(), method.getSignature() ); }
/** * Constructs a class file including no members. * * @param isInterface * true if this is an interface. false if this is a class. * @param classname * a fully-qualified class name * @param superclass * a fully-qualified super class name or null. */ public ClassFile(boolean isInterface, String classname, String superclass) { major = MAJOR_VERSION; minor = 0; // JDK 1.3 or later constPool = new ConstPool(classname); thisClass = constPool.getThisClassInfo(); if (isInterface) accessFlags = AccessFlag.INTERFACE | AccessFlag.ABSTRACT; else accessFlags = AccessFlag.SUPER; initSuperclass(superclass); interfaces = null; fields = new ArrayList<FieldInfo>(); methods = new ArrayList<MethodInfo>(); thisclassname = classname; attributes = new ArrayList<AttributeInfo>(); attributes.add(new SourceFileAttribute(constPool, getSourcefileName(thisclassname))); }
private ConstPool compact0() { ConstPool cp = new ConstPool(thisclassname); thisClass = cp.getThisClassInfo(); String sc = getSuperclass(); if (sc != null) superClass = cp.addClassInfo(getSuperclass()); if (interfaces != null) { int n = interfaces.length; for (int i = 0; i < n; ++i) interfaces[i] = cp.addClassInfo(constPool.getClassInfo(interfaces[i])); } return cp; }
private ConstPool compact0() { ConstPool cp = new ConstPool(thisclassname); thisClass = cp.getThisClassInfo(); String sc = getSuperclass(); if (sc != null) superClass = cp.addClassInfo(getSuperclass()); if (interfaces != null) for (int i = 0; i < interfaces.length; ++i) interfaces[i] = cp.addClassInfo(constPool.getClassInfo(interfaces[i])); return cp; }
}; final int[] localData = { constPool.getThisClassInfo(), constPool.addClassInfo( "java/lang/Object" ), constPool.addClassInfo( "[Ljava/lang/Object;" ),
/** * Add Method to ConstPool. If method was not in the ConstPool will add and return index, otherwise will return index of already existing entry of constpool */ private static int addMethod(ConstPool cPool, CtMethod method) { // addMethodrefInfo is a safe add, if constant already present it return the existing value without adding. return cPool.addMethodrefInfo( cPool.getThisClassInfo(), method.getName(), method.getSignature() ); }
/** * Add Method to ConstPool. If method was not in the ConstPool will add and return index, otherwise will return index of already existing entry of constpool */ private static int addMethod(ConstPool cPool, CtMethod method) { // addMethodrefInfo is a safe add, if constant already present it return the existing value without adding. return cPool.addMethodrefInfo( cPool.getThisClassInfo(), method.getName(), method.getSignature() ); }
public static int addMethod(ConstPool cPool, CtMethod method) { return cPool.addMethodrefInfo( cPool.getThisClassInfo(), method.getName(), method.getSignature() ); }
private ConstPool compact0() { ConstPool cp = new ConstPool(thisclassname); thisClass = cp.getThisClassInfo(); String sc = getSuperclass(); if (sc != null) superClass = cp.addClassInfo(getSuperclass()); if (interfaces != null) { int n = interfaces.length; for (int i = 0; i < n; ++i) interfaces[i] = cp.addClassInfo(constPool.getClassInfo(interfaces[i])); } return cp; }
private ConstPool compact0() { ConstPool cp = new ConstPool(thisclassname); thisClass = cp.getThisClassInfo(); String sc = getSuperclass(); if (sc != null) superClass = cp.addClassInfo(getSuperclass()); if (interfaces != null) { int n = interfaces.length; for (int i = 0; i < n; ++i) interfaces[i] = cp.addClassInfo(constPool.getClassInfo(interfaces[i])); } return cp; }
private ConstPool compact0() { ConstPool cp = new ConstPool(thisclassname); thisClass = cp.getThisClassInfo(); String sc = getSuperclass(); if (sc != null) superClass = cp.addClassInfo(getSuperclass()); if (interfaces != null) { int n = interfaces.length; for (int i = 0; i < n; ++i) interfaces[i] = cp.addClassInfo(constPool.getClassInfo(interfaces[i])); } return cp; }
private ConstPool compact0() { ConstPool cp = new ConstPool(thisclassname); thisClass = cp.getThisClassInfo(); String sc = getSuperclass(); if (sc != null) superClass = cp.addClassInfo(getSuperclass()); if (interfaces != null) { int n = interfaces.length; for (int i = 0; i < n; ++i) interfaces[i] = cp.addClassInfo(constPool.getClassInfo(interfaces[i])); } return cp; }
private ConstPool compact0() { ConstPool cp = new ConstPool(thisclassname); thisClass = cp.getThisClassInfo(); String sc = getSuperclass(); if (sc != null) superClass = cp.addClassInfo(getSuperclass()); if (interfaces != null) { int n = interfaces.length; for (int i = 0; i < n; ++i) interfaces[i] = cp.addClassInfo(constPool.getClassInfo(interfaces[i])); } return cp; }
private int transformInvokevirtualsIntoGetfields(ClassFile classfile, CodeIterator iter, int pos) { ConstPool cp = classfile.getConstPool(); int c = iter.byteAt(pos); if (c != Opcode.GETFIELD) { return pos; } int index = iter.u16bitAt(pos + 1); String fieldName = cp.getFieldrefName(index); String className = cp.getFieldrefClassName(index); if ( !filter.handleReadAccess( className, fieldName ) ) { return pos; } String desc = "()" + cp.getFieldrefType( index ); int read_method_index = cp.addMethodrefInfo( cp.getThisClassInfo(), EACH_READ_METHOD_PREFIX + fieldName, desc ); iter.writeByte(Opcode.INVOKEVIRTUAL, pos); iter.write16bit(read_method_index, pos + 1); return pos; }
private int transformInvokevirtualsIntoGetfields(ClassFile classfile, CodeIterator iter, int pos) { ConstPool cp = classfile.getConstPool(); int c = iter.byteAt(pos); if (c != Opcode.GETFIELD) { return pos; } int index = iter.u16bitAt(pos + 1); String fieldName = cp.getFieldrefName(index); String className = cp.getFieldrefClassName(index); if ( !filter.handleReadAccess( className, fieldName ) ) { return pos; } String desc = "()" + cp.getFieldrefType( index ); int read_method_index = cp.addMethodrefInfo( cp.getThisClassInfo(), EACH_READ_METHOD_PREFIX + fieldName, desc ); iter.writeByte(Opcode.INVOKEVIRTUAL, pos); iter.write16bit(read_method_index, pos + 1); return pos; }
private void addGetFieldHandlerMethod(ClassFile classfile) throws CannotCompileException { ConstPool cp = classfile.getConstPool(); int this_class_index = cp.getThisClassInfo(); MethodInfo minfo = new MethodInfo(cp, GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR); /* local variable | this | */ Bytecode code = new Bytecode(cp, 2, 1); // aload_0 // load this code.addAload(0); // getfield // get field "$JAVASSIST_CALLBACK" defined already code.addOpcode(Opcode.GETFIELD); int field_index = cp.addFieldrefInfo(this_class_index, HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR); code.addIndex(field_index); // areturn // return the value of the field code.addOpcode(Opcode.ARETURN); minfo.setCodeAttribute(code.toCodeAttribute()); minfo.setAccessFlags(AccessFlag.PUBLIC); classfile.addMethod(minfo); }
private void addGetFieldHandlerMethod(ClassFile classfile) throws CannotCompileException { ConstPool cp = classfile.getConstPool(); int this_class_index = cp.getThisClassInfo(); MethodInfo minfo = new MethodInfo(cp, GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR); /* local variable | this | */ Bytecode code = new Bytecode(cp, 2, 1); // aload_0 // load this code.addAload(0); // getfield // get field "$JAVASSIST_CALLBACK" defined already code.addOpcode(Opcode.GETFIELD); int field_index = cp.addFieldrefInfo(this_class_index, HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR); code.addIndex(field_index); // areturn // return the value of the field code.addOpcode(Opcode.ARETURN); minfo.setCodeAttribute(code.toCodeAttribute()); minfo.setAccessFlags(AccessFlag.PUBLIC); classfile.addMethod(minfo); }
private void addSetFieldHandlerMethod(ClassFile classfile) throws CannotCompileException { ConstPool cp = classfile.getConstPool(); int this_class_index = cp.getThisClassInfo(); MethodInfo minfo = new MethodInfo(cp, SETFIELDHANDLER_METHOD_NAME, SETFIELDHANDLER_METHOD_DESCRIPTOR); /* local variables | this | callback | */ Bytecode code = new Bytecode(cp, 3, 3); // aload_0 // load this code.addAload(0); // aload_1 // load callback code.addAload(1); // putfield // put field "$JAVASSIST_CALLBACK" defined already code.addOpcode(Opcode.PUTFIELD); int field_index = cp.addFieldrefInfo(this_class_index, HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR); code.addIndex(field_index); // return code.addOpcode(Opcode.RETURN); minfo.setCodeAttribute(code.toCodeAttribute()); minfo.setAccessFlags(AccessFlag.PUBLIC); classfile.addMethod(minfo); }
private void addSetFieldHandlerMethod(ClassFile classfile) throws CannotCompileException { ConstPool cp = classfile.getConstPool(); int this_class_index = cp.getThisClassInfo(); MethodInfo minfo = new MethodInfo(cp, SETFIELDHANDLER_METHOD_NAME, SETFIELDHANDLER_METHOD_DESCRIPTOR); /* local variables | this | callback | */ Bytecode code = new Bytecode(cp, 3, 3); // aload_0 // load this code.addAload(0); // aload_1 // load callback code.addAload(1); // putfield // put field "$JAVASSIST_CALLBACK" defined already code.addOpcode(Opcode.PUTFIELD); int field_index = cp.addFieldrefInfo(this_class_index, HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR); code.addIndex(field_index); // return code.addOpcode(Opcode.RETURN); minfo.setCodeAttribute(code.toCodeAttribute()); minfo.setAccessFlags(AccessFlag.PUBLIC); classfile.addMethod(minfo); }