Package javassist.bytecode

Examples of javassist.bytecode.ConstPool


    fieldInfo.setAccessFlags( AccessFlag.PRIVATE | AccessFlag.TRANSIENT );
    classfile.addField( fieldInfo );
  }

  private void addGetFieldHandlerMethod(ClassFile classfile) throws CannotCompileException, BadBytecode {
    final ConstPool constPool = classfile.getConstPool();
    final int thisClassInfo = constPool.getThisClassInfo();
    final MethodInfo getterMethodInfo = new MethodInfo(
        constPool,
        GETFIELDHANDLER_METHOD_NAME,
        GETFIELDHANDLER_METHOD_DESCRIPTOR
    );

    /* local variable | this | */
    final Bytecode code = new Bytecode( constPool, 2, 1 );
    // aload_0 // load this
    code.addAload( 0 );
    // getfield // get field "$JAVASSIST_CALLBACK" defined already
    code.addOpcode( Opcode.GETFIELD );
    final int fieldIndex = constPool.addFieldrefInfo( thisClassInfo, HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR );
    code.addIndex( fieldIndex );
    // areturn // return the value of the field
    code.addOpcode( Opcode.ARETURN );
    getterMethodInfo.setCodeAttribute( code.toCodeAttribute() );
    getterMethodInfo.setAccessFlags( AccessFlag.PUBLIC );
View Full Code Here


    }
    classfile.addMethod( getterMethodInfo );
  }

  private void addSetFieldHandlerMethod(ClassFile classfile) throws CannotCompileException, BadBytecode {
    final ConstPool constPool = classfile.getConstPool();
    final int thisClassInfo = constPool.getThisClassInfo();
    final MethodInfo methodInfo = new MethodInfo(
        constPool,
        SETFIELDHANDLER_METHOD_NAME,
        SETFIELDHANDLER_METHOD_DESCRIPTOR
    );

    /* local variables | this | callback | */
    final Bytecode code = new Bytecode(constPool, 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 );
    final int fieldIndex = constPool.addFieldrefInfo( thisClassInfo, HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR );
    code.addIndex( fieldIndex );
    // return
    code.addOpcode( Opcode.RETURN );
    methodInfo.setCodeAttribute( code.toCodeAttribute() );
    methodInfo.setAccessFlags( AccessFlag.PUBLIC );
View Full Code Here

      }
    }
  }

  private void addReadMethod(ClassFile classfile, FieldInfo finfo) throws CannotCompileException, BadBytecode {
    final ConstPool constPool = classfile.getConstPool();
    final int thisClassInfo = constPool.getThisClassInfo();
    final String readMethodDescriptor = "()" + finfo.getDescriptor();
    final MethodInfo readMethodInfo = new MethodInfo(
        constPool,
        EACH_READ_METHOD_PREFIX + finfo.getName(),
        readMethodDescriptor
    );

    /* local variables | target obj | each oldvalue | */
    final Bytecode code = new Bytecode(constPool, 5, 3);
    // aload_0
    code.addAload( 0 );
    // getfield // get each field
    code.addOpcode( Opcode.GETFIELD );
    final int baseFieldIndex = constPool.addFieldrefInfo( thisClassInfo, finfo.getName(), finfo.getDescriptor() );
    code.addIndex( baseFieldIndex );
    // aload_0
    code.addAload( 0 );
    // invokeinterface : invoke Enabled.getInterceptFieldCallback()
    final int enabledClassIndex = constPool.addClassInfo( FIELD_HANDLED_TYPE_NAME );
    code.addInvokeinterface(
        enabledClassIndex,
        GETFIELDHANDLER_METHOD_NAME,
        GETFIELDHANDLER_METHOD_DESCRIPTOR,
        1
View Full Code Here

    }
    classfile.addMethod( readMethodInfo );
  }

  private void addWriteMethod(ClassFile classfile, FieldInfo finfo) throws CannotCompileException, BadBytecode {
    final ConstPool constPool = classfile.getConstPool();
    final int thisClassInfo = constPool.getThisClassInfo();
    final String writeMethodDescriptor = "(" + finfo.getDescriptor() + ")V";
    final MethodInfo writeMethodInfo = new MethodInfo(
        constPool,
        EACH_WRITE_METHOD_PREFIX+ finfo.getName(),
        writeMethodDescriptor
    );

    /* local variables | target obj | each oldvalue | */
    final Bytecode code = new Bytecode(constPool, 6, 3);
    // aload_0
    code.addAload( 0 );
    // invokeinterface : enabled.getInterceptFieldCallback()
    final int enabledClassIndex = constPool.addClassInfo( FIELD_HANDLED_TYPE_NAME );
    code.addInvokeinterface(
        enabledClassIndex,
        GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
        1
    );
    // ifnonnull (label1)
    code.addOpcode( Opcode.IFNONNULL );
    code.addIndex( 9 );
    // aload_0
    code.addAload( 0 );
    // *load_1
    addTypeDependDataLoad( code, finfo.getDescriptor(), 1 );
    // putfield
    code.addOpcode( Opcode.PUTFIELD );
    final int baseFieldIndex = constPool.addFieldrefInfo( thisClassInfo, finfo.getName(), finfo.getDescriptor() );
    code.addIndex( baseFieldIndex );
    code.growStack( -Descriptor.dataSize( finfo.getDescriptor() ) );
    // return ;
    code.addOpcode( Opcode.RETURN );
    // aload_0
View Full Code Here

      codeAttr.setAttribute( smt );
    }
  }

  private int transformInvokevirtualsIntoGetfields(ClassFile classfile, CodeIterator iter, int pos) {
    final ConstPool constPool = classfile.getConstPool();
    final int c = iter.byteAt( pos );
    if ( c != Opcode.GETFIELD ) {
      return pos;
    }

    final int index = iter.u16bitAt( pos + 1 );
    final String fieldName = constPool.getFieldrefName( index );
    final String className = constPool.getFieldrefClassName( index );
    if ( !filter.handleReadAccess( className, fieldName ) ) {
      return pos;
    }

    final String fieldReaderMethodDescriptor = "()" + constPool.getFieldrefType( index );
    final int fieldReaderMethodIndex = constPool.addMethodrefInfo(
        constPool.getThisClassInfo(),
        EACH_READ_METHOD_PREFIX + fieldName,
        fieldReaderMethodDescriptor
    );
    iter.writeByte( Opcode.INVOKEVIRTUAL, pos );
    iter.write16bit( fieldReaderMethodIndex, pos + 1 );
View Full Code Here

    iter.write16bit( fieldReaderMethodIndex, pos + 1 );
    return pos;
  }

  private int transformInvokevirtualsIntoPutfields(ClassFile classfile, CodeIterator iter, int pos) {
    final ConstPool constPool = classfile.getConstPool();
    final int c = iter.byteAt( pos );
    if ( c != Opcode.PUTFIELD ) {
      return pos;
    }

    final int index = iter.u16bitAt( pos + 1 );
    final String fieldName = constPool.getFieldrefName( index );
    final String className = constPool.getFieldrefClassName( index );
    if ( !filter.handleWriteAccess( className, fieldName ) ) {
      return pos;
    }

    final String fieldWriterMethodDescriptor = "(" + constPool.getFieldrefType( index ) + ")V";
    final int fieldWriterMethodIndex = constPool.addMethodrefInfo(
        constPool.getThisClassInfo(),
        EACH_WRITE_METHOD_PREFIX + fieldName,
        fieldWriterMethodDescriptor
    );
    iter.writeByte( Opcode.INVOKEVIRTUAL, pos );
    iter.write16bit( fieldWriterMethodIndex, pos + 1 );
View Full Code Here

  private static void addInvokeFieldHandlerMethod(
      ClassFile classfile,
      Bytecode code,
      String typeName,
      boolean isReadMethod) {
    final ConstPool constPool = classfile.getConstPool();
    // invokeinterface
    final int callbackTypeIndex = constPool.addClassInfo( FIELD_HANDLER_TYPE_NAME );
    if ( ( typeName.charAt( 0 ) == 'L' )
        && ( typeName.charAt( typeName.length() - 1 ) == ';' )
        || ( typeName.charAt( 0 ) == '[' ) ) {
      // reference type
      final int indexOfL = typeName.indexOf( 'L' );
View Full Code Here

   * @param classfile The class descriptor
   *
   * @throws CannotCompileException Indicates trouble with the underlying Javassist calls
   */
  private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException {
    final ConstPool constPool = classfile.getConstPool();
    final String constructorSignature = "()V";
    final MethodInfo constructorMethodInfo = new MethodInfo( constPool, MethodInfo.nameInit, constructorSignature );

    final Bytecode code = new Bytecode( constPool, 0, 1 );
    // aload_0
View Full Code Here

    constructorMethodInfo.setAccessFlags( AccessFlag.PUBLIC );
    classfile.addMethod( constructorMethodInfo );
  }

  private void addGetter(ClassFile classfile, final Method[] getters) throws CannotCompileException {
    final ConstPool constPool = classfile.getConstPool();
    final int targetBeanConstPoolIndex = constPool.addClassInfo( this.targetBean.getName() );
    final String desc = GET_SETTER_DESC;
    final MethodInfo getterMethodInfo = new MethodInfo( constPool, GENERATED_GETTER_NAME, desc );

    final Bytecode code = new Bytecode( constPool, 6, 4 );
    /* | this | bean | args | raw bean | */
 
View Full Code Here

    getterMethodInfo.setAccessFlags( AccessFlag.PUBLIC );
    classfile.addMethod( getterMethodInfo );
  }

  private void addSetter(ClassFile classfile, final Method[] setters) throws CannotCompileException {
    final ConstPool constPool = classfile.getConstPool();
    final int targetTypeConstPoolIndex = constPool.addClassInfo( this.targetBean.getName() );
    final String desc = GET_SETTER_DESC;
    final MethodInfo setterMethodInfo = new MethodInfo( constPool, GENERATED_SETTER_NAME, desc );

    final Bytecode code = new Bytecode( constPool, 4, 6 );
    StackMapTable stackmap = null;
    /* | this | bean | args | i | raw bean | exception | */
    if ( setters.length > 0 ) {
      // required to exception table
      int start;
      int end;
      // iconst_0 // i
      code.addIconst( 0 );
      // istore_3 // store i
      code.addIstore( 3 );
      // aload_1 // load the bean
      code.addAload( 1 );
      // checkcast // cast the bean into a raw bean
      code.addCheckcast( this.targetBean.getName() );
      // astore 4 // store the raw bean
      code.addAstore( 4 );
      /* current stack len = 0 */
      // start region to handling exception (BulkAccessorException)
      start = code.currentPc();
      int lastIndex = 0;
      for ( int i = 0; i < setters.length; ++i ) {
        if ( setters[i] != null ) {
          final int diff = i - lastIndex;
          if ( diff > 0 ) {
            // iinc 3, 1
            code.addOpcode( Opcode.IINC );
            code.add( 3 );
            code.add( diff );
            lastIndex = i;
          }
        }
        /* current stack len = 0 */
        // aload 4 // load the raw bean
        code.addAload( 4 );
        // aload_2 // load the args
        code.addAload( 2 );
        // iconst_i
        code.addIconst( i );
        // aaload
        code.addOpcode( Opcode.AALOAD );
        // checkcast
        final Class[] setterParamTypes = setters[i].getParameterTypes();
        final Class setterParamType = setterParamTypes[0];
        if ( setterParamType.isPrimitive() ) {
          // checkcast (case of primitive type)
          // invokevirtual (case of primitive type)
          this.addUnwrapper( code, setterParamType );
        }
        else {
          // checkcast (case of reference type)
          code.addCheckcast( setterParamType.getName() );
        }
        /* current stack len = 2 */
        final String rawSetterMethodDesc = RuntimeSupport.makeDescriptor( setters[i] );
        if ( !this.targetBean.isInterface() ) {
          // invokevirtual
          code.addInvokevirtual( targetTypeConstPoolIndex, setters[i].getName(), rawSetterMethodDesc );
        }
        else {
          // invokeinterface
          final Class[] params = setters[i].getParameterTypes();
          int size;
          if ( params[0].equals( Double.TYPE ) || params[0].equals( Long.TYPE ) ) {
            size = 3;
          }
          else {
            size = 2;
          }

          code.addInvokeinterface( targetTypeConstPoolIndex, setters[i].getName(), rawSetterMethodDesc, size );
        }
      }

      // end region to handling exception (BulkAccessorException)
      end = code.currentPc();
      // return
      code.addOpcode( Opcode.RETURN );
      /* current stack len = 0 */
      // register in exception table
      final int throwableTypeIndex = constPool.addClassInfo( THROWABLE_CLASS_NAME );
      final int handlerPc = code.currentPc();
      code.addExceptionHandler( start, end, handlerPc, throwableTypeIndex );
      // astore 5 // store exception
      code.addAstore( 5 );
      // new // BulkAccessorException
      code.addNew( BULKEXCEPTION_CLASS_NAME );
      // dup
      code.addOpcode( Opcode.DUP );
      // aload 5 // load exception
      code.addAload( 5 );
      // iload_3 // i
      code.addIload( 3 );
      // invokespecial // BulkAccessorException.<init>
      final String consDesc = "(Ljava/lang/Throwable;I)V";
      code.addInvokespecial( BULKEXCEPTION_CLASS_NAME, MethodInfo.nameInit, consDesc );
      // athrow
      code.addOpcode( Opcode.ATHROW );
      final StackMapTable.Writer writer = new StackMapTable.Writer(32);
      final int[] localTags = {
          StackMapTable.OBJECT,
          StackMapTable.OBJECT,
          StackMapTable.OBJECT,
          StackMapTable.INTEGER
      };
      final int[] localData = {
          constPool.getThisClassInfo(),
          constPool.addClassInfo( "java/lang/Object" ),
          constPool.addClassInfo( "[Ljava/lang/Object;" ),
          0
      };
      final int[] stackTags = {
          StackMapTable.OBJECT
      };
View Full Code Here

TOP

Related Classes of javassist.bytecode.ConstPool

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.