Search code examples
javajava.util.concurrentbyte-buddy

How can i return Future<T> using returns method in Bytebuddy?


I am intercepting a method which returns Future where i know that ‘T’ is subclass of ‘E’. How can i use returns() method of net.bytebuddy.matcher.ElementMatchers to achieve that. I want something like returns(Future<isSubtypeOf(E.class)>).


Solution

  • You can implement an ElementMatcher<MethodDescription> yourself, which is the easiest. Byte Buddy offers methods on the TypeDescription.Genric API which allows you to read all type information. Otherwise, ElementMatchers has factory methods for returnsGeneric where you can match a parameterized type as well.