exceptions: Jackson 3 unchecked JacksonException hierarchy, Spring Boot 4.2 error handling, OpenRewrite and sed migration
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4HkWoC7hLzk61vZa8
This commit is contained in:
Executable
+15
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
# Compiles each file in src/broken/ against Jackson 3 and records javac's own message.
|
||||
# These files are deliberately outside src/main so the Maven build never sees them.
|
||||
set -uo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
CP="target/classes:$(cat target/cp.txt)"
|
||||
OUT=output/09-compile-errors.txt
|
||||
: > "$OUT"
|
||||
for f in src/broken/*.java; do
|
||||
echo "=== $(basename "$f") ===" >> "$OUT"
|
||||
tmp=$(mktemp -d)
|
||||
javac -Xlint:none -d "$tmp" -cp "$CP" "$f" 2>&1 | grep -vE "Picked up|^Note:" | sed "s#^src/broken/##" >> "$OUT"
|
||||
echo >> "$OUT"
|
||||
done
|
||||
echo "wrote $OUT"
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
# A deliberately small text-substitution migration for exception handling.
|
||||
# Usage: scripts/migrate-regex.sh <project-dir> (edits the files in place: use a copy or a clean branch)
|
||||
# Needs GNU sed. It renames types and packages. It cannot see which catch blocks are now wrong.
|
||||
set -euo pipefail
|
||||
find "$1" -name '*.java' -print0 | xargs -0 sed -i -E \
|
||||
-e 's/com\.fasterxml\.jackson\.core\.JsonProcessingException/tools.jackson.core.JacksonException/g' \
|
||||
-e 's/com\.fasterxml\.jackson\.core\.JsonParseException/tools.jackson.core.exc.StreamReadException/g' \
|
||||
-e 's/com\.fasterxml\.jackson\.core\.JsonGenerationException/tools.jackson.core.exc.StreamWriteException/g' \
|
||||
-e 's/com\.fasterxml\.jackson\.core\.io\.JsonEOFException/tools.jackson.core.exc.UnexpectedEndOfInputException/g' \
|
||||
-e 's/com\.fasterxml\.jackson\.databind\.JsonMappingException/tools.jackson.databind.DatabindException/g' \
|
||||
-e 's/com\.fasterxml\.jackson\.(core|databind|dataformat|datatype|module)/tools.jackson.\1/g' \
|
||||
-e 's/\bJsonProcessingException\b/JacksonException/g' \
|
||||
-e 's/\bJsonParseException\b/StreamReadException/g' \
|
||||
-e 's/\bJsonGenerationException\b/StreamWriteException/g' \
|
||||
-e 's/\bJsonEOFException\b/UnexpectedEndOfInputException/g' \
|
||||
-e 's/\bJsonMappingException\b/DatabindException/g' \
|
||||
-e 's/\.getFieldName\(\)/.getPropertyName()/g'
|
||||
# jackson-annotations keeps the old package; the substitutions above never touch com.fasterxml.jackson.annotation.
|
||||
find "$1" -name pom.xml -print0 | xargs -0 sed -i -E \
|
||||
-e 's#<groupId>com\.fasterxml\.jackson\.core</groupId>#<groupId>tools.jackson.core</groupId>#' \
|
||||
-e 's#<version>2\.23\.0</version>#<version>3.3.0</version>#'
|
||||
Executable
+48
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env bash
|
||||
# Runs both migrations on throwaway copies of migration/before and records the diffs, the build
|
||||
# result and the behaviour tests. Writes output/30..36.
|
||||
set -uo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
MODULE=$PWD
|
||||
MVN="${MVN:-mvn} ${MVN_ARGS:-}"
|
||||
WORK=$(mktemp -d)
|
||||
clean() { grep -vE "Picked up|^WARNING" | sed "s#$WORK#<work>#g; s#$HOME#~#g"; }
|
||||
summary() { # compiler errors or the surefire verdict, without timings
|
||||
clean | grep -E '^\[ERROR\].*\.java|Tests run:|Tests in error|^\[ERROR\] |BUILD' | sed -E 's/, Time elapsed: [0-9.]+ s//; s/ -- Time elapsed.*(<<<.*)/ \1/' | head -30
|
||||
}
|
||||
|
||||
(cd migration/before && $MVN -B test 2>&1 | summary) > output/30-before-jackson2-tests.txt
|
||||
rm -rf migration/before/target
|
||||
|
||||
# --- A: OpenRewrite ---
|
||||
cp -r migration/before "$WORK/rewrite"
|
||||
(cd "$WORK/rewrite" && git init -q && git add -A && git -c user.email=x@x -c user.name=x commit -qm before \
|
||||
&& $MVN -B -U org.openrewrite.maven:rewrite-maven-plugin:6.46.1:run \
|
||||
-Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-jackson:1.29.0 \
|
||||
-Drewrite.activeRecipes=org.openrewrite.java.jackson.UpgradeJackson_2_3 > "$WORK/rewrite.log" 2>&1)
|
||||
grep -E 'BUILD|Changes have been made| org\.openrewrite' "$WORK/rewrite.log" | clean > output/31-openrewrite-run.txt
|
||||
(cd "$WORK/rewrite" && git diff --no-color -- pom.xml src) > output/32-openrewrite.diff.txt
|
||||
(cd "$WORK/rewrite" && $MVN -B test 2>&1 | summary) > output/33-openrewrite-tests.txt
|
||||
|
||||
# --- B: sed ---
|
||||
cp -r migration/before "$WORK/regex"
|
||||
(cd "$WORK/regex" && git init -q && git add -A && git -c user.email=x@x -c user.name=x commit -qm before)
|
||||
"$MODULE/scripts/migrate-regex.sh" "$WORK/regex"
|
||||
{
|
||||
echo "\$ scripts/migrate-regex.sh <copy>; mvn test"
|
||||
(cd "$WORK/regex" && $MVN -B test 2>&1 | summary)
|
||||
echo
|
||||
echo "\$ scripts/review-catches.sh <copy>"
|
||||
"$MODULE/scripts/review-catches.sh" "$WORK/regex"
|
||||
} > output/34-regex.txt
|
||||
# --- C: sed, then fix only what the compiler points at ---
|
||||
sed -i '/Order orDefault/,/Order load/ s/catch (IOException e)/catch (tools.jackson.core.JacksonException e)/' "$WORK/regex/src/main/java/com/ankurm/orders/OrderCodec.java"
|
||||
{
|
||||
echo "\$ # after fixing the single compile error (orDefault now catches JacksonException)"
|
||||
(cd "$WORK/regex" && $MVN -B test 2>&1 | summary)
|
||||
} > output/35-regex-after-compile-fix.txt
|
||||
|
||||
# --- D: the OpenRewrite result plus the hand fixes ---
|
||||
(cd migration/after && $MVN -B test 2>&1 | summary) > output/36-after-hand-fixes-tests.txt
|
||||
rm -rf migration/after/target
|
||||
echo "work dir kept at $WORK"
|
||||
Executable
+12
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
# Lists the places a rename cannot judge. Every hit needs a human look.
|
||||
# Usage: scripts/review-catches.sh <project-dir>
|
||||
set -uo pipefail
|
||||
cd "$1"
|
||||
files=$(grep -rlE 'tools\.jackson|com\.fasterxml\.jackson\.(core|databind)|OrderCodec' --include='*.java' src | sort)
|
||||
echo "-- catch/throws IOException in files that touch Jackson (or its wrappers)"
|
||||
grep -nE 'catch *\([^)]*(IOException)|throws [^{;]*IOException' $files
|
||||
echo "-- catch IllegalArgumentException (convertValue no longer wraps)"
|
||||
grep -nE 'catch *\([^)]*IllegalArgumentException' $files
|
||||
echo "-- generic catches that now also receive Jackson errors"
|
||||
grep -nE 'catch *\((final )?(RuntimeException|Exception|Throwable) ' $files || echo "(none)"
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env bash
|
||||
# Regenerates everything in output/. Run from anywhere; needs JDK 25 and Maven.
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
MVN="${MVN:-mvn} ${MVN_ARGS:-}"
|
||||
quiet() { grep -vE "Picked up|^WARNING" || true; }
|
||||
|
||||
build() { # $1 = Jackson 3 version
|
||||
$MVN -B -q clean compile dependency:build-classpath -Dmdep.outputFile=target/cp.txt -Djackson3.version="$1" 2>&1 | quiet
|
||||
CP="target/classes:$(cat target/cp.txt)"
|
||||
}
|
||||
run() { # $1 = output file, $2 = class
|
||||
java -cp "$CP" "com.ankurm.exceptions.$2" 2>&1 | quiet > "output/$1.txt"
|
||||
echo "wrote output/$1.txt"
|
||||
}
|
||||
|
||||
mkdir -p output
|
||||
: > output/10-missing-primitive-by-version.txt
|
||||
for v in 3.1.8 3.2.4; do
|
||||
build "$v"
|
||||
java -cp "$CP" com.ankurm.exceptions.E09MissingPrimitive 2>&1 | quiet >> output/10-missing-primitive-by-version.txt
|
||||
done
|
||||
build 3.3.0
|
||||
java -cp "$CP" com.ankurm.exceptions.E09MissingPrimitive 2>&1 | quiet >> output/10-missing-primitive-by-version.txt
|
||||
|
||||
run 01-hierarchy E01Hierarchy
|
||||
run 02-triggers E02Triggers
|
||||
run 03-silent-catch E03SilentCatch
|
||||
run 04-behaviour-diff E04BehaviourDiff
|
||||
run 05-location-and-path E05Location
|
||||
run 06-stack-traces E06StackTraces
|
||||
run 07-handling E07Handling
|
||||
run 08-custom-and-collect E08CustomAndCollect
|
||||
./scripts/broken.sh
|
||||
|
||||
# Spring Boot tests write output/20..24, the migration script writes output/30..36
|
||||
(cd boot && $MVN -B -q test > /dev/null)
|
||||
echo "wrote output/20..24"
|
||||
./scripts/migrate.sh
|
||||
Reference in New Issue
Block a user