Can ejb interceptors change the return value of a method before the calling class receives it?

If we have code in a field that has a method that calls another product and returns a list of objects. And we need to make changes to the code to make it more flexible in filling out the returned list, can we at the interim create a hook for a client that intercepts the method before it returns the list and removes elements from the list before the product that calls the method gets list.

eg.

OurCode.search () returns a list of found objects

Other product calls OurCode.search, accepts 100 items

Can we create an interceptor that intercepts before returning OurCode.search and modifies the List of foundObjects, removing unnecessary items? This will only be a temporary fix until the next release.

+7
source share
1 answer

Although I do not recommend doing it this way (for the sake of clarity, and it seems to me that the “interim fix” will become permanent), you can do this with Interceptors.

@AroundInvoke Object filterSearchResults(InvocationContext ctx) throws Exception { Object result = ctx.proceed(); if ( result != null) { List<SearchResult> results = (List<SearchResult>)result; // do whatever you want to to with your results here return results; } return result; } 
+10
source

All Articles