net.bytebuddy.jar.asm.Label Java Examples

The following examples show how to use net.bytebuddy.jar.asm.Label. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleExceptionHandler.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
private StackManipulation postTrigger(final Label endTryBlock, final Label startCatchBlock) {
    return new StackManipulation() {
        @Override
        public boolean isValid() {
            return true;
        }

        @Override
        public Size apply(MethodVisitor mv, Implementation.Context ic) {
            mv.visitLabel(endTryBlock);
            // and then do catch block
            mv.visitLabel(startCatchBlock);

            //although this StackManipulation does not alter the stack on it's own
            //however when an exception is caught
            //the exception will be on the top of the stack (being placed there by the JVM)
            //we need to increment the stack size
            return new Size(1, 0);
        }
    };
}
 
Example #2
Source File: SimpleExceptionHandler.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
private StackManipulation preTrigger(final Label startTryBlock,
                                     final Label endTryBlock,
                                     final Label startCatchBlock) {
    return new StackManipulation() {
        @Override
        public boolean isValid() {
            return true;
        }

        @Override
        public Size apply(MethodVisitor mv, Implementation.Context ic) {
            final String name = exceptionType.getName();
            mv.visitTryCatchBlock(startTryBlock, endTryBlock, startCatchBlock, name.replace(".", "/"));
            mv.visitLabel(startTryBlock);
            return new Size(0,0);
        }
    };
}
 
Example #3
Source File: SimpleExceptionHandler.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor,
                  Implementation.Context implementationContext,
                  MethodDescription instrumentedMethod) {

    final Label startTryBlock = new Label();
    final Label endTryBlock = new Label();
    final Label startCatchBlock = new Label();

    final StackManipulation preTriggerAppender = preTrigger(startTryBlock, endTryBlock, startCatchBlock);
    final StackManipulation postTriggerAppender = postTrigger(endTryBlock, startCatchBlock);

    final StackManipulation delegate = new StackManipulation.Compound(
            preTriggerAppender, exceptionThrowingAppender, postTriggerAppender, exceptionHandlerAppender
    );

    final StackManipulation.Size operandStackSize = delegate.apply(methodVisitor, implementationContext);
    return new Size(operandStackSize.getMaximalSize(), instrumentedMethod.getStackSize() + newLocalVariablesCount);
}
 
Example #4
Source File: UsingSwitchStackManipulation.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    final List<StackManipulation> stackManipulations = new ArrayList<StackManipulation>();

    stackManipulations.add(loadFieldIndexArg());

    final Label[] labels = new Label[props.size()];
    for (int i = 0, len = labels.length; i < len; ++i) {
        labels[i] = new Label();
    }
    final Label defaultLabel = new Label();
    stackManipulations.add(new TableSwitchStackManipulation(labels, defaultLabel));

    for (int i = 0, len = labels.length; i < len; ++i) {
        stackManipulations.add(new VisitLabelStackManipulation(labels[i]));
        stackManipulations.add(singlePropStackManipulationSupplier.supply(props.get(i)));
    }
    stackManipulations.add(new VisitLabelStackManipulation(defaultLabel));
    // and if no match, generate exception:
    stackManipulations.add(new GenerateIllegalPropertyCountExceptionStackManipulation(props.size()));

    final StackManipulation.Compound compound = new StackManipulation.Compound(stackManipulations);
    return compound.apply(methodVisitor, implementationContext);
}
 
Example #5
Source File: AdviceExceptionHandler.java    From kanela with Apache License 2.0 6 votes vote down vote up
/**
 * Produces the following bytecode:
 *
 * <pre>
 * } catch(Throwable throwable) {
 *     kanela.agent.bootstrap.log.LoggerHandler.error("An error occurred while trying to apply an advisor", throwable)
 * }
 * </pre>
 */
