質問

For my work with aspectJ and invokedynamic I need to weave in an advice into a method when a pointcut matches the called method. First I have exchanged the bootstrapmethod of the invokedynamic-instruction in the script with my own one. Now in my bootstrap method I want to return a CallSite that first invokes the called method and after that an advice that I have defined.

The problem is that the I have to return a CallSite build out of ONE MethodHandle with the same typesignature used in the original bootstrapmethod. I thought about combining two methodhandles (original + advice) into a new one and using that for the returning CallSite. But I can't find the right methods for doing it. MethodHandles.foldArguments seems to be promising, but it didn't work for me. Another idea was to build a MethodHandle from a wrappermethod that invokes the original methodhandle and the advice, but the problem is the right type signature.

Has anyone ideas or suggentions for me? How can I patch a MethodHandle with an advice?

Thank you.

役に立ちましたか?

解決 2

I have tried it with MethodHandles.guradwithtest and it works. But its not very convenient and worked only with invoking something before the original methodhandle.

But now I have solved it another way. I made a wrappermethod that gets the methodhandles and invokes them in the desired way. Then i get that wrappermethod as MethodHandle and insert the arguments. Then wrappermethod.asCollector(...) and wrappermethod.asType(...) Thats it. Sorry for the short explanation. You can ask for more details.

他のヒント

The imho most easy way for a post processing is to use MethodHandles#filterReturnValue. This allows the target method to be executed and then a filter on the return value. You can simply return what goes in, plus do what ever you need to do with your advice. Of course you will need to do type transformations to fit the types together, but that should not be a major problem. For a preprocessing I would probably filter an argument, that I added in before and then drop.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top