1 /*
   2  * Copyright (c) 2012, 2015, 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 package jdk.vm.ci.meta;
  24 
  25 import java.lang.reflect.Field;
  26 import java.lang.reflect.Modifier;
  27 
  28 /**
  29  * Miscellaneous collection of utility methods used by {@code jdk.vm.ci.meta} and its clients.
  30  */
  31 public class MetaUtil {
  32 
  33     /**
  34      * Extends the functionality of {@link Class#getSimpleName()} to include a non-empty string for
  35      * anonymous and local classes.
  36      *
  37      * @param clazz the class for which the simple name is being requested
  38      * @param withEnclosingClass specifies if the returned name should be qualified with the name(s)
  39      *            of the enclosing class/classes of {@code clazz} (if any). This option is ignored
  40      *            if {@code clazz} denotes an anonymous or local class.
  41      * @return the simple name
  42      */
  43     public static String getSimpleName(Class<?> clazz, boolean withEnclosingClass) {
  44         final String simpleName = clazz.getSimpleName();
  45         if (simpleName.length() != 0) {
  46             if (withEnclosingClass) {
  47                 String prefix = "";
  48                 Class<?> enclosingClass = clazz;
  49                 while ((enclosingClass = enclosingClass.getEnclosingClass()) != null) {
  50                     prefix = enclosingClass.getSimpleName() + "." + prefix;
  51                 }
  52                 return prefix + simpleName;
  53             }
  54             return simpleName;
  55         }
  56         // Must be an anonymous or local class
  57         final String name = clazz.getName();
  58         int index = name.indexOf('$');
  59         if (index == -1) {
  60             return name;
  61         }
  62         index = name.lastIndexOf('.', index);
  63         if (index == -1) {
  64             return name;
  65         }
  66         return name.substring(index + 1);
  67     }
  68 
  69     /**
  70      * Converts a type name in internal form to an external form.
  71      *
  72      * @param name the internal name to convert
  73      * @param qualified whether the returned name should be qualified with the package name
  74      * @param classForNameCompatible specifies if the returned name for array types should be in
  75      *            {@link Class#forName(String)} format (e.g., {@code "[Ljava.lang.Object;"},
  76      *            {@code "[[I"}) or in Java source code format (e.g., {@code "java.lang.Object[]"},
  77      *            {@code "int[][]"} ).
  78      */
  79     public static String internalNameToJava(String name, boolean qualified, boolean classForNameCompatible) {
  80         switch (name.charAt(0)) {
  81             case 'L': {
  82                 String result = name.substring(1, name.length() - 1).replace('/', '.');
  83                 if (!qualified) {
  84                     final int lastDot = result.lastIndexOf('.');
  85                     if (lastDot != -1) {
  86                         result = result.substring(lastDot + 1);
  87                     }
  88                 }
  89                 return result;
  90             }
  91             case '[':
  92                 return classForNameCompatible ? name.replace('/', '.') : internalNameToJava(name.substring(1), qualified, classForNameCompatible) + "[]";
  93             default:
  94                 if (name.length() != 1) {
  95                     throw new IllegalArgumentException("Illegal internal name: " + name);
  96                 }
  97                 return JavaKind.fromPrimitiveOrVoidTypeChar(name.charAt(0)).getJavaName();
  98         }
  99     }
 100 
 101     /**
 102      * Convenient shortcut for calling
 103      * {@link #appendLocation(StringBuilder, ResolvedJavaMethod, int)} without having to supply a
 104      * {@link StringBuilder} instance and convert the result to a string.
 105      */
 106     public static String toLocation(ResolvedJavaMethod method, int bci) {
 107         return appendLocation(new StringBuilder(), method, bci).toString();
 108     }
 109 
 110     /**
 111      * Appends a string representation of a location specified by a given method and bci to a given
 112      * {@link StringBuilder}. If a stack trace element with a non-null file name and non-negative
 113      * line number is {@linkplain ResolvedJavaMethod#asStackTraceElement(int) available} for the
 114      * given method, then the string returned is the {@link StackTraceElement#toString()} value of
 115      * the stack trace element, suffixed by the bci location. For example:
 116      *
 117      * <pre>
 118      *     java.lang.String.valueOf(String.java:2930) [bci: 12]
 119      * </pre>
 120      *
 121      * Otherwise, the string returned is the value of applying {@link JavaMethod#format(String)}
 122      * with the format string {@code "%H.%n(%p)"}, suffixed by the bci location. For example:
 123      *
 124      * <pre>
 125      *     java.lang.String.valueOf(int) [bci: 12]
 126      * </pre>
 127      *
 128      * @param sb
 129      * @param method
 130      * @param bci
 131      */
 132     public static StringBuilder appendLocation(StringBuilder sb, ResolvedJavaMethod method, int bci) {
 133         if (method != null) {
 134             StackTraceElement ste = method.asStackTraceElement(bci);
 135             if (ste.getFileName() != null && ste.getLineNumber() > 0) {
 136                 sb.append(ste);
 137             } else {
 138                 sb.append(method.format("%H.%n(%p)"));
 139             }
 140         } else {
 141             sb.append("Null method");
 142         }
 143         return sb.append(" [bci: ").append(bci).append(']');
 144     }
 145 
 146     static void appendProfile(StringBuilder buf, AbstractJavaProfile<?, ?> profile, int bci, String type, String sep) {
 147         if (profile != null) {
 148             AbstractProfiledItem<?>[] pitems = profile.getItems();
 149             if (pitems != null) {
 150                 buf.append(String.format("%s@%d:", type, bci));
 151                 for (int j = 0; j < pitems.length; j++) {
 152                     AbstractProfiledItem<?> pitem = pitems[j];
 153                     buf.append(String.format(" %.6f (%s)%s", pitem.getProbability(), pitem.getItem(), sep));
 154                 }
 155                 if (profile.getNotRecordedProbability() != 0) {
 156                     buf.append(String.format(" %.6f <other %s>%s", profile.getNotRecordedProbability(), type, sep));
 157                 } else {
 158                     buf.append(String.format(" <no other %s>%s", type, sep));
 159                 }
 160             }
 161         }
 162     }
 163 
 164     /**
 165      * Converts a Java source-language class name into the internal form.
 166      *
 167      * @param className the class name
 168      * @return the internal name form of the class name
 169      */
 170     public static String toInternalName(String className) {
 171         if (className.startsWith("[")) {
 172             /* Already in the correct array style. */
 173             return className.replace('.', '/');
 174         }
 175 
 176         StringBuilder result = new StringBuilder();
 177         String base = className;
 178         while (base.endsWith("[]")) {
 179             result.append("[");
 180             base = base.substring(0, base.length() - 2);
 181         }
 182 
 183         switch (base) {
 184             case "boolean":
 185                 result.append("Z");
 186                 break;
 187             case "byte":
 188                 result.append("B");
 189                 break;
 190             case "short":
 191                 result.append("S");
 192                 break;
 193             case "char":
 194                 result.append("C");
 195                 break;
 196             case "int":
 197                 result.append("I");
 198                 break;
 199             case "float":
 200                 result.append("F");
 201                 break;
 202             case "long":
 203                 result.append("J");
 204                 break;
 205             case "double":
 206                 result.append("D");
 207                 break;
 208             case "void":
 209                 result.append("V");
 210                 break;
 211             default:
 212                 result.append("L").append(base.replace('.', '/')).append(";");
 213                 break;
 214         }
 215         return result.toString();
 216     }
 217 
 218     /**
 219      * Gets a string representation of an object based soley on its class and its
 220      * {@linkplain System#identityHashCode(Object) identity hash code}. This avoids and calls to
 221      * virtual methods on the object such as {@link Object#hashCode()}.
 222      */
 223     public static String identityHashCodeString(Object obj) {
 224         if (obj == null) {
 225             return "null";
 226         }
 227         return obj.getClass().getName() + "@" + System.identityHashCode(obj);
 228     }
 229 
 230     /**
 231      * Used to lookup constants from {@link Modifier} that are not public (VARARGS, SYNTHETIC etc.).
 232      */
 233     static int getNonPublicModifierStaticField(String name) {
 234         try {
 235             Field field = Modifier.class.getDeclaredField(name);
 236             field.setAccessible(true);
 237             return field.getInt(null);
 238         } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
 239             throw new InternalError(e);
 240         }
 241     }
 242 }