Skip to content

Conversation

@JayBazuzi
Copy link
Contributor

@JayBazuzi JayBazuzi commented Jan 9, 2026

No need to override accessibility of the ctor.

Fixes #370

Summary by Sourcery

Support package-level settings classes that only expose static members and may have private constructors, by lazily instantiating settings classes only when non-static fields are accessed.

New Features:

  • Allow PackageSettings classes with only static members and private constructors to be used with PackageLevelSettings.

Bug Fixes:

  • Prevent failures when resolving package-level settings for classes whose constructors are not publicly accessible but only static members are needed.

Tests:

  • Add tests covering static-only PackageSettings classes with private constructors and adjust existing package settings test fixtures accordingly.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 9, 2026

Reviewer's Guide

Refines PackageLevelSettings instantiation so that package-level settings classes with only static fields do not require an accessible constructor, and adds tests to cover both instance and static-only PackageSettings scenarios including private constructors.

Sequence diagram for retrieving package settings with static-only PackageSettings

sequenceDiagram
  actor Test
  participant PackageLevelSettings
  participant Reflection as Class_And_Field_Reflection

  Test->>PackageLevelSettings: getSettingsFor(packageName, visitedPackages)
  PackageLevelSettings->>Reflection: loadClass(packageName.PackageSettings)
  Reflection-->>PackageLevelSettings: Class clazz
  PackageLevelSettings->>Reflection: clazz.getDeclaredFields()
  Reflection-->>PackageLevelSettings: Field[] fields
  loop for each field
    PackageLevelSettings->>PackageLevelSettings: check Modifier.isStatic(field.modifiers)
    alt static field
      PackageLevelSettings->>PackageLevelSettings: getFieldValue(field, null)
    else instance field
      opt first instance field
        PackageLevelSettings->>PackageLevelSettings: createInstance(clazz)
      end
      PackageLevelSettings->>PackageLevelSettings: getFieldValue(field, instance)
    end
  end
  PackageLevelSettings-->>Test: Map settings
Loading

Class diagram for PackageLevelSettings and PackageSettings test fixtures

classDiagram
class PackageLevelSettings {
  -PackageLevelSettings()
  static Map~String, Settings~ getSettingsFor(String packageName, HashSet~String~ visitedPackages)
  static Object createInstance(Class clazz)
  static Object getFieldValue(Field field, Object instance)
}

class PackageSettings_instance {
  -PackageSettings_instance()
  Object instanceField
  static Object staticField
}

class PackageSettings_static {
  -PackageSettings_static()
  static Object staticField
}

PackageLevelSettings ..> PackageSettings_instance : uses via reflection
PackageLevelSettings ..> PackageSettings_static : uses via reflection
Loading

File-Level Changes

Change Details Files
Delay instantiation of PackageSettings classes until a non-static field is encountered, allowing classes with only static members to omit an accessible constructor.
  • Initialize the instance reference to null before iterating declared fields of the PackageSettings class.
  • Instantiate the settings class lazily inside the loop only when a non-static field is first processed.
  • Preserve existing behavior of reading static fields without needing an instance.
approvaltests-util/src/main/java/org/packagesettings/PackageLevelSettings.java
Update and extend test coverage to verify behavior for both instance-based and static-only PackageSettings classes, including those with private constructors.
  • Remove the unnecessary private constructor from the existing non-final PackageSettings test fixture that uses instance fields.
  • Add a new static-only PackageSettings test fixture with a private constructor and only static fields.
  • Add a new JUnit test that verifies PackageLevelSettings.get() works for the static-only PackageSettings class with a private constructor.
  • Add an approved output file for the new test to lock in expected behavior.
approvaltests-tests/src/test/java/org/packagesettings/subpackage/PackageSettings.java
approvaltests-tests/src/test/java/org/packagesettings/subpackage_static/PackageSettingsTest.java
approvaltests-tests/src/test/java/org/packagesettings/subpackage_static/PackageSettings.java
approvaltests-tests/src/test/java/org/packagesettings/subpackage_static/PackageSettingsTest.testRetrieveValueWithPrivateConstructor.approved.txt

Assessment against linked issues

Issue Objective Addressed Explanation
#370 Update the PackageSettings/PackageLevelSettings loading logic so that classes whose fields are all static (and which may have a private constructor) are correctly handled without requiring instantiation.
#370 Add automated tests to verify that a PackageSettings class with only static members and a private constructor is correctly used by PackageLevelSettings.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PackageSettings class doesn't work with private constructor

2 participants