private static StackManipulation getStackManipulation() {
    return new StackManipulation() {
        @Override
        public boolean isValid() {
            return true;
        }

        @Override
        public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
            val endCatchBlock = new Label();
            //message
            methodVisitor.visitLdcInsn("An error occurred while trying to apply an advisor");
            // logger, message, throwable => throwable, message, logger
            methodVisitor.visitInsn(Opcodes.SWAP);
            methodVisitor.visitMethodInsn(INVOKESTATIC, "kanela/agent/bootstrap/log/LoggerHandler", "error", "(Ljava/lang/String;Ljava/lang/Throwable;)V", false);
            methodVisitor.visitJumpInsn(GOTO, endCatchBlock);
            // ending catch block
            methodVisitor.visitLabel(endCatchBlock);
            return new StackManipulation.Size(-1, 1);
        }
    };
}
 
Example #6
Source File: DoWrite.java    From curiostack with MIT License 5 votes vote down vote up
private static StackManipulation checkPrimitiveDefault(
    StackManipulation getValue,
    StackManipulation loadDefault,
    Class<?> variableType,
    Label afterPrint) {
  return new StackManipulation.Compound(
      getValue, loadDefault, new IfEqual(variableType, afterPrint));
}
 
Example #7
Source File: DoParse.java    From curiostack with MIT License 5 votes vote down vote up
/**
 * Returns the {@link StackManipulation} for setting the value of a field. This will be all the
 * elements for a repeated field.
 *
 * @param info description of the field to set.
 * @param beforeReadField jump target for before reading a field, used once this field is
 *     completed being set.
 * @param locals the method local variables
 * @param fieldsByName the instance fields
 */
private StackManipulation setFieldValue(
    ProtoFieldInfo info,
    Label beforeReadField,
    LocalVariables<LocalVariable> locals,
    Map<String, FieldDescription> fieldsByName) {
  if (info.isMapField()) {
    return setMapFieldValue(info, beforeReadField, locals, fieldsByName);
  } else {
    final StackManipulation setConcreteValue = invoke(info.setValueMethod());
    final StackManipulation setSingleValue;
    if (info.valueJavaType() == JavaType.MESSAGE) {
      setSingleValue =
          new StackManipulation.Compound(
              TypeCasting.to(new ForLoadedType(info.javaClass())), setConcreteValue);
    } else if (info.valueType() == Type.ENUM && !info.isRepeated()) {
      // For non-repeated enums, we treat unknown as the default value.
      setSingleValue =
          new StackManipulation.Compound(ParseSupport_mapUnknownEnumValue, setConcreteValue);
    } else {
      setSingleValue = setConcreteValue;
    }
    if (info.descriptor().isRepeated()) {
      return setRepeatedFieldValue(info, beforeReadField, locals, fieldsByName, setSingleValue);
    } else {
      // Set a singular value, e.g.,
      // builder.setFoo(readValue());
      return new StackManipulation.Compound(
          locals.load(LocalVariable.builder),
          locals.load(LocalVariable.parser),
          readValue(info, fieldsByName, locals),
          setSingleValue,
          Removal.SINGLE,
          new Goto(beforeReadField));
    }
  }
}
 
Example #8
Source File: CallSiteInfoAddingMethodVisitor.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Override
public void visitLineNumber(int line, Label start) {
    super.visitLineNumber(line, start);
    currentLine = line;
}
 
Example #9
Source File: InlineDirtyCheckingHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Size apply(
		MethodVisitor methodVisitor,
		Context implementationContext,
		MethodDescription instrumentedMethod) {
	// if (arg != field) {
	methodVisitor.visitVarInsn( Type.getType( persistentField.getType().asErasure().getDescriptor() ).getOpcode( Opcodes.ILOAD ), 1 );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	if ( persistentField.getDeclaringType().asErasure().equals( managedCtClass ) ) {
		methodVisitor.visitFieldInsn(
				Opcodes.GETFIELD,
				persistentField.getDeclaringType().asErasure().getInternalName(),
				persistentField.getName(),
				persistentField.getDescriptor()
		);
	}
	else {
		methodVisitor.visitMethodInsn(
				Opcodes.INVOKEVIRTUAL,
				persistentField.getDeclaringType().asErasure().getInternalName(),
				EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + persistentField.getName(),
				Type.getMethodDescriptor( Type.getType( persistentField.getDescriptor() ) ),
				false
		);
	}
	int branchCode;
	if ( persistentField.getType().isPrimitive() ) {
		if ( persistentField.getType().represents( long.class ) ) {
			methodVisitor.visitInsn( Opcodes.LCMP );
		}
		else if ( persistentField.getType().represents( float.class ) ) {
			methodVisitor.visitInsn( Opcodes.FCMPL );
		}
		else if ( persistentField.getType().represents( double.class ) ) {
			methodVisitor.visitInsn( Opcodes.DCMPL );
		}
		else {
			methodVisitor.visitInsn( Opcodes.ISUB );
		}
		branchCode = Opcodes.IFEQ;
	}
	else {
		methodVisitor.visitMethodInsn(
				Opcodes.INVOKESTATIC,
				Type.getInternalName( Objects.class ),
				"deepEquals",
				Type.getMethodDescriptor( Type.getType( boolean.class ), Type.getType( Object.class ), Type.getType( Object.class ) ),
				false
		);
		branchCode = Opcodes.IFNE;
	}
	Label skip = new Label();
	methodVisitor.visitJumpInsn( branchCode, skip );
	// this.$$_hibernate_trackChange(fieldName)
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitLdcInsn( persistentField.getName() );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.TRACKER_CHANGER_NAME,
			Type.getMethodDescriptor( Type.getType( void.class ), Type.getType( String.class ) ),
			false
	);
	// }
	methodVisitor.visitLabel( skip );
	if ( implementationContext.getClassFileVersion().isAtLeast( ClassFileVersion.JAVA_V6 ) ) {
		methodVisitor.visitFrame( Opcodes.F_SAME, 0, null, 0, null );
	}
	return new Size( 1 + 2 * persistentField.getType().asErasure().getStackSize().getSize(), instrumentedMethod.getStackSize() );
}
 
Example #10
Source File: ReturnHandlingMethodVisitor.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Override
public void visitLineNumber(int line, Label start) {
    super.visitLineNumber(line, start);
    currentLine = line;
}
 
Example #11
Source File: JumpStackManipulation.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
public static JumpStackManipulation if_icmpne(Label label) {
    return new JumpStackManipulation(IF_ICMPNE, -2, label);
}
 
Example #12
Source File: JumpStackManipulation.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
public static JumpStackManipulation ifne(Label label) {
    return new JumpStackManipulation(IFNE, -1, label);
}
 
Example #13
Source File: JumpStackManipulation.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
private JumpStackManipulation(int opcde, int stackImpact, Label label) {
    this.label = label;
    this.stackImpact = stackImpact;
    this.opcde = opcde;
}
 
Example #14
Source File: TableSwitchStackManipulation.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
public TableSwitchStackManipulation(Label[] labels, Label defaultLabel) {
    this.labels = labels;
    this.defaultLabel = defaultLabel;
}
 
Example #15
Source File: VisitLabelStackManipulation.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
public VisitLabelStackManipulation(Label label) {
    this.label = label;
}
 
Example #16
Source File: DoParse.java    From curiostack with MIT License 4 votes vote down vote up
/**
 * Returns the {@link StackManipulation} for setting the value of a map field.
 *
 * <p>Roughly equivalent to:
 *
 * <pre>{@code
 * ParseSupport.parseObjectStart(parser);
 * while (!ParseSupport.checkObjectEnd(parser.currentToken())) {
 *   builder.putFoo(readKey(), readValue());
 * }
 * }</pre>
 */
