Skip to content

Remove reflection in AutoConfigurationChecker#getConfigurations#1

Open
GaetanoCerciello wants to merge 1 commit intomainfrom
remove-reflection-auto-configuration
Open

Remove reflection in AutoConfigurationChecker#getConfigurations#1
GaetanoCerciello wants to merge 1 commit intomainfrom
remove-reflection-auto-configuration

Conversation

@GaetanoCerciello
Copy link
Owner

Overview

This PR removes the reflection-based approach in AutoConfigurationChecker#getConfigurations()
and replaces it with ArchUnit's public API, resolving the TODO comment that flagged this
fragile implementation.

Problem

The original implementation used Spring's ReflectionUtils to access a private method
JavaClasses.of(), which is not part of ArchUnit's public API and is brittle:

Method method = ReflectionUtils.findMethod(JavaClasses.class, "of", Iterable.class);
ReflectionUtils.makeAccessible(method);
return (JavaClasses) ReflectionUtils.invokeMethod(method, null, this.classes.values());

Solution
Uses ArchUnit's public DescribedPredicate API with the .that() method to create
a filtered view of the original JavaClasses containing only configuration classes:

DescribedPredicate<JavaClass> inConfigurations = DescribedPredicate
        .describe("is in configurations", c -> this.classes.containsKey(c.getName()));
return this.allClasses.that(inConfigurations);

Changes Made
Removed imports: java.lang.reflect.Method and org.springframework.util.ReflectionUtils
Added field: private final JavaClasses allClasses to AutoConfigurations class
Added constructor: AutoConfigurations(JavaClasses allClasses) to accept the full JavaClasses
Modified: check() method now passes javaClasses to the constructor
Refactored: getConfigurations() to use public ArchUnit API instead of reflection

@GaetanoCerciello GaetanoCerciello force-pushed the remove-reflection-auto-configuration branch from 25c383b to 16dd10a Compare February 5, 2026 09:03
Signed-off-by: producer <producer@MacBookPro.home>
Signed-off-by: Gaetano Cerciello <cerciello980@gmail.com>
@GaetanoCerciello GaetanoCerciello force-pushed the remove-reflection-auto-configuration branch from 16dd10a to 7fc299e Compare February 5, 2026 09:16
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.

1 participant