Nope, java.lang.reflect.Proxy won't work either, because of things like:
ChannelInitializer:
void initChannel(Channel ch) throws Exception {
String me = ch.context(this).name();
ch.pipeline().addAfter(me, null, new WhateverHandler());
}
fails with a NullPointerException because context(this) returns null, because "this" is not actually in the pipeline(), Proxy(this) is!
Sigh!
While in theory, I could extend handlerAdded(ctx) and save a ref to the ctx to be used in initChannel(), that would work for my own code, but is unlikely to work for anyone else's!
Perhaps there is a way to achieve what I want without the shenanigans?
What I'm ultimately trying to do is "blame" the ChannelHandler that throws an Exception.
So while I can implement exceptionCaught() in the last handler of the pipeline, and catch the Exception, it's impossible to know which ChannelHandler actually threw that Exception, other than by stepping through the stackTrace, and looking for my own code (or Netty code, as the case may be!) However, if there is more than one instance of a particular ChannelHandler (e.g. tunnelling SSL in SSL), that still doesn't tell you which *instance* threw the exception.
My solution to that was to alternate legitimate ChannelHandlers with an ExceptionCatcher ChannelHandler. Each ExceptionCatcher has a reference to the ChannelHandler before it. The first ExceptionCatcher to have its exceptionCaught() method called can blame the ChannelHandler immediately preceding it.
HOWEVER, should that ChannelHandler remove itself from the pipeline, or insert other ChannelHandlers, it all falls down, because the ExceptionCatcher gets left behind!
An alternative way to approach this would be to extend DefaultChannelPipeline, but that kind of thing seems to be deprecated in netty 4.
Any suggestions?
Thanks!
Rogan