1
votes

I'm trying some method interceptor which can intercept a method in a class. I tried using cglib, bytebuddy. I cannot use normal Java proxy class, since it is a class. Is there any way to intercept my final method. Here is, what I've tried so for.

//My Target class,

public class Hello {
    public final  String sayHello(){
    //return lower case hello
    return "hello";
    }
}

//Main Application

public class InterApp {
    public static void main(String[] d) throws Exception {
        new InterApp().loadclassDD();
    }

    public void loadclassDD() throws Exception {
       //Byte-buddy
        Hello helloObject1 = new ByteBuddy()
                .subclass(Hello.class)
                .method(named("sayHello"))
                .intercept(MethodDelegation.to(LoggerInterceptor.class))
                .make()
                .load(getClass().getClassLoader(),
                        ClassLoadingStrategy.Default.WRAPPER).getLoaded()
                .newInstance();
        System.out.println(helloObject1.sayHello());
       //CGLIB
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Hello.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object arg0, Method arg1, Object[] arg2,
                    MethodProxy arg3) throws Throwable {
                System.out.println("method name " + arg1.getName());
                return arg3.invokeSuper(arg0, arg2).toString().toUpperCase();
            }
        });
        Hello proxy = (Hello) enhancer.create();
        System.out.println(proxy.sayHello());
    }
}

//LoggerInterceptor - Byte-buddy implementation

public class LoggerInterceptor {
public static String log(@SuperCall Callable<String> zuper)throws Exception {
    System.out.println("Method intercepted");
    return zuper.call().toUpperCase();
}
}

If I remove the final quantifier from the method both are working(Providing upper case HELLO as output). What is reason for this.? Is there any way to achieve this? Correct me, if my understanding is wrong.

2
Since a final method can not bet overridden, I suspect the created classes do not do it or maybe the JVM detects the final method and calls that and not the newly created method. - Gerhard
Sounds like black magic may be required. Change the superclass with javassist, for example, and reload it. Then proxy. But really, maybe the method just shouldn't be final to begin with if you want to override it. - kutschkem
I cannot use javassist. It's triple licence won't fix for me. 5 lines of javassist code will solve my problem actually. Tried it also. - Pasupathi Rajamanickam
I think ASM would help me. Trying that. OMG, I need to put 5 days effort to do hello world I guess. Very complex. - Pasupathi Rajamanickam

2 Answers

0
votes

You could introduce an interface which is implemented by your final class. That way the proxy method would become an option, as the proxy can delegate to the original class, while still keeping the final aspect of your implementation in place.

Byteman will fail because you are trying to subclass and override the final method. It does not matter if you do this handcoded or via some bytecode manipulation lib. It simply violates the contract that your class defined. So even if it worked, it would probably still be a bad idea, because the author of the original class defined the final aspect for a reason.

0
votes

Contrary to what the above answer seems to suggest it /is/ possible to use Byteman to change the definition of the method sayHello even though it is final. For example, the following Byteman rule:

RULE change return value CLASS Hello METHOD sayHello AT EXIT IF true DO $! = $! + " from Byteman" ENDRULE

will update the returned String to be "Hello from Byteman".