Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Nested classes in custom Condition implementations are not registered in the image #719

Closed
bclozel opened this issue Apr 9, 2021 · 4 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@bclozel
Copy link
Contributor

bclozel commented Apr 9, 2021

When running the sample Sleuth application provided in #673 with the native image, we're getting the following:

java.lang.IllegalStateException: Error processing condition on org.springframework.cloud.sleuth.autoconfig.otel.OtelAutoConfiguration
	at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60) ...
...
Caused by: java.lang.IllegalStateException: java.io.FileNotFoundException: class path resource [org/springframework/cloud/sleuth/autoconfig/otel/AnyTracerModePropertySetCondition$OnAutoTracerMode.class] cannot be opened because it does not exist
...
Caused by: java.io.FileNotFoundException: class path resource [org/springframework/cloud/sleuth/autoconfig/otel/AnyTracerModePropertySetCondition$OnAutoTracerMode.class] cannot be opened because it does not exist

Tracing back to the source of the problem, we're seeing that an auto-configuration class is annotated with a custom condition:

@ConditionalOnOtelEnabled
public class OtelAutoConfiguration {

And that condition itself points to the condition implementation:

@Conditional({AnyTracerModePropertySetCondition.class})
public @interface ConditionalOnOtelEnabled {
}

While we are providing a @NativeHint to register all classes in the @Conditional annotation, it seems that in this case the condition is relying on a package private static class, which is causing this issue:

package org.springframework.cloud.sleuth.autoconfig.otel;

import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase;

final class AnyTracerModePropertySetCondition extends AnyNestedCondition {
    private AnyTracerModePropertySetCondition() {
        super(ConfigurationPhase.REGISTER_BEAN);
    }

    @ConditionalOnProperty(
        value = {"spring.sleuth.tracer.mode"},
        havingValue = "AUTO",
        matchIfMissing = true
    )
    static class OnAutoTracerMode {
        OnAutoTracerMode() {
        }
    }
}

At this point, I'm not sure if we should consider this usage pattern as invalid or if we can improve the AOT processing to handle this case.

@bclozel bclozel added type: enhancement A general enhancement theme: aot labels Apr 9, 2021
@sdeleuze
Copy link
Contributor

sdeleuze commented Apr 9, 2021

Any thoughts @aclement ?

@aclement
Copy link
Contributor

aclement commented Apr 9, 2021

I feel like I've seen this kind of pattern in another place too. Including @Conditional annotated nested classes within a type implementing NestedCondition doesn't seem totally unreasonable to catch these.

@aclement
Copy link
Contributor

aclement commented Apr 9, 2021

I had already done part of it (the support was there for recognizing NestedCondition types). I think I was playing around with it when working with actuator hints. So I finished that off. I updated the features sample app to include a scenario like above to verify correctness.

@aclement aclement self-assigned this Apr 9, 2021
@aclement aclement added this to the 0.9.2 milestone Apr 9, 2021
@aclement
Copy link
Contributor

aclement commented Apr 9, 2021

It found another couple of situations actually too, including JCacheAvailableCondition

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: enhancement A general enhancement
Development

No branches or pull requests

3 participants