Package net.bytebuddy.build
Class CachedReturnPlugin
java.lang.Object
net.bytebuddy.build.Plugin.ForElementMatcher
net.bytebuddy.build.CachedReturnPlugin
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Plugin
,Plugin.Factory
,ElementMatcher<TypeDescription>
A plugin that caches the return value of a method in a synthetic field. The caching mechanism is not thread-safe but can be used in a
concurrent setup if the cached value is frozen, i.e. only defines
final
fields. In this context, it is possible that
the method is executed multiple times by different threads but at the same time, this approach avoids a volatile
field
declaration. For methods with a primitive return type, the type's default value is used to indicate that a method was not yet invoked.
For methods that return a reference type, null
is used as an indicator. If a method returns such a value, this mechanism will
not work. This plugin does not need to be closed.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static @interface
Indicates the field that stores the cached value.protected static class
An offset mapping for the cached field.static @interface
Indicates methods that should be cached, i.e.Nested classes/interfaces inherited from interface net.bytebuddy.matcher.ElementMatcher
ElementMatcher.Junction<S>
Nested classes/interfaces inherited from interface net.bytebuddy.build.Plugin
Plugin.Engine, Plugin.Factory, Plugin.ForElementMatcher, Plugin.NoOp, Plugin.WithPreprocessor
Nested classes/interfaces inherited from interface net.bytebuddy.build.Plugin.Factory
Plugin.Factory.Simple, Plugin.Factory.UsingReflection
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
The infix symbol for advice classes.private final Map<TypeDescription,
TypeDescription> A map of advice types mapped by their argument type.private final ClassFileLocator
The class file locator to use.private static final MethodDescription.InDefinedShape
A description of theCachedReturnPlugin.Enhance.value()
method.private static final String
An infix between a field and the random suffix if no field name is chosen.private final RandomString
A random string to use for avoid field name collisions. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapply
(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator) Applies this plugin.void
close()
make()
Returns a plugin that can be used for a transformation and which is subsequently closed.Methods inherited from class net.bytebuddy.build.Plugin.ForElementMatcher
matches
-
Field Details
-
NAME_INFIX
An infix between a field and the random suffix if no field name is chosen.- See Also:
-
ADVICE_INFIX
The infix symbol for advice classes.- See Also:
-
ENHANCE_VALUE
A description of theCachedReturnPlugin.Enhance.value()
method. -
randomString
A random string to use for avoid field name collisions. -
classFileLocator
The class file locator to use. -
adviceByType
A map of advice types mapped by their argument type. All advice types are precompiled using Java 6 to allow for releasing Byte Buddy with a Java 5 byte code level where compiled classes do not contain stack map frames. Byte Buddy filters stack map frames when applying advice in newer version but it cannot add stack map frames without explicit frame computation which is expensive which is why precompilation was used. To avoid loading Java classes in incompatible versions, all advice types are resolved using a type pool.
-
-
Constructor Details
-
CachedReturnPlugin
public CachedReturnPlugin()Creates a plugin for caching method return values.
-
-
Method Details
-
make
Returns a plugin that can be used for a transformation and which is subsequently closed.- Specified by:
make
in interfacePlugin.Factory
- Returns:
- The plugin to use for type transformations.
-
apply
public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator) Applies this plugin.- Specified by:
apply
in interfacePlugin
- Parameters:
builder
- The builder to use as a basis for the applied transformation.typeDescription
- The type being transformed.classFileLocator
- A class file locator that can locate other types in the scope of the project.- Returns:
- The supplied builder with additional transformations registered.
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-