Skip to content

Instantly share code, notes, and snippets.

@nafSadh
Created June 30, 2015 06:58
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 nafSadh/cf17b2b3ef366a38d443 to your computer and use it in GitHub Desktop.
Save nafSadh/cf17b2b3ef366a38d443 to your computer and use it in GitHub Desktop.
This is a simple python script that can list all the methods in a file.
methodSignature = '(public|protected|private|static|\s) +[\w\<\>\[\]]+\s+(\w+) *\([^\)]*\) *(\{?|[^;])'
import traceback
def main():
import getopt, sys, re
try:
options, args = getopt.getopt(sys.argv[1:], 'i:h', ['input=','help'])
except getopt.GetoptError as err:
# print help information and exit:
print(err) # will print something like 'option -a not recognized'
sys.exit(2)
javaFile = ''
# read args
for opt, arg in options:
if opt in ('-i', '--input'):
javaFile = arg
if javaFile is '': javaFile = sys.argv[1]
try:
with open(javaFile, "rU") as file:
lines = file.readlines()
print '//',javaFile
print '//',len(lines), 'lines of code'
methods = {}
for line in lines:
try:
m = re.search(methodSignature,line)
if len(m.group(0))>0 :
methodLine = m.group(0)
methodLine = methodLine.replace(' {','')
access, type, methodSig = methodLine.split(' ',2)
# print m.group(0)
methods[methodSig] = (access, type)
except: a=1
#print '\nSorted list\n'
for methodSig in sorted(methods.keys()):
(access,type) = methods[methodSig]
print '{} {} {};'.format(access,type,methodSig)
except Exception, e:
traceback.print_exc()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment