How to analyze code from a different class than is currently being analyzed in SonarQube?

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() {
        //close
    }
}

This is the first case. Plain:

public class ClassOne {

    public void methodA() {
        MyType z = null;
        try {
            z = new MyType();
            // do sth
        } finally {
            z.close(); // correct use
        }
    }

    public void methodB() {
        MyType z = new MyType();
        // do sth
        // incorrect use, should be closed here
    }
}

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();
            // do sth
        } finally {
            z.close(); // correct use
        }
    }

    public void methodB() {
        MyType z = jump();
        // do sth
        // incorrect use, should be closed here
    }
}

The third case, one of which I cannot handle:

public class ClassOne {

    public void methodA() {
        MyType z = null;
        try {
            z = new ClassTwo().creator();
            // do sth
        } finally {
            z.close(); // correct use
        }
    }

    public void methodB() {
        MyType z = new ClassTwo().creator();;
        // do sth
        // incorrect use, should be closed here
    }
}


public class ClassTwo {
    MyType creator() {
        return new MyType();
    }
}

. . , , , , .

? ? (, API)

.

+4
1

: API .

: sonarqube java : (aka CompilationUnit) - , , . : .

, , , API, .

- , .

, - , , , ( "" - ), .

+1

All Articles