Java: Added new query java/visible-for-testing-abuse

This commit is contained in:
Napalys Klicius
2025-08-06 10:53:59 +02:00
parent 2d9470ded8
commit 0c14d93bc6
10 changed files with 231 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
| packageone/SourcePackage.java:8:21:8:32 | Annotated.m1 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:9:29:9:30 | m1 | element |
| packagetwo/Source.java:7:17:7:29 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:12:16:12:16 | f | element |
| packagetwo/Source.java:8:20:8:30 | Annotated.m | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:7:19:7:19 | m | element |
| packagetwo/Source.java:9:28:9:47 | new AnnotatedClass(...) | Access of $@ annotated with VisibleForTesting found in production code. | packageone/AnnotatedClass.java:4:14:4:27 | AnnotatedClass | element |

View File

@@ -0,0 +1 @@
Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql

View File

@@ -0,0 +1,6 @@
package packageone;
@VisibleForTesting
public class AnnotatedClass {
public AnnotatedClass() {}
}

View File

@@ -0,0 +1,10 @@
package packageone;
import packagetwo.Annotated;
public class SourcePackage extends Annotated {
void f() {
AnnotatedClass a = new AnnotatedClass(); // COMPLIANT - same package
String s1 = Annotated.m1; // NON_COMPLIANT
}
}

View File

@@ -0,0 +1,4 @@
package packageone;
public @interface VisibleForTesting {
}

View File

@@ -0,0 +1,15 @@
package packagetwo;
import packageone.*;
public class Annotated {
@VisibleForTesting
static String m;
@VisibleForTesting
static protected String m1;
@VisibleForTesting
static int f() {
return 1;
}
}

View File

@@ -0,0 +1,12 @@
package packagetwo;
import packageone.*;
public class Source {
void f() {
int i = Annotated.f(); // NON_COMPLIANT
String s = Annotated.m; // NON_COMPLIANT
AnnotatedClass a = new AnnotatedClass(); // NON_COMPLIANT
String s1 = Annotated.m1; // COMPLIANT - same package
}
}

View File

@@ -0,0 +1,12 @@
package packagetwo;
import packageone.*;
public class Test {
void f() {
int i = Annotated.f(); // COMPLIANT
String s = Annotated.m; // COMPLIANT
AnnotatedClass a = new AnnotatedClass(); // COMPLIANT
String s1 = Annotated.m1; // COMPLIANT
}
}