extract method object from anonymous class: update method call site when context should be changed; do not change call site though

This commit is contained in:
anna
2011-10-11 14:06:36 +02:00
parent 88456363ad
commit 1793a5ad90
6 changed files with 106 additions and 2 deletions

View File

@@ -0,0 +1,11 @@
class Test {
void foo() {
new Runnable() {
public void run() {
<selection>int i = 0;
int j = 0;</selection>
System.out.println(i + j);
}
}
}
}

View File

@@ -0,0 +1,31 @@
class Test {
void foo() {
new Runnable() {
public void run() {
Inner inner = new Inner().invoke();
int i = inner.getI();
int j = inner.getJ();
System.out.println(i + j);
}
}
}
private class Inner {
private int i;
private int j;
public int getI() {
return i;
}
public int getJ() {
return j;
}
public Inner invoke() {
i = 0;
j = 0;
return this;
}
}
}

View File

@@ -0,0 +1,13 @@
class Test {
void foo() {
new Runnable() {
public void run() {
<selection>String var = null;
if (var == null) {
return;
}</selection>
System.out.println(var);
}
};
}
}

View File

@@ -0,0 +1,35 @@
class Test {
void foo() {
new Runnable() {
public void run() {
Inner inner = new Inner().invoke();
if (inner.is()) return;
String var = inner.getVar();
System.out.println(var);
}
};
}
private class Inner {
private boolean myResult;
private String var;
boolean is() {
return myResult;
}
public String getVar() {
return var;
}
public Inner invoke() {
var = null;
if (var == null) {
myResult = true;
return this;
}
myResult = false;
return this;
}
}
}