2009年9月7日月曜日

デザインゴール01

目印

public @interface ByteCodeInject {

}



継続オブジェクト

public class Continuation {

@ByteCodeInject
public Continuation() {
// TODO:
}

@ByteCodeInject
public void Call() {
// TODO:
}

public void Save(String key, Object o) {
// TODO:
}

public Object Load(String key) {
// TODO:
return null;
}
}



注入前

public class ContinuationTestBefore extends HttpServlet {

@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {

Continuation c = null;
// 実際は DataStore とかにシリアライズ予定。
// DataStore からデシリアライズ。 継続オブジェクトが取れるようなら、継続。
if (c != null) {
c.Call();
}

int x;
int y;

if (Math.random() < 0.5) {
x = 50;
y = 50;
}
else {
x = 100;
y = 100;
}

c = new Continuation();

for (int i = 0; i < 100; i++) {

x += i;
y -= i;

if (i % 5 == 4) {
c = new Continuation();
}
}
}
}



注入後

public class ContinuationTestAfter extends HttpServlet {

@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {

Continuation c = null;
// 実際は DataStore とかにシリアライズ予定
// DataStore からデシリアライズ。 継続オブジェクトが取れるようなら、継続。
if (c != null) {
c.Call();
goto LABEL1;
}

int x;
int y;

if (Math.random() < 0.5) {
x = 50;
y = 50;
}
else {
x = 100;
y = 100;
}

c = new Continuation();
final class _165D8F20 implements Serializable {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
_165D8F20 _165D8F20_s = new _165D8F20();
c.Save("_165D8F20", _165D8F20_s);
if (false) {
LABEL1: {
_165D8F20 _165D8F20_l = (_165D8F20)c.Load("_165D8F20");
x = _165D8F20_l.getX();
y = _165D8F20_l.getY();
}
}

for (int i = 0; i < 100; i++) {

x += i;
y -= i;

if (i % 5 == 4) {
c = new Continuation();
final class _165D8F21 implements Serializable {
private int x;
private int y;
private int i;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
_165D8F21 _165D8F21_s = new _165D8F21();
c.Save("_165D8F21", _165D8F21_s);
LABEL2: {
_165D8F21 _165D8F21_l = (_165D8F21)c.Load("_165D8F21");
x = _165D8F21_l.getX();
y = _165D8F21_l.getY();
i = _165D8F21_l.getI();
}
}
}
}
}

3 件のコメント: