Skip to content

[java] New rule: InternalApiUsage #6889

Closed

Description

@zbynek

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 @VisibleForTesting should 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

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

Activity

  1. added
    a:new-rule
    on Jul 17, 2026
  2. changed the title [-][java] new rule InternalApiUsage[/-] [+][java] New rule: InternalApiUsage[/+] on Jul 18, 2026
  3. added this to the 7.28.0 milestone on Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:new-rule

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions