Top Description Inners Fields Constructors Methods
net.bytebuddy.build

public Class CachedReturnPlugin

extends ForElementMatcher
implements Factory
Class Inheritance
All Implemented Interfaces
net.bytebuddy.build.Plugin.Factory
Annotations
@Enhance
Imports
edu.umd.cs.findbugs.annotations.SuppressFBWarnings, net.bytebuddy.asm.Advice, net.bytebuddy.description.method.MethodDescription, net.bytebuddy.description.modifier.FieldPersistence, .Ownership, .SyntheticState, .Visibility, net.bytebuddy.description.type.TypeDescription, net.bytebuddy.dynamic.ClassFileLocator, .DynamicType, net.bytebuddy.implementation.bytecode.assign.Assigner, net.bytebuddy.pool.TypePool, net.bytebuddy.utility.RandomString, java.lang.annotation.*, java.util.HashMap, .Map

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 and Inner Type Summary

Modifier and TypeClass and Description
protected static @interface
CachedReturnPlugin.CacheField

Indicates the field that stores the cached value.

protected static class
CachedReturnPlugin.CacheFieldOffsetMapping

An offset mapping for the cached field.

public static @interface
CachedReturnPlugin.Enhance

Indicates methods that should be cached, i.e. where the return value is stored in a synthetic field.

Field Summary

Modifier and TypeField and Description
private static final String
ADVICE_INFIX

The infix symbol for advice classes.

private final Map<TypeDescription, TypeDescription>
adviceByType

A map of advice types mapped by their argument type.

private final ClassFileLocator
classFileLocator

The class file locator to use.

private static final MethodDescription.InDefinedShape
ENHANCE_VALUE

A description of the Enhance#value() method.

private final boolean
ignoreExistingFields

true if existing fields should be ignored if the field name was explicitly given.

private static final String
NAME_INFIX

An infix between a field and the random suffix if no field name is chosen.

private final RandomString
randomString

A random string to use for avoid field name collisions.

Constructor Summary

AccessConstructor and Description
public
CachedReturnPlugin()

Creates a plugin for caching method return values.

public
CachedReturnPlugin(boolean
true if existing fields should be ignored if the field name was explicitly given.
ignoreExistingFields
)

Creates a plugin for caching method return values.

Method Summary

Modifier and TypeMethod and Description
public DynamicType.Builder<?>
apply(DynamicType.Builder<?>
The builder to use as a basis for the applied transformation.
builder
,
TypeDescription
The type being transformed.
typeDescription
,
ClassFileLocator
A class file locator that can locate other types in the scope of the project.
classFileLocator
)

Implements net.bytebuddy.build.Plugin.apply.

Applies this plugin.
public void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.
public Plugin
make()

Implements net.bytebuddy.build.Plugin.Factory.make.

Returns a plugin that can be used for a transformation and which is subsequently closed.
Inherited from net.bytebuddy.build.Plugin.ForElementMatcher:
matches

Field Detail

ADVICE_INFIXback to summary
private static final String ADVICE_INFIX

The infix symbol for advice classes.

adviceByTypeback to summary
private final Map<TypeDescription, TypeDescription> 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.

Annotations
@ValueHandling:IGNORE
classFileLocatorback to summary
private final ClassFileLocator classFileLocator

The class file locator to use.

ENHANCE_VALUEback to summary
private static final MethodDescription.InDefinedShape ENHANCE_VALUE

A description of the Enhance#value() method.

ignoreExistingFieldsback to summary
private final boolean ignoreExistingFields

true if existing fields should be ignored if the field name was explicitly given.

NAME_INFIXback to summary
private static final String NAME_INFIX

An infix between a field and the random suffix if no field name is chosen.

randomStringback to summary
private final RandomString randomString

A random string to use for avoid field name collisions.

Annotations
@ValueHandling:IGNORE

Constructor Detail

CachedReturnPluginback to summary
public CachedReturnPlugin()

Creates a plugin for caching method return values. If a field name exists before applying this plugin, an exception is raised.

CachedReturnPluginback to summary
public CachedReturnPlugin(boolean ignoreExistingFields)

Creates a plugin for caching method return values.

Parameters
ignoreExistingFields:boolean

true if existing fields should be ignored if the field name was explicitly given.

Method Detail

applyback to summary
public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator)

Implements net.bytebuddy.build.Plugin.apply.

Doc from net.bytebuddy.build.Plugin.apply.

Applies this plugin.

Parameters
builder:DynamicType.Builder<?>

The builder to use as a basis for the applied transformation.

typeDescription:TypeDescription

The type being transformed.

classFileLocator:ClassFileLocator

A class file locator that can locate other types in the scope of the project.

Returns:DynamicType.Builder<?>

The supplied builder with additional transformations registered.

Annotations
@SuppressFBWarnings:NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE
justification:Annotation presence is required by matcher.
closeback to summary
public void close()

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

makeback to summary
public Plugin make()

Implements net.bytebuddy.build.Plugin.Factory.make.

Doc from net.bytebuddy.build.Plugin.Factory.make.

Returns a plugin that can be used for a transformation and which is subsequently closed.

Returns:Plugin

The plugin to use for type transformations.

net.bytebuddy.build back to summary

protected @Interface CachedReturnPlugin.CacheField

extends Annotation
Annotations
@Target:PARAMETER
@Retention:RUNTIME

Indicates the field that stores the cached value.

Method Summary

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString
net.bytebuddy.build back to summary

protected Class CachedReturnPlugin.CacheFieldOffsetMapping

extends Object
implements OffsetMapping
Class Inheritance
  • java.lang.Object
  • net.bytebuddy.build.CachedReturnPlugin.CacheFieldOffsetMapping
All Implemented Interfaces
net.bytebuddy.asm.Advice.OffsetMapping
Annotations
@Enhance

An offset mapping for the cached field.

Field Summary

Modifier and TypeField and Description
private final String
name

The field's name.

Constructor Summary

AccessConstructor and Description
protected
CacheFieldOffsetMapping(String
The field's name.
name
)

Creates an offset mapping for the cached field.

Method Summary

Modifier and TypeMethod and Description
public Advice.OffsetMapping.Target
resolve(TypeDescription
The instrumented type.
instrumentedType
,
MethodDescription
The instrumented method for which the mapping is to be resolved.
instrumentedMethod
,
Assigner
The assigner to use.
assigner
,
Advice.ArgumentHandler
The argument handler to use for resolving offsets of the local variable array of the instrumented method.
argumentHandler
,
Advice.OffsetMapping.Sort
The sort of the advice method being resolved.
sort
)

Implements net.bytebuddy.asm.Advice.OffsetMapping.resolve.

Resolves an offset mapping to a given target offset.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

nameback to summary
private final String name

The field's name.

Constructor Detail

CacheFieldOffsetMappingback to summary
protected CacheFieldOffsetMapping(String name)

Creates an offset mapping for the cached field.

Parameters
name:String

The field's name.

Method Detail

resolveback to summary
public Advice.OffsetMapping.Target resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Advice.ArgumentHandler argumentHandler, Advice.OffsetMapping.Sort sort)

Implements net.bytebuddy.asm.Advice.OffsetMapping.resolve.

Doc from net.bytebuddy.asm.Advice.OffsetMapping.resolve.

Resolves an offset mapping to a given target offset.

Parameters
instrumentedType:TypeDescription

The instrumented type.

instrumentedMethod:MethodDescription

The instrumented method for which the mapping is to be resolved.

assigner:Assigner

The assigner to use.

argumentHandler:Advice.ArgumentHandler

The argument handler to use for resolving offsets of the local variable array of the instrumented method.

sort:Advice.OffsetMapping.Sort

The sort of the advice method being resolved.

Returns:Advice.OffsetMapping.Target

A suitable target mapping.

net.bytebuddy.build back to summary

public @Interface CachedReturnPlugin.Enhance

extends Annotation
Annotations
@Documented
@Target:METHOD
@Retention:RUNTIME

Indicates methods that should be cached, i.e. where the return value is stored in a synthetic field. For this to be possible, the returned value should not be altered and the instance must be thread-safe if the value might be used from multiple threads.

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

The fields name or an empty string if the name should be generated randomly.
value
()

The fields name or an empty string if the name should be generated randomly.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

valueback to summary
public String value()

The fields name or an empty string if the name should be generated randomly.

Returns:String

The fields name or an empty string if the name should be generated randomly.