Search code examples
javalambdajvmbytecodejvm-bytecode

What is a bootstrap method argument - Java Bytecode


Bootstrap method with 3 method arguments:

SourceFile: "Class.java"
BootstrapMethods:
  0: #883 REF_invokeStatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
    Method arguments:
      #884 ()Ljava/lang/Object;
      #885 REF_invokeVirtual java/lang/Module.getClassLoader:()Ljava/lang/ClassLoader;
      #886 ()Ljava/lang/ClassLoader;

Bootstrap methods with single argument:

BootstrapMethods:
  0: #284 REF_invokeStatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
    Method arguments:
      #285 \u0001\u0001
  1: #284 REF_invokeStatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
    Method arguments:
      #290 \u0001 is not a \u0001

I've started looking into Java bytecode and want to know:

  • What really is a bootstrap method argument?
  • The second argument of the first instance seems to have reference to lamda expression. What is that?

Note: This is not about how to determine that an object is a lambda. I just want to understand what is a bootstrap method argument, and its relevance to lambda expressions, just by inspecting the bytecode.


Solution

  • Bootstrap methods are used to resolve invokedynamic instruction.

    invokedynamic is a general purpose mechanism, not only for lambdas, so it is absolutely possible for a bootstrap method to have an arbitrary number of arguments, which meaning depends on a method being called (see the above links).

    invokedynamic is also used to compile lambda expressions. In this case the instruction typically refers to LambdaMetafactory.metafactory or LambdaMetafactory.altMetafactory as a bootstrap method. The arguments of these bootstrap methods are described in Javadoc.