JavaGoto

Four Easy Steps to Using Gotos in your Java program

1. Make sure your class extends the Goto interface.

2. Use jump(n) instead of goto and label(n) as the target. Labels and jumps must have set integer values, such as: label(12), jump(12), etc.

3. Computed gotos use multijump(n,10,20,30 …), where n is the index of label and where n is a value you change at runtime.

4. Use GotoLoader.newInstance(class) or GotoLoader.load(classname)  to load the main class (or instance) or only the classes (and instances) you want gotos in.

Debuggers precisely follow the flow of all jumps and labels making flow analysis convenient and easy.

You are done!

You can find the source code here:

https://github.com/footloosejava/JavaGoto

Compile the project, put the produced jar in your compile path and then try the code below:

import com.javagoto.gotos.Goto;
import com.javagoto.gotos.transformers.GotoLoader;

public class Example1 extends Goto implements Runnable {

    public void run() {
        int x = 0;
        label(0);

        if (x > 0) {
            System.out.println("We jumped back to label 0 before reaching this code!");
            jump(1);
        }

        x++;
        jump(0);
        label(1);
    }

    public static void main(String args[]) throws Exception  {
        Runnable demo = (Runnable) GotoLoader.newInstance(Example1.class.getName());
        demo.run();
    }
}

For-loop and do-while loop behavior with Gotos

When jumping in and out of loops, the last known state of the loop-initialized variables is retained. You are free to leave and re-renter other language structures at-will.

loopsgotoexample1

Advanced feature – Unreachable Assertion

If a label appears in a region of unreachable code, simply insert ‘unreachable()’ or ‘unreachable(reason)’ to assert that a code region should not be crossed:

unreachableExample1

Code Example

 

Advanced feature – Multiple Return Points

If a label appears in a region of code after a return, the previous return will not allow the compiler to process the code as the next block of code will be unreachable. To workaround this, use the semantic ‘ if(  yes()  ) return’

The yes() method is a protected final method of the Goto class and will efficiently compute the boolean return value at runtime. This allows for multiple labels whose scope would normally prevent the placement of returns after them:

ifyesExample

 

 

 

 

 

 

 

Code Example

Fail-Safe features of Goto.class

All classes extending Goto will fail on instantiation in the event that they were not loaded through the GotoLoader.

Debugging On/Off Setting

The debugging mode can be turned on and off for any instance using setGotoDebug(true), as follows:

debugMode

 

 

 

 

 

 

 

 

Example Code

The debugging output clearly identifies the jumps and labels by class, object hashcode, and program flow:

Gotchas

1) Load the project main class, or a root class that all ther classes with Gotos share a classloader with, via GotoLoader.load(class) or GotoLoader.newInstance(class).

I suggest Runnable or Callable or the one provide in the library called ProjectStarter. All other child classes referenced used in the transformed main class can be use as is:

starterInterfaceExample1
Code Example

2) Gotos and Labels work in instance methods. Your goto enabled classes can have static methods, just none that have goto’s or labels in them.

3) A goto cannot direct flow out of a block synchronized using java’s ‘synchronized’ keyword. The example below will result in an IllegalMonitorState exception. The solution is to use Java locks instead of ‘synchronized’.

illegalMonitorStateException3

 

 

 

 

 

Code Example

4) A goto may be used to direct flow out of a ‘try-finally’ block. In this instance, the code in ‘finally’ will be skipped. However, all code normally exiting the ‘try’ will have it’s ‘finally’ clause run. This example will not invoke the code in a finally clause:

tryfinallyjump

 

 

 

 

 

Code Example

 

They said it!

“If you want to go somewhere, goto is the best way to get there.” Ken Thompson

“Now that I have discussed how to remove go to statements, I will turn around and show why there are occasions when I actually wish to insert them into a go to-less program. The reason is that I like well-documented programs very much, but I dislike inefficient ones; and there are some cases where I simply seem to need go to statements …” Structured Programming with go to Statements. Donald E. Knuth (Stanford University)

 

Contact Information

Email contact: "foot" + "loose" + "java" + "@gmail.com"

Leave a Reply