How to Intercept a Method Invocation with Standard Java Features (No Aspectj etc)

How do I intercept a method invocation with standard java features (no AspectJ etc)?

As you note, you cannot use JDK dynamic proxies (no interface), but using Spring and CGLIB (JAR included with Spring), you can do the following:

public class Foo
{
public void setBar()
{
throw new UnsupportedOperationException("should not go here");
}

public void redirected()
{
System.out.println("Yiha");
}
}

Foo foo = new Foo();
ProxyFactory pf = new ProxyFactory(foo);

pf.addAdvice(new MethodInterceptor()
{
public Object invoke(MethodInvocation mi) throws Throwable
{
if (mi.getMethod().getName().startsWith("set"))
{
Method redirect = mi.getThis().getClass().getMethod("redirected");
redirect.invoke(mi.getThis());
}
return null;
}
});

Foo proxy = (Foo) pf.getProxy();
proxy.setBar(); // prints "Yiha"

How to intercept each method call within given method using Spring AOP or AspectJ

In order to

  • weave into private methods,
  • handle self-invocation within one class,
  • dynamically determine control flow and limit interception to only methods called directly or indirectly by your interface method

you need to switch from Spring AOP (proxy-based, many limitations, slow) to AspectJ using LTW (load-time weaving) as described in the Spring manual.

Here is an example in pure AspectJ (no Spring, Just Java SE) which you can easily adapt to your needs:

Sample interface

package de.scrum_master.app;

public interface TextTransformer {
String transform(String text);
}

Class implementing interface incl. main method:

As you can see, I made up an example like yours and also made the methods spend time in order to have something to measure in the aspect later:

package de.scrum_master.app;

public class Application implements TextTransformer {
@Override
public String transform(String text) {
String geekSpelling;
try {
geekSpelling = toGeekSpelling(text);
return toUpperCase(geekSpelling);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

}

private String toGeekSpelling(String text) throws InterruptedException {
Thread.sleep(100);
return replaceVovels(text).replaceAll("[lL]", "1");
}

private String replaceVovels(String text) throws InterruptedException {
Thread.sleep(75);
return text.replaceAll("[oO]", "0").replaceAll("[eE]", "Ɛ");
}

private String toUpperCase(String text) throws InterruptedException {
Thread.sleep(50);
return text.toUpperCase();
}

public static void main(String[] args) throws InterruptedException {
System.out.println(new Application().transform("Hello world!"));
}
}

Aspect:

package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import static java.lang.System.currentTimeMillis;

@Aspect
public class TimingAspect {
@Around("execution(* *(..)) && cflow(execution(* de.scrum_master.app.TextTransformer.*(..)))")
public Object measureExecutionTime(ProceedingJoinPoint thisJoinPoint) throws Throwable {
long startTime = currentTimeMillis();
Object result = thisJoinPoint.proceed();
System.out.println(thisJoinPoint + " -> " + (currentTimeMillis() - startTime) + " ms");
return result;
}
}

Console log:

execution(String de.scrum_master.app.Application.replaceVovels(String)) -> 75 ms
execution(String de.scrum_master.app.Application.toGeekSpelling(String)) -> 189 ms
execution(String de.scrum_master.app.Application.toUpperCase(String)) -> 63 ms
execution(String de.scrum_master.app.Application.transform(String)) -> 252 ms
HƐ110 W0R1D!

You can also exclude the transform(..) method by just changing the pointcut from cflow() to cflowbelow():

@Around("execution(* *(..)) && cflowbelow(execution(* de.scrum_master.app.TextTransformer.*(..)))")

Then the console log is just:

execution(String de.scrum_master.app.Application.replaceVovels(String)) -> 77 ms
execution(String de.scrum_master.app.Application.toGeekSpelling(String)) -> 179 ms
execution(String de.scrum_master.app.Application.toUpperCase(String)) -> 62 ms
HƐ110 W0R1D!

Incidentally, please do read an AspectJ and/or Spring AOP manual.

How to intercept method which handles its own exceptions using AspectJ

Yes, you can. You need a handler() pointcut:

package de.scrum_master.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LogAspect {
@AfterThrowing(value = "execution(* *(..))", throwing = "e")
public void log(JoinPoint thisJoinPoint, Throwable e) {
System.out.println(thisJoinPoint + " -> " + e);
}

@Before("handler(*) && args(e)")
public void logCaughtException(JoinPoint thisJoinPoint, Exception e) {
System.out.println(thisJoinPoint + " -> " + e);
}
}

Log output, assuming class Example is in package de.scrum_master.app:

***** Calling method with catch block *****
handler(catch(ArithmeticException)) -> java.lang.ArithmeticException: / by zero
Can not divide by zero
***** Calling method without catch block *****
execution(void de.scrum_master.app.Example.divideByZeroWithNoCatch()) -> java.lang.ArithmeticException: / by zero
execution(void de.scrum_master.app.Example.main(String[])) -> java.lang.ArithmeticException: / by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
at de.scrum_master.app.Example.divideByZeroWithNoCatch(Example.java:13)
at de.scrum_master.app.Example.main(Example.java:21)

Update: If you want to know where the exception handler is located, there is a simple way: use the enclosing joinpoint's static part. You can also get information about parameter names and types etc. Just use code completion in order to see which methods are available.

@Before("handler(*) && args(e)")
public void logCaughtException(
JoinPoint thisJoinPoint,
JoinPoint.EnclosingStaticPart thisEnclosingJoinPointStaticPart,
Exception e
) {
// Exception handler
System.out.println(thisJoinPoint.getSignature() + " -> " + e);

// Method signature + parameter types/names
MethodSignature methodSignature = (MethodSignature) thisEnclosingJoinPointStaticPart.getSignature();
System.out.println(" " + methodSignature);
Class<?>[] paramTypes = methodSignature.getParameterTypes();
String[] paramNames = methodSignature.getParameterNames();
for (int i = 0; i < paramNames.length; i++)
System.out.println(" " + paramTypes[i].getName() + " " + paramNames[i]);

// Method annotations - attention, reflection!
Method method = methodSignature.getMethod();
for (Annotation annotation: method.getAnnotations())
System.out.println(" " + annotation);
}

Now update your code like this:

package de.scrum_master.app;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
int id();
String name();
String remark();
}
package de.scrum_master.app;

