top of page

Goto Support

With Runtime Converter version 3.1.1, we will now be supporting the "goto" feature. We've implemented it just because we can and not because anyone actually uses it. The Runtime Converter passed extensive testing without the "goto" feature ever being seen except in some arcane test, and we never thought we'd implement it, but we have.

How to implement "goto" in Java

While Java has not implemented the "goto" feature in the Java language, it has nevertheless implemented the goto feature in Java bytecode. In fact, the break statement in a loop uses the "goto" bytecode instruction (https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings).

By wrapping the code with a goto in an anonymous class and modifying the bytecode of that class at runtime, plus creating a new instance with reflection, we have a working "goto" feature made from a switch, if, and break statement block:

while (true) { if (GotoStubs.gotoStub("a")) { break; } }

The "label" element works the same way:

while (true) { if (GotoStubs.labelStub("a")) { break; } }

Both statements provide a goto statement from the java compiler. The runtime converter library then takes the label goto instruction destination address and writes it to the "gotoStub" goto, after adjusting the offset, since Java goto bytecode uses relative addresses. Note that the Runtime Converter Library does not do bytecode modification in general, except when code needs to use a "goto".

Implications of using "goto" from Java bytecode

The implications of using goto in Java bytecode is that it is fully safe for testing and experimental purposes. The "goto" language feature is also not recommend to be used in code for stylistic reasons. Additionally, changes to the Java compiler could break the feature, in which case the break statements would revert to break statements rather than labels or goto.

Other libraries

There is a similar goto library available on GitHub (https://github.com/footloosejava/goto). We did not use it because of the GPL restrictions. Their implementation has a cleaner syntax without the switch statements, but also uses significantly more code.

Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page