1+ /*
2+ * Copyright (c) 2022, 2022, 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+ */
125package com .oracle .svm .core ;
226
327import com .oracle .svm .core .util .VMError ;
1438import java .lang .reflect .Method ;
1539
1640/**
17- * An abstract provider class that defines a protocol for generating unique short names for
18- * Java methods and fields which are compatible for use as local linker symbols. An instance
19- * of this class may be registered as an image singleton in order to ensure that a specific
20- * naming convention is adopted.
41+ * An abstract provider class that defines a protocol for generating unique short names for Java
42+ * methods and fields which are compatible for use as local linker symbols. An instance of this
43+ * class may be registered as an image singleton in order to ensure that a specific naming
44+ * convention is adopted.
2145 */
2246public abstract class UniqueShortNameProvider {
2347 private static UniqueShortNameProvider provider = null ;
@@ -35,10 +59,11 @@ public static synchronized UniqueShortNameProvider provider() {
3559 }
3660
3761 /**
38- * Returns a short, reasonably descriptive, but still unique name for the provided method
39- * which can be used as a linker local symbol.
62+ * Returns a short, reasonably descriptive, but still unique name for the provided method which
63+ * can be used as a linker local symbol.
4064 */
4165 public abstract String uniqueShortName (ResolvedJavaMethod method );
66+
4267 /**
4368 * Returns a short, reasonably descriptive, but still unique name for a method as characterized
4469 * by its loader name, owner class, method selector, signature and status as a method proper or
@@ -53,30 +78,33 @@ public static synchronized UniqueShortNameProvider provider() {
5378 public abstract String uniqueShortName (Member m );
5479
5580 /**
56- * Returns a short, reasonably descriptive, but still unique name derived from the provided method
57- * that can be used as the name of a stub method defined on some factory class that invokes it.
58- * Note that although the value returned by this method is unique to the called method it's purpose
59- * is to uniquely define a single component of the full name of the stub method i.e. the method
60- * selector.
81+ * Returns a short, reasonably descriptive, but still unique name derived from the provided
82+ * method that can be used as the name of a stub method defined on some factory class that
83+ * invokes it. Note that although the value returned by this method is unique to the called
84+ * method it's purpose is to uniquely define a single component of the full name of the stub
85+ * method i.e. the method selector.
6186 */
6287
6388 public abstract String uniqueStubName (ResolvedJavaMethod method );
89+
6490 /**
6591 * Returns a unique identifier for a class loader that can be folded into the unique short name
6692 * of methods where needed in order to disambiguate name collisions that can arise when the same
6793 * class bytecode is loaded by more than one loader. For a few special loaders, such as the JDK
6894 * runtime's builtin loaders and GraalVM Native's top level loaders an empty string is returned.
6995 * There is no need to qualify the method name in this case since the method defined via these
7096 * loaders is taken to be the primary instance. This is safe so long as the delegation model for
71- * this special set of loaders ensures that none of them will replicate a class loaded by another
72- * loader in the set.
97+ * this special set of loaders ensures that none of them will replicate a class loaded by
98+ * another loader in the set.
99+ *
73100 * @param loader The loader whose identifier is to be returned.
74- * @return A unique identifier for the classloader or the empty string when the loader is one
75- * of the special set whose method names do not need qualification.
101+ * @return A unique identifier for the classloader or the empty string when the loader is one of
102+ * the special set whose method names do not need qualification.
76103 */
77104 public abstract String classLoaderNameAndId (ClassLoader loader );
78105
79106 private static final Field classLoaderNameAndId = ReflectionUtil .lookupField (ClassLoader .class , "nameAndId" );
107+
80108 protected String lookupNameAndIdField (ClassLoader loader ) {
81109 try {
82110 return (String ) classLoaderNameAndId .get (loader );
@@ -86,20 +114,16 @@ protected String lookupNameAndIdField(ClassLoader loader) {
86114 }
87115
88116 /**
89- * Default implementation for unique method and field short names which concatenates the (unqualified)
90- * owner class name and method or field selector with an SHA1 digest of the fully qualified Java
91- * name of the method or field. If a loader prefix is provided it is added as prefix to the Java
92- * name before generating the SHA1 digest.
117+ * Default implementation for unique method and field short names which concatenates the
118+ * (unqualified) owner class name and method or field selector with an SHA1 digest of the fully
119+ * qualified Java name of the method or field. If a loader prefix is provided it is added as
120+ * prefix to the Java name before generating the SHA1 digest.
93121 */
94122 private static class DefaultUniqueShortNameProvider extends UniqueShortNameProvider {
95123 @ Override
96124 public String uniqueShortName (ResolvedJavaMethod m ) {
97125 return uniqueShortName ("" , m .getDeclaringClass (), m .getName (), m .getSignature (), m .isConstructor ());
98126 }
99- public String uniqueStubName (ResolvedJavaMethod m ) {
100- // the default short name works as a name for the stub method
101- return uniqueShortName ("" , m .getDeclaringClass (), m .getName (), m .getSignature (), m .isConstructor ());
102- }
103127
104128 @ Override
105129 public String uniqueShortName (String loaderName , ResolvedJavaType declaringClass , String methodName , Signature methodSignature , boolean isConstructor ) {
@@ -114,8 +138,8 @@ public String uniqueShortName(String loaderName, ResolvedJavaType declaringClass
114138 }
115139
116140 return stripPackage (declaringClass .toJavaName ()) + "_" +
117- (isConstructor ? "constructor" : methodName ) + "_" +
118- SubstrateUtil .digest (sb .toString ());
141+ (isConstructor ? "constructor" : methodName ) + "_" +
142+ SubstrateUtil .digest (sb .toString ());
119143 }
120144
121145 @ Override
@@ -139,14 +163,17 @@ public String uniqueShortName(Member m) {
139163 }
140164
141165 return stripPackage (m .getDeclaringClass ().getTypeName ()) + "_" +
142- (m instanceof Constructor ? "constructor" : m .getName ()) + "_" +
143- SubstrateUtil .digest (fullName .toString ());
166+ (m instanceof Constructor ? "constructor" : m .getName ()) + "_" +
167+ SubstrateUtil .digest (fullName .toString ());
144168 }
145- private static String stripPackage (String qualifiedClassName ) {
146- /* Anonymous classes can contain a '/' which can lead to an invalid binary name. */
147- return qualifiedClassName .substring (qualifiedClassName .lastIndexOf ("." ) + 1 ).replace ("/" , "" );
169+
170+ @ Override
171+ public String uniqueStubName (ResolvedJavaMethod m ) {
172+ // the default short name works as a name for the stub method
173+ return uniqueShortName ("" , m .getDeclaringClass (), m .getName (), m .getSignature (), m .isConstructor ());
148174 }
149175
176+ @ Override
150177 public String classLoaderNameAndId (ClassLoader loader ) {
151178 // no need to qualify classes loaded by a bootstrap loader
152179 if (loader == null ) {
@@ -160,4 +187,10 @@ public String classLoaderNameAndId(ClassLoader loader) {
160187 return name ;
161188 }
162189 }
190+
191+ private static String stripPackage (String qualifiedClassName ) {
192+ /* Anonymous classes can contain a '/' which can lead to an invalid binary name. */
193+ return qualifiedClassName .substring (qualifiedClassName .lastIndexOf ("." ) + 1 ).replace ("/" , "" );
194+ }
195+
163196}
0 commit comments