|
| 1 | +/* |
| 2 | + * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.dv8tion.jda.tasks |
| 18 | + |
| 19 | +import org.gradle.api.DefaultTask |
| 20 | +import org.gradle.api.GradleException |
| 21 | +import org.gradle.api.file.ConfigurableFileCollection |
| 22 | +import org.gradle.api.provider.Property |
| 23 | +import org.gradle.api.tasks.CacheableTask |
| 24 | +import org.gradle.api.tasks.Input |
| 25 | +import org.gradle.api.tasks.InputFiles |
| 26 | +import org.gradle.api.tasks.PathSensitive |
| 27 | +import org.gradle.api.tasks.PathSensitivity |
| 28 | +import org.gradle.api.tasks.TaskAction |
| 29 | +import java.io.File |
| 30 | + |
| 31 | +@CacheableTask |
| 32 | +abstract class VerifyBytecodeVersion : DefaultTask() { |
| 33 | + @get:InputFiles |
| 34 | + @get:PathSensitive(PathSensitivity.RELATIVE) |
| 35 | + abstract val classes: ConfigurableFileCollection |
| 36 | + |
| 37 | + @get:Input |
| 38 | + abstract val expectedMajorVersion: Property<Int> |
| 39 | + |
| 40 | + @TaskAction |
| 41 | + fun verify() { |
| 42 | + val badFiles = classes.filter { it.readBytecodeVersion() != expectedMajorVersion.get() }.toList() |
| 43 | + |
| 44 | + if (badFiles.isNotEmpty()) { |
| 45 | + println("Found ${badFiles.size} files that did not have the bytecode version ${expectedMajorVersion.get()}") |
| 46 | + println("The following classes have the wrong bytecode version:") |
| 47 | + for (file in badFiles) { |
| 48 | + println("\t- ${file.path} has version ${file.readBytecodeVersion()}") |
| 49 | + } |
| 50 | + |
| 51 | + throw GradleException("Some compiled classes have the wrong bytecode version.") |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +private val validClassHeaderBytes = listOf(0xCA.toByte(), 0xFE.toByte(), 0xBA.toByte(), 0xBE.toByte()) |
| 57 | + |
| 58 | +private fun File.readBytecodeVersion(): Int? = inputStream().buffered().use { stream -> |
| 59 | + val header = stream.readNBytes(8) |
| 60 | + if (header.size < 8) { |
| 61 | + return null |
| 62 | + } |
| 63 | + |
| 64 | + if (header.take(4) != validClassHeaderBytes) { |
| 65 | + return null |
| 66 | + } |
| 67 | + |
| 68 | + // Big-endian major version: bytes[6..7] |
| 69 | + return ((header[6].toInt() and 0xFF) shl 8) or (header[7].toInt() and 0xFF) |
| 70 | +} |
0 commit comments