Tie

Java Method Interception

Cook Book

Intercepting by return type

Problem:

You want to apply an interceptor around any methods that return a particular type of object. e.g. You want to create a log entry any time that a method returns a SecretKey.

Solution

Use the ReturnTypeRule.

e.g.

ReturnTypeRule rule = new ReturnTypeRule();
rule.addInheritedType(javax.crypto.SecretKey.class);

InterceptionBuilder builder = new InterceptionBuilder();
builder.whenRuleMatches( rule, new LoggingInterceptor() );

MethodInterceptorStack stack = builder.done();