Skip to content

Commit 8365230

Browse files
committed
Add an example using an interface
1 parent 3f9d67b commit 8365230

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package mytest;
2+
3+
public interface IntConsumer {
4+
void accept(int num);
5+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package mytest;
2+
3+
import net.openhft.compiler.CompilerUtils;
4+
import org.junit.Test;
5+
6+
import java.net.URL;
7+
import java.net.URLClassLoader;
8+
9+
import static org.junit.Assert.fail;
10+
11+
public class RuntimeCompileTest {
12+
static String code = "package mytest;\n" +
13+
"public class Test implements IntConsumer {\n" +
14+
" public void accept(int num) {\n" +
15+
" if ((byte) num != num)\n" +
16+
" throw new IllegalArgumentException();\n" +
17+
" }\n" +
18+
"}\n";
19+
20+
@Test
21+
public void outOfBounds() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
22+
ClassLoader cl = new URLClassLoader(new URL[0]);
23+
Class aClass = CompilerUtils.CACHED_COMPILER.
24+
loadFromJava(cl, "mytest.Test", code);
25+
IntConsumer consumer = (IntConsumer) aClass.newInstance();
26+
consumer.accept(1); // ok
27+
try {
28+
consumer.accept(128); // no ok
29+
fail();
30+
} catch (IllegalArgumentException expected) {
31+
}
32+
}
33+
}
34+

0 commit comments

Comments
 (0)