mirror of
https://github.com/tiennm99/lombok.git
synced 2026-09-05 08:17:35 +00:00
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.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,7 +33,7 @@ import org.junit.Test;
|
||||
|
||||
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 class TestPostCompiler {
|
||||
|
||||
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"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user