Class DynamicType.Builder.AbstractBase.Adapter<U>

    • Field Detail

      • fieldRegistry

        protected final FieldRegistry fieldRegistry
        The current field registry.
      • methodRegistry

        protected final MethodRegistry methodRegistry
        The current method registry.
      • typeAttributeAppender

        protected final TypeAttributeAppender typeAttributeAppender
        The type attribute appender to apply onto the instrumented type.
      • asmVisitorWrapper

        protected final AsmVisitorWrapper asmVisitorWrapper
        The ASM visitor wrapper to apply onto the class writer.
      • classFileVersion

        protected final ClassFileVersion classFileVersion
        The class file version to define auxiliary types in.
      • auxiliaryTypeNamingStrategy

        protected final AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy
        The naming strategy for auxiliary types to apply.
      • annotationValueFilterFactory

        protected final AnnotationValueFilter.Factory annotationValueFilterFactory
        The annotation value filter factory to apply.
      • annotationRetention

        protected final AnnotationRetention annotationRetention
        The annotation retention to apply.
      • implementationContextFactory

        protected final Implementation.Context.Factory implementationContextFactory
        The implementation context factory to apply.
      • methodGraphCompiler

        protected final MethodGraph.Compiler methodGraphCompiler
        The method graph compiler to use.
      • typeValidation

        protected final TypeValidation typeValidation
        Determines if a type should be explicitly validated.
      • ignoredMethods

        protected final LatentMatcher<? super MethodDescription> ignoredMethods
        A matcher for identifying methods that should be excluded from instrumentation.
    • Constructor Detail

      • Adapter

        protected Adapter​(InstrumentedType.WithFlexibleName instrumentedType,
                          FieldRegistry fieldRegistry,
                          MethodRegistry methodRegistry,
                          TypeAttributeAppender typeAttributeAppender,
                          AsmVisitorWrapper asmVisitorWrapper,
                          ClassFileVersion classFileVersion,
                          AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
                          AnnotationValueFilter.Factory annotationValueFilterFactory,
                          AnnotationRetention annotationRetention,
                          Implementation.Context.Factory implementationContextFactory,
                          MethodGraph.Compiler methodGraphCompiler,
                          TypeValidation typeValidation,
                          LatentMatcher<? super MethodDescription> ignoredMethods)
        Creates a new default type writer for creating a new type that is not based on an existing class file.
        Parameters:
        instrumentedType - The instrumented type to be created.
        fieldRegistry - The current field registry.
        methodRegistry - The current method registry.
        typeAttributeAppender - The type attribute appender to apply onto the instrumented type.
        asmVisitorWrapper - The ASM visitor wrapper to apply onto the class writer.
        classFileVersion - The class file version to define auxiliary types in.
        auxiliaryTypeNamingStrategy - The naming strategy for auxiliary types to apply.
        annotationValueFilterFactory - The annotation value filter factory to apply.
        annotationRetention - The annotation retention to apply.
        implementationContextFactory - The implementation context factory to apply.
        methodGraphCompiler - The method graph compiler to use.
        typeValidation - Determines if a type should be explicitly validated.
        ignoredMethods - A matcher for identifying methods that should be excluded from instrumentation.
    • Method Detail

      • defineField

        public DynamicType.Builder.FieldDefinition.Optional.Valuable<U> defineField​(java.lang.String name,
                                                                                    TypeDefinition type,
                                                                                    int modifiers)
        Description copied from interface: DynamicType.Builder
        Defines the specified field as a field of the built dynamic type.
        Parameters:
        name - The name of the field.
        type - The type of the field. Can also be TargetType if the field type should be equal to the currently instrumented type.
        modifiers - The modifiers of the field.
        Returns:
        A new builder that is equal to this builder but with the given field defined for the instrumented type. Furthermore, it is possible to optionally define a value, annotations or custom attributes for the field.
      • field

        public DynamicType.Builder.FieldDefinition.Valuable<U> field​(LatentMatcher<? super FieldDescription> matcher)
        Description copied from interface: DynamicType.Builder

        Matches a field that is already declared by the instrumented type. This gives opportunity to change that field's default value, annotations or custom attributes. Using a latent matcher gives opportunity to resolve an ElementMatcher based on the instrumented type before applying the matcher.

        When a type is redefined or rebased, any annotations that the field declared previously is preserved as it is if Byte Buddy is configured to retain such annotations by AnnotationRetention.ENABLED. If any existing annotations should be altered, annotation retention must be disabled.

        If a field is already matched by a previously specified field matcher, the new field definition gets precedence over the previous definition, i.e. the previous field definition is no longer applied.

        Parameters:
        matcher - The matcher that determines what declared fields are affected by the subsequent specification.
        Returns:
        A builder that allows for changing a field's definition.
      • defineMethod

        public DynamicType.Builder.MethodDefinition.ParameterDefinition.Initial<U> defineMethod​(java.lang.String name,
                                                                                                TypeDefinition returnType,
                                                                                                int modifiers)
        Description copied from interface: DynamicType.Builder
        Defines the specified method to be declared by the instrumented type. Method parameters or parameter types, declared exceptions and type variables can be defined in subsequent steps.
        Parameters:
        name - The name of the method.
        returnType - The method's return type. Can also be TargetType if the return type should be equal to the currently instrumented type.
        modifiers - The method's modifiers.
        Returns:
        A builder that allows for further defining the method, either by adding more properties or by defining an implementation.
      • defineConstructor

        public DynamicType.Builder.MethodDefinition.ParameterDefinition.Initial<U> defineConstructor​(int modifiers)
        Description copied from interface: DynamicType.Builder
        Defines the specified constructor to be declared by the instrumented type. Method parameters or parameter types, declared exceptions and type variables can be defined in subsequent steps.
        Parameters:
        modifiers - The constructor's modifiers.
        Returns:
        A builder that allows for further defining the constructor, either by adding more properties or by defining an implementation.
      • invokable

        public DynamicType.Builder.MethodDefinition.ImplementationDefinition<U> invokable​(LatentMatcher<? super MethodDescription> matcher)
        Description copied from interface: DynamicType.Builder

        Matches a method or constructor that is already declared or inherited by the instrumented type. This gives opportunity to change or to override that method's or constructor's implementation, default value, annotations or custom attributes. It is also possible to make a method abstract. Using a latent matcher gives opportunity to resolve an ElementMatcher based on the instrumented type before applying the matcher.

        When a type is redefined or rebased, any annotations that the method or constructor declared previously is preserved as it is if Byte Buddy is configured to retain such annotations by AnnotationRetention.ENABLED. If any existing annotations should be altered, annotation retention must be disabled.

        If a method or constructor is already matched by a previously specified matcher, the new definition gets precedence over the previous definition, i.e. the previous definition is no longer applied.

        Note that the specified definition does never apply for methods that are explicitly ignored.

        Important: It is possible to instrument the dynamic type's initializer. Depending on the used TypeResolutionStrategy, the type initializer might be run before Byte Buddy could apply any LoadedTypeInitializers which are responsible for preparing the instrumented type prior to the initializer's execution. For preparing the type prior to executing the initializer, an TypeResolutionStrategy.Active resolver must be chosen.

        Parameters:
        matcher - The matcher that determines what declared methods or constructors are affected by the subsequent specification.
        Returns:
        A builder that allows for changing a method's or constructor's definition.
      • implement

        public DynamicType.Builder.MethodDefinition.ImplementationDefinition.Optional<U> implement​(java.util.Collection<? extends TypeDefinition> interfaceTypes)
        Description copied from interface: DynamicType.Builder

        Implements the supplied interfaces for the instrumented type. Optionally, it is possible to define the methods that are defined by the interfaces or the interfaces' super interfaces. This excludes methods that are explicitly ignored.

        Note: This methods implements the supplied types as is, i.e. any TypeDescription values are implemented as raw types if they declare type variables or an owner type.

        Parameters:
        interfaceTypes - The interface types to implement.
        Returns:
        A new builder that is equal to this builder but with the interfaces implemented by the instrumented type.
      • ignoreAlso

        public DynamicType.Builder<U> ignoreAlso​(LatentMatcher<? super MethodDescription> ignoredMethods)
        Description copied from interface: DynamicType.Builder

        Specifies to exclude any method that is matched by the supplied matcher from instrumentation. Previously supplied matchers remain valid after supplying a new matcher, i.e. any method that is matched by a previously supplied matcher is always ignored. Using a latent matcher gives opportunity to resolve an ElementMatcher based on the instrumented type before applying the matcher.

        When ignoring a type, previously registered matchers are applied before this matcher. If a previous matcher indicates that a type is to be ignored, this matcher is no longer executed.

        Parameters:
        ignoredMethods - The matcher for determining what methods to exclude from instrumentation.
        Returns:
        A new builder that is equal to this builder but that is excluding any method that is matched by the supplied matcher from instrumentation.
      • initializer

        public DynamicType.Builder<U> initializer​(ByteCodeAppender byteCodeAppender)
        Description copied from interface: DynamicType.Builder

        Executes the supplied byte code appender within the beginning of the instrumented type's type initializer. The supplied byte code appender must not return from the method. If several byte code appenders are supplied, they are executed within their application order.

        This method should only be used for preparing an instrumented type with a specific configuration. Normally, a byte code appender is applied via Byte Buddy's standard API by invoking DynamicType.Builder.invokable(ElementMatcher) using the ElementMatchers.isTypeInitializer() matcher.

        Parameters:
        byteCodeAppender - The byte code appender to execute within the instrumented type's type initializer.
        Returns:
        A new builder that is equal to this builder but with the supplied byte code appender being executed within the instrumented type's type initializer.
      • initializer

        public DynamicType.Builder<U> initializer​(LoadedTypeInitializer loadedTypeInitializer)
        Description copied from interface: DynamicType.Builder
        Executes the supplied loaded type initializer when loading the created instrumented type. If several loaded type initializers are supplied, each loaded type initializer is executed in its registration order.
        Parameters:
        loadedTypeInitializer - The loaded type initializer to execute upon loading the instrumented type.
        Returns:
        A new builder that is equal to this builder but with the supplied loaded type initializer executed upon loading the instrumented type.
      • name

        public DynamicType.Builder<U> name​(java.lang.String name)
        Description copied from interface: DynamicType.Builder
        Names the dynamic type by the supplied name. The name needs to be fully qualified and in the binary format (packages separated by dots: foo.Bar). A type's package determines what other types are visible to the instrumented type and what methods can be overridden or be represented in method signatures or as field types.
        Parameters:
        name - The fully qualified name of the generated class in a binary format.
        Returns:
        A new builder that is equal to this builder but with the instrumented type named by the supplied name.
      • modifiers

        public DynamicType.Builder<U> modifiers​(int modifiers)
        Description copied from interface: DynamicType.Builder
        Defines the supplied modifiers as the modifiers of the instrumented type.
        Parameters:
        modifiers - The modifiers of the instrumented type.
        Returns:
        A new builder that is equal to this builder but with the supplied modifiers applied onto the instrumented type.
      • merge

        public DynamicType.Builder<U> merge​(java.util.Collection<? extends ModifierContributor.ForType> modifierContributors)
        Description copied from interface: DynamicType.Builder
        Merges the supplied modifier contributors with the modifiers of the instrumented type and defines them as the instrumented type's new modifiers.
        Parameters:
        modifierContributors - The modifiers of the instrumented type.
        Returns:
        A new builder that is equal to this builder but with the supplied modifiers merged into the instrumented type's modifiers.
      • typeVariable

        public DynamicType.Builder.TypeVariableDefinition<U> typeVariable​(java.lang.String symbol,
                                                                          java.util.Collection<? extends TypeDefinition> bounds)
        Description copied from interface: DynamicType.Builder
        Defines the supplied type variable with the given bound as a type variable of the instrumented type.
        Parameters:
        symbol - The type variable's symbol.
        bounds - The type variable's upper bounds. Can also be TargetType if the bound type should be equal to the currently instrumented type.
        Returns:
        A new builder that is equal to this builder but with the given type variable defined for the instrumented type.
      • transform

        public DynamicType.Builder<U> transform​(ElementMatcher<? super TypeDescription.Generic> matcher,
                                                Transformer<TypeVariableToken> transformer)
        Description copied from interface: DynamicType.Builder
        Transforms any type variable that is defined by this type if it is matched by the supplied matcher.
        Parameters:
        matcher - The matcher to decide what type variables to transform.
        transformer - The transformer to apply to the matched type variables.
        Returns:
        A new builder that is equal to this builder but with the supplied transformer applied to all type variables.
      • attribute

        public DynamicType.Builder<U> attribute​(TypeAttributeAppender typeAttributeAppender)
        Description copied from interface: DynamicType.Builder
        Applies the given type attribute appender onto the instrumented type. Using a type attribute appender, it is possible to append any type of meta data to a type, not only Java Annotations.
        Parameters:
        typeAttributeAppender - The type attribute appender to apply.
        Returns:
        A new builder that is equal to this builder but with the supplied type attribute appender applied to the instrumented type.
      • annotateType

        public DynamicType.Builder<U> annotateType​(java.util.Collection<? extends AnnotationDescription> annotations)
        Description copied from interface: DynamicType.Builder
        Annotates the instrumented type with the supplied annotations.
        Parameters:
        annotations - The annotations to add to the instrumented type.
        Returns:
        A new builder that is equal to this builder but with the annotations added to the instrumented type.
      • visit

        public DynamicType.Builder<U> visit​(AsmVisitorWrapper asmVisitorWrapper)
        Description copied from interface: DynamicType.Builder
        Applies the supplied AsmVisitorWrapper onto the ClassVisitor during building a dynamic type. Using an ASM visitor, it is possible to manipulate byte code directly. Byte Buddy does not validate directly created byte code and it remains the responsibility of the visitor's implementor to generate legal byte code. If several ASM visitor wrappers are registered, they are applied on top of another in their registration order.
        Parameters:
        asmVisitorWrapper - The ASM visitor wrapper to apply during
        Returns:
        A new builder that is equal to this builder and applies the ASM visitor wrapper.
      • materialize

        protected abstract DynamicType.Builder<U> materialize​(InstrumentedType.WithFlexibleName instrumentedType,
                                                              FieldRegistry fieldRegistry,
                                                              MethodRegistry methodRegistry,
                                                              TypeAttributeAppender typeAttributeAppender,
                                                              AsmVisitorWrapper asmVisitorWrapper,
                                                              ClassFileVersion classFileVersion,
                                                              AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
                                                              AnnotationValueFilter.Factory annotationValueFilterFactory,
                                                              AnnotationRetention annotationRetention,
                                                              Implementation.Context.Factory implementationContextFactory,
                                                              MethodGraph.Compiler methodGraphCompiler,
                                                              TypeValidation typeValidation,
                                                              LatentMatcher<? super MethodDescription> ignoredMethods)
        Materializes the supplied state of a dynamic type builder.
        Parameters:
        instrumentedType - The instrumented type.
        fieldRegistry - The current field registry.
        methodRegistry - The current method registry.
        typeAttributeAppender - The type attribute appender to apply onto the instrumented type.
        asmVisitorWrapper - The ASM visitor wrapper to apply onto the class writer.
        classFileVersion - The class file version to define auxiliary types in.
        auxiliaryTypeNamingStrategy - The naming strategy for auxiliary types to apply.
        annotationValueFilterFactory - The annotation value filter factory to apply.
        annotationRetention - The annotation retention to apply.
        implementationContextFactory - The implementation context factory to apply.
        methodGraphCompiler - The method graph compiler to use.
        typeValidation - The type validation state.
        ignoredMethods - A matcher for identifying methods that should be excluded from instrumentation.
        Returns:
        A type builder that represents the supplied arguments.