#!/usr/bin/env bash # classfile-api: regenerates every file in output/. # JDK25=/path/to/jdk-25 [JDK24=/path/to/jdk-24 JDK26=/path/to/jdk-26] ./scripts/run-all.sh # Needs Maven (or an existing target/lib with the ASM jars) for the ASM comparison. JDK24/JDK26 are optional: output/07 is skipped without them. set -uo pipefail HERE="$(cd "$(dirname "$0")/.." && pwd)" cd "$HERE" JDK25="${JDK25:-/opt/jdks/jdk-25.0.5+8}" J="$JDK25/bin" OUT="$HERE/output"; mkdir -p "$OUT" CP="target/c:target/lib/*" JV="$("$J/java" -version 2>&1 | grep -v '^Picked' | sed -n 2p | sed 's/.*(build \(.*\))/\1/')" # 1. ASM jars (the only dependency) and compile. Unsetting JAVA_TOOL_OPTIONS keeps proxy banners out of the transcripts. if [ ! -d target/lib ]; then JAVA_HOME="$JDK25" mvn -B -q -T1 dependency:copy-dependencies -DoutputDirectory=target/lib || exit 1 fi unset JAVA_TOOL_OPTIONS rm -rf target/c target/gen target/instrumented; mkdir -p target/c echo "javac: $("$J/javac" -version 2>&1)" "$J/javac" --release 25 -Xlint:all -cp "target/lib/*" -d target/c src/main/java/demo/*.java || exit 1 run() { # run
[args] -- prints the command, then the output, then the exit code local f="$1"; shift { echo "\$ java -cp classes:asm-9.11.1.jar... $* ($JV)"; "$J/java" -Xmx512m -cp "$CP" "$@" 2>&1; echo "exit=$?"; } > "$OUT/$f" } run 01-parse.txt demo.ParseDemo run 02-generate.txt demo.GenerateDemo target/gen/demo { echo "\$ javap -c -p -v demo.Generated (the class GenerateDemo wrote; $JV)" "$J/javap" -c -p -v -cp target/gen demo.Generated 2>&1 | grep -v -e '^Classfile ' -e '^ Last modified' -e '^ SHA-256'; } > "$OUT/03-javap-generated.txt" run 04-broken-generate.txt demo.BrokenGenerate run 05-transform.txt demo.TransformDemo target/instrumented meth() { awk -v pat="$1" '$0 ~ pat {f=1} f{print} f && /^$/ {f=0}'; } # one method block of javap -c output { echo "\$ javap -c -p demo.Target (original, as javac produced it; fib and boom only)" "$J/javap" -c -p -cp target/c demo.Target | meth 'static int fib' "$J/javap" -c -p -cp target/c demo.Target | meth 'static void boom' echo "\$ javap -c -p demo.Target (naive transform: records on normal return only)" "$J/javap" -c -p -cp target/instrumented/naive demo.Target | meth 'static int fib' "$J/javap" -c -p -cp target/instrumented/naive demo.Target | meth 'static void boom' echo "\$ javap -c -p demo.Target (full transform: plus a catch-all handler)" "$J/javap" -c -p -cp target/instrumented/full demo.Target | meth 'static int fib' "$J/javap" -c -p -cp target/instrumented/full demo.Target | meth 'static void boom' } > "$OUT/06-javap-instrumented.txt" # 2. ASM comparison, same tasks run 08-asm-parse.txt demo.AsmParse run 09-asm-generate.txt demo.AsmGenerate run 10-asm-instrument.txt demo.AsmInstrument run 15-asm-expand-frames.txt demo.AsmGotcha run 11-version-limits.txt demo.VersionLimits # size of the equivalent code, counting only the method(s) that do the job (non-blank, non-comment lines) body() { awk -v pat="$2" '$0 ~ pat {f=1} f && !/^[[:space:]]*(\/\/|\*|\/\*|$)/ {n++} f && /^ }$/ {f=0} END {print n+0}' "$1"; } { echo "Lines of code in the method(s) that do the job (non-blank, non-comment), counted by scripts/run-all.sh:" printf "%-26s java.lang.classfile %3d ASM %3d\n" "generate demo.Generated" "$(body src/main/java/demo/GenerateDemo.java 'static byte\[\] generate')" "$(body src/main/java/demo/AsmGenerate.java 'static byte\[\] generate')" printf "%-26s java.lang.classfile %3d ASM %3d\n" "timing instrumentation" "$(( $(body src/main/java/demo/TimingTransform.java 'public static byte\[\] instrument') + $(body src/main/java/demo/TimingTransform.java 'private static final class Timer') ))" "$(body src/main/java/demo/AsmInstrument.java 'public static byte\[\] instrument')" } > "$OUT/12-line-counts.txt" # Bench: three separate JVMs, because a single run on a shared machine is not trustworthy if [ -z "${SKIP_BENCH:-}" ]; then { for i in 1 2 3; do echo "--- run $i of 3 ---"; "$J/java" -Xmx512m -cp "$CP" demo.Bench 2>&1 | grep -v '^PASS'; done; } > "$OUT/13-bench.txt" fi # 3. Public API of java.lang.classfile across JDK 24, 25, 26 (needs the other JDKs) if [ -n "${JDK24:-}" ] && [ -n "${JDK26:-}" ]; then api() { # api local classes classes=$("$1/bin/jimage" list "$1/lib/modules" | awk '/^Module: java.base/{f=1;next} /^Module:/{f=0} f' \ | grep -E 'java/lang/classfile/.*\.class$' | grep -v -e package-info -e Snippets | sed 's/\.class$//;s#/#.#g;s/^ *//') # shellcheck disable=SC2086 "$1/bin/javap" -public -cp none $classes 2>/dev/null | grep -v '^Compiled from' > "$2" } T="$(mktemp -d)"; trap 'rm -rf "$T"' EXIT api "$JDK24" "$T/24.txt"; api "$JDK25" "$T/25.txt"; api "$JDK26" "$T/26.txt" { echo "\$ javap -public java.lang.classfile.** on each JDK, then diff by class (24: $("$JDK24/bin/java" -version 2>&1 | grep -v '^Picked' | sed -n 2p), 26: $("$JDK26/bin/java" -version 2>&1 | grep -v '^Picked' | sed -n 2p))" echo; echo "=== JDK 24 -> JDK 25 ==="; python3 -I scripts/api_diff.py "$T/24.txt" "$T/25.txt" echo; echo "=== JDK 25 -> JDK 26 ==="; python3 -I scripts/api_diff.py "$T/25.txt" "$T/26.txt" echo; echo "=== is it a preview API on JDK 24? ==="; echo "\$ javap -v java.lang.classfile.ClassFile | grep -ci PreviewFeature" for j in "$JDK24" "$JDK25" "$JDK26"; do echo "$("$j/bin/java" -version 2>&1 | grep -v '^Picked' | sed -n 2p | sed 's/.*(build \(.*\))/\1/'): $("$j/bin/javap" -v java.lang.classfile.ClassFile | grep -ci PreviewFeature)"; done echo; echo "=== @since of ClassFile ==="; for j in "$JDK24" "$JDK25" "$JDK26"; do unzip -p "$j/lib/src.zip" java.base/java/lang/classfile/ClassFile.java | grep -m1 '@since' | sed "s|^|$(basename "$j"): |"; done } > "$OUT/07-api-diff-24-25-26.txt" else echo "JDK24/JDK26 not set: skipped output/07-api-diff-24-25-26.txt" fi echo done