package org.graalvm.compiler.replacements.test;
import java.util.ArrayList;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.extended.BytecodeExceptionNode.BytecodeExceptionKind;
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
import jdk.vm.ci.meta.ResolvedJavaMethod;
@RunWith(Parameterized.class)
public class ArrayStoreBytecodeExceptionTest extends BytecodeExceptionTest {
private static class Exceptions {
private static Object[] array = new Exceptions[1];
public static void throwArrayStore(Object obj) {
array[0] = obj;
}
}
@Override
protected void registerInvocationPlugins(InvocationPlugins invocationPlugins) {
invocationPlugins.register(new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode obj) {
return throwBytecodeException(b, BytecodeExceptionKind.ARRAY_STORE, obj);
}
}, Exceptions.class, "throwArrayStore", Object.class);
super.registerInvocationPlugins(invocationPlugins);
}
public static void arrayStoreSnippet(Object obj) {
Exceptions.throwArrayStore(obj);
}
@Parameter(0) public Object object;
@Parameter(1) public Class<?> cls;
@Parameters(name = "{1}")
public static Collection<Object[]> data() {
Object[] objects = {"string", 42, new int[0], new String[0], new double[0][]};
ArrayList<Object[]> ret = new ArrayList<>(objects.length);
for (Object o : objects) {
ret.add(new Object[]{o, o.getClass()});
}
return ret;
}
@Test
public void testArrayStoreException() {
test("arrayStoreSnippet", object);
}
}