Package net.bytebuddy.build
Class CachedReturnPlugin
- java.lang.Object
-
- net.bytebuddy.build.Plugin.ForElementMatcher
-
- net.bytebuddy.build.CachedReturnPlugin
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,Plugin,Plugin.Factory,ElementMatcher<TypeDescription>
@Enhance public class CachedReturnPlugin extends Plugin.ForElementMatcher implements Plugin.Factory
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 definesfinalfields. In this context, it is possible that the method is executed multiple times by different threads but at the same time, this approach avoids avolatilefield 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,nullis 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 Classes Modifier and Type Class Description protected static classCachedReturnPlugin.AdviceResolverA resolver forAdvicethat caches a method's return type.protected static interfaceCachedReturnPlugin.CacheFieldIndicates the field that stores the cached value.protected static classCachedReturnPlugin.CacheFieldOffsetMappingAn offset mapping for the cached field.static interfaceCachedReturnPlugin.EnhanceIndicates 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.WithInitialization, Plugin.WithPreprocessor
-
Nested classes/interfaces inherited from interface net.bytebuddy.build.Plugin.Factory
Plugin.Factory.Simple, Plugin.Factory.UsingReflection
-
-
Field Summary
Fields Modifier and Type Field Description private static MethodDescription.InDefinedShapeENHANCE_VALUEA description of theCachedReturnPlugin.Enhance.value()method.private booleanignoreExistingFieldstrueif existing fields should be ignored if the field name was explicitly given.private static java.lang.StringNAME_INFIXAn infix between a field and the random suffix if no field name is chosen.private RandomStringrandomStringA random string to use for avoid field name collisions.
-
Constructor Summary
Constructors Constructor Description CachedReturnPlugin()Creates a plugin for caching method return values.CachedReturnPlugin(boolean ignoreExistingFields)Creates a plugin for caching method return values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DynamicType.Builder<?>apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator)Applies this plugin.voidclose()Pluginmake()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 Detail
-
NAME_INFIX
private static final java.lang.String NAME_INFIX
An infix between a field and the random suffix if no field name is chosen.- See Also:
- Constant Field Values
-
ENHANCE_VALUE
private static final MethodDescription.InDefinedShape ENHANCE_VALUE
A description of theCachedReturnPlugin.Enhance.value()method.
-
ignoreExistingFields
private final boolean ignoreExistingFields
trueif existing fields should be ignored if the field name was explicitly given.
-
randomString
@ValueHandling(IGNORE) private final RandomString randomString
A random string to use for avoid field name collisions.
-
-
Constructor Detail
-
CachedReturnPlugin
public CachedReturnPlugin()
Creates a plugin for caching method return values. If a field name exists before applying this plugin, an exception is raised.
-
CachedReturnPlugin
public CachedReturnPlugin(boolean ignoreExistingFields)
Creates a plugin for caching method return values.- Parameters:
ignoreExistingFields-trueif existing fields should be ignored if the field name was explicitly given.
-
-
Method Detail
-
make
public Plugin make()
Returns a plugin that can be used for a transformation and which is subsequently closed.- Specified by:
makein 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:
applyin 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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
-