1 /*
   2  * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  * 
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun in the LICENSE file that accompanied this code.
  10  * 
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  * 
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  * 
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package java.dyn;
  27 
  28 /**
  29  * An <code>invokedynamic</code> call site, as reified to the bootstrap method.
  30  * Every instance of a call site corresponds to a distinct instance
  31  * of the <code>invokedynamic</code> instruction.
  32  * Call sites have state, one reference word, called the <code>target</code>,
  33  * and typed as a {@link MethodHandle}.  When this state is null (as it is
  34  * initially) the call site is in the unlinked state.  Otherwise, it is said
  35  * to be linked to its target.
  36  * <p>
  37  * When an unlinked call site is executed, a bootstrap routine is called
  38  * to finish the execution of the call site, and optionally to link
  39  * the call site.
  40  * <p>
  41  * @author John Rose, JSR 292 EG
  42  */
  43 public abstract class CallSite {
  44     protected MethodHandle target;
  45     protected final Object caller;  // usually a class
  46     protected final String name;
  47     protected final MethodType type;
  48 
  49     protected CallSite(Object caller, String name, MethodType type) {
  50         this.caller = caller;
  51         this.name = name;
  52         this.type = type;
  53     }
  54 
  55     /**
  56      * Report the current linkage state of the call site.  (This is mutable.)
  57      * The value is null if and only if the call site is currently unlinked.
  58      * @return the current linkage state of the call site
  59      */
  60     public MethodHandle getTarget() {
  61         return target;
  62     }
  63 
  64     /**
  65      * Link or relink the call site, by setting its target method.
  66      * @param target the new target, or null if it is to be unlinked
  67      * @throws WrongMethodTypeException if the new target is not null
  68      *         and has a method type that differs from the call site type
  69      */
  70     public void setTarget(MethodHandle target) {
  71         checkTarget(target);
  72         this.target = target;
  73     }
  74     
  75     protected void checkTarget(MethodHandle target) {
  76         if (!canSetTarget(target))
  77             throw new WrongMethodTypeException(String.valueOf(target));
  78     }
  79 
  80     protected boolean canSetTarget(MethodHandle target) {
  81         return (target == null || target.type() == type());
  82     }
  83 
  84     /**
  85      * Report the class containing the call site.
  86      * This is immutable static context.
  87      * @return class containing the call site
  88      */
  89     public Class<?> callerClass() {
  90         return (Class) caller;
  91     }
  92 
  93     /**
  94      * Report the method name specified in the {@code invokedynamic} instruction.
  95      * This is immutable static context.
  96      * @return method name specified by the call site
  97      */
  98     public String name() {
  99         return name;
 100     }
 101 
 102     /*
 103      * Report the result and parameter types, derived from the invocation descriptor, and resolved
 104      * against the calling class's class loader.
 105      * This is immutable static context.
 106      * @return method type specified by the call site
 107      */
 108     public MethodType type() {
 109         return type;
 110     }
 111 
 112     @Override
 113     public String toString() {
 114         return "CallSite#"+hashCode()+"["+name+type+" => "+target+"]";
 115     }
 116 }