Make dump-jar filtering faster
Filtering was too slow on a big jar file.
Flag: EXEMPT host side test change only
Bug: 292141694
Test: manual
Change-Id: I5ff56f158976fe6661151e2a0aa80e837c840d59
diff --git a/tools/hoststubgen/scripts/dump-jar b/tools/hoststubgen/scripts/dump-jar
index fe546fe..8765245 100755
--- a/tools/hoststubgen/scripts/dump-jar
+++ b/tools/hoststubgen/scripts/dump-jar
@@ -26,12 +26,12 @@
Dump a *.class file
- dump-jar [-v] [-s] [-o OUTPUT-FILENAME] JAR-FILE[: filename regex] [...]
+ dump-jar [-v] [-s] [-o OUTPUT-FILENAME] JAR-FILE[: class internal name regex] [...]
Dump a jar file.
If a filename contains a ':', then the following part
- will be used to filter files in the jar file.
+ will be used to filter files in the jar file that matches against class internal names.
For example, "file.jar:/MyClass$" will only dump "MyClass" in file.jar.
@@ -78,16 +78,6 @@
JAVAP_OPTS="-p -c -v"
fi
-
-# Normalize a java class name.
-# Convert '.' to '/'
-# Remove the *.class suffix.
-normalize() {
- local name="$1"
- name="${name%.class}" # Remove the .class suffix.
- echo "$name" | tr '.' '/'
-}
-
# Convert the output for `-s` as needed.
filter_output() {
if (( $simple )) ; then
@@ -124,6 +114,12 @@
fi
}
+# Read jar file names and remove the .class suffix.
+# Also remove non-class files.
+to_internal_names() {
+ sed -ne 's/\.class$//p'
+}
+
for file in "${@}"; do
# *.class?
@@ -136,7 +132,7 @@
# Take the regex. Remove everything up to : in $file
regex=""
if [[ "$file" =~ : ]] ; then
- regex="$(normalize "${file##*:}")"
+ regex="${file##*:}"
fi
# Remove everything after ':', inclusively, in $file.
@@ -151,13 +147,9 @@
echo
fi
- jar tf "$file" | grep '\.class$' | sort | while read -r class ; do
- if normalize "$class" | grep -q -- "$regex" ; then
- echo "## Class: $class"
- javap $dump_code_opt $JAVAP_OPTS -cp $file ${class%.class}
- else
- (( $verbose )) && echo "## Skipping class: $class"
- fi
+ jar tf "$file" | sort | to_internal_names | grep -- "$regex" | while read -r class ; do
+ echo "## Class: $class.class"
+ javap $dump_code_opt $JAVAP_OPTS -cp "$file" "${class}"
done
else