quarkusquarkus-rest-client

Remove unwanted @Singleton Bean from Quarkus in Extension


I develop a quarkus extension using this guide which uses another 3rd party quarkus extension as dependency (I have no control over the source code from the 3rd party). The 3rd party extension registers a bean via @Singleton and this bean is always present when I use my extension in a quarkus app (I see it in the dev UI)

This is annoying because this @Singleton Bean is exposing some information that I do not want to be exposed, thus at runtime I want to get rid of the @Singleton Bean from this extension.

Thus I searched for an appropriate BuildItem and wanted to write a @BuildStep in my extension to get rid of this bean. But I cannot find any suitable BuildItem to actually remove Beans "manually" or force to remove beans from a quarkus app. Only to add them, like in this guide.

Can you please help me and tell me how I can reliably remove the bean from my quarkus app?

I am not sharing any code, because it just doesn't work anyways so it's no use.


Solution

  • You can leverage jakarta.enterprise.inject.Vetoed and Arc's io.quarkus.arc.processor.AnnotationsTransformer to do what you want.

    Something like this should do:

    public class VetoingAnnotationTransformer implements AnnotationsTransformer {
    
        private final Set<DotName> vetoedClasses;
    
        public VetoingAnnotationTransformer(Set<DotName> vetoedClasses) {
            this.vetoedClasses = vetoedClasses;
        }
    
        @Override
        public boolean appliesTo(AnnotationTarget.Kind kind) {
            return AnnotationTarget.Kind.CLASS == kind;
        }
    
        @Override
        public void transform(TransformationContext context) {
            if (vetoedClasses.contains(context.getTarget().asClass().name())) {
                context.transform().add(ResteasyReactiveDotNames.VETOED).done();
            }
        }
    }