Skip to content

Instantly share code, notes, and snippets.

@TkTech
Created December 3, 2012 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TkTech/4194268 to your computer and use it in GitHub Desktop.
Save TkTech/4194268 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf8 -*-
import re
import sys
import getopt
from jawa import JarFile, ConstantMethodRef, ConstantInterfaceMethodRef
_method_types = (ConstantMethodRef, ConstantInterfaceMethodRef)
def find_methods(cf, methods):
def match(constant):
if not isinstance(constant, _method_types):
return False
for method in methods:
if method.search(constant.name_and_type.name.value):
return True
return False
for c in cf.constants.find(f=match):
yield c
def find_uses(cf, constants):
for constant in constants:
print('[{0}] used in [{1}]'.format(
constant.name_and_type.name.value,
cf.this.name.value
))
for method in cf.methods.find(f=lambda c: c.code):
for ins in method.code.disassemble():
for operand in ins.operands:
if isinstance(operand, dict):
continue
elif operand.op_type != 30:
continue
elif operand.value != constant.index:
continue
yield method, ins, constant
def main(argv):
try:
opts, args = getopt.gnu_getopt(argv[1:], '', [
'jar='
])
except getopt.GetoptError as e:
print(str(e))
return 1
sources = []
for o, a in opts:
if o == '--jar':
sources.append(a)
methods = [re.compile(m) for m in args]
for source in sources:
with JarFile(source) as jf:
for _, cf in jf.all_classes():
results = list(find_methods(cf, methods))
if not results:
continue
for method, ins, const in find_uses(cf, results):
flags = method.access_flags.to_dict()
flags = [k for k, v in flags.items() if v]
l = []
l.append(' '.join(flags))
l.append(method.returns.name)
l.append(method.name.value)
l.append('({0})'.format(
', '.join(
a.name + ('[]' * a.dimensions) for a in method.args
)
))
print(' {0}'.format(' '.join(l)))
if __name__ == '__main__':
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment