From d6685f6aef7edc92a14173b45fd62126acd58b28 Mon Sep 17 00:00:00 2001 From: Alexander Kriegisch Date: Sun, 6 Aug 2023 09:12:38 +0700 Subject: [PATCH] Improve error message in LazyClassGen.getJavaClassBytesIncludingReweavable No longer report "Unable to find ASM classes", if simply an ASM processing error occurred. In that case, report "Error processing class file". Relates to #250. Signed-off-by: Alexander Kriegisch --- .../org/aspectj/weaver/bcel/LazyClassGen.java | 22 ++++++++++++------- .../aspectj/weaver/bcel/asm/AsmDetector.java | 4 ++-- .../weaver/bcel/asm/StackMapAdder.java | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/weaver/src/main/java/org/aspectj/weaver/bcel/LazyClassGen.java b/weaver/src/main/java/org/aspectj/weaver/bcel/LazyClassGen.java index 61ebfb7cf..0a21db2c9 100644 --- a/weaver/src/main/java/org/aspectj/weaver/bcel/LazyClassGen.java +++ b/weaver/src/main/java/org/aspectj/weaver/bcel/LazyClassGen.java @@ -753,14 +753,20 @@ public final class LazyClassGen { // are required (unless turning off the verifier) if ((myGen.getMajor() == Constants.MAJOR_1_6 && world.shouldGenerateStackMaps()) || myGen.getMajor() > Constants.MAJOR_1_6) { if (!AsmDetector.isAsmAround) { - // Fix https://github.com/eclipse-aspectj/aspectj/issues/251, using "replace('Ä', 'Ö')" to avoid non-relocated - // class names to be embedded into the error message during compile time, making it end up in the class constant - // pool unwantedly, as this clashes with post-compile ASM package relocation. - final String errorMessage = "Unable to find ASM classes (" + - AsmDetector.CLASS_READER.replace('Ä', 'Ö') + ", " + AsmDetector.CLASS_VISITOR.replace('Ä', 'Ö') + ") " + - "for stackmap generation. Stackmap generation for woven code is required to avoid verify errors " + - "on a Java 1.7 or higher runtime."; - throw new BCException(errorMessage, AsmDetector.reasonAsmIsMissing); + if ( + AsmDetector.rootCause instanceof ClassNotFoundException || + AsmDetector.rootCause instanceof NoClassDefFoundError + ) { + // Fix https://github.com/eclipse-aspectj/aspectj/issues/251, using "replace('Ä', 'Ö')" to avoid + // non-relocated class names to be embedded into the error message during compile time, making it end up in + // the class constant pool unwantedly, as this clashes with post-compile ASM package relocation. + String errorMessage = "Unable to find ASM classes (" + + AsmDetector.CLASS_READER.replace('Ä', 'Ö') + ", " + AsmDetector.CLASS_VISITOR.replace('Ä', 'Ö') + ") " + + "for stackmap generation. Stackmap generation for woven code is required to avoid verify errors " + + "on a Java 1.7 or higher runtime."; + throw new BCException(errorMessage, AsmDetector.rootCause); + } + throw new BCException("Error processing class file", AsmDetector.rootCause); } wovenClassFileData = StackMapAdder.addStackMaps(world, myGen.getClassName(), wovenClassFileData); } diff --git a/weaver/src/main/java/org/aspectj/weaver/bcel/asm/AsmDetector.java b/weaver/src/main/java/org/aspectj/weaver/bcel/asm/AsmDetector.java index a12a5617e..09177511c 100644 --- a/weaver/src/main/java/org/aspectj/weaver/bcel/asm/AsmDetector.java +++ b/weaver/src/main/java/org/aspectj/weaver/bcel/asm/AsmDetector.java @@ -20,7 +20,7 @@ public class AsmDetector { public static final String CLASS_READER = "org.objectweb.asm.ClassReader"; public static final String CLASS_VISITOR = "org.objectweb.asm.ClassVisitor"; public static boolean isAsmAround; - public static Throwable reasonAsmIsMissing; + public static Throwable rootCause; static { try { @@ -30,7 +30,7 @@ public class AsmDetector { isAsmAround = true; } catch (Exception e) { isAsmAround = false; - reasonAsmIsMissing = e; + rootCause = e; } //System.out.println(isAsmAround ? "ASM detected" : "No ASM found"); } diff --git a/weaver/src/main/java/org/aspectj/weaver/bcel/asm/StackMapAdder.java b/weaver/src/main/java/org/aspectj/weaver/bcel/asm/StackMapAdder.java index ae22ed067..1c742cd08 100644 --- a/weaver/src/main/java/org/aspectj/weaver/bcel/asm/StackMapAdder.java +++ b/weaver/src/main/java/org/aspectj/weaver/bcel/asm/StackMapAdder.java @@ -49,7 +49,7 @@ public class StackMapAdder { System.err.println("AspectJ Internal Error: unable to add stackmap attributes to class '"+classname+"'. " + t.getMessage()); t.printStackTrace(); AsmDetector.isAsmAround = false; - AsmDetector.reasonAsmIsMissing = t; + AsmDetector.rootCause = t; return data; } } -- 2.39.5