Class AgentBuilder.Transformer.ForAdvice

  • All Implemented Interfaces:
    AgentBuilder.Transformer
    Enclosing interface:
    AgentBuilder.Transformer

    @Enhance
    public static class AgentBuilder.Transformer.ForAdvice
    extends java.lang.Object
    implements AgentBuilder.Transformer
    A transformer for applying an Advice where this advice class might reference types of both the agent's and the user's class loader. Using this transformer, it is possible to apply advice without including any library dependencies of this advice class which are then rather looked up from the transformed class's class loader. For this to work, it is required to register the advice class's class loader manually via the include methods and to reference the advice class by its fully-qualified name. The advice class is then never loaded by rather described by a TypePool.
    • Field Detail

      • exceptionHandler

        private final Advice.ExceptionHandler exceptionHandler
        The exception handler to register for the advice.
      • assigner

        private final Assigner assigner
        The assigner to use for the advice.
      • classFileLocator

        private final ClassFileLocator classFileLocator
        The class file locator to query for the advice class.
      • locationStrategy

        private final AgentBuilder.LocationStrategy locationStrategy
        The location strategy to use for class loaders when resolving advice classes.
      • auxiliaries

        private final java.util.List<java.lang.String> auxiliaries
        The names of auxiliary types to inject.
    • Constructor Detail

      • ForAdvice

        public ForAdvice()
        Creates a new advice transformer with a default setup.
      • ForAdvice

        public ForAdvice​(Advice.WithCustomMapping advice)
        Creates a new advice transformer which applies the given advice.
        Parameters:
        advice - The configured advice to use.
      • ForAdvice

        protected ForAdvice​(Advice.WithCustomMapping advice,
                            Advice.ExceptionHandler exceptionHandler,
                            Assigner assigner,
                            ClassFileLocator classFileLocator,
                            AgentBuilder.PoolStrategy poolStrategy,
                            AgentBuilder.LocationStrategy locationStrategy,
                            java.util.List<AgentBuilder.Transformer.ForAdvice.Entry> entries,
                            java.util.List<java.lang.String> auxiliaries)
        Creates a new advice transformer.
        Parameters:
        advice - The configured advice to use.
        exceptionHandler - The exception handler to use.
        assigner - The assigner to use.
        classFileLocator - The class file locator to use.
        poolStrategy - The pool strategy to use for looking up an advice.
        locationStrategy - The location strategy to use for class loaders when resolving advice classes.
        entries - The advice entries to apply.
        auxiliaries - The names of auxiliary types to inject.
    • Method Detail

      • transform

        public DynamicType.Builder<?> transform​(DynamicType.Builder<?> builder,
                                                TypeDescription typeDescription,
                                                @MaybeNull
                                                java.lang.ClassLoader classLoader,
                                                @MaybeNull
                                                JavaModule module,
                                                @MaybeNull
                                                java.security.ProtectionDomain protectionDomain)
        Allows for a transformation of a DynamicType.Builder.
        Specified by:
        transform in interface AgentBuilder.Transformer
        Parameters:
        builder - The dynamic builder to transform.
        typeDescription - The description of the type currently being instrumented.
        classLoader - The class loader of the instrumented class. Might be null to represent the bootstrap class loader.
        module - The class's module or null if the current VM does not support modules.
        protectionDomain - The protection domain of the transformed type or null if not available
        Returns:
        A transformed version of the supplied builder.
      • wrap

        protected AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper wrap​(TypeDescription typeDescription,
                                                                                 @MaybeNull
                                                                                 java.lang.ClassLoader classLoader,
                                                                                 @MaybeNull
                                                                                 JavaModule module,
                                                                                 @MaybeNull
                                                                                 java.security.ProtectionDomain protectionDomain,
                                                                                 Advice advice)
        Allows for decoration of advice for subclass implementations of this transformer. Note that a subclass is not retained when using the builder methods of this class. Subclasses should also override the make method of this class to allow propagating the custom configuration.
        Parameters:
        typeDescription - The description of the type currently being instrumented.
        classLoader - The class loader of the instrumented class. Might be null to represent the bootstrap class loader.
        module - The class's module or null if the current VM does not support modules.
        protectionDomain - The protection domain of the transformed type or null if not available
        advice - The advice to wrap.
        Returns:
        A visitor wrapper that represents the supplied advice.
      • with

        public AgentBuilder.Transformer.ForAdvice with​(AgentBuilder.PoolStrategy poolStrategy)
        Registers a pool strategy for creating a TypePool that should be used for creating the advice class.
        Parameters:
        poolStrategy - The pool strategy to use.
        Returns:
        A new instance of this advice transformer that applies the supplied pool strategy.
      • with

        public AgentBuilder.Transformer.ForAdvice with​(AgentBuilder.LocationStrategy locationStrategy)
        Registers a location strategy for creating a ClassFileLocator from the class loader that is supplied during transformation that should be used for looking up advice-relevant classes.
        Parameters:
        locationStrategy - The location strategy to use.
        Returns:
        A new instance of this advice transformer that applies the supplied location strategy.
      • include

        public AgentBuilder.Transformer.ForAdvice include​(java.lang.ClassLoader... classLoader)
        Includes the supplied class loaders as a source for looking up an advice class or its dependencies. Note that the supplied class loaders are queried for types before the class loader of the instrumented class.
        Parameters:
        classLoader - The class loaders to include when looking up classes in their order. Duplicates are filtered.
        Returns:
        A new instance of this advice transformer that considers the supplied class loaders as a lookup source.
      • include

        public AgentBuilder.Transformer.ForAdvice include​(ClassFileLocator... classFileLocator)
        Includes the supplied class file locators as a source for looking up an advice class or its dependencies. Note that the supplied class loaders are queried for types before the class loader of the instrumented class.
        Parameters:
        classFileLocator - The class file locators to include when looking up classes in their order. Duplicates are filtered.
        Returns:
        A new instance of this advice transformer that considers the supplied class file locators as a lookup source.
      • include

        public AgentBuilder.Transformer.ForAdvice include​(java.util.List<? extends ClassFileLocator> classFileLocators)
        Includes the supplied class file locators as a source for looking up an advice class or its dependencies. Note that the supplied class loaders are queried for types before the class loader of the instrumented class.
        Parameters:
        classFileLocators - The class file locators to include when looking up classes in their order. Duplicates are filtered.
        Returns:
        A new instance of this advice transformer that considers the supplied class file locators as a lookup source.
      • advice

        public AgentBuilder.Transformer.ForAdvice advice​(ElementMatcher<? super MethodDescription> matcher,
                                                         java.lang.String name)
        Applies the given advice class onto all methods that satisfy the supplied matcher.
        Parameters:
        matcher - The matcher to determine what methods the advice should be applied to.
        name - The fully-qualified, binary name of the advice class.
        Returns:
        A new instance of this advice transformer that applies the given advice to all matched methods of an instrumented type.
      • advice

        public AgentBuilder.Transformer.ForAdvice advice​(LatentMatcher<? super MethodDescription> matcher,
                                                         java.lang.String name)
        Applies the given advice class onto all methods that satisfy the supplied matcher.
        Parameters:
        matcher - The matcher to determine what methods the advice should be applied to.
        name - The fully-qualified, binary name of the advice class.
        Returns:
        A new instance of this advice transformer that applies the given advice to all matched methods of an instrumented type.
      • advice

        public AgentBuilder.Transformer.ForAdvice advice​(ElementMatcher<? super MethodDescription> matcher,
                                                         java.lang.String enter,
                                                         java.lang.String exit)
        Applies the given advice class onto all methods that satisfy the supplied matcher.
        Parameters:
        matcher - The matcher to determine what methods the advice should be applied to.
        enter - The fully-qualified, binary name of the enter advice class.
        exit - The fully-qualified, binary name of the exit advice class.
        Returns:
        A new instance of this advice transformer that applies the given advice to all matched methods of an instrumented type.
      • advice

        public AgentBuilder.Transformer.ForAdvice advice​(LatentMatcher<? super MethodDescription> matcher,
                                                         java.lang.String enter,
                                                         java.lang.String exit)
        Applies the given advice class onto all methods that satisfy the supplied matcher.
        Parameters:
        matcher - The matcher to determine what methods the advice should be applied to.
        enter - The fully-qualified, binary name of the enter advice class.
        exit - The fully-qualified, binary name of the exit advice class.
        Returns:
        A new instance of this advice transformer that applies the given advice to all matched methods of an instrumented type.
      • auxiliary

        public AgentBuilder.Transformer.ForAdvice auxiliary​(java.lang.String... auxiliary)
        Adds the given auxiliary types for injection.
        Parameters:
        auxiliary - The names of the auxiliary types to inject.
        Returns:
        A new instance of this advice transformer that resolves and adds the specified auxiliary types.
      • auxiliary

        public AgentBuilder.Transformer.ForAdvice auxiliary​(java.util.List<java.lang.String> auxiliaries)
        Adds the given auxiliary types for injection.
        Parameters:
        auxiliaries - The names of the auxiliary types to inject.
        Returns:
        A new instance of this advice transformer that resolves and adds the specified auxiliary types.