Skip to content

Commit 8217cbf

Browse files
committed
[GR-33934] Use method class's typeName for HostFunction#toDisplayString in case the function represents a static method.
PullRequest: graal/11720
2 parents a211b72 + 41b7402 commit 8217cbf

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

truffle/src/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/HostAccessTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.lang.reflect.Field;
5555
import java.lang.reflect.Method;
5656
import java.lang.reflect.Proxy;
57+
import java.math.BigInteger;
5758
import java.net.URL;
5859
import java.net.URLClassLoader;
5960
import java.nio.ByteBuffer;
@@ -1946,4 +1947,14 @@ public void testArgAdapterInstantiation() {
19461947
assertEquals(RETURNED_STRING, evalTestLanguage(c, ArgAdapterInstantiationTestLanguage.class, "argument constructor adapter success").asString());
19471948
}
19481949
}
1950+
1951+
@Test
1952+
public void testHostFunctionDisplayName() {
1953+
try (Context cxt = Context.newBuilder().allowHostAccess(HostAccess.ALL).allowHostClassLookup((s) -> true).build()) {
1954+
assertEquals(BigInteger.class.getName() + ".valueOf", cxt.asValue(BigInteger.class).getMember("static").getMember("valueOf").toString());
1955+
assertEquals(Class.class.getName() + ".getName", cxt.asValue(BigInteger.class).getMember("getName").toString());
1956+
assertEquals(BigInteger.class.getName() + ".add", cxt.asValue(BigInteger.ZERO).getMember("add").toString());
1957+
assertEquals(int[].class.getName() + ".clone", cxt.asValue(new int[0]).getMember("clone").toString());
1958+
}
1959+
}
19491960
}

truffle/src/com.oracle.truffle.host/src/com/oracle/truffle/host/HostFunction.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,11 @@ String toDisplayString(@SuppressWarnings("unused") boolean allowSideEffects) {
103103
if (obj == null) {
104104
return "null";
105105
}
106-
String typeName = obj.getClass().getTypeName();
107-
return typeName + "." + method.getName();
106+
String declaringClassName = method.getDeclaringClassName();
107+
if (declaringClassName == null) {
108+
declaringClassName = obj.getClass().getName();
109+
}
110+
return declaringClassName + "." + method.getName();
108111
}
109112

110113
@ExportMessage

truffle/src/com.oracle.truffle.host/src/com/oracle/truffle/host/HostMethodDesc.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ abstract class HostMethodDesc {
6464

6565
abstract String getName();
6666

67+
abstract String getDeclaringClassName();
68+
6769
abstract SingleMethod[] getOverloads();
6870

6971
boolean isInternal() {
@@ -166,6 +168,11 @@ public String getName() {
166168
return getReflectionMethod().getName();
167169
}
168170

171+
@Override
172+
String getDeclaringClassName() {
173+
return getReflectionMethod().getDeclaringClass().getName();
174+
}
175+
169176
@Override
170177
public SingleMethod[] getOverloads() {
171178
return new SingleMethod[]{this};
@@ -431,6 +438,11 @@ public String getName() {
431438
return "clone";
432439
}
433440

441+
@Override
442+
String getDeclaringClassName() {
443+
return null;
444+
}
445+
434446
@Override
435447
public String toString() {
436448
return "Method[clone]";
@@ -476,6 +488,11 @@ public String getName() {
476488
return getOverloads()[0].getName();
477489
}
478490

491+
@Override
492+
String getDeclaringClassName() {
493+
return getOverloads()[0].getDeclaringClassName();
494+
}
495+
479496
@Override
480497
public boolean isMethod() {
481498
return getOverloads()[0].isMethod();

truffle/src/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLJavaInteropTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
import java.io.ByteArrayOutputStream;
5050
import java.io.UnsupportedEncodingException;
51+
import java.math.BigInteger;
5152
import java.util.HashMap;
5253
import java.util.List;
5354
import java.util.Map;
@@ -67,14 +68,20 @@ public class SLJavaInteropTest {
6768
@Before
6869
public void create() {
6970
os = new ByteArrayOutputStream();
70-
context = Context.newBuilder().allowHostAccess(HostAccess.ALL).out(os).build();
71+
context = Context.newBuilder().allowHostAccess(HostAccess.ALL).allowHostClassLookup((s) -> true).out(os).build();
7172
}
7273

7374
@After
7475
public void dispose() {
7576
context.close();
7677
}
7778

79+
@Test
80+
public void testHostFunctionDisplayName() {
81+
assertEquals(BigInteger.class.getName() + ".valueOf", context.eval("sl", "function main() {\n" + " return java(\"java.math.BigInteger\").valueOf;\n" + "}\n").toString());
82+
assertEquals(BigInteger.class.getName() + ".add", context.eval("sl", "function main() {\n" + " return java(\"java.math.BigInteger\").ZERO.add;\n" + "}\n").toString());
83+
}
84+
7885
@Test
7986
public void asFunction() throws Exception {
8087
String scriptText = "function test() {\n" + " println(\"Called!\");\n" + "}\n";

0 commit comments

Comments
 (0)