Skip to content

Commit 66482b6

Browse files
committed
Java: updated visible-for-testing-abuse meta data and docs.
1 parent cc497be commit 66482b6

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
# J-T-003: Accessing any method, field or class annotated with `@VisibleForTesting` from production code is discouraged
1+
## Overview
22

33
Accessing class members annotated with `@VisibleForTesting` from production code goes against the intention of the annotation and may indicate programmer error.
44

5-
## Overview
6-
7-
The `@VisibleForTesting` serves to increase visibility of methods, fields or classes for the purposes of testing. Accessing methods, fields or classes that are annotated with `@VisibleForTesting` in production code (not test code) abuses the intention of the annotation.
5+
The `@VisibleForTesting` annotation serves to increase visibility of methods, fields or classes for the purposes of testing. Accessing these annotated elements in production code (not test code) abuses the intention of the annotation.
86

97
## Recommendation
108

@@ -14,15 +12,14 @@ Only access methods, fields or classes annotated with `@VisibleForTesting` from
1412

1513
```java
1614
public class Annotated {
17-
@VisibleForTesting static int f(){}
15+
@VisibleForTesting static int f() { return 42; }
1816
}
1917

2018
/* src/test/java/Test.java */
2119
int i = Annotated.f(); // COMPLIANT
2220

2321
/* src/main/Source.java */
24-
int i = Annotated.f(); // NON_COMPLIANT
25-
22+
int i = Annotated.f(); // NON_COMPLIANT
2623
```
2724

2825
## Implementation notes
@@ -31,9 +28,9 @@ This rule alerts on any implementation of the annotation `VisibleForTesting`, re
3128

3229
The rule also uses the following logic to determine what an abuse of the annotation is:
3330

34-
1) If public or protected member/type is annotated with `VisibleForTesting`, it's assumed that package-private access is enough for production code. Therefore the rule alerts when a public or protected member/type annotated with `VisibleForTesting` is used outside of its declaring package.
35-
2) If package-private member/type is annotated with `VisibleForTesting`, it's assumed that private access is enough for production code. Therefore the rule alerts when a package-private member/type annotated with `VisibleForTesting` is used outside its declaring class.
31+
1. If a public or protected member/type is annotated with `@VisibleForTesting`, it's assumed that package-private access is enough for production code. Therefore the rule alerts when a public or protected member/type annotated with `@VisibleForTesting` is used outside of its declaring package.
32+
2. If a package-private member/type is annotated with `@VisibleForTesting`, it's assumed that private access is enough for production code. Therefore the rule alerts when a package-private member/type annotated with `@VisibleForTesting` is used outside its declaring class.
3633

3734
## References
38-
- Example Specific Implementation of a VisibleForTesting Annotation: [AssertJ VisibleForTesting](https://javadoc.io/doc/org.assertj/assertj-core/latest/org/assertj/core/util/VisibleForTesting.html)
39-
- Assumptions of what level of access is permittable for each access modifier and the annotation: [JetBrains VisibleForTesting](https://javadoc.io/doc/org.jetbrains/annotations/22.0.0/org/jetbrains/annotations/VisibleForTesting.html)
35+
- Javadoc: [AssertJ VisibleForTesting](https://javadoc.io/doc/org.assertj/assertj-core/latest/org/assertj/core/util/VisibleForTesting.html).
36+
- Javadoc: [JetBrains VisibleForTesting](https://javadoc.io/doc/org.jetbrains/annotations/22.0.0/org/jetbrains/annotations/VisibleForTesting.html).

java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @id java/visible-for-testing-abuse
3-
* @name Accessing any method, field or class annotated with `@VisibleForTesting` from production code is discouraged
4-
* @description Accessing any method, field or class annotated with `@VisibleForTesting` from
3+
* @name Use of VisibleForTesting in production code
4+
* @description Accessing methods, fields or classes annotated with `@VisibleForTesting` from
55
* production code goes against the intention of the annotation and may indicate
66
* programmer error.
77
* @kind problem

0 commit comments

Comments
 (0)