import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * @author Anastasiya Solodkaya.
 */
public class WhileLoopExample2 {
    private int i = 0;

    void body(int k) {
        i++;
    }

    boolean pred(int k) {
        return i < k;
    }

    MethodHandle predHandle() throws NoSuchMethodException, IllegalAccessException {
        return MethodHandles.lookup().findVirtual(getClass(), "pred", MethodType.methodType(boolean.class, int.class)).bindTo(this);
    }

    MethodHandle bodyHandle() throws NoSuchMethodException, IllegalAccessException {
        return MethodHandles.lookup().findVirtual(getClass(), "body", MethodType.methodType(void.class, int.class)).bindTo(this);
    }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException {
        WhileLoopExample2 example = new WhileLoopExample2();
        MethodHandles.whileLoop(null, example.predHandle(), example.bodyHandle());
    }
}
