Small question regarding a Java and Spotbugs issue please.

I have a very simple class:

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Question {

    private final        MeterRegistry        meterRegistry;

    @Autowired
    public Question(final MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
    }

    public void foo() {
        Counter.builder("some-name").register(meterRegistry).increment();
    }

}

And as you see in the class, I have an object MeterRegistry meterRegistry.

This object comes from io.micrometer.core.instrument package, I have no control over it.

100% reproducible, I am getting this from Findbugs/Spotbugs:

 Question(MeterRegistry) may expose internal representation by storing an externally mutable object into Question.meterRegistry

May I ask how can I fix tis please?

I tried looking up the API for a .clone() method or something equivalent, no luck. I am quite interested into solving this.

Thank you

0

There are 0 answers