private StackManipulation setMapFieldValue(
    ProtoFieldInfo info,
    Label beforeReadField,
    LocalVariables<LocalVariable> locals,
    Map<String, FieldDescription> fieldsByName) {
  final StackManipulation setConcreteValue = invoke(info.setValueMethod());
  final StackManipulation setMapEntry;
  if (info.valueJavaType() == JavaType.MESSAGE) {
    setMapEntry =
        new StackManipulation.Compound(
            TypeCasting.to(new ForLoadedType(info.javaClass())), setConcreteValue);
  } else {
    setMapEntry = setConcreteValue;
  }
  Label mapStart = new Label();
  Label afterSet = new Label();

  StackManipulation.Compound beforeReadKey =
      new StackManipulation.Compound(
          locals.load(LocalVariable.parser),
          ParseSupport_parseObjectStart,
          new SetJumpTargetLabel(mapStart),
          locals.load(LocalVariable.parser),
          Parser_currentToken,
          ParseSupport_checkObjectEnd,
          new IfTrue(beforeReadField));

  StackManipulation.Compound setValueAndPrepareForNext =
      new StackManipulation.Compound(
          setMapEntry,
          Removal.SINGLE,
          new SetJumpTargetLabel(afterSet),
          locals.load(LocalVariable.parser),
          Parser_nextToken,
          Removal.SINGLE,
          new Goto(mapStart));

  if (info.valueType() == Type.ENUM) {
    // We special-case enum since we may need to skip unknown values.
    final LocalVariable keyVar;
    switch (info.mapKeyField().valueJavaType()) {
      case INT:
        keyVar = LocalVariable.intMapKey;
        break;
      case LONG:
        keyVar = LocalVariable.longMapKey;
        break;
      case BOOLEAN:
        keyVar = LocalVariable.boolMapKey;
        break;
      case STRING:
        keyVar = LocalVariable.stringMapKey;
        break;
      default:
        throw new IllegalArgumentException("Invalid map key type");
    }

    return new StackManipulation.Compound(
        beforeReadKey,
        locals.load(LocalVariable.parser),
        readValue(info.mapKeyField(), fieldsByName, locals),
        locals.store(keyVar),
        locals.load(LocalVariable.parser),
        Parser_nextToken,
        Removal.SINGLE,
        locals.load(LocalVariable.parser),
        readValue(info, fieldsByName, locals),
        locals.store(LocalVariable.intvalue),
        locals.load(LocalVariable.intvalue),
        IntegerConstant.forValue(-1),
        new IfEqual(int.class, afterSet),
        locals.load(LocalVariable.builder),
        locals.load(keyVar),
        locals.load(LocalVariable.intvalue),
        setValueAndPrepareForNext);
  } else {
    return new StackManipulation.Compound(
        beforeReadKey,
        locals.load(LocalVariable.builder),
        locals.load(LocalVariable.parser),
        readValue(info.mapKeyField(), fieldsByName, locals),
        locals.load(LocalVariable.parser),
        Parser_nextToken,
        Removal.SINGLE,
        locals.load(LocalVariable.parser),
        readValue(info, fieldsByName, locals),
        setValueAndPrepareForNext);
  }
}
 
Example #17
Source File: DoParse.java    From curiostack with MIT License 4 votes vote down vote up
/**
 * Returns the {@link StackManipulation} for setting the value of a normal repeated field.
 *
 * <p>Roughly equivalent to:
 *
 * <pre>{@code
 * ParseSupport.parseArrayStart(parser);
 * while (!ParseSupport.checkArrayEnd(parser)) {
 *   builder.addFoo(readValue());
 * }
 * }</pre>
 */
