java.lang.invoke

Class CallSite

  • Direct Known Subclasses:
    ConstantCallSite, MutableCallSite, VolatileCallSite

    public abstract class CallSite
    extends Object
    A CallSite is a holder for a variable MethodHandle, which is called its target. An invokedynamic instruction linked to a CallSite delegates all calls to the site's current target. A CallSite may be associated with several invokedynamic instructions, or it may be "free floating", associated with none. In any case, it may be invoked through an associated method handle called its dynamic invoker.

    CallSite is an abstract class which does not allow direct subclassing by users. It has three immediate, concrete subclasses that may be either instantiated or subclassed.

    • If a mutable target is not required, an invokedynamic instruction may be permanently bound by means of a constant call site.
    • If a mutable target is required which has volatile variable semantics, because updates to the target must be immediately and reliably witnessed by other threads, a volatile call site may be used.
    • Otherwise, if a mutable target is required, a mutable call site may be used.

    A non-constant call site may be relinked by changing its target. The new target must have the same type as the previous target. Thus, though a call site can be relinked to a series of successive targets, it cannot change its type.

    Here is a sample use of call sites and bootstrap methods which links every dynamic call site to print its arguments:

    static void test() throws Throwable {
        // THE FOLLOWING LINE IS PSEUDOCODE FOR A JVM INSTRUCTION
        InvokeDynamic[#bootstrapDynamic].baz("baz arg", 2, 3.14);
    }
    private static void printArgs(Object... args) {
      System.out.println(java.util.Arrays.deepToString(args));
    }
    private static final MethodHandle printArgs;
    static {
      MethodHandles.Lookup lookup = MethodHandles.lookup();
      Class thisClass = lookup.lookupClass();  // (who am I?)
      printArgs = lookup.findStatic(thisClass,
          "printArgs", MethodType.methodType(void.class, Object[].class));
    }
    private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String name, MethodType type) {
      // ignore caller and name, but match the type:
      return new ConstantCallSite(printArgs.asType(type));
    }
    
    • Method Detail

      • type

        public MethodType type()
        Returns the type of this call site's target. Although targets may change, any call site's type is permanent, and can never change to an unequal type. The setTarget method enforces this invariant by refusing any new target that does not have the previous target's type.
        Returns:
        the type of the current target, which is also the type of any future target
      • dynamicInvoker

        public abstract MethodHandle dynamicInvoker()
        Produces a method handle equivalent to an invokedynamic instruction which has been linked to this call site.

        This method is equivalent to the following code:

         MethodHandle getTarget, invoker, result;
         getTarget = MethodHandles.publicLookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class));
         invoker = MethodHandles.exactInvoker(this.type());
         result = MethodHandles.foldArguments(invoker, getTarget)
         
        Returns:
        a method handle which always invokes this call site's current target

Traduction non disponible

Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Version en cache

18/11/2024 06:25:35 Cette version de la page est en cache (à la date du 18/11/2024 06:25:35) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.

Document créé le 11/06/2005, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-java/lang/invoke/callsite.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :fr Manuel PHP : https://docs.oracle.com

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut