1 /*
   2  * Copyright (c) 2018, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.PrintStream;
  25 import java.lang.invoke.CallSite;
  26 import java.lang.invoke.IntrinsicFactory;
  27 import java.lang.invoke.MethodHandles;
  28 import java.lang.invoke.MethodType;
  29 import java.util.ArrayList;
  30 import java.util.Collections;
  31 import java.util.Formatter;
  32 import java.util.List;
  33 import java.util.Locale;
  34 
  35 public class JavacIntrinsicsSupport extends Basic {
  36 
  37 
  38     static void formatAndCheck(String fs, String exp, Locale l, Object... args) {
  39         // Invoke via formatterFormatBootstrap intrinsic
  40         Formatter f = new Formatter(new StringBuilder(), l);
  41         formatterFormat(f, fs, args);
  42         ck(fs, exp, f.toString());
  43         // Invoke via formatterLocaleFormatBootstrap intrinsic
  44         f = new Formatter(new StringBuilder());
  45         formatterLocaleFormat(f, l, fs, args);
  46         ck(fs, exp, f.toString());
  47         // Invoke again via stringLocaleFormatBootstrap instrinsic
  48         ck(fs, exp, stringLocaleFormat(fs, l, args));
  49     }
  50 
  51     static void formatterFormat(Formatter f, String fs, Object... args) {
  52         CallSite cs;
  53         List<Object> invokeArgs;
  54         try {
  55             cs = IntrinsicFactory.formatterFormatBootstrap(MethodHandles.lookup(), "format",
  56                     getFormatterFormatMethodType(false, args), fs);
  57 
  58             invokeArgs = new ArrayList<>();
  59             invokeArgs.add(f);
  60             if (args != null) {
  61                 Collections.addAll(invokeArgs, args);
  62             } else {
  63                 invokeArgs.add(null);
  64             }
  65         } catch (Throwable t) {
  66             throw new BootstrapMethodError(t);
  67         }
  68         try {
  69             cs.dynamicInvoker().invokeWithArguments(invokeArgs);
  70         } catch (RuntimeException r) {
  71             throw r;
  72         } catch (Throwable t) {
  73             throw new RuntimeException(t);
  74         }
  75     }
  76 
  77     static void formatterLocaleFormat(Formatter f, Locale l, String fs, Object... args) {
  78         CallSite cs;
  79         List<Object> invokeArgs;
  80         try {
  81             cs = IntrinsicFactory.formatterLocaleFormatBootstrap(MethodHandles.lookup(), "format",
  82                     getFormatterFormatMethodType(true, args), fs);
  83             invokeArgs = new ArrayList<>();
  84             invokeArgs.add(f);
  85             invokeArgs.add(l);
  86             if (args != null) {
  87                 Collections.addAll(invokeArgs, args);
  88             } else {
  89                 invokeArgs.add(null);
  90             }
  91         } catch (Throwable t) {
  92             throw new BootstrapMethodError(t);
  93         }
  94         try {
  95             cs.dynamicInvoker().invokeWithArguments(invokeArgs);
  96         } catch (RuntimeException r) {
  97             throw r;
  98         } catch (Throwable t) {
  99             throw new RuntimeException(t);
 100         }
 101     }
 102 
 103     static String stringFormat(String fs, Object... args) {
 104         CallSite cs;
 105         List<Object> invokeArgs;
 106         try {
 107             cs = IntrinsicFactory.staticStringFormatBootstrap(MethodHandles.lookup(), "format",
 108                     getStringFormatMethodType(false, args), fs);
 109             invokeArgs = new ArrayList<>();
 110             if (args != null) {
 111                 Collections.addAll(invokeArgs, args);
 112             } else {
 113                 invokeArgs.add(null);
 114             }
 115         } catch (Throwable t) {
 116             throw new BootstrapMethodError(t);
 117         }
 118         try {
 119             return (String) cs.dynamicInvoker().invokeWithArguments(invokeArgs);
 120         } catch (RuntimeException r) {
 121             throw r;
 122         } catch (Throwable t) {
 123             throw new RuntimeException(t);
 124         }
 125     }
 126 
 127     static String stringLocaleFormat(String fs, Locale l, Object... args) {
 128         CallSite cs;
 129         List<Object> invokeArgs;
 130         try {
 131             cs = IntrinsicFactory.staticStringLocaleFormatBootstrap(MethodHandles.lookup(), "format",
 132                     getStringFormatMethodType(true, args), fs);
 133             invokeArgs = new ArrayList<>();
 134             invokeArgs.add(l);
 135             if (args != null) {
 136                 Collections.addAll(invokeArgs, args);
 137             } else {
 138                 invokeArgs.add(null);
 139             }
 140         } catch (Throwable t) {
 141             throw new BootstrapMethodError(t);
 142         }
 143         try {
 144             return (String) cs.dynamicInvoker().invokeWithArguments(invokeArgs);
 145         } catch (RuntimeException r) {
 146             throw r;
 147         } catch (Throwable t) {
 148             throw new RuntimeException(t);
 149         }
 150     }
 151 
 152     private static MethodType getFormatterFormatMethodType(boolean hasLocale, Object... args) {
 153         MethodType mt = MethodType.methodType(Formatter.class, Formatter.class);
 154         if (hasLocale) {
 155             mt = mt.appendParameterTypes(Locale.class);
 156         }
 157         if (args != null) {
 158             for (Object arg : args) {
 159                 mt = mt.appendParameterTypes(arg == null ? Object.class : arg.getClass());
 160             }
 161         } else {
 162             mt = mt.appendParameterTypes(Object[].class);
 163         }
 164         return mt;
 165     }
 166 
 167     private static MethodType getStringFormatMethodType(boolean hasLocale, Object... args) {
 168         MethodType mt = MethodType.methodType(String.class);
 169         if (hasLocale) {
 170             mt = mt.appendParameterTypes(Locale.class);
 171         }
 172         if (args != null) {
 173             for (Object arg : args) {
 174                 mt = mt.appendParameterTypes(arg == null ? Object.class : arg.getClass());
 175             }
 176         } else {
 177             mt = mt.appendParameterTypes(Object[].class);
 178         }
 179         return mt;
 180     }
 181 
 182     private static MethodType getPrintStreamFormatMethodType(Object... args) {
 183         MethodType mt = MethodType.methodType(PrintStream.class, PrintStream.class, Locale.class);
 184         if (args != null) {
 185             for (Object arg : args) {
 186                 mt = mt.appendParameterTypes(arg == null ? Object.class : arg.getClass());
 187             }
 188         } else {
 189             mt = mt.appendParameterTypes(Object[].class);
 190         }
 191         return mt;
 192     }
 193 }