1 /*
   2  * Copyright (c) 2008, 2009, Oracle and/or its affiliates. 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.  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  * 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.dyn;
  27 
  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  * <em>CLASS WILL BE REMOVED FOR PFD:</em>
  38  * Static routines for controlling invokedynamic behavior.
  39  * 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     private static final Access IMPL_TOKEN = Access.getToken();
  45 
  46     private Linkage() {}  // do not instantiate
  47 
  48     /**
  49      * <em>METHOD WILL BE REMOVED FOR PFD:</em>
  50      * Register a <em>bootstrap method</em> to use when linking dynamic call sites within
  51      * 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         if (callc != null && !VerifyAccess.isSamePackage(callerClass, callc))
  58             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     public static
  69     void registerBootstrapMethod(Class<?> runtime, String name) {
  70         Class callerClass = Reflection.getCallerClass(2);
  71         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     public static
  80     void registerBootstrapMethod(String name) {
  81         Class callerClass = Reflection.getCallerClass(2);
  82         registerBootstrapMethodLookup(callerClass, callerClass, name);
  83     }
  84 
  85     private static
  86     void registerBootstrapMethodLookup(Class<?> callerClass, Class<?> runtime, String name) {
  87         Lookup lookup = new Lookup(IMPL_TOKEN, callerClass);
  88         MethodHandle bootstrapMethod;
  89         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             = MethodType.methodType(CallSite.class,
  99                                     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      * @deprecated Use {@linkplain CallSite#setTarget call site target setting}
 105      * and {@link VolatileCallSite#invalidateAll call site invalidation} instead.
 106      */
 107     public static
 108     Object invalidateAll() {
 109         throw new UnsupportedOperationException();
 110     }
 111 
 112     /**
 113      * <em>METHOD WILL BE REMOVED FOR PFD:</em>
 114      * Invalidate all {@code invokedynamic} call sites in the bytecodes
 115      * of any methods of the given class.
 116      * @deprecated Use {@linkplain CallSite#setTarget call site target setting}
 117      * and {@link VolatileCallSite#invalidateAll call site invalidation} instead.
 118      */
 119     public static
 120     Object invalidateCallerClass(Class<?> callerClass) {
 121         throw new UnsupportedOperationException();
 122     }
 123 }