|
1 | 1 | package org.enso.os.environment.jni;
|
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertEquals; |
3 | 4 | import static org.junit.Assert.assertTrue;
|
4 | 5 |
|
| 6 | +import org.graalvm.nativeimage.StackValue; |
5 | 7 | import org.graalvm.nativeimage.c.type.CTypeConversion;
|
6 | 8 | import org.junit.Test;
|
7 | 9 |
|
8 | 10 | public class LoadClassTest {
|
9 | 11 | private static final String PATH = System.getProperty("java.home");
|
10 | 12 |
|
11 | 13 | @Test
|
12 |
| - public void loadJavaLangShortClass() { |
| 14 | + public void invokeParseShortMethod() { |
13 | 15 | var env = JVM.create(PATH).env();
|
14 | 16 | assertTrue("JNI created", env.isNonNull());
|
15 | 17 |
|
16 | 18 | var findClassFn = env.getFunctions().getFindClass();
|
| 19 | + var getStaticMethodIDFn = env.getFunctions().getGetStaticMethodID(); |
| 20 | + var newStringFn = env.getFunctions().getNewStringUTF(); |
| 21 | + var callStaticMethodFn = env.getFunctions().getCallStaticIntMethodA(); |
17 | 22 |
|
18 |
| - try (var nameHolder = CTypeConversion.toCString("java/lang/Short")) { |
19 |
| - var clazz = findClassFn.call(env, nameHolder.get()); |
| 23 | + try (var shortName = CTypeConversion.toCString("java/lang/Short"); |
| 24 | + var valueOfName = CTypeConversion.toCString("parseShort"); |
| 25 | + var valueOfSig = CTypeConversion.toCString("(Ljava/lang/String;)S"); |
| 26 | + var toParse = CTypeConversion.toCString("345"); ) { |
| 27 | + var Short = findClassFn.call(env, shortName.get()); |
20 | 28 |
|
21 |
| - assertTrue("Short class is loaded", clazz.isNonNull()); |
| 29 | + assertTrue("Short class is loaded", Short.isNonNull()); |
| 30 | + |
| 31 | + var valueOf = getStaticMethodIDFn.call(env, Short, valueOfName.get(), valueOfSig.get()); |
| 32 | + assertTrue("valueOf method found", valueOf.isNonNull()); |
| 33 | + |
| 34 | + var args = StackValue.get(JNI.JValue.class); |
| 35 | + var str = newStringFn.call(env, toParse.get()); |
| 36 | + args.setJObject(str); |
| 37 | + var res = callStaticMethodFn.call(env, Short, valueOf, args); |
| 38 | + assertEquals(345, res); |
22 | 39 | }
|
23 | 40 | }
|
24 | 41 | }
|
0 commit comments