Linkage.java 5.1 KB
Newer Older
1
/*
O
ohair 已提交
2
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
3 4 5 6
 * 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
7
 * published by the Free Software Foundation.  Oracle designates this
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
10 11 12 13 14 15 16 17 18 19 20
 *
 * 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.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
24 25 26 27
 */

package java.dyn;

28
import java.dyn.MethodHandles.Lookup;
29
import java.util.WeakHashMap;
30
import sun.dyn.Access;
31
import sun.dyn.MethodHandleImpl;
32
import sun.dyn.util.VerifyAccess;
33
import sun.reflect.Reflection;
34
import static sun.dyn.MemberName.newIllegalArgumentException;
35 36

/**
37 38 39
 * <em>CLASS WILL BE REMOVED FOR PFD:</em>
 * Static routines for controlling invokedynamic behavior.
 * Replaced by non-static APIs.
40
 * @author John Rose, JSR 292 EG
41
 * @deprecated This class will be removed in the Public Final Draft.
42 43
 */
public class Linkage {
44 45
    private static final Access IMPL_TOKEN = Access.getToken();

46 47 48
    private Linkage() {}  // do not instantiate

    /**
49
     * <em>METHOD WILL BE REMOVED FOR PFD:</em>
50 51
     * Register a <em>bootstrap method</em> to use when linking dynamic call sites within
     * a given caller class.
52
     * @deprecated Use @{@link BootstrapMethod} annotations instead.
53 54
     */
    public static
55
    void registerBootstrapMethod(Class callerClass, MethodHandle bootstrapMethod) {
56
        Class callc = Reflection.getCallerClass(2);
57 58
        if (callc != null && !VerifyAccess.isSamePackage(callerClass, callc))
            throw new IllegalArgumentException("cannot set bootstrap method on "+callerClass);
59
        MethodHandleImpl.registerBootstrap(IMPL_TOKEN, callerClass, bootstrapMethod);
60 61 62
    }

    /**
63
     * <em>METHOD WILL BE REMOVED FOR PFD:</em>
64
     * Simplified version of {@code registerBootstrapMethod} for self-registration,
65
     * to be called from a static initializer.
66
     * @deprecated Use @{@link BootstrapMethod} annotations instead.
67 68 69
     */
    public static
    void registerBootstrapMethod(Class<?> runtime, String name) {
70 71
        Class callerClass = Reflection.getCallerClass(2);
        registerBootstrapMethodLookup(callerClass, runtime, name);
72 73 74
    }

    /**
75
     * <em>METHOD WILL BE REMOVED FOR PFD:</em>
76
     * Simplified version of {@code registerBootstrapMethod} for self-registration,
77
     * @deprecated Use @{@link BootstrapMethod} annotations instead.
78 79 80
     */
    public static
    void registerBootstrapMethod(String name) {
81 82 83 84 85 86 87 88 89
        Class callerClass = Reflection.getCallerClass(2);
        registerBootstrapMethodLookup(callerClass, callerClass, name);
    }

    private static
    void registerBootstrapMethodLookup(Class<?> callerClass, Class<?> runtime, String name) {
        Lookup lookup = new Lookup(IMPL_TOKEN, callerClass);
        MethodHandle bootstrapMethod;
        try {
90
            bootstrapMethod = lookup.findStatic(runtime, name, BOOTSTRAP_METHOD_TYPE);
91
        } catch (NoAccessException ex) {
92
            throw new IllegalArgumentException("no such bootstrap method in "+runtime+": "+name, ex);
93 94
        }
        MethodHandleImpl.registerBootstrap(IMPL_TOKEN, callerClass, bootstrapMethod);
95 96
    }

97
    private static final MethodType BOOTSTRAP_METHOD_TYPE
98 99
            = MethodType.methodType(CallSite.class,
                                    Class.class, String.class, MethodType.class);
100 101

    /**
102
     * <em>METHOD WILL BE REMOVED FOR PFD:</em>
103
     * Invalidate all <code>invokedynamic</code> call sites everywhere.
104 105 106
     * @deprecated Use {@linkplain MutableCallSite#setTarget call site target setting},
     * {@link MutableCallSite#syncAll call site update pushing},
     * and {@link SwitchPoint#guardWithTest target switching} instead.
107 108 109
     */
    public static
    Object invalidateAll() {
110
        throw new UnsupportedOperationException();
111 112 113
    }

    /**
114
     * <em>METHOD WILL BE REMOVED FOR PFD:</em>
115
     * Invalidate all {@code invokedynamic} call sites in the bytecodes
116
     * of any methods of the given class.
117 118 119
     * @deprecated Use {@linkplain MutableCallSite#setTarget call site target setting},
     * {@link MutableCallSite#syncAll call site update pushing},
     * and {@link SwitchPoint#guardWithTest target switching} instead.
120 121 122
     */
    public static
    Object invalidateCallerClass(Class<?> callerClass) {
123
        throw new UnsupportedOperationException();
124 125
    }
}