JEB2 script 中调用 api 重命名所有关联函数

先将光标输入点放到源码文件中的函数名称处~然后使用F2快捷键运行此脚本(JEB 中 F2快捷键的功能是运行最近使用的脚本)


因为此脚本会取光标处的函数,然后查找与其具有相同同重载源的所有函数。。。


PS: JEB2 的脚本,让人写得真是费劲。。。





# -*- coding: utf-8 -*-

"""
Sample client script for PNF Software's JEB2.

More samples are available on our website and within the scripts/ folder.

Refer to SCRIPTS.TXT for more information.
"""

import string
import re,collections
from com.pnfsoftware.jeb.client.api import IScript
from com.pnfsoftware.jeb.client.api import IScript, IGraphicalClientContext
from com.pnfsoftware.jeb.core import RuntimeProjectUtil
from com.pnfsoftware.jeb.core.actions import Actions, ActionContext, ActionXrefsData
from com.pnfsoftware.jeb.core.events import JebEvent, J
from com.pnfsoftware.jeb.core.output import AbstractUnitRepresentation, UnitRepresentationAdapter
from com.pnfsoftware.jeb.core.units.code import ICodeUnit, ICodeItem
from com.pnfsoftware.jeb.core.units.code.java import IJavaSourceUnit, IJavaStaticField, IJavaNewArray, IJavaConstant, IJavaCall, IJavaField, IJavaMethod, IJavaClass
from com.pnfsoftware.jeb.core.actions import ActionTypeHierarchyData
from com.pnfsoftware.jeb.core.actions import ActionRenameData
from com.pnfsoftware.jeb.core.util import DecompilerHelper
from com.pnfsoftware.jeb.core.output.text import ITextDocument
from com.pnfsoftware.jeb.core.units.code.android import IDexUnit
from com.pnfsoftware.jeb.core.actions import ActionOverridesData
from com.pnfsoftware.jeb.core.units import UnitUtil
from com.pnfsoftware.jeb.core.units import UnitAddress






class JEB2_AutoRenameOverrideMethod(IScript):
  def run(self, ctx):
    engctx = ctx.getEnginesContext()
    if not engctx:
      print('Back-end engines not initialized')
      return

    projects = engctx.getProjects()
    if not projects:
      print('There is no opened project')
      return

    # 逻辑开始
    self.debug = 0    #0=False, 1=True
    self.ctx = ctx
    self.prj = projects[0]
    errMsg1 = u'请移动鼠标,将光标输入点放到源码文件中的函数名称处~然后使用F2快捷键运行此脚本(JEB 中 F2快捷键的功能是运行最近使用的脚本)'
    errMsg2 = u'未输入新函数名称,脚本已退出'

    self.focusView = ctx.getFocusedView()
    if not self.focusView:
      print(errMsg1)
      return

    self.focusFragment = self.focusView.getActiveFragment()
    if not self.focusFragment:
      print(errMsg1)
      return

    self.focusUnit = self.focusFragment.getUnit()
    if not self.focusUnit:
      print(errMsg1)
      return

    self.activeAddress = self.focusFragment.getActiveAddress()
    self.activeItem = self.focusFragment.getActiveItem()

    itemStr = self.activeItem.toString()
    if(itemStr.find('cid=METHOD_NAME') < 0):
      print(errMsg1)
      return

    self.focusUnit2 = RuntimeProjectUtil.findUnitsByType(self.prj, ICodeUnit, False)[0]
##    print(self.focusUnit)
##    print(self.focusUnit2)
##    return

    # 弹出输入框用以输入新函数名
    newMethodName = self.ctx.displayQuestionBox('input new function name', 'input new function name\n\n', '')
    if newMethodName == None:
      print(errMsg2)
      return

    actCntx = ActionContext(self.focusUnit, Actions.QUERY_OVERRIDES, self.activeItem.getItemId(), self.activeAddress)
    actData = ActionOverridesData()

    # 获取所有重载了此方法的函数清单,然后逐个重命名
    self.nTotal = 0
    self.nSucc = 0
    self.nFail = 0
    if(self.focusUnit.prepareExecution(actCntx, actData)):
      try:
        bRlt = self.focusUnit.executeAction(actCntx, actData)
        if(not bRlt):
          print('executeAction fail!')
        else:
          overrideAddrList = actData.getAddresses()
          self.nTotal = len(overrideAddrList)
          for addr in overrideAddrList:
            #print('renaming %s' % addr)
            if(self.RenameMeghod(addr, newMethodName)):
              self.nSucc += 1
            else:
              self.nFail += 1
            if(self.debug):
              break;
      except Exception,e:
        print Exception,":",e

    print('total:%d succ:%d fail:%d' %(self.nTotal, self.nSucc, self.nFail))
    print('Done.')
  ## end of run




  # 对指定函数进行重命名
  def RenameMeghod(self, methodAddr, newMethodName):
    m = self.GetMethodByAddress(methodAddr)
    if(not m):
      print(u'失败 %s' % methodAddr)
      return False

    actCntx = ActionContext(self.focusUnit, Actions.RENAME, m.getItemId(), methodAddr)
    actData = ActionRenameData()
    actData.setNewName(newMethodName)

    if(self.focusUnit.prepareExecution(actCntx, actData)):
      # 执行重命名动作
      try:
        bRlt = self.focusUnit.executeAction(actCntx, actData)
        if(not bRlt):
          print(u'失败 %s' % methodAddr)
        else:
          print('%s => %s' %(methodAddr, newMethodName))
          return True
      except Exception,e:
        print Exception,":",e
    return False
  ## end of RenameMeghod



  def GetUnitByAddress(self, addr):
    decomp = DecompilerHelper.getDecompiler(self.focusUnit2)
    if not decomp:
      print('There is no decompiler available for code unit %s' % self.focusUnit2)
      return

    tmpUnit = decomp.decompile(addr)
    self.ctx.openView(tmpUnit)
    if(self.ctx.getFocusedView().getActiveFragment().setActiveAddress(methodAddr)):
      print('setActiveAddress succ')
    return tmpUnit
  ## end of GetUnitByAddress


  def GetMethodByAddress(self, addr):
    found = 0
    self.codeUnit = RuntimeProjectUtil.findUnitsByType(self.prj, ICodeUnit, False)
    if(not self.codeUnit):
      return None
    for unit in self.codeUnit:
        classes = unit.getClasses()
        if(not classes):
          continue
        for c in classes:
          cAddr = c.getAddress()
          if(not cAddr):
            continue
          if(addr.find(cAddr) == 0):
            mlist = c.getMethods()
            if(not mlist):
              continue
            for m in mlist:
              mAddr = m.getAddress()
              if(addr == mAddr):
                #print(mAddr)
                return m
    return None

  ## end of GetMethodByAddress



就这,半玩半写的花了一整天,哈哈~~~


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值