Class ExceptionMethod

java.lang.Object
net.bytebuddy.implementation.ExceptionMethod
All Implemented Interfaces:
InstrumentedType.Prepareable, ByteCodeAppender, Implementation

@Enhance public class ExceptionMethod extends Object implements Implementation, ByteCodeAppender
This implementation causes a Throwable to be thrown when the instrumented method is invoked. Be aware that the Java Virtual machine does not care about exception declarations and will throw any Throwable from any method even if the method does not declared a checked exception.
  • Field Details

    • constructionDelegate

      private final ExceptionMethod.ConstructionDelegate constructionDelegate
      The construction delegation which is responsible for creating the exception to be thrown.
  • Constructor Details

    • ExceptionMethod

      public ExceptionMethod(ExceptionMethod.ConstructionDelegate constructionDelegate)
      Creates a new instance of an implementation for throwing throwables.
      Parameters:
      constructionDelegate - A delegate that is responsible for calling the Throwable's constructor.
  • Method Details

    • throwing

      public static Implementation throwing(Class<? extends Throwable> throwableType)
      Creates an implementation that creates a new instance of the given Throwable type on each method invocation which is then thrown immediately. For this to be possible, the given type must define a default constructor which is visible from the instrumented type.
      Parameters:
      throwableType - The type of the Throwable.
      Returns:
      An implementation that will throw an instance of the Throwable on each method invocation of the instrumented methods.
    • throwing

      public static Implementation throwing(TypeDescription throwableType)
      Creates an implementation that creates a new instance of the given Throwable type on each method invocation which is then thrown immediately. For this to be possible, the given type must define a default constructor which is visible from the instrumented type.
      Parameters:
      throwableType - The type of the Throwable.
      Returns:
      An implementation that will throw an instance of the Throwable on each method invocation of the instrumented methods.
    • throwing

      public static Implementation throwing(Class<? extends Throwable> throwableType, String message)
      Creates an implementation that creates a new instance of the given Throwable type on each method invocation which is then thrown immediately. For this to be possible, the given type must define a constructor that takes a single String as its argument.
      Parameters:
      throwableType - The type of the Throwable.
      message - The string that is handed to the constructor. Usually an exception message.
      Returns:
      An implementation that will throw an instance of the Throwable on each method invocation of the instrumented methods.
    • throwing

      public static Implementation throwing(TypeDescription throwableType, String message)
      Creates an implementation that creates a new instance of the given Throwable type on each method invocation which is then thrown immediately. For this to be possible, the given type must define a constructor that takes a single String as its argument.
      Parameters:
      throwableType - The type of the Throwable.
      message - The string that is handed to the constructor. Usually an exception message.
      Returns:
      An implementation that will throw an instance of the Throwable on each method invocation of the instrumented methods.
    • prepare

      public InstrumentedType prepare(InstrumentedType instrumentedType)
      Prepares a given instrumented type.
      Specified by:
      prepare in interface InstrumentedType.Prepareable
      Parameters:
      instrumentedType - The instrumented type in its current form.
      Returns:
      The prepared instrumented type.
    • appender

      public ByteCodeAppender appender(Implementation.Target implementationTarget)
      Creates a byte code appender that determines the implementation of the instrumented type's methods.
      Specified by:
      appender in interface Implementation
      Parameters:
      implementationTarget - The target of the current implementation.
      Returns:
      A byte code appender for implementing methods delegated to this implementation. This byte code appender is also responsible for handling methods that were added by this implementation on the call to InstrumentedType.Prepareable.prepare(InstrumentedType).
    • apply

      public ByteCodeAppender.Size apply(org.objectweb.asm.MethodVisitor methodVisitor, Implementation.Context implementationContext, MethodDescription instrumentedMethod)
      Applies this byte code appender to a type creation process.
      Specified by:
      apply in interface ByteCodeAppender
      Parameters:
      methodVisitor - The method visitor to which the byte code appender writes its code to.
      implementationContext - The implementation context of the current type creation process.
      instrumentedMethod - The method that is the target of the instrumentation.
      Returns:
      The required size for the applied byte code to run.