Package net.bytebuddy.implementation
Class DefaultMethodCall
- java.lang.Object
-
- net.bytebuddy.implementation.DefaultMethodCall
-
- All Implemented Interfaces:
InstrumentedType.Prepareable,Implementation
@Enhance public class DefaultMethodCall extends java.lang.Object implements Implementation
ThisImplementationinvokes a default method for the methods it instruments. A default method is potentially ambiguous if a method of identical signature is defined in several interfaces. For this reason, this implementation allows for the specification of prioritized interfaces whose default methods, if a method of equivalent signature is defined for a specific interface. All prioritized interfaces are searched for default methods in the order of their specification. If no prioritized interface defines a default method of equivalent signature to the given instrumented method, any non-prioritized interface is searched for a suitable default method. If exactly one of those interfaces defines a suitable default method, this method is invoked. If no method or more than one method is identified as a suitable default method, an exception is thrown.
When it comes to default methods, the Java programming language specifies stronger requirements for the legitimacy of invoking a default method than the Java runtime. The Java compiler requires a method to be the most specific method in its defining type's type hierarchy, i.e. the method must not be overridden by another interface or class type. Additionally, the method must be invokable from an interface type which is directly implemented by the instrumented type. The Java runtime only requires the second condition to be fulfilled which is why this implementation only checks the later condition, as well.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classDefaultMethodCall.AppenderThe appender for implementing aDefaultMethodCall.-
Nested classes/interfaces inherited from interface net.bytebuddy.implementation.Implementation
Implementation.Composable, Implementation.Compound, Implementation.Context, Implementation.Simple, Implementation.SpecialMethodInvocation, Implementation.Target
-
Nested classes/interfaces inherited from interface net.bytebuddy.dynamic.scaffold.InstrumentedType.Prepareable
InstrumentedType.Prepareable.NoOp
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<TypeDescription>prioritizedInterfacesA list of prioritized interfaces in the order in which a method should be attempted to be called.
-
Constructor Summary
Constructors Modifier Constructor Description protectedDefaultMethodCall(java.util.List<TypeDescription> prioritizedInterfaces)Creates a newDefaultMethodCallimplementation for a given list of prioritized interfaces.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteCodeAppenderappender(Implementation.Target implementationTarget)Creates a byte code appender that determines the implementation of the instrumented type's methods.private java.util.List<TypeDescription>filterRelevant(TypeDescription typeDescription)Filters the relevant prioritized interfaces for a given type, i.e.InstrumentedTypeprepare(InstrumentedType instrumentedType)Prepares a given instrumented type.static Implementationprioritize(java.lang.Class<?>... prioritizedInterface)Creates aDefaultMethodCallimplementation which searches the given list of interface types for a suitable default method in their order.static Implementationprioritize(java.lang.Iterable<? extends java.lang.Class<?>> prioritizedInterfaces)Creates aDefaultMethodCallimplementation which searches the given list of interface types for a suitable default method in their order.static Implementationprioritize(java.util.Collection<? extends TypeDescription> prioritizedInterfaces)Creates aDefaultMethodCallimplementation which searches the given list of interface types for a suitable default method in their order.static Implementationprioritize(TypeDescription... prioritizedInterface)Creates aDefaultMethodCallimplementation which searches the given list of interface types for a suitable default method in their order.static ImplementationunambiguousOnly()Creates aDefaultMethodCallimplementation without prioritizing any interface.
-
-
-
Field Detail
-
prioritizedInterfaces
private final java.util.List<TypeDescription> prioritizedInterfaces
A list of prioritized interfaces in the order in which a method should be attempted to be called.
-
-
Constructor Detail
-
DefaultMethodCall
protected DefaultMethodCall(java.util.List<TypeDescription> prioritizedInterfaces)
Creates a newDefaultMethodCallimplementation for a given list of prioritized interfaces.- Parameters:
prioritizedInterfaces- A list of prioritized interfaces in the order in which a method should be attempted to be called.
-
-
Method Detail
-
prioritize
public static Implementation prioritize(java.lang.Class<?>... prioritizedInterface)
Creates aDefaultMethodCallimplementation which searches the given list of interface types for a suitable default method in their order. If no such prioritized interface is suitable, because it is either not defined on the instrumented type or because it does not define a suitable default method, any remaining interface is searched for a suitable default method. If no or more than one method defines a suitable default method, an exception is thrown.- Parameters:
prioritizedInterface- A list of prioritized default method interfaces in their prioritization order.- Returns:
- An implementation which calls an instrumented method's compatible default method that considers the given interfaces to be prioritized in their order.
-
prioritize
public static Implementation prioritize(java.lang.Iterable<? extends java.lang.Class<?>> prioritizedInterfaces)
Creates aDefaultMethodCallimplementation which searches the given list of interface types for a suitable default method in their order. If no such prioritized interface is suitable, because it is either not defined on the instrumented type or because it does not define a suitable default method, any remaining interface is searched for a suitable default method. If no or more than one method defines a suitable default method, an exception is thrown.- Parameters:
prioritizedInterfaces- A list of prioritized default method interfaces in their prioritization order.- Returns:
- An implementation which calls an instrumented method's compatible default method that considers the given interfaces to be prioritized in their order.
-
prioritize
public static Implementation prioritize(TypeDescription... prioritizedInterface)
Creates aDefaultMethodCallimplementation which searches the given list of interface types for a suitable default method in their order. If no such prioritized interface is suitable, because it is either not defined on the instrumented type or because it does not define a suitable default method, any remaining interface is searched for a suitable default method. If no or more than one method defines a suitable default method, an exception is thrown.- Parameters:
prioritizedInterface- A list of prioritized default method interfaces in their prioritization order.- Returns:
- An implementation which calls an instrumented method's compatible default method that considers the given interfaces to be prioritized in their order.
-
prioritize
public static Implementation prioritize(java.util.Collection<? extends TypeDescription> prioritizedInterfaces)
Creates aDefaultMethodCallimplementation which searches the given list of interface types for a suitable default method in their order. If no such prioritized interface is suitable, because it is either not defined on the instrumented type or because it does not define a suitable default method, any remaining interface is searched for a suitable default method. If no or more than one method defines a suitable default method, an exception is thrown.- Parameters:
prioritizedInterfaces- A collection of prioritized default method interfaces in their prioritization order.- Returns:
- An implementation which calls an instrumented method's compatible default method that considers the given interfaces to be prioritized in their order.
-
unambiguousOnly
public static Implementation unambiguousOnly()
Creates aDefaultMethodCallimplementation without prioritizing any interface. Instead, any interface that is defined for a given type is searched for a suitable default method. If exactly one interface defines a suitable default method, this method is invoked from the instrumented method. Otherwise, an exception is thrown.- Returns:
- An implementation which calls an instrumented method's compatible default method if such a method is unambiguous.
-
prepare
public InstrumentedType prepare(InstrumentedType instrumentedType)
Prepares a given instrumented type.- Specified by:
preparein 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:
appenderin 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).
-
filterRelevant
private java.util.List<TypeDescription> filterRelevant(TypeDescription typeDescription)
Filters the relevant prioritized interfaces for a given type, i.e. finds the types that are directly declared on a given instrumented type.- Parameters:
typeDescription- The instrumented type for which the prioritized interfaces are to be filtered.- Returns:
- A list of prioritized interfaces that are additionally implemented by the given type.
-
-