Wrap ASM exception with jetifier's one.
So that AGP can handle it.
Bug: b/140747218
Test: Added
Change-Id: Idf93bc27ef1592468bad8cf3ca6a2b264f448009
diff --git a/jetifier/jetifier/processor/src/main/kotlin/com/android/tools/build/jetifier/processor/transform/bytecode/ByteCodeTransformer.kt b/jetifier/jetifier/processor/src/main/kotlin/com/android/tools/build/jetifier/processor/transform/bytecode/ByteCodeTransformer.kt
index 58301ec..c7702aa 100644
--- a/jetifier/jetifier/processor/src/main/kotlin/com/android/tools/build/jetifier/processor/transform/bytecode/ByteCodeTransformer.kt
+++ b/jetifier/jetifier/processor/src/main/kotlin/com/android/tools/build/jetifier/processor/transform/bytecode/ByteCodeTransformer.kt
@@ -28,7 +28,6 @@
class ByteCodeTransformer internal constructor(
private val context: TransformationContext
) : Transformer {
-
// Does not yet support single bytecode file transformation, file has to be within archive.
override fun canTransform(file: ArchiveFile) = file.isClassFile() && !file.isSingleFile
@@ -37,7 +36,11 @@
val writer = ClassWriter(0 /* flags */)
val remapper = CoreRemapperImpl(context, writer)
- reader.accept(remapper.classRemapper, 0 /* flags */)
+ try {
+ reader.accept(remapper.classRemapper, 0 /* flags */)
+ } catch (e: ArrayIndexOutOfBoundsException) {
+ throw InvalidByteCodeException("Error processing '${file.relativePath}' bytecode.", e)
+ }
if (!remapper.changesDone) {
file.setNewDataSilently(writer.toByteArray())
@@ -47,4 +50,13 @@
file.updateRelativePath(remapper.rewritePath(file.relativePath))
}
-}
\ No newline at end of file
+}
+
+/**
+ * Thrown when rewriting a library with bytecode that can't be processed via ASM.
+ */
+// Happens for instance in b/140747218
+class InvalidByteCodeException(
+ message: String,
+ exception: Throwable
+) : Exception(message, exception)
\ No newline at end of file
diff --git a/jetifier/jetifier/processor/src/test/kotlin/com/android/tools/build/jetifier/processor/transform/bytecode/ByteCodeTransformerTest.kt b/jetifier/jetifier/processor/src/test/kotlin/com/android/tools/build/jetifier/processor/transform/bytecode/ByteCodeTransformerTest.kt
new file mode 100644
index 0000000..65ee514
--- /dev/null
+++ b/jetifier/jetifier/processor/src/test/kotlin/com/android/tools/build/jetifier/processor/transform/bytecode/ByteCodeTransformerTest.kt
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.tools.build.jetifier.processor.transform.bytecode
+
+import com.android.tools.build.jetifier.core.config.Config
+import com.android.tools.build.jetifier.processor.FileMapping
+import com.android.tools.build.jetifier.processor.Processor
+import org.junit.Test
+import java.io.File
+
+class ByteCodeTransformerTest {
+ @Test(expected = InvalidByteCodeException::class)
+ fun malformedBytecode_shouldThrowException() {
+ val processor = Processor.createProcessor3(config = Config.EMPTY)
+ processor.transform2(
+ input = setOf(
+ FileMapping(
+ File(javaClass
+ .getResource("/malformedBytecodeTest/malformedBytecodeArchive.zip").file),
+ File("test")
+ )
+ )
+ )
+ }
+}
\ No newline at end of file
diff --git a/jetifier/jetifier/processor/src/test/resources/malformedBytecodeTest/malformedBytecodeArchive.zip b/jetifier/jetifier/processor/src/test/resources/malformedBytecodeTest/malformedBytecodeArchive.zip
new file mode 100644
index 0000000..6bdda32
--- /dev/null
+++ b/jetifier/jetifier/processor/src/test/resources/malformedBytecodeTest/malformedBytecodeArchive.zip
Binary files differ