public class Example {
@MyAnnotation(id = 11, name = "John", remark = "my best friend")
public void divideByZeroWithCatch(int dividend, String someText) {
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Can not divide by zero");
}
}

public void divideByZeroWithNoCatch() {
int b = 5 / 0;
}

public static void main(String[] args) {
Example e = new Example();
System.out.println("***** Calling method with catch block *****");
e.divideByZeroWithCatch(123, "Hello world!");
System.out.println("***** Calling method without catch block *****");
e.divideByZeroWithNoCatch();
}
}

Then the console log says:

***** Calling method with catch block *****
catch(ArithmeticException) -> java.lang.ArithmeticException: / by zero
void de.scrum_master.app.Example.divideByZeroWithCatch(int, String)
int dividend
java.lang.String someText
@de.scrum_master.app.MyAnnotation(id=11, name=John, remark=my best friend)
Can not divide by zero
***** Calling method without catch block *****
execution(void de.scrum_master.app.Example.divideByZeroWithNoCatch()) -> java.lang.ArithmeticException: / by zero
execution(void de.scrum_master.app.Example.main(String[])) -> java.lang.ArithmeticException: / by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
at de.scrum_master.app.Example.divideByZeroWithNoCatch(Example.java:14)
at de.scrum_master.app.Example.main(Example.java:22)

If that is good enough for you, then you are fine. But beware, the static part is not the full joinpoint, so you cannot access parameter values from there. In order to do that you have to do manual bookkeeping. And this is possibly expensive and can slow down your application. But for what it is worth, I show you how to do it:

package de.scrum_master.aspect;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;

