Class InvokeDynamicExamples

java.lang.Object
net.jrodolfo.java_evolution.java07.invokedynamic.InvokeDynamicExamples

public class InvokeDynamicExamples extends Object
Demonstrates Java 7 invokedynamic linkage support.

Ordinary Java source does not directly spell an invokedynamic instruction. This example demonstrates the Java 7 linkage building blocks in java.lang.invoke building blocks. Later Java features such as lambdas can use this JVM linkage infrastructure, but they are not part of this Java 7 example.

  • Constructor Details

    • InvokeDynamicExamples

      public InvokeDynamicExamples()
  • Method Details

    • invokeConstantCallSite

      public String invokeConstantCallSite(String prefix, String name) throws Throwable
      Creates a bootstrap-like constant call site and invokes it.
      Parameters:
      prefix - greeting prefix
      name - name to greet
      Returns:
      greeting produced through the call site's dynamic invoker
      Throws:
      Throwable - when method-handle lookup or invocation fails
    • constantCallSiteType

      public MethodType constantCallSiteType() throws NoSuchMethodException, IllegalAccessException
      Returns the method type associated with the bootstrap-linked call site.
      Returns:
      method type exposed by the call site
      Throws:
      NoSuchMethodException - when the target method cannot be found
      IllegalAccessException - when the target method is not accessible
    • retargetMutableCallSite

      public InvokeDynamicExamples.RetargetResult retargetMutableCallSite(String value) throws Throwable
      Demonstrates a mutable call site whose target changes at runtime.
      Parameters:
      value - value to transform
      Returns:
      results before and after retargeting
      Throws:
      Throwable - when method-handle lookup or invocation fails
    • incompatibleTargetFailure

      public WrongMethodTypeException incompatibleTargetFailure() throws NoSuchMethodException, IllegalAccessException
      Attempts to retarget a mutable call site to an incompatible method type.
      Returns:
      the exception raised by the call-site type check
      Throws:
      NoSuchMethodException - when the target method cannot be found
      IllegalAccessException - when the target method is not accessible
    • join

      public static String join(String prefix, String name)
      Target used by the constant call-site example.
      Parameters:
      prefix - greeting prefix
      name - name to greet
      Returns:
      joined greeting
    • uppercase

      public static String uppercase(String value)
      Target used before mutable call-site retargeting.
      Parameters:
      value - value to transform
      Returns:
      uppercase value
    • lowercase

      public static String lowercase(String value)
      Target used after mutable call-site retargeting.
      Parameters:
      value - value to transform
      Returns:
      lowercase value
    • length

      public static int length(String value)
      Incompatible target used to demonstrate call-site type checks.
      Parameters:
      value - value to measure
      Returns:
      value length