Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A bash script to count the visibility modifiers in a jar file.
#!/bin/bash
if [ $# != 1 ]; then
echo "Pass jar as sole argument"
exit 1
fi
tmp=`mktemp -d`
unzip -d "$tmp" $1 1> /dev/null
classes=`find "$tmp" -name '*.class'`
api_javap=`echo "$classes" | \grep -v '.internal.' | xargs javap -p`
api_methods=`echo "$api_javap" | \grep -E "^ "`
api_methods_total=`echo "$api_methods" | wc -l`
api_methods_public=`echo "$api_methods" | \grep " public" | wc -l`
api_methods_package=`echo "$api_methods" | \grep -E -v " (public|private|protected)" | wc -l`
api_methods_protected=`echo "$api_methods" | \grep " protected" | wc -l`
api_methods_private=`echo "$api_methods" | \grep " private" | wc -l`
api_classes=`echo "$api_javap" | \grep -E ' (class|interface) '`
api_classes_total=`echo "$api_classes" | wc -l`
api_classes_public=`echo "$api_classes" | \grep "public" | wc -l`
api_classes_package=`echo "$api_classes" | \grep -E -v "(public|private|protected)" | wc -l`
api_classes_protected=`echo "$api_classes" | \grep "protected" | wc -l`
api_classes_private=`echo "$api_classes" | \grep "private" | wc -l`
internal_javap=`echo "$classes" | \grep '.internal.' | xargs javap -p`
internal_methods=`echo "$internal_javap" | \grep -E "^ "`
internal_methods_total=`echo "$internal_methods" | wc -l`
internal_methods_public=`echo "$internal_methods" | \grep " public" | wc -l`
internal_methods_package=`echo "$internal_methods" | \grep -E -v " (public|private|protected)" | wc -l`
internal_methods_protected=`echo "$internal_methods" | \grep " protected" | wc -l`
internal_methods_private=`echo "$internal_methods" | \grep " private" | wc -l`
internal_classes=`echo "$internal_javap" | \grep -E ' (class|interface) '`
internal_classes_total=`echo "$internal_classes" | wc -l`
internal_classes_public=`echo "$internal_classes" | \grep "public" | wc -l`
internal_classes_package=`echo "$internal_classes" | \grep -E -v "(public|private|protected)" | wc -l`
internal_classes_protected=`echo "$internal_classes" | \grep "protected" | wc -l`
internal_classes_private=`echo "$internal_classes" | \grep "private" | wc -l`
echo "
Public Packages
===============
Methods Classes
------------------------------
Public : $api_methods_public $api_classes_public
Package : $api_methods_package $api_classes_package
Protected : $api_methods_protected $api_classes_protected
Private : $api_methods_private $api_classes_private
------------------------------
$api_methods_total $api_classes_total
Internal Packages
=================
Methods Classes
-----------------------------
Public : $internal_methods_public $internal_classes_public
Package : $internal_methods_package $internal_classes_package
Protected : $internal_methods_protected $internal_classes_protected
Private : $internal_methods_private $internal_classes_private
-----------------------------
$internal_methods_total $internal_classes_total"
rm -r "$tmp"
$ ./visibilities.sh okhttp-3.0.0-RC1.jar
Public Packages
===============
Methods Classes
------------------------------
Public : 618 46
Package : 283 13
Protected : 2 0
Private : 237 0
------------------------------
1140 59
Internal Packages
=================
Methods Classes
-----------------------------
Public : 559 49
Package : 398 24
Protected : 13 0
Private : 423 0
-----------------------------
1393 73
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.