annotate jvmci/jdk.vm.ci.meta/src/jdk/vm/ci/meta/MetaUtil.java @ 23393:1d4ce2d19e52

clean up and minimize JVMCI (JDK-8156835)
author Doug Simon <doug.simon@oracle.com>
date Thu, 12 May 2016 20:57:31 +0200
parents 1bbd4a7c274b
children d6bd0b9cd0b6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1 /*
22536
dc1aeef79e7e Refactoring: Rename Kind to JavaKind.
Roland Schatz <roland.schatz@oracle.com>
parents: 22054
diff changeset
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
4 *
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
8 *
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
13 * accompanied this code).
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
14 *
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
18 *
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
21 * questions.
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
22 */
22672
1bbd4a7c274b Rename jdk.internal.jvmci to jdk.vm.ci
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 22569
diff changeset
23 package jdk.vm.ci.meta;
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
24
22569
ec96f33a101d updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents: 22536
diff changeset
25 import java.lang.reflect.Field;
ec96f33a101d updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents: 22536
diff changeset
26 import java.lang.reflect.Modifier;
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
27
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
28 /**
22672
1bbd4a7c274b Rename jdk.internal.jvmci to jdk.vm.ci
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 22569
diff changeset
29 * Miscellaneous collection of utility methods used by {@code jdk.vm.ci.meta} and its clients.
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
30 */
5541
b4c406861c33 More renamings to drop Ri* prefix completely. Deleted graph.BitMap class and replaced with java.util.BitSet.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 5540
diff changeset
31 public class MetaUtil {
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
32
9100
d24955427b0b Remove MetaUtil.getMirrorOrFail; Add necessary functionality to the Graal API so that all previous usages of java.lang.Class can now use ResolvedJavaType
Christian Wimmer <christian.wimmer@oracle.com>
parents: 8610
diff changeset
33 /**
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
34 * Extends the functionality of {@link Class#getSimpleName()} to include a non-empty string for
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
35 * anonymous and local classes.
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 15018
diff changeset
36 *
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
37 * @param clazz the class for which the simple name is being requested
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
38 * @param withEnclosingClass specifies if the returned name should be qualified with the name(s)
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
39 * of the enclosing class/classes of {@code clazz} (if any). This option is ignored
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
40 * if {@code clazz} denotes an anonymous or local class.
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
41 * @return the simple name
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
42 */
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
43 public static String getSimpleName(Class<?> clazz, boolean withEnclosingClass) {
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
44 final String simpleName = clazz.getSimpleName();
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
45 if (simpleName.length() != 0) {
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
46 if (withEnclosingClass) {
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
47 String prefix = "";
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
48 Class<?> enclosingClass = clazz;
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
49 while ((enclosingClass = enclosingClass.getEnclosingClass()) != null) {
7830
a452bd211e43 fix for GRAAL-121 (MetaUtil.getSimpleName() shuffles inner class names for nesting levels >2)
Michael Haupt <michael.haupt@oracle.com>
parents: 7744
diff changeset
50 prefix = enclosingClass.getSimpleName() + "." + prefix;
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
51 }
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
52 return prefix + simpleName;
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
53 }
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
54 return simpleName;
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
55 }
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
56 // Must be an anonymous or local class
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
57 final String name = clazz.getName();
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
58 int index = name.indexOf('$');
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
59 if (index == -1) {
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
60 return name;
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
61 }
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
62 index = name.lastIndexOf('.', index);
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
63 if (index == -1) {
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
64 return name;
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
65 }
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
66 return name.substring(index + 1);
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
67 }
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
68
23393
1d4ce2d19e52 clean up and minimize JVMCI (JDK-8156835)
Doug Simon <doug.simon@oracle.com>
parents: 22672
diff changeset
69 /**
1d4ce2d19e52 clean up and minimize JVMCI (JDK-8156835)
Doug Simon <doug.simon@oracle.com>
parents: 22672
diff changeset
70 * Converts a type name in internal form to an external form.
1d4ce2d19e52 clean up and minimize JVMCI (JDK-8156835)
Doug Simon <doug.simon@oracle.com>
parents: 22672
diff changeset
71 *
1d4ce2d19e52 clean up and minimize JVMCI (JDK-8156835)
Doug Simon <doug.simon@oracle.com>
parents: 22672
diff changeset
72 * @param name the internal name to convert
1d4ce2d19e52 clean up and minimize JVMCI (JDK-8156835)
Doug Simon <doug.simon@oracle.com>
parents: 22672
diff changeset
73 * @param qualified whether the returned name should be qualified with the package name
1d4ce2d19e52 clean up and minimize JVMCI (JDK-8156835)
Doug Simon <doug.simon@oracle.com>
parents: 22672
diff changeset
74 * @param classForNameCompatible specifies if the returned name for array types should be in
1d4ce2d19e52 clean up and minimize JVMCI (JDK-8156835)
Doug Simon <doug.simon@oracle.com>
parents: 22672
diff changeset
75 * {@link Class#forName(String)} format (e.g., {@code "[Ljava.lang.Object;"},
1d4ce2d19e52 clean up and minimize JVMCI (JDK-8156835)
Doug Simon <doug.simon@oracle.com>
parents: 22672
diff changeset
76 * {@code "[[I"}) or in Java source code format (e.g., {@code "java.lang.Object[]"},
1d4ce2d19e52 clean up and minimize JVMCI (JDK-8156835)
Doug Simon <doug.simon@oracle.com>
parents: 22672
diff changeset
77 * {@code "int[][]"} ).
1d4ce2d19e52 clean up and minimize JVMCI (JDK-8156835)
Doug Simon <doug.simon@oracle.com>
parents: 22672
diff changeset
78 */
1d4ce2d19e52 clean up and minimize JVMCI (JDK-8156835)
Doug Simon <doug.simon@oracle.com>
parents: 22672
diff changeset
79 public static String internalNameToJava(String name, boolean qualified, boolean classForNameCompatible) {
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
80 switch (name.charAt(0)) {
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
81 case 'L': {
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
82 String result = name.substring(1, name.length() - 1).replace('/', '.');
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
83 if (!qualified) {
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
84 final int lastDot = result.lastIndexOf('.');
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
85 if (lastDot != -1) {
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
86 result = result.substring(lastDot + 1);
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
87 }
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
88 }
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
89 return result;
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
90 }
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
91 case '[':
7676
ca9061b6694c getMethodDescriptor does not need to be in the Signature interface
Christian Wimmer <christian.wimmer@oracle.com>
parents: 7675
diff changeset
92 return classForNameCompatible ? name.replace('/', '.') : internalNameToJava(name.substring(1), qualified, classForNameCompatible) + "[]";
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
93 default:
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
94 if (name.length() != 1) {
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
95 throw new IllegalArgumentException("Illegal internal name: " + name);
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
96 }
22536
dc1aeef79e7e Refactoring: Rename Kind to JavaKind.
Roland Schatz <roland.schatz@oracle.com>
parents: 22054
diff changeset
97 return JavaKind.fromPrimitiveOrVoidTypeChar(name.charAt(0)).getJavaName();
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
98 }
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
99 }
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
100
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
101 /**
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
102 * Convenient shortcut for calling
16490
cac0a7d1c325 moved profileToString(ProfilingInfo info, ResolvedJavaMethod method, String sep) from MetaUtil to be a default method in ProfilingInfo
Doug Simon <doug.simon@oracle.com>
parents: 16488
diff changeset
103 * {@link #appendLocation(StringBuilder, ResolvedJavaMethod, int)} without having to supply a
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
104 * {@link StringBuilder} instance and convert the result to a string.
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
105 */
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
106 public static String toLocation(ResolvedJavaMethod method, int bci) {
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
107 return appendLocation(new StringBuilder(), method, bci).toString();
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
108 }
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
109
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
110 /**
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
111 * Appends a string representation of a location specified by a given method and bci to a given
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
112 * {@link StringBuilder}. If a stack trace element with a non-null file name and non-negative
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
113 * line number is {@linkplain ResolvedJavaMethod#asStackTraceElement(int) available} for the
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
114 * given method, then the string returned is the {@link StackTraceElement#toString()} value of
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
115 * the stack trace element, suffixed by the bci location. For example:
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 15018
diff changeset
116 *
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
117 * <pre>
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
118 * java.lang.String.valueOf(String.java:2930) [bci: 12]
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
119 * </pre>
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 15018
diff changeset
120 *
16480
10c12d09a8d2 moved format(String format, JavaMethod method) from MetaUtil to be a default method in JavaMethod
Doug Simon <doug.simon@oracle.com>
parents: 16479
diff changeset
121 * Otherwise, the string returned is the value of applying {@link JavaMethod#format(String)}
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7357
diff changeset
122 * with the format string {@code "%H.%n(%p)"}, suffixed by the bci location. For example:
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 15018
diff changeset
123 *
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
124 * <pre>
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
125 * java.lang.String.valueOf(int) [bci: 12]
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
126 * </pre>
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 15018
diff changeset
127 *
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
128 * @param sb
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
129 * @param method
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
130 * @param bci
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
131 */
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
132 public static StringBuilder appendLocation(StringBuilder sb, ResolvedJavaMethod method, int bci) {
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
133 if (method != null) {
6539
2463eb24b644 Cleanup of Graal API: Rename methods so that it follows the getXxx naming convention and so that they are similar to the names of the java.lang.reflect classes. Remove unused methods.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 6328
diff changeset
134 StackTraceElement ste = method.asStackTraceElement(bci);
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
135 if (ste.getFileName() != null && ste.getLineNumber() > 0) {
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
136 sb.append(ste);
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
137 } else {
16480
10c12d09a8d2 moved format(String format, JavaMethod method) from MetaUtil to be a default method in JavaMethod
Doug Simon <doug.simon@oracle.com>
parents: 16479
diff changeset
138 sb.append(method.format("%H.%n(%p)"));
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
139 }
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
140 } else {
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
141 sb.append("Null method");
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
142 }
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
143 return sb.append(" [bci: ").append(bci).append(']');
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
144 }
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
145
16490
cac0a7d1c325 moved profileToString(ProfilingInfo info, ResolvedJavaMethod method, String sep) from MetaUtil to be a default method in ProfilingInfo
Doug Simon <doug.simon@oracle.com>
parents: 16488
diff changeset
146 static void appendProfile(StringBuilder buf, AbstractJavaProfile<?, ?> profile, int bci, String type, String sep) {
9760
c76b43ed5089 Added infrastructure for recording invoked methods in the profiling information.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9473
diff changeset
147 if (profile != null) {
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 15018
diff changeset
148 AbstractProfiledItem<?>[] pitems = profile.getItems();
9760
c76b43ed5089 Added infrastructure for recording invoked methods in the profiling information.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9473
diff changeset
149 if (pitems != null) {
9764
51545c49083a Minor refactoring.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9760
diff changeset
150 buf.append(String.format("%s@%d:", type, bci));
9760
c76b43ed5089 Added infrastructure for recording invoked methods in the profiling information.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9473
diff changeset
151 for (int j = 0; j < pitems.length; j++) {
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 15018
diff changeset
152 AbstractProfiledItem<?> pitem = pitems[j];
9760
c76b43ed5089 Added infrastructure for recording invoked methods in the profiling information.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9473
diff changeset
153 buf.append(String.format(" %.6f (%s)%s", pitem.getProbability(), pitem.getItem(), sep));
c76b43ed5089 Added infrastructure for recording invoked methods in the profiling information.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9473
diff changeset
154 }
c76b43ed5089 Added infrastructure for recording invoked methods in the profiling information.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9473
diff changeset
155 if (profile.getNotRecordedProbability() != 0) {
9764
51545c49083a Minor refactoring.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9760
diff changeset
156 buf.append(String.format(" %.6f <other %s>%s", profile.getNotRecordedProbability(), type, sep));
9760
c76b43ed5089 Added infrastructure for recording invoked methods in the profiling information.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9473
diff changeset
157 } else {
9764
51545c49083a Minor refactoring.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9760
diff changeset
158 buf.append(String.format(" <no other %s>%s", type, sep));
9760
c76b43ed5089 Added infrastructure for recording invoked methods in the profiling information.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9473
diff changeset
159 }
c76b43ed5089 Added infrastructure for recording invoked methods in the profiling information.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9473
diff changeset
160 }
c76b43ed5089 Added infrastructure for recording invoked methods in the profiling information.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9473
diff changeset
161 }
c76b43ed5089 Added infrastructure for recording invoked methods in the profiling information.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9473
diff changeset
162 }
c76b43ed5089 Added infrastructure for recording invoked methods in the profiling information.
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9473
diff changeset
163
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
164 /**
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
165 * Converts a Java source-language class name into the internal form.
15040
288c23143d47 Fix most raw type references.
Josef Eisl <josef.eisl@jku.at>
parents: 15018
diff changeset
166 *
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
167 * @param className the class name
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
168 * @return the internal name form of the class name
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
169 */
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
170 public static String toInternalName(String className) {
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
171 if (className.startsWith("[")) {
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
172 /* Already in the correct array style. */
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
173 return className.replace('.', '/');
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
174 }
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
175
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
176 StringBuilder result = new StringBuilder();
7015
62818f156081 added ResolvedJavaType.isClass(Class c) to replace some uses of ResolvedJavaType.toJava()
Doug Simon <doug.simon@oracle.com>
parents: 6699
diff changeset
177 String base = className;
62818f156081 added ResolvedJavaType.isClass(Class c) to replace some uses of ResolvedJavaType.toJava()
Doug Simon <doug.simon@oracle.com>
parents: 6699
diff changeset
178 while (base.endsWith("[]")) {
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
179 result.append("[");
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
180 base = base.substring(0, base.length() - 2);
7015
62818f156081 added ResolvedJavaType.isClass(Class c) to replace some uses of ResolvedJavaType.toJava()
Doug Simon <doug.simon@oracle.com>
parents: 6699
diff changeset
181 }
62818f156081 added ResolvedJavaType.isClass(Class c) to replace some uses of ResolvedJavaType.toJava()
Doug Simon <doug.simon@oracle.com>
parents: 6699
diff changeset
182
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
183 switch (base) {
16726
734247341e6e use string switch in MetaUtil
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16715
diff changeset
184 case "boolean":
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
185 result.append("Z");
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
186 break;
16726
734247341e6e use string switch in MetaUtil
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16715
diff changeset
187 case "byte":
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
188 result.append("B");
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
189 break;
16726
734247341e6e use string switch in MetaUtil
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16715
diff changeset
190 case "short":
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
191 result.append("S");
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
192 break;
16726
734247341e6e use string switch in MetaUtil
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16715
diff changeset
193 case "char":
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
194 result.append("C");
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
195 break;
16726
734247341e6e use string switch in MetaUtil
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16715
diff changeset
196 case "int":
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
197 result.append("I");
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
198 break;
16726
734247341e6e use string switch in MetaUtil
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16715
diff changeset
199 case "float":
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
200 result.append("F");
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
201 break;
16726
734247341e6e use string switch in MetaUtil
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16715
diff changeset
202 case "long":
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
203 result.append("J");
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
204 break;
16726
734247341e6e use string switch in MetaUtil
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16715
diff changeset
205 case "double":
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
206 result.append("D");
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
207 break;
16726
734247341e6e use string switch in MetaUtil
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16715
diff changeset
208 case "void":
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
209 result.append("V");
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
210 break;
16726
734247341e6e use string switch in MetaUtil
Lukas Stadler <lukas.stadler@oracle.com>
parents: 16715
diff changeset
211 default:
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
212 result.append("L").append(base.replace('.', '/')).append(";");
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
213 break;
7015
62818f156081 added ResolvedJavaType.isClass(Class c) to replace some uses of ResolvedJavaType.toJava()
Doug Simon <doug.simon@oracle.com>
parents: 6699
diff changeset
214 }
21963
bb3f0968303c Fix MetaUtil.toInternalName
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21798
diff changeset
215 return result.toString();
5719
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
216 }
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
217
429accae15aa moved some methods from CodeUtil to MetaUtil
Doug Simon <doug.simon@oracle.com>
parents: 5547
diff changeset
218 /**
18499
bd74da0a76f3 implemented toString() for classes that throw UnsupportedError for hashCode()
Doug Simon <doug.simon@oracle.com>
parents: 18465
diff changeset
219 * Gets a string representation of an object based soley on its class and its
bd74da0a76f3 implemented toString() for classes that throw UnsupportedError for hashCode()
Doug Simon <doug.simon@oracle.com>
parents: 18465
diff changeset
220 * {@linkplain System#identityHashCode(Object) identity hash code}. This avoids and calls to
bd74da0a76f3 implemented toString() for classes that throw UnsupportedError for hashCode()
Doug Simon <doug.simon@oracle.com>
parents: 18465
diff changeset
221 * virtual methods on the object such as {@link Object#hashCode()}.
bd74da0a76f3 implemented toString() for classes that throw UnsupportedError for hashCode()
Doug Simon <doug.simon@oracle.com>
parents: 18465
diff changeset
222 */
bd74da0a76f3 implemented toString() for classes that throw UnsupportedError for hashCode()
Doug Simon <doug.simon@oracle.com>
parents: 18465
diff changeset
223 public static String identityHashCodeString(Object obj) {
bd74da0a76f3 implemented toString() for classes that throw UnsupportedError for hashCode()
Doug Simon <doug.simon@oracle.com>
parents: 18465
diff changeset
224 if (obj == null) {
bd74da0a76f3 implemented toString() for classes that throw UnsupportedError for hashCode()
Doug Simon <doug.simon@oracle.com>
parents: 18465
diff changeset
225 return "null";
bd74da0a76f3 implemented toString() for classes that throw UnsupportedError for hashCode()
Doug Simon <doug.simon@oracle.com>
parents: 18465
diff changeset
226 }
bd74da0a76f3 implemented toString() for classes that throw UnsupportedError for hashCode()
Doug Simon <doug.simon@oracle.com>
parents: 18465
diff changeset
227 return obj.getClass().getName() + "@" + System.identityHashCode(obj);
bd74da0a76f3 implemented toString() for classes that throw UnsupportedError for hashCode()
Doug Simon <doug.simon@oracle.com>
parents: 18465
diff changeset
228 }
22011
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
229
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
230 /**
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
231 * Used to lookup constants from {@link Modifier} that are not public (VARARGS, SYNTHETIC etc.).
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
232 */
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
233 static int getNonPublicModifierStaticField(String name) {
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
234 try {
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
235 Field field = Modifier.class.getDeclaredField(name);
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
236 field.setAccessible(true);
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
237 return field.getInt(null);
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
238 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
239 throw new InternalError(e);
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
240 }
4c9d4fe1cbd8 Add ModifiersProvider.isSynthetic, isVarArgs and isBridge.
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21963
diff changeset
241 }
5507
dc71b06d09f8 Moving classes from cri.ri to api.meta.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
242 }