private StackManipulation setRepeatedFieldValue(
    ProtoFieldInfo info,
    Label beforeReadField,
    LocalVariables<LocalVariable> locals,
    Map<String, FieldDescription> fieldsByName,
    StackManipulation setSingleValue) {
  Label arrayStart = new Label();

  StackManipulation.Compound beforeRead =
      new StackManipulation.Compound(
          locals.load(LocalVariable.parser),
          ParseSupport_parseArrayStart,
          new SetJumpTargetLabel(arrayStart),
          locals.load(LocalVariable.parser),
          ParseSupport_throwIfRepeatedNull,
          locals.load(LocalVariable.parser),
          ParseSupport_checkArrayEnd,
          new IfTrue(beforeReadField));

  Label afterSet = new Label();

  StackManipulation.Compound setValueAndPrepareForNext =
      new StackManipulation.Compound(
          setSingleValue,
          Removal.SINGLE,
          new SetJumpTargetLabel(afterSet),
          locals.load(LocalVariable.parser),
          Parser_nextValue,
          Removal.SINGLE,
          new Goto(arrayStart));

  if (info.valueType() == Type.ENUM) {
    // We special-case enum since we may need to skip unknown values.
    return new StackManipulation.Compound(
        beforeRead,
        locals.load(LocalVariable.parser),
        readValue(info, fieldsByName, locals),
        locals.store(LocalVariable.intvalue),
        locals.load(LocalVariable.intvalue),
        IntegerConstant.forValue(-1),
        new IfEqual(int.class, afterSet),
        locals.load(LocalVariable.builder),
        locals.load(LocalVariable.intvalue),
        setValueAndPrepareForNext);
  } else {
    return new StackManipulation.Compound(
        beforeRead,
        locals.load(LocalVariable.builder),
        locals.load(LocalVariable.parser),
        readValue(info, fieldsByName, locals),
        setValueAndPrepareForNext);
  }
}
 
Example #18
Source File: DoWrite.java    From curiostack with MIT License 4 votes vote down vote up
/**
 * Retrurns a {@link StackManipulation} that checks whether the value in the message is a default
 * value, and if so jumps to after serialization to skip the serialization of the default value.
 * e.g.,
 *
 * <pre>{code
 *   if (message.getFoo() != field.getDefaultValue) {
 *     ...
 *   }
 *   // afterSerializeField
 * }</pre>
 */
private static StackManipulation checkDefaultValue(
    ProtoFieldInfo info,
    LocalVariables<LocalVariable> locals,
    StackManipulation getValue,
    StackManipulation getDefaultInstance,
    Label afterSerializeField) {
  if (info.isInOneof()) {
    // For one-ofs, we can just check the set field number directly.
    return new StackManipulation.Compound(
        locals.load(LocalVariable.message),
        CodeGenUtil.invoke(info.oneOfCaseMethod()),
        EnumLite_getNumber,
        IntegerConstant.forValue(info.descriptor().getNumber()),
        new IfIntsNotEqual(afterSerializeField));
  } else if (!info.isRepeated()) {
    switch (info.valueJavaType()) {
      case INT:
        return checkPrimitiveDefault(
            getValue,
            IntegerConstant.forValue((int) info.descriptor().getDefaultValue()),
            int.class,
            afterSerializeField);
      case LONG:
        return checkPrimitiveDefault(
            getValue,
            LongConstant.forValue((long) info.descriptor().getDefaultValue()),
            long.class,
            afterSerializeField);
      case FLOAT:
        return checkPrimitiveDefault(
            getValue,
            FloatConstant.forValue((float) info.descriptor().getDefaultValue()),
            float.class,
            afterSerializeField);
      case DOUBLE:
        return checkPrimitiveDefault(
            getValue,
            DoubleConstant.forValue((double) info.descriptor().getDefaultValue()),
            double.class,
            afterSerializeField);
      case BOOLEAN:
        return checkPrimitiveDefault(
            getValue,
            IntegerConstant.forValue((boolean) info.descriptor().getDefaultValue()),
            boolean.class,
            afterSerializeField);
      case ENUM:
        return checkPrimitiveDefault(
            getValue,
            IntegerConstant.forValue(
                ((EnumValueDescriptor) info.descriptor().getDefaultValue()).getNumber()),
            int.class,
            afterSerializeField);
      case STRING:
        return new StackManipulation.Compound(
            getValue,
            new TextConstant((String) info.descriptor().getDefaultValue()),
            Object_equals,
            new IfEqual(Object.class, afterSerializeField));
      case BYTE_STRING:
        // We'll use the default instance to get the default value for types that can't be
        // loaded into the constant pool. Since it's a constant, the somewhat indirect reference
        // should get inlined and be the same as a class constant.
        return new StackManipulation.Compound(
            getValue,
            getDefaultInstance,
            invoke(info.getValueMethod()),
            Object_equals,
            new IfEqual(Object.class, afterSerializeField));
      case MESSAGE:
        return new StackManipulation.Compound(
            locals.load(LocalVariable.message),
            invoke(info.hasValueMethod()),
            new IfFalse(afterSerializeField));
      default:
        throw new IllegalStateException("Unknown JavaType: " + info.valueJavaType());
    }
  } else {
    return new StackManipulation.Compound(
        locals.load(LocalVariable.message),
        CodeGenUtil.invoke(info.repeatedValueCountMethod()),
        new IfFalse(afterSerializeField));
  }
}
 
Example #19
Source File: IfEqual.java    From curiostack with MIT License 4 votes vote down vote up
public IfEqual(Class<?> variableType, Label destination) {
  this.variableType = variableType;
  this.destination = destination;
}
 
Example #20
Source File: SetJumpTargetLabel.java    From curiostack with MIT License 4 votes vote down vote up
public SetJumpTargetLabel(Label label) {
  this.label = label;
}
 
Example #21
Source File: IfNotNull.java    From curiostack with MIT License 4 votes vote down vote up
public IfNotNull(Label destination) {
  this.destination = destination;
}
 
Example #22
Source File: Goto.java    From curiostack with MIT License 4 votes vote down vote up
public Goto(Label destination) {
  this.destination = destination;
}
 
Example #23
Source File: IfTrue.java    From curiostack with MIT License 4 votes vote down vote up
public IfTrue(Label destination) {
  this.destination = destination;
}
 
Example #24
Source File: IfFalse.java    From curiostack with MIT License 4 votes vote down vote up
public IfFalse(Label destination) {
  this.destination = destination;
}
 
Example #25
Source File: IfRefsNotEqual.java    From curiostack with MIT License 4 votes vote down vote up
public IfRefsNotEqual(Label destination) {
  this.destination = destination;
}
 
Example #26
Source File: IfRefsEqual.java    From curiostack with MIT License 4 votes vote down vote up
public IfRefsEqual(Label destination) {
  this.destination = destination;
}
 
Example #27
Source File: IfIntsNotEqual.java    From curiostack with MIT License 4 votes vote down vote up
public IfIntsNotEqual(Label destination) {
  this.destination = destination;
}
 
Example #28
Source File: FieldWriterAppender.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Size apply(
		MethodVisitor methodVisitor,
		Implementation.Context implementationContext,
		MethodDescription instrumentedMethod) {
	TypeDescription dispatcherType = persistentFieldAsDefined.getType().isPrimitive()
			? persistentFieldAsDefined.getType().asErasure()
			: TypeDescription.OBJECT;
	// if ( this.$$_hibernate_getInterceptor() != null )
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.INTERCEPTOR_GETTER_NAME,
			Type.getMethodDescriptor( Type.getType( PersistentAttributeInterceptor.class ) ),
			false
	);
	Label noInterceptor = new Label();
	methodVisitor.visitJumpInsn( Opcodes.IFNULL, noInterceptor );
	// this (for field write)
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	// this.$$_hibernate_getInterceptor();
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.INTERCEPTOR_GETTER_NAME,
			Type.getMethodDescriptor( Type.getType( PersistentAttributeInterceptor.class ) ),
			false
	);
	// .writeXXX( self, fieldName, field, arg1 );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitLdcInsn( persistentFieldAsDefined.getName() );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	fieldRead( methodVisitor );
	methodVisitor.visitVarInsn( Type.getType( dispatcherType.getDescriptor() ).getOpcode( Opcodes.ILOAD ), 1 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEINTERFACE,
			Type.getInternalName( PersistentAttributeInterceptor.class ),
			"write" + EnhancerImpl.capitalize( dispatcherType.getSimpleName() ),
			Type.getMethodDescriptor(
					Type.getType( dispatcherType.getDescriptor() ),
					Type.getType( Object.class ),
					Type.getType( String.class ),
					Type.getType( dispatcherType.getDescriptor() ),
					Type.getType( dispatcherType.getDescriptor() )
			),
			true
	);
	// arg1 = (cast) XXX
	if ( !dispatcherType.isPrimitive() ) {
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, persistentFieldAsDefined.getType().asErasure().getInternalName() );
	}
	fieldWrite( methodVisitor );
	// return
	methodVisitor.visitInsn( Opcodes.RETURN );
	// else
	methodVisitor.visitLabel( noInterceptor );
	if ( implementationContext.getClassFileVersion().isAtLeast( ClassFileVersion.JAVA_V6 ) ) {
		methodVisitor.visitFrame( Opcodes.F_SAME, 0, null, 0, null );
	}
	// this (for field write)
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	// arg1 = (cast) XXX
	methodVisitor.visitVarInsn( Type.getType( dispatcherType.getDescriptor() ).getOpcode( Opcodes.ILOAD ), 1 );
	if ( !dispatcherType.isPrimitive() ) {
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, persistentFieldAsDefined.getType().asErasure().getInternalName() );
	}
	fieldWrite( methodVisitor );
	// return
	methodVisitor.visitInsn( Opcodes.RETURN );
	return new Size( 4 + 2 * persistentFieldAsDefined.getType().getStackSize().getSize(), instrumentedMethod.getStackSize() );
}
 
Example #29
Source File: FieldReaderAppender.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Size apply(
		MethodVisitor methodVisitor,
		Implementation.Context implementationContext,
		MethodDescription instrumentedMethod) {
	TypeDescription dispatcherType = persistentFieldAsDefined.getType().isPrimitive()
			? persistentFieldAsDefined.getType().asErasure()
			: TypeDescription.OBJECT;
	// if ( this.$$_hibernate_getInterceptor() != null )
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.INTERCEPTOR_GETTER_NAME,
			Type.getMethodDescriptor( Type.getType( PersistentAttributeInterceptor.class ) ),
			false
	);
	Label skip = new Label();
	methodVisitor.visitJumpInsn( Opcodes.IFNULL, skip );
	// this (for field write)
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	// this.$$_hibernate_getInterceptor();
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.INTERCEPTOR_GETTER_NAME,
			Type.getMethodDescriptor( Type.getType( PersistentAttributeInterceptor.class ) ),
			false
	);
	// .readXXX( self, fieldName, field );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitLdcInsn( persistentFieldAsDefined.getName() );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	fieldRead( methodVisitor );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEINTERFACE,
			Type.getInternalName( PersistentAttributeInterceptor.class ),
			"read" + EnhancerImpl.capitalize( dispatcherType.getSimpleName() ),
			Type.getMethodDescriptor(
					Type.getType( dispatcherType.getDescriptor() ),
					Type.getType( Object.class ),
					Type.getType( String.class ),
					Type.getType( dispatcherType.getDescriptor() )
			),
			true
	);
	// field = (cast) XXX
	if ( !dispatcherType.isPrimitive() ) {
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, persistentFieldAsDefined.getType().asErasure().getInternalName() );
	}
	fieldWrite( methodVisitor );
	// end if
	methodVisitor.visitLabel( skip );
	if ( implementationContext.getClassFileVersion().isAtLeast( ClassFileVersion.JAVA_V6 ) ) {
		methodVisitor.visitFrame( Opcodes.F_SAME, 0, null, 0, null );
	}
	// return field
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	fieldRead( methodVisitor );
	if ( !persistentField.getType().isPrimitive()
			&& !persistentField.getType().asErasure().getInternalName().equals( persistentFieldAsDefined.getType().asErasure().getInternalName() ) ) {
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, persistentField.getType().asErasure().getInternalName() );
	}
	methodVisitor.visitInsn( Type.getType( persistentFieldAsDefined.getType().asErasure().getDescriptor() ).getOpcode( Opcodes.IRETURN ) );
	return new Size( 4 + persistentFieldAsDefined.getType().getStackSize().getSize(), instrumentedMethod.getStackSize() );
}