False positive: "static" base class members should not be accessed via derived types (java:S3252)

  • versions used SonarLint 7.1
  • with different package there is different visbility.
  • in class Cb Sonar says Use static access with "a.Ca1" for "V1".
  • but if i do it Java says Ca1 cannot be resolved to a variable

package a

package a;

class Ca1 {
	public static final int V1 = 1;

	public void doSomethingA() {
		// Do something
	}
}
package a;

public class Ca2 extends Ca1 {
	public static final int V2 = 2;
}

package b

package b;

import a.Ca2;

public class Cb {
	public int doSomethingB() {
		return Ca2.V1;
	}
}

Hi @Matteo_A_Sorrenti,
I can confirm the behavior of the rule locally, and the fix suggested is indeed not appropriate.

However, I am not sure this a False Positive. Exposing constants through a child class, without passing through a method, makes the code more difficult to maintain.

I propose that we change the issue message to suggest exposing the member through a method as an alternative.

Hi @Dorian_Burihabwa,
I can be agree with you, but it’s possible that package a in a library and package b is my code.
If the message appears in the class Cb I will not be able to fix it :wink:

Hey @Matteo_A_Sorrenti, thanks for having a look again after I created the initial ticket.

I have updated the ticket to an FP report.

Cheers,

Dorian