@Aspect
public class LogAspect {
private ThreadLocal<JoinPoint> enclosingJoinPoint;

@AfterThrowing(value = "execution(* *(..))", throwing = "e")
public void log(JoinPoint thisJoinPoint, Throwable e) {
System.out.println(thisJoinPoint + " -> " + e);
}

@Before("execution(* *(..)) && within(de.scrum_master.app..*)")
public void recordJoinPoint(JoinPoint thisJoinPoint) {
if (enclosingJoinPoint == null)
enclosingJoinPoint = ThreadLocal.withInitial(() -> thisJoinPoint);
else
enclosingJoinPoint.set(thisJoinPoint);
}

@Before("handler(*) && args(e)")
public void logCaughtException(JoinPoint thisJoinPoint, Exception e) {
// Exception handler
System.out.println(thisJoinPoint + " -> " + e);

// Method signature + parameter types/names
JoinPoint enclosingJP = enclosingJoinPoint.get();
MethodSignature methodSignature = (MethodSignature) enclosingJP.getSignature();
System.out.println(" " + methodSignature);
Class<?>[] paramTypes = methodSignature.getParameterTypes();
String[] paramNames = methodSignature.getParameterNames();
Object[] paramValues = enclosingJP.getArgs();
for (int i = 0; i < paramNames.length; i++)
System.out.println(" " + paramTypes[i].getName() + " " + paramNames[i] + " = " + paramValues[i]);

// Target object upon which method is executed
System.out.println(" " + enclosingJP.getTarget());

// Method annotations - attention, reflection!
Method method = methodSignature.getMethod();
for (Annotation annotation: method.getAnnotations())
System.out.println(" " + annotation);
}
}

Why do we need a ThreadLocal member for the joinpoint bookkeeping? Well, because obviously we would get into problems in multi-threaded applications otherwise.

Now the console log says:

***** Calling method with catch block *****
handler(catch(ArithmeticException)) -> java.lang.ArithmeticException: / by zero
void de.scrum_master.app.Example.divideByZeroWithCatch(int, String)
int dividend = 123
java.lang.String someText = Hello world!
de.scrum_master.app.Example@4783da3f
@de.scrum_master.app.MyAnnotation(id=11, name=John, remark=my best friend)
Can not divide by zero
***** Calling method without catch block *****
execution(void de.scrum_master.app.Example.divideByZeroWithNoCatch()) -> java.lang.ArithmeticException: / by zero
execution(void de.scrum_master.app.Example.main(String[])) -> java.lang.ArithmeticException: / by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
at de.scrum_master.app.Example.divideByZeroWithNoCatch(Example.java:14)
at de.scrum_master.app.Example.main(Example.java:22)

Java: Is it possible to always execute a certain function before other functions are called? (Like @Before in JUnit)

Use a dynamic proxy in which you can filter to those methods before which your specific "before" method should be called. And call it in those cases before dispatching the call. Please see the answer from How do I intercept a method invocation with standard java features (no AspectJ etc)?

UPDATE:

An interface is needed to be separated for the proxy. The refresh() method cannot remain private. It must be public and part of the interface (which is not nice here) to be able to be called from the proxy.

package CallBefore;

public interface ExampleInterface {
void function1();

void function2();

void otherFunction();

void refresh();
}

Your class implements that interface:

package CallBefore;

public class Example implements ExampleInterface {

@Override
public void function1() {
System.out.println("function1() has been called");
}

@Override
public void function2() {
System.out.println("function2() has been called");
}

@Override
public void otherFunction() {
System.out.println("otherFunction() has been called");
}

@Override
public void refresh() {
System.out.println("refresh() has been called");
}
}

The proxy which does the trick. It filters the needed methods and calls refresh().

package CallBefore;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ExampleProxy implements InvocationHandler {

private ExampleInterface obj;

public static ExampleInterface newInstance(ExampleInterface obj) {
return (ExampleInterface) java.lang.reflect.Proxy.newProxyInstance(obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(), new ExampleProxy(obj));
}

private ExampleProxy(ExampleInterface obj) {
this.obj = obj;
}

@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
Object result;
try {
if (m.getName().startsWith("function")) {
obj.refresh();
}
result = m.invoke(obj, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
} catch (Exception e) {
throw new RuntimeException("unexpected invocation exception: " + e.getMessage());
}
return result;
}
}

The usage:

package CallBefore;

public class Main {

public static void main(String[] args) {

ExampleInterface proxy = ExampleProxy.newInstance(new Example());
proxy.function1();
proxy.function2();
proxy.otherFunction();
proxy.refresh();
}
}

Output:

refresh() has been called
function1() has been called
refresh() has been called
function2() has been called
otherFunction() has been called
refresh() has been called


Related Topics



Leave a reply



Submit