Case: I am writing a SonarQube rule that should check if a manually created object is closed. When this is not the case, the problem must be raised.
Assume that the part involved in determining whether an object is created manually (or not) is simple and does not matter. For this example, this will be a constructor call. However, there are other ways to instantiate an object that is not suitable for closure.
These are the cases that I would like to touch upon. Suppose we have the following class:
public class MyType {
public void close() {
}
}
This is the first case. Plain:
public class ClassOne {
public void methodA() {
MyType z = null;
try {
z = new MyType();
} finally {
z.close();
}
}
public void methodB() {
MyType z = new MyType();
}
}
Second, a little harder:
public class ClassOne {
MyType creator() {
return new MyType();
}
MyType jump() {
return creator();
}
public void methodA() {
MyType z = null;
try {
z = jump();
} finally {
z.close();
}
}
public void methodB() {
MyType z = jump();
}
}
The third case, one of which I cannot handle:
public class ClassOne {
public void methodA() {
MyType z = null;
try {
z = new ClassTwo().creator();
} finally {
z.close();
}
}
public void methodB() {
MyType z = new ClassTwo().creator();;
}
}
public class ClassTwo {
MyType creator() {
return new MyType();
}
}
. . , , , , .
? ? (, API)
.