Skip to content

Instantly share code, notes, and snippets.

@kamchy
Created January 18, 2022 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamchy/ce692b80cfed0a3dcac6a3aaaa68c9b0 to your computer and use it in GitHub Desktop.
Save kamchy/ce692b80cfed0a3dcac6a3aaaa68c9b0 to your computer and use it in GitHub Desktop.
Source file for testing JVM bytecode while reading JVM Specification
class Compilation {
void simpleLoop() {
int i;
for (i = 0; i < 100; i++) {
; // Loop body is empty
}
}
int align2grain(int i, int grain) {
return ((i + grain-1) & ~(grain-1));
}
void useManyNumeric() {
int i = 100;
int j = 1000000;
long l1 = 1;
long l2 = 0xffffffff;
double d = 2.2;
}
void whileInt() {
int i = 0;
while (i < 100) {
i++;
}
}
static int staticAdd(int a, int b) {
return a + b;
}
int nonstaticAdd (int a, int b) {
return a + b;
}
int addThree(int a, int b, int c) {
return nonstaticAdd(a, nonstaticAdd(b, c));
}
Object make() {
return new Object();
}
int[] makeA(int len) {
int[] a = new int[len];
a[len-1] = 3;
return a;
}
int chooseNear(int i) {
switch (i) {
case 0: return 0;
case 1: return 1;
case 2: return 2;
default: return -1;
}
}
int chooseFar(int i) {
switch (i) {
case 0: return 0;
case -100: return -1;
case 100: return 1;
default: return -2;
}
}
static class TestExc extends Exception {
private static final long serialVersionUID = 23;
}
void cantBeZero(int i) throws TestExc {
if (i == 0) {
throw new TestExc();
}
}
void handleTestExc(TestExc e) {
}
void trycatch() {
try {
cantBeZero(0);
} catch (TestExc e) {
handleTestExc(e);
}
}
void tryFinally() throws TestExc {
try {
cantBeZero(0);
} finally {
System.out.println("Always printed");
}
}
void tryCatchFinally() throws TestExc {
try {
cantBeZero(0);
} catch (TestExc e) {
handleTestExc(e);
} finally {
System.out.println("Always printed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment