From 496acffd7d451a73564b7cd993944cf30c454665 Mon Sep 17 00:00:00 2001 From: Ole Ludwig Date: Sun, 4 Feb 2024 15:21:01 +0100 Subject: [PATCH 1/2] Fix removal of SneakyThrows and PreventNullAnalysis from bytecode The SneakyThrowsRemover and PreventNullAnalysisRemover were leaving traces of the calls to lombok.Lombok.sneakyThrow and lombok.Lombok.preventNullAnalysis behind because they were using a constructor of ClassWriter which reuses the constant pool of the original class for performance optimizations. So although the calls to sneakyThrows and preventNullAnalysis get removed from the methods there were still entries in the constant pool for them. The constant pool will be regenerated with this change so that the entries for sneakyThrows and preventNullAnalysis gets removed. --- .../lombok/bytecode/FixedClassWriter.java | 6 +++- .../bytecode/PreventNullAnalysisRemover.java | 2 +- .../lombok/bytecode/SneakyThrowsRemover.java | 2 +- .../PostCompilePreventNullAnalysis.java | 15 ++++++++++ .../src/lombok/bytecode/TestPostCompiler.java | 28 ++++++++++++++++++- 5 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 test/bytecode/resource/PostCompilePreventNullAnalysis.java diff --git a/src/core/lombok/bytecode/FixedClassWriter.java b/src/core/lombok/bytecode/FixedClassWriter.java index f18dc3a4b2..6d86fe9d5b 100644 --- a/src/core/lombok/bytecode/FixedClassWriter.java +++ b/src/core/lombok/bytecode/FixedClassWriter.java @@ -28,6 +28,10 @@ class FixedClassWriter extends ClassWriter { FixedClassWriter(ClassReader classReader, int flags) { super(classReader, flags); } + + FixedClassWriter(int flags) { + super(flags); + } @Override protected String getCommonSuperClass(String type1, String type2) { //By default, ASM will attempt to live-load the class types, which will fail if meddling with classes in an @@ -40,4 +44,4 @@ class FixedClassWriter extends ClassWriter { return "java/lang/Object"; } } -} \ No newline at end of file +} diff --git a/src/core/lombok/bytecode/PreventNullAnalysisRemover.java b/src/core/lombok/bytecode/PreventNullAnalysisRemover.java index 8ae7af5b48..654a308a71 100644 --- a/src/core/lombok/bytecode/PreventNullAnalysisRemover.java +++ b/src/core/lombok/bytecode/PreventNullAnalysisRemover.java @@ -44,7 +44,7 @@ public class PreventNullAnalysisRemover implements PostCompilerTransformation { byte[] fixedByteCode = fixJSRInlining(original); ClassReader reader = new ClassReader(fixedByteCode); - ClassWriter writer = new FixedClassWriter(reader, 0); + ClassWriter writer = new FixedClassWriter(0); final AtomicBoolean changesMade = new AtomicBoolean(); diff --git a/src/core/lombok/bytecode/SneakyThrowsRemover.java b/src/core/lombok/bytecode/SneakyThrowsRemover.java index ea1c3cec11..b716245db0 100644 --- a/src/core/lombok/bytecode/SneakyThrowsRemover.java +++ b/src/core/lombok/bytecode/SneakyThrowsRemover.java @@ -46,7 +46,7 @@ public class SneakyThrowsRemover implements PostCompilerTransformation { byte[] fixedByteCode = fixJSRInlining(original); ClassReader reader = new ClassReader(fixedByteCode); - ClassWriter writer = new ClassWriter(reader, 0); + ClassWriter writer = new ClassWriter(0); final AtomicBoolean changesMade = new AtomicBoolean(); diff --git a/test/bytecode/resource/PostCompilePreventNullAnalysis.java b/test/bytecode/resource/PostCompilePreventNullAnalysis.java new file mode 100644 index 0000000000..0c9b387b50 --- /dev/null +++ b/test/bytecode/resource/PostCompilePreventNullAnalysis.java @@ -0,0 +1,15 @@ +public class PostCompilePreventNullAnalysis { + public void test() { + Object o = "Hello World!"; + try { + System.out.println(o); + } finally { + if (o != null) { + if (lombok.Lombok.preventNullAnalysis(o) != null) { + o.toString(); + } + } + } + } + +} diff --git a/test/bytecode/src/lombok/bytecode/TestPostCompiler.java b/test/bytecode/src/lombok/bytecode/TestPostCompiler.java index 2704ecec8e..3814e00df8 100644 --- a/test/bytecode/src/lombok/bytecode/TestPostCompiler.java +++ b/test/bytecode/src/lombok/bytecode/TestPostCompiler.java @@ -33,7 +33,7 @@ public class TestPostCompiler { @Test - public void testPostCompiler() throws IOException { + public void testPostCompilerSneakyThrows() { byte[] compiled = TestClassFileMetaData.compile(new File("test/bytecode/resource/PostCompileSneaky.java")); DiagnosticsReceiver receiver = new DiagnosticsReceiver() { @Override public void addWarning(String message) { @@ -50,5 +50,31 @@ public void testPostCompiler() throws IOException { assertNotSame("Post-compiler did not do anything; we expected it to remove a Lombok.sneakyThrow() call.", compiled, transformed); assertTrue("After removing a sneakyThrow the classfile got... bigger (or stayed equal in size). Huh?", transformed.length < compiled.length); + + assertFalse("After post compilation, expected no lombok.Lombok.sneakyThrow() call in compiled code, but it's there", + new ClassFileMetaData(transformed).usesMethod("lombok/Lombok", "sneakyThrow")); + } + + @Test + public void testPostCompilerPreventNullAnalysis() { + byte[] compiled = TestClassFileMetaData.compile(new File("test/bytecode/resource/PostCompilePreventNullAnalysis.java")); + DiagnosticsReceiver receiver = new DiagnosticsReceiver() { + @Override public void addWarning(String message) { + fail("Warning during post compilation processing of a sneakyThrow call: " + message); + } + + @Override public void addError(String message) { + fail("Error during post compilation processing of a sneakyThrow call: " + message); + } + }; + assertTrue("Before post compilation, expected lombok.Lombok.preventNullAnalysis() call in compiled code, but it's not there", + new ClassFileMetaData(compiled).usesMethod("lombok/Lombok", "preventNullAnalysis")); + byte[] transformed = PostCompiler.applyTransformations(compiled, "PostCompilePreventNullAnalysis.java", receiver); + + assertNotSame("Post-compiler did not do anything; we expected it to remove a Lombok.preventNullAnalysis() call.", compiled, transformed); + assertTrue("After removing a sneakyThrow the classfile got... bigger (or stayed equal in size). Huh?", transformed.length < compiled.length); + + assertFalse("After post compilation, expected no lombok.Lombok.preventNullAnalysis() call in compiled code, but it's there", + new ClassFileMetaData(transformed).usesMethod("lombok/Lombok", "preventNullAnalysis")); } } From 04795298c182366f8716d0e79a44d995afccee24 Mon Sep 17 00:00:00 2001 From: Ole Ludwig Date: Wed, 7 Feb 2024 19:00:39 +0100 Subject: [PATCH 2/2] Update authors file --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index b291072f7c..7113176428 100755 --- a/AUTHORS +++ b/AUTHORS @@ -34,6 +34,7 @@ Mateusz Matela Michael Dardis Michael Ernst Michiel Verheul +Ole Ludwig Pascal Bihler Peter Grant Philipp Eichhorn