Package net.bytebuddy.implementation
Class ExceptionMethod
- java.lang.Object
-
- net.bytebuddy.implementation.ExceptionMethod
-
- All Implemented Interfaces:
InstrumentedType.Prepareable
,ByteCodeAppender
,Implementation
@Enhance public class ExceptionMethod extends java.lang.Object implements Implementation, ByteCodeAppender
This implementation causes aThrowable
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 anyThrowable
from any method even if the method does not declared a checked exception.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ExceptionMethod.ConstructionDelegate
A construction delegate is responsible for calling aThrowable
's constructor.-
Nested classes/interfaces inherited from interface net.bytebuddy.implementation.bytecode.ByteCodeAppender
ByteCodeAppender.Compound, ByteCodeAppender.Simple, ByteCodeAppender.Size
-
Nested classes/interfaces inherited from interface net.bytebuddy.implementation.Implementation
Implementation.Composable, Implementation.Compound, Implementation.Context, Implementation.Simple, Implementation.SpecialMethodInvocation, Implementation.Target
-
-
Field Summary
Fields Modifier and Type Field Description private ExceptionMethod.ConstructionDelegate
constructionDelegate
The construction delegation which is responsible for creating the exception to be thrown.
-
Constructor Summary
Constructors Constructor Description ExceptionMethod(ExceptionMethod.ConstructionDelegate constructionDelegate)
Creates a new instance of an implementation for throwing throwables.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteCodeAppender
appender(Implementation.Target implementationTarget)
Creates a byte code appender that determines the implementation of the instrumented type's methods.ByteCodeAppender.Size
apply(org.objectweb.asm.MethodVisitor methodVisitor, Implementation.Context implementationContext, MethodDescription instrumentedMethod)
Applies this byte code appender to a type creation process.InstrumentedType
prepare(InstrumentedType instrumentedType)
Prepares a given instrumented type.static Implementation
throwing(java.lang.Class<? extends java.lang.Throwable> throwableType)
Creates an implementation that creates a new instance of the givenThrowable
type on each method invocation which is then thrown immediately.static Implementation
throwing(java.lang.Class<? extends java.lang.Throwable> throwableType, java.lang.String message)
Creates an implementation that creates a new instance of the givenThrowable
type on each method invocation which is then thrown immediately.static Implementation
throwing(TypeDescription throwableType)
Creates an implementation that creates a new instance of the givenThrowable
type on each method invocation which is then thrown immediately.static Implementation
throwing(TypeDescription throwableType, java.lang.String message)
Creates an implementation that creates a new instance of the givenThrowable
type on each method invocation which is then thrown immediately.
-
-
-
Field Detail
-
constructionDelegate
private final ExceptionMethod.ConstructionDelegate constructionDelegate
The construction delegation which is responsible for creating the exception to be thrown.
-
-
Constructor Detail
-
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 theThrowable
's constructor.
-
-
Method Detail
-
throwing
public static Implementation throwing(java.lang.Class<? extends java.lang.Throwable> throwableType)
Creates an implementation that creates a new instance of the givenThrowable
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 theThrowable
.- 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 givenThrowable
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 theThrowable
.- Returns:
- An implementation that will throw an instance of the
Throwable
on each method invocation of the instrumented methods.
-
throwing
public static Implementation throwing(java.lang.Class<? extends java.lang.Throwable> throwableType, java.lang.String message)
Creates an implementation that creates a new instance of the givenThrowable
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 singleString
as its argument.- Parameters:
throwableType
- The type of theThrowable
.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, java.lang.String message)
Creates an implementation that creates a new instance of the givenThrowable
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 singleString
as its argument.- Parameters:
throwableType
- The type of theThrowable
.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 interfaceInstrumentedType.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 interfaceImplementation
- 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 interfaceByteCodeAppender
- 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.
-
-