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.  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 com.sun.tools.javac.intrinsics;
  27 
  28 import java.lang.constant.ClassDesc;
  29 import java.lang.constant.ConstantDesc;
  30 import java.lang.constant.ConstantDescs;
  31 import java.lang.constant.DynamicCallSiteDesc;
  32 import java.lang.constant.MethodTypeDesc;
  33 import java.util.Formatter;
  34 import java.util.Locale;
  35 
  36 import static java.lang.constant.ConstantDescs.CD_CallSite;
  37 import static java.lang.constant.ConstantDescs.CD_String;
  38 
  39 /**
  40  *  <p><b>This is NOT part of any supported API.
  41  *  If you write code that depends on this, you do so at your own risk.
  42  *  This code and its internal interfaces are subject to change or
  43  *  deletion without notice.</b>
  44  */
  45 public class FormatterProcessor implements IntrinsicProcessor {
  46     @Override
  47     public void register(Intrinsics intrinsics) {
  48         this.intrinsics = intrinsics;
  49         intrinsics.register(this,
  50                 String.class, "format", String.class, String.class, Object[].class);
  51         intrinsics.register(this,
  52                 String.class, "format", String.class, Locale.class, String.class, Object[].class);
  53         intrinsics.register(this,
  54                 String.class, "format", String.class, Object[].class);
  55         intrinsics.register(this,
  56                 String.class, "format", String.class, Locale.class, Object[].class);
  57         intrinsics.register(this,
  58                 Formatter.class, "format", Formatter.class, String.class, Object[].class);
  59         intrinsics.register(this,
  60                 Formatter.class, "format", Formatter.class, Locale.class, String.class, Object[].class);
  61     }
  62 
  63     Intrinsics intrinsics;
  64 
  65     static String lowerFirst(String string) {
  66         return string.substring(0, 1).toLowerCase() + string.substring(1);
  67     }
  68 
  69     static String upperFirst(String string) {
  70         return string.substring(0, 1).toUpperCase() + string.substring(1);
  71     }
  72 
  73     private static final ClassDesc CD_Locale = ClassDesc.of("java.util.Locale");
  74     private static final ClassDesc CD_Formatter = ClassDesc.of("java.util.Formatter");
  75     private static final String BSM_NAME = "formatterBootstrap";
  76 
  77     @Override
  78     public Result tryIntrinsify(ClassDesc ownerDesc,
  79                                 String methodName,
  80                                 MethodTypeDesc methodType,
  81                                 boolean isStatic,
  82                                 ClassDesc[] argClassDescs,
  83                                 ConstantDesc[] constantArgs) {
  84         if (intrinsics.isArrayVarArg(argClassDescs, methodType.parameterCount())) {
  85             return new Result.None();
  86         }
  87 
  88         boolean isStringMethod = CD_String.equals(ownerDesc);
  89         boolean hasLocale = CD_Locale.equals(methodType.parameterType(0));
  90         int formatArg = hasLocale ? 2 : 1;
  91 
  92         if (isStringMethod) {
  93             formatArg = isStatic && hasLocale ? 1 : 0;
  94         }
  95 
  96         ConstantDesc constantFormat = constantArgs[formatArg];
  97 
  98         if (constantFormat == null) {
  99             return new Result.None();
 100         }
 101 
 102         if (ownerDesc == CD_String) {
 103             String strFormat = (String)constantFormat;
 104             strFormat = strFormat.replaceAll("%%", "");
 105             int numberOfConversionChars = strFormat.length() - strFormat.replaceAll("%", "").length();
 106             if (numberOfConversionChars == 0) {
 107                 // just LDC the format str
 108                 return new Result.Ldc(((String)constantFormat).replaceAll("%%", "%"));
 109             }
 110         }
 111 
 112         MethodTypeDesc methodTypeLessFormat = methodType.dropParameterTypes(formatArg, formatArg + 1);
 113 
 114         return new Result.Indy(
 115                 DynamicCallSiteDesc.of(
 116                         ConstantDescs.ofCallsiteBootstrap(
 117                                 CD_Formatter,
 118                                 BSM_NAME,
 119                                 CD_CallSite
 120                         ),
 121                         methodName,
 122                         methodTypeLessFormat,
 123                         new ConstantDesc[] {constantFormat, isStringMethod ? 1 : 0, hasLocale ? 1 : 0}),
 124                         intrinsics.dropArg(argClassDescs.length, formatArg)
 125         );
 126     }
 127  }