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

The following examples show how to use net.bytebuddy.jar.asm.MethodVisitor#visitLabel() . 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: 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 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
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 4
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 5
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 6
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 7
Source File: SetJumpTargetLabel.java    From curiostack with MIT License 4 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
  methodVisitor.visitLabel(label);
  return new Size(0, 0);
}
 
Example 8
Source File: VisitLabelStackManipulation.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.visitLabel(label);
    return new Size(0, 0);
}