Skip to content

Instantly share code, notes, and snippets.

@nrosner
nrosner / jarstats.py
Created March 4, 2017 02:25
A Python script to count the number of classes and methods in a .jar file using javap.
#!/usr/bin/env python
import sys, os, re, zipfile, subprocess
# Rudimentary parsing of javap output
regex_type = r'[a-zA-Z0-9.<>, ?$[\]]+'
regex_class = r'(?:(public|private|protected) )?((?:(?:static|abstract|final) ?)*)(class|interface) (' + regex_type + ') (?:extends ((?:' + regex_type +'),?)+ )?(?:implements ((?:[a-zA-Z0-9\\.<>\\?\\$])+,?)+ )?{([^}]+)}'
regex_method = r'(?:(public|private|protected) )?((?:static|abstract|final) ?)*(?:(' + regex_type + ') )?([a-zA-Z]+)\\(([^\\)]*)\\)'
regex_field = r'(?:(public|private|protected) )?((?:(?:static|abstract|final) ?)*)(' + regex_type + ') ([a-zA-Z0-9]+)'
RE_TYPE = re.compile(regex_type)