[pr #2637] recent versions of ecj double-close the classfile stream, causing corrupt classfiles

Would crash with java.lang.ClassFormatError: Extra bytes at the end of class file de/lomboktest/Application
Fixes https://github.com/mplushnikov/lombok-intellij-plugin/issues/969

figuring out the problem was the hard work - credits to @Rawi01 for discovering this
This commit is contained in:
Reinier Zwitserloot committed 2020-12-04 04:17:05 +01:00
1 parent da2c793083
commit b586c543cd
1 file changed
+10
+10
View File
@@ -28,6 +28,7 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public final class PostCompiler {
private PostCompiler() {/* prevent instantiation*/};
@@ -67,8 +68,17 @@ public final class PostCompiler {
public static OutputStream wrapOutputStream(final OutputStream originalStream, final String fileName, final DiagnosticsReceiver diagnostics) throws IOException {
if (System.getProperty("lombok.disablePostCompiler", null) != null) return originalStream;
// close() can be called more than once and should be idempotent, therefore, ensure we never transform more than once.
final AtomicBoolean closed = new AtomicBoolean();
return new ByteArrayOutputStream() {
@Override public void close() throws IOException {
if (closed.getAndSet(true)) {
originalStream.close();
return;
}
// no need to call super
byte[] original = toByteArray();
byte[] copy = null;