/*
 * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 * 
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 * 
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package java.dyn;

/**
 * An <code>invokedynamic</code> call site, as reified to the bootstrap method.
 * Every instance of a call site corresponds to a distinct instance
 * of the <code>invokedynamic</code> instruction.
 * Call sites have state, one reference word, called the <code>target</code>,
 * and typed as a {@link MethodHandle}.  When this state is null (as it is
 * initially) the call site is in the unlinked state.  Otherwise, it is said
 * to be linked to its target.
 * <p>
 * When an unlinked call site is executed, a bootstrap routine is called
 * to finish the execution of the call site, and optionally to link
 * the call site.
 * <p>
 * @author John Rose, JSR 292 EG
 */
public abstract class CallSite {
    protected MethodHandle target;
    protected final Object caller;  // usually a class
    protected final String name;
    protected final MethodType type;

    protected CallSite(Object caller, String name, MethodType type) {
        this.caller = caller;
        this.name = name;
        this.type = type;
    }

    /**
     * Report the current linkage state of the call site.  (This is mutable.)
     * The value is null if and only if the call site is currently unlinked.
     * @return the current linkage state of the call site
     */
    public MethodHandle getTarget() {
        return target;
    }

    /**
     * Link or relink the call site, by setting its target method.
     * @param target the new target, or null if it is to be unlinked
     * @throws WrongMethodTypeException if the new target is not null
     *         and has a method type that differs from the call site type
     */
    public void setTarget(MethodHandle target) {
        checkTarget(target);
        this.target = target;
    }
    
    protected void checkTarget(MethodHandle target) {
        if (!canSetTarget(target))
            throw new WrongMethodTypeException(String.valueOf(target));
    }

    protected boolean canSetTarget(MethodHandle target) {
        return (target == null || target.type() == type());
    }

    /**
     * Report the class containing the call site.
     * This is immutable static context.
     * @return class containing the call site
     */
    public Class<?> callerClass() {
        return (Class) caller;
    }

    /**
     * Report the method name specified in the {@code invokedynamic} instruction.
     * This is immutable static context.
     * @return method name specified by the call site
     */
    public String name() {
        return name;
    }

    /*
     * Report the result and parameter types, derived from the invocation descriptor, and resolved
     * against the calling class's class loader.
     * This is immutable static context.
     * @return method type specified by the call site
     */
    public MethodType type() {
        return type;
    }

    @Override
    public String toString() {
        return "CallSite#"+hashCode()+"["+name+type+" => "+target+"]";
    }
}
