lundi 15 janvier 2018

Is it possible to determine the classname of an outer class from an instance of an anonymous inner class

I have a somewhat atypical logging situation where I want to write a log message on behalf of an outer class when only provided the instance of an anonymous inner class. I'm using slf4j.

Lets say i have a class:

public abstract class AFoo implements IFoo

Then I have an outer class that has a field with an abstract inner IFoo class:

public class Outer {
    protected IFoo myFoo = new AFoo() { }
} 

Third, I have some utility class to generate some log message:

public SomeFooLoggerUtil {
    public static writeLogMessagesForFoo(IFoo somefoo) {
        Logger logger = LoggerFactory.getLogger(somefoo.getClass());
    }
}

Inside writeLogMessagesForFoo, what I want to do is say "if somefoo is an instance of an anonymous inner class, then use its outer class instead". So if somefoo is the in-line abstract AFoo from Outside, then Outside.class will be the logger used instead of AFoo$1.

Is this possible?





Aucun commentaire:

Enregistrer un commentaire