Repository navigation
Closed
Labels
a:new-ruleProposal to add a new built-in rule
Milestone
Description
Proposed Rule Name: InternalApiUsage
Proposed Category: Design
Description:
Methods and fields with certain annotations should be used only from a restricted set of classes. Specifically,
- members annotated with
@VisibleForTestingshould only be used from their own compilation unit or from a test class. - members annotated with
@API(status = INTERNAL)should not be called from another library
Code Sample:
// A.java
class A {
@VisibleForTesting
String secret = "shibboleth";
@VisibleForTesting
String getSecret() {
return secret;
}
}
// B.java
class B {
void main() {
System.out.println(new A().secret); //violation
System.out.println(new A().getSecret()); //violation
}
}
// ATest.java
import org.junit.jupiter.api.*;
class ATest {
@Test
void testSecret() {
Assertions.assertEquals("shibboleth", new A().secret); // this is OK, there is a test annotation in this class
}
}Possible Properties:
- It might make sense to have configurable list of properties that discourage using an API across compilation unit boundaries, package boundaries or library boundaries.
Notes
Visible for testing -- checking by simple name might be the best strategy, since this annotation exists in many libraries
- https://github.com/JetBrains/java-annotations/blob/master/src/jvmMain/java/org/jetbrains/annotations/VisibleForTesting.java
- https://developer.android.com/reference/androidx/annotation/VisibleForTesting
- https://guava.dev/releases/19.0/api/docs/com/google/common/annotations/VisibleForTesting.html
API restrictions -- https://github.com/apiguardian-team/apiguardian seems to be universally usable, but there are other sets of annotations such as
https://github.com/jenkinsci/lib-access-modifier mostly used by the Jenkins plugins
Metadata
Metadata
Assignees
Labels
a:new-ruleProposal to add a new built-in rule
Activity
[-][java] new rule InternalApiUsage[/-][+][java] New rule: InternalApiUsage[/+][doc] Update release notes (pmd#6994, pmd#6889)