{"id":293,"date":"2014-04-13T14:48:32","date_gmt":"2014-04-13T22:48:32","guid":{"rendered":"http:\/\/www.android-decompiler.com\/blog\/?p=293"},"modified":"2018-12-19T13:39:43","modified_gmt":"2018-12-19T21:39:43","slug":"using-the-ast-tagging-api","status":"publish","type":"post","link":"https:\/\/www.pnfsoftware.com\/blog\/using-the-ast-tagging-api\/","title":{"rendered":"Using the AST Tagging API"},"content":{"rendered":"<p>JEB version <a href=\"https:\/\/www.pnfsoftware.com\/jeb1\/changelist\">1.5.201404100<\/a> introduces new methods to the AST IElement objects,\u00a0<a href=\"https:\/\/www.pnfsoftware.com\/jeb1\/apidoc\/jeb\/api\/ast\/IElement.html#attachTag(java.lang.String, java.lang.Object)\">attachTag()<\/a> and <a href=\"https:\/\/www.pnfsoftware.com\/jeb1\/apidoc\/jeb\/api\/ast\/IElement.html#retrieveTag(java.lang.String)\">retrieveTag()<\/a>. These methods allow an API user to <a href=\"https:\/\/www.pnfsoftware.com\/jeb1\/apidoc\/jeb\/api\/Tag.html\">tag<\/a> elements of Abstract Syntax Trees. When a tagged tree is rendered (that is, when decompiled Java code is being generated), tags are processed and provided to the user alongside the decompiled code, with associated text coordinates (line, column). Within the API documentation, a &#8220;located tag&#8221; is referred to as a <a href=\"https:\/\/www.pnfsoftware.com\/jeb1\/apidoc\/jeb\/api\/Mark.html\">mark<\/a>.<\/p>\n<p>One example use case: Tagging nodes of an AST can be useful if the yielded source code is of specific interest, and potentially require follow-up human analysis.<\/p>\n<p>The example below shows how one can navigate a Class tree, looking for specific calls to <em>findViewById<\/em>:<\/p>\n<pre>def processTree(e):\r\n  if isinstance(e, Call) and e.getMethod().getName() == 'findViewById' and ... :\r\n    print 'Tagging Call element:', e #e.getMethod().getName()\r\n    e.attachTag('testTag', 'Calling interesting findViewById')\r\n  if e:\r\n    # recursively process sub-elements\r\n    for e1 in e.getSubElements():\r\n      processTree(e1)\r\n\r\nsig = ...\r\nast = self.jeb.getDecompiledClassTree(sig)  # assume the class was decompiled\r\nprocessTree(e)<\/pre>\n<p><span style=\"line-height: 1.714285714; font-size: 1rem;\">The Class tree can be rendered by calling the newly introduced overloaded <a href=\"https:\/\/www.pnfsoftware.com\/jeb1\/apidoc\/jeb\/api\/JebInstance.html#decompile(java.lang.String, boolean, boolean, java.util.List)\">decompile(sig, is_class, regenerate, marks)<\/a> method:<\/span><\/p>\n<pre>marks = []\r\ndecompiled_class = self.jeb.decompile(sig, True, False, marks)\r\nprint marks<\/pre>\n<p>Remember to set <em>regenerate<\/em> to False since you want to avoid re-decompilation (doing so would generate a new, tag-less AST).<\/p>\n<p>The <em>marks<\/em> array will contain the precise locations (lines and columns) of each tag within the <em>decompiled_class<\/em> text buffer.<\/p>\n<p>Hopefully, this simplistic example showed you how to use the new AST tagging methods. Happy reversing and code analysis.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JEB version 1.5.201404100 introduces new methods to the AST IElement objects,\u00a0attachTag() and retrieveTag(). These methods allow an API user to tag elements of Abstract Syntax Trees. When a tagged tree is rendered (that is, when decompiled Java code is being generated), tags are processed and provided to the user alongside the decompiled code, with associated &hellip; <a href=\"https:\/\/www.pnfsoftware.com\/blog\/using-the-ast-tagging-api\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using the AST Tagging API<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,3],"tags":[],"class_list":["post-293","post","type-post","status-publish","format-standard","hentry","category-api-jeb1","category-decompilation"],"_links":{"self":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/posts\/293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/comments?post=293"}],"version-history":[{"count":0,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/posts\/293\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/media?parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/categories?post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/tags?post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}