Skip to content

Commit

Permalink
Merge pull request projectlombok#3602 from oleludwig/fix_sneak_throws…
Browse files Browse the repository at this point in the history
…_and_prevent_null_analysis_remover

Fix removal of SneakyThrows and PreventNullAnalysis from bytecode
  • Loading branch information
Rawi01 authored Feb 7, 2024
2 parents f3a4b1b + 0479529 commit 8bea251
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Mateusz Matela <[email protected]>
Michael Dardis <[email protected]>
Michael Ernst <[email protected]>
Michiel Verheul <[email protected]>
Ole Ludwig <[email protected]>
Pascal Bihler <[email protected]>
Peter Grant <[email protected]>
Philipp Eichhorn <[email protected]>
Expand Down
6 changes: 5 additions & 1 deletion src/core/lombok/bytecode/FixedClassWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,4 +44,4 @@ class FixedClassWriter extends ClassWriter {
return "java/lang/Object";
}
}
}
}
2 changes: 1 addition & 1 deletion src/core/lombok/bytecode/PreventNullAnalysisRemover.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/core/lombok/bytecode/SneakyThrowsRemover.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
15 changes: 15 additions & 0 deletions test/bytecode/resource/PostCompilePreventNullAnalysis.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}

}
28 changes: 27 additions & 1 deletion test/bytecode/src/lombok/bytecode/TestPostCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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"));
}
}

0 comments on commit 8bea251

Please sign in to comment.