I've been searching how to raise a warning in Java if a specific object from a class that has a close() function in it doesn't at some point call that close() function (as for example for the BufferedReader class).
In searching through this, I found the Closeable and AutoCloseable interfaces and how they work, as well as this questionn that helped a lot : Force warning when class is not closed
But I still face the issue that when the object in question is created through a function, it is not supposed that it hasn't been closed.
For instance, in this code :
//X.java
import java.io.Closeable;
import java.io.IOException;
public class X implements Closeable{
@Override
public void close() throws IOException {
System.out.println("CLOSE");
}
}
//Main.java
public static void main(String[] args) throws IOException {
X x = new X();
System.out.println(x);
}
A ressource leak warning is raised, but in this one (same X.java) :
//Main.java
public class Main {
public static void main(String[] args) {
X x = getX();
System.out.println(x);
}
public static X getX(){
return new X();
}
}
No warning is raised where I want one to be.
Thanks !