Java Code Examples for net.bytebuddy.jar.asm.MethodVisitor#visitJumpInsn()

The following examples show how to use net.bytebuddy.jar.asm.MethodVisitor#visitJumpInsn() . 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: IfEqual.java    From curiostack with MIT License 6 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
  final int opcode;
  Size size = new Size(-StackSize.of(variableType).getSize() * 2, 0);
  if (variableType == int.class || variableType == boolean.class) {
    opcode = Opcodes.IF_ICMPEQ;
  } else if (variableType == long.class) {
    methodVisitor.visitInsn(Opcodes.LCMP);
    opcode = Opcodes.IFEQ;
  } else if (variableType == float.class) {
    methodVisitor.visitInsn(Opcodes.FCMPG);
    opcode = Opcodes.IFEQ;
  } else if (variableType == double.class) {
    methodVisitor.visitInsn(Opcodes.DCMPG);
    opcode = Opcodes.IFEQ;
  } else {
    // Reference type comparison assumes the result of Object.equals is already on the stack.
    opcode = Opcodes.IFNE;
    // There is only a boolean on the stack, so we only consume one item, unlike the others that
    // consume both.
    size = new Size(-1, 0);
  }
  methodVisitor.visitJumpInsn(opcode, destination);
  return size;
}
 
Example 2
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 3
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 4
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() );
}
 
Example 5
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 6
Source File: IfIntsNotEqual.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
  methodVisitor.visitJumpInsn(Opcodes.IF_ICMPNE, destination);
  return new Size(-StackSize.SINGLE.getSize() * 2, 0);
}
 
Example 7
Source File: IfRefsEqual.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
  methodVisitor.visitJumpInsn(Opcodes.IF_ACMPEQ, destination);
  return new Size(-StackSize.SINGLE.getSize() * 2, 0);
}
 
Example 8
Source File: IfRefsNotEqual.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
  methodVisitor.visitJumpInsn(Opcodes.IF_ACMPNE, destination);
  return new Size(-StackSize.SINGLE.getSize() * 2, 0);
}
 
Example 9
Source File: IfFalse.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
  methodVisitor.visitJumpInsn(Opcodes.IFEQ, destination);
  return StackSize.SINGLE.toDecreasingSize();
}
 
Example 10
Source File: IfTrue.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
  methodVisitor.visitJumpInsn(Opcodes.IFNE, destination);
  return StackSize.SINGLE.toDecreasingSize();
}
 
Example 11
Source File: Goto.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
  methodVisitor.visitJumpInsn(Opcodes.GOTO, destination);
  return new Size(0, 0);
}
 
Example 12
Source File: IfNotNull.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
  methodVisitor.visitJumpInsn(Opcodes.IFNONNULL, destination);
  return StackSize.SINGLE.toDecreasingSize();
}
 
Example 13
Source File: JumpStackManipulation.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    methodVisitor.visitJumpInsn(opcde, label);
    return new Size(stackImpact, 0);
}