{"action":"created","issue":{"url":"https://api.github.com/repos/raphw/byte-buddy/issues/104","repository_url":"https://api.github.com/repos/raphw/byte-buddy","labels_url":"https://api.github.com/repos/raphw/byte-buddy/issues/104/labels{/name}","comments_url":"https://api.github.com/repos/raphw/byte-buddy/issues/104/comments","events_url":"https://api.github.com/repos/raphw/byte-buddy/issues/104/events","html_url":"https://github.com/raphw/byte-buddy/issues/104","id":135392377,"number":104,"title":"How to redefine class with MethodDelegation?","user":{"login":"hengyunabc","id":1683936,"avatar_url":"https://avatars.githubusercontent.com/u/1683936?v=3","gravatar_id":"","url":"https://api.github.com/users/hengyunabc","html_url":"https://github.com/hengyunabc","followers_url":"https://api.github.com/users/hengyunabc/followers","following_url":"https://api.github.com/users/hengyunabc/following{/other_user}","gists_url":"https://api.github.com/users/hengyunabc/gists{/gist_id}","starred_url":"https://api.github.com/users/hengyunabc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hengyunabc/subscriptions","organizations_url":"https://api.github.com/users/hengyunabc/orgs","repos_url":"https://api.github.com/users/hengyunabc/repos","events_url":"https://api.github.com/users/hengyunabc/events{/privacy}","received_events_url":"https://api.github.com/users/hengyunabc/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/raphw/byte-buddy/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":{"login":"raphw","id":4489328,"avatar_url":"https://avatars.githubusercontent.com/u/4489328?v=3","gravatar_id":"","url":"https://api.github.com/users/raphw","html_url":"https://github.com/raphw","followers_url":"https://api.github.com/users/raphw/followers","following_url":"https://api.github.com/users/raphw/following{/other_user}","gists_url":"https://api.github.com/users/raphw/gists{/gist_id}","starred_url":"https://api.github.com/users/raphw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/raphw/subscriptions","organizations_url":"https://api.github.com/users/raphw/orgs","repos_url":"https://api.github.com/users/raphw/repos","events_url":"https://api.github.com/users/raphw/events{/privacy}","received_events_url":"https://api.github.com/users/raphw/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/raphw/byte-buddy/milestones/36","html_url":"https://github.com/raphw/byte-buddy/milestones/1.2.3","labels_url":"https://api.github.com/repos/raphw/byte-buddy/milestones/36/labels","id":1594764,"number":36,"title":"1.2.3","description":null,"creator":{"login":"raphw","id":4489328,"avatar_url":"https://avatars.githubusercontent.com/u/4489328?v=3","gravatar_id":"","url":"https://api.github.com/users/raphw","html_url":"https://github.com/raphw","followers_url":"https://api.github.com/users/raphw/followers","following_url":"https://api.github.com/users/raphw/following{/other_user}","gists_url":"https://api.github.com/users/raphw/gists{/gist_id}","starred_url":"https://api.github.com/users/raphw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/raphw/subscriptions","organizations_url":"https://api.github.com/users/raphw/orgs","repos_url":"https://api.github.com/users/raphw/repos","events_url":"https://api.github.com/users/raphw/events{/privacy}","received_events_url":"https://api.github.com/users/raphw/received_events","type":"User","site_admin":false},"open_issues":1,"closed_issues":1,"state":"open","created_at":"2016-02-20T13:48:59Z","updated_at":"2016-02-22T11:49:42Z","due_on":null,"closed_at":null},"comments":3,"created_at":"2016-02-22T11:38:58Z","updated_at":"2016-02-23T08:12:57Z","closed_at":null,"body":"\r\n```java\r\nclass Bar {\r\n  String m() { return \"bar\"; }\r\n}\r\n\r\nclass Foo {\r\n  String m() { return \"foo\"; }\r\n}\r\n```\r\n```java\r\nclass LogInterceptor {\r\n\t@RuntimeType\r\n\tpublic static Object intercept(@SuperCall Callable<?> zuper) throws Exception {\r\n\t\tSystem.out.println(\"method start\");\r\n\t\ttry {\r\n\t\t\treturn zuper.call();\r\n\t\t} finally {\r\n\t\t\tSystem.out.println(\"method end\");\r\n\t\t}\r\n\t}\r\n\r\n}\r\n```\r\n\r\nTest Code: \r\n```java\r\npublic class Test {\r\n\tpublic static void main(String[] args) {\r\n\t\tByteBuddyAgent.install();\r\n\t\t\r\n//\t\tredefine();\r\n\t\t\r\n\t\tintercept();\r\n\t}\r\n\t\r\n\tpublic static void redefine() {\r\n\t\tFoo foo = new Foo();\r\n\t\tSystem.out.println(foo.m());\r\n\t\t\r\n\t\tnew ByteBuddy().redefine(Bar.class)\r\n\t\t\t\t\t.name(Foo.class.getName())\r\n\t\t\t\t\t.make()\r\n\t\t\t\t\t.load(Foo.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent());\r\n\r\n\t\tSystem.out.println(foo.m());\r\n\t}\r\n\t\r\n\tpublic static void intercept() {\r\n\t\tFoo foo = new Foo();\r\n\t\tSystem.out.println(foo.m());\r\n\t\t\r\n\t\tnew ByteBuddy().redefine(Foo.class).method(ElementMatchers.named(\"m\"))\r\n//\t\t\t\t.intercept(FixedValue.value(\"Byte Buddy!\"))\r\n\t\t\t\t.intercept(MethodDelegation.to(LogInterceptor.class))\r\n\t\t\t\t.make()\r\n\t\t\t\t.load(Foo.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent());\r\n\r\n\t\tSystem.out.println(foo.m());\r\n\t}\r\n}\r\n```\r\n\r\nFrom issue 41 , I know that ````Test.intercept()```` will throw an exception.\r\n\r\nBut how to redefine a class , log all methods call?\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/raphw/byte-buddy/issues/comments/187596572","html_url":"https://github.com/raphw/byte-buddy/issues/104#issuecomment-187596572","issue_url":"https://api.github.com/repos/raphw/byte-buddy/issues/104","id":187596572,"user":{"login":"raphw","id":4489328,"avatar_url":"https://avatars.githubusercontent.com/u/4489328?v=3","gravatar_id":"","url":"https://api.github.com/users/raphw","html_url":"https://github.com/raphw","followers_url":"https://api.github.com/users/raphw/followers","following_url":"https://api.github.com/users/raphw/following{/other_user}","gists_url":"https://api.github.com/users/raphw/gists{/gist_id}","starred_url":"https://api.github.com/users/raphw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/raphw/subscriptions","organizations_url":"https://api.github.com/users/raphw/orgs","repos_url":"https://api.github.com/users/raphw/repos","events_url":"https://api.github.com/users/raphw/events{/privacy}","received_events_url":"https://api.github.com/users/raphw/received_events","type":"User","site_admin":false},"created_at":"2016-02-23T08:12:57Z","updated_at":"2016-02-23T08:12:57Z","body":"What you are trying to do is exactly what Byte Buddy would do when using a rebasement. Unfortunately, a rebasement needs to copy the original method into a new method in order to low the external `@SuperCall` trigger.\r\n\r\nWhat you can do: \r\n\r\n1. You define an `AgentBuilder` and modify the class before it is loaded for the first time. This way, it is possible to transform the method as you intended it originally.\r\n2. Alternatively, you can use Byte Buddy to prepent the ASM code to your method. This way, you do not require Byte Buddy to create external triggers. This is possible using a `AsmVisitorWrapper.ForDeclaredMethods` where you can prepend byte code to existing methods."}}