Class IndyBootstrap

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String LOOKUP_EXPOSER_CLASS_NAME
      The name of the class we use as the lookup class during the invokedynamic bootstrap flow.
    • Constructor Summary

      Constructors 
      Constructor Description
      IndyBootstrap()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.invoke.ConstantCallSite bootstrap​(java.lang.invoke.MethodHandles.Lookup lookup, java.lang.String adviceMethodName, java.lang.invoke.MethodType adviceMethodType, java.lang.Object... args)
      Is called by java.lang.IndyBootstrapDispatcher#bootstrap via reflection.
      static java.lang.reflect.Method getIndyBootstrapMethod​(org.slf4j.Logger logger)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOOKUP_EXPOSER_CLASS_NAME

        public static final java.lang.String LOOKUP_EXPOSER_CLASS_NAME
        The name of the class we use as the lookup class during the invokedynamic bootstrap flow. The bytecode of this class is injected into the plugin class loader, then loaded from that class loader and used as the lookup class to link the instrumented call site to the advice method.
        See Also:
        Constant Field Values
    • Constructor Detail

      • IndyBootstrap

        public IndyBootstrap()
    • Method Detail

      • getIndyBootstrapMethod

        public static java.lang.reflect.Method getIndyBootstrapMethod​(org.slf4j.Logger logger)
      • bootstrap

        @Nullable
        public static java.lang.invoke.ConstantCallSite bootstrap​(java.lang.invoke.MethodHandles.Lookup lookup,
                                                                  java.lang.String adviceMethodName,
                                                                  java.lang.invoke.MethodType adviceMethodType,
                                                                  java.lang.Object... args)
        Is called by java.lang.IndyBootstrapDispatcher#bootstrap via reflection.

        This is to make it impossible for OSGi or other filtering class loaders to restrict access to classes in the bootstrap class loader. Normally, additional classes that have been injected have to be explicitly allowed via the org.osgi.framework.bootdelegation system property. But because we inject our class directly in the java.lang package which has to be on the allow list of filtering class loaders we can be sure that any other class can always call that class.

        This method gets called the first time an instrumented method is called. On instrumentation an invokedynamic instruction is inserted that delays the linking to the target method until runtime. The linking to the target method is done in this method.

        If not already created, the lookup of the target method creates a dedicated class loader for the classes in the plugin that has instrumented the instrumentedType. Via package scanning it finds all the classes that are in the package of the advice that was responsible for the instrumentation. It then links to the advice loaded from the plugin class loader. The advice can access both agent types and the types of the instrumented library.

        Exceptions and null return values are handled by java.lang.IndyBootstrapDispatcher#bootstrap.

        This is how a bootstrap method looks like in the class file:
         BootstrapMethods:
           1: #1060 REF_invokeStatic java/lang/IndyBootstrapDispatcher.bootstrap:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
             Method arguments:
               #1049 co.elastic.apm.agent.bci.InstrumentationTest$CommonsLangInstrumentation
               #1050 0
               #12 org/apache/commons/lang3/StringUtils
               #1072 isNotEmpty
               #1075 REF_invokeStatic org/apache/commons/lang3/StringUtils.isNotEmpty:(Ljava/lang/CharSequence;)Z
         
        And this is how a invokedynamic instruction looks like inside methods, referencing above bootstrap method
             invokedynamic #1076,  0           // InvokeDynamic #1:onEnter:()V
         
        Parameters:
        lookup - A java.lang.invoke.MethodHandles.Lookup representing the instrumented method.
        adviceMethodName - A String representing the advice method name.
        adviceMethodType - A MethodType representing the arguments and return type of the advice method.
        args - Additional arguments that are provided by Byte Buddy:
        • A String of the binary target class name.
        • A int with value 0 for an enter advice and {code 1} for an exist advice.
        • A Class representing the class implementing the instrumented method.
        • A String with the name of the instrumented method.
        • A MethodHandle representing the instrumented method unless the target is the type's static initializer.
        Returns:
        a ConstantCallSite that is the target of the invokedynamic