{"id":62,"date":"2005-11-20T11:57:16","date_gmt":"2005-11-20T11:57:16","guid":{"rendered":"https:\/\/kari.world.ikari.fi\/2005\/11\/20\/obfuscation-safe-reflection\/"},"modified":"2005-11-20T11:57:16","modified_gmt":"2005-11-20T11:57:16","slug":"obfuscation-safe-reflection","status":"publish","type":"post","link":"https:\/\/kari.world.ikari.fi\/?p=62","title":{"rendered":"Obfuscation safe reflection"},"content":{"rendered":"<p>Q: How to ensure that code can be obfuscated, but reflection API benefits for action invoking could be used?<\/p>\n<p>A: In JDK 5.0 solution exists, just use annotations.<\/p>\n<p>So how to do such.<\/p>\n<p>1) Define appropriate annotation<\/p>\n<p>[code lang=&#8221;java&#8221;]<br \/>\n@Retention(RetentionPolicy.RUNTIME)<br \/>\n@Target(ElementType.METHOD)<br \/>\npublic @interface Call {<br \/>\n    int value();<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>2) Define reflection action to use annotation definition<\/p>\n<p>[code lang=&#8221;java&#8221;]<br \/>\npublic class KCallAction extends KAction {<\/p>\n<p>    public KCallAction(int pID) {<br \/>\n        super(pID);<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    public void actionPerformed(ActionContext pCtx)<br \/>\n    {<br \/>\n        final int id = getId();<br \/>\n        final Window window = pCtx.getWindow();<\/p>\n<p>        for (Method method : window.getClass().getDeclaredMethods()) {<br \/>\n            Call call = method.getAnnotation(Call.class);<br \/>\n            if (call!=null &#038;&#038; call.value()==id) {<br \/>\n                try {<br \/>\n                    method.invoke(window, pCtx);<br \/>\n                } catch (Exception e) {<br \/>\n                    LOG.error(&#8221;Call failed: &#8221; + id, e);<br \/>\n                }<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>}<br \/>\n[\/code]<\/p>\n<p>3) Apply into actual use<\/p>\n<p>[code lang=&#8221;java&#8221;]<br \/>\npublic class CallFrame extends KApplicationFrame {<\/p>\n<p>    protected void createMenus() {<br \/>\n        KMenu menu = new KMenu(TestConstants.R_MENU_FILE, new Action[] {<br \/>\n            new KCallAction(TestConstants.R_OPEN)<br \/>\n        });<br \/>\n        setJMenuBar(new JMenuBar());<br \/>\n        getJMenuBar().add(menu.create());<br \/>\n    }<\/p>\n<p>    @Call(TestConstants.R_OPEN)<br \/>\n    public void openFile(ActionContext pCtx) {<br \/>\n        LOG.info(&#8221;OPENInvoke:&#8221; + pCtx);<br \/>\n    }<\/p>\n<p>    public static void main(String[] pArgs) {<br \/>\n        AppUtil.start(CallFrame.class, pArgs);<br \/>\n    }<\/p>\n<p>}<br \/>\n[\/code]<\/p>\n<p>4) Enjoy benefits!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q: How to ensure that code can be obfuscated, but reflection API benefits for action invoking could be used? A: In JDK 5.0 solution exists, just use annotations. So how to do such. 1) Define appropriate annotation [code lang=&#8221;java&#8221;] @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Call { int value(); } [\/code] 2) Define reflection action to use&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,20],"tags":[],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-java","category-swing"],"_links":{"self":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=62"}],"version-history":[{"count":0,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/posts\/62\/revisions"}],"wp:attachment":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}