Skip to content

Commit

Permalink
fix: i love gradle ^__^
Browse files Browse the repository at this point in the history
  • Loading branch information
broccolai committed Oct 17, 2023
1 parent c1f9941 commit 87d6060
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,4 @@ gradle-app.setting
# Custom Ignores
build.sh
.env
bukkit/run
6 changes: 6 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ subprojects {
processResources {
expand("version" to rootProject.version)
}

register("inspectDependencies") {
doLast {
inspectDependenciesForProject(project)
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/Configurations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ fun Project.setupShadowJar() {
exclude(dependency("org.checkerframework:"))
}

exclude("**/Utils21.class")

relocate(
rootProject.group,
"com.github.benmanes.caffeine",
Expand Down
61 changes: 61 additions & 0 deletions buildSrc/src/main/kotlin/DependencyInspection.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import java.io.File
import java.nio.ByteBuffer
import java.nio.ByteOrder


fun inspectDependenciesForProject(project: Project) {
val inspectConfig = createInspectionConfiguration(project)
val files = resolveDependencies(inspectConfig, project) ?: return

if (files.isEmpty()) {
println("No dependencies found for project ${project.name}.")
return
}

files.forEach { file ->
inspectClassFilesInJar(file, project)
}
}

fun createInspectionConfiguration(project: Project): Configuration {
return project.configurations.create("inspectConfig").apply {
extendsFrom(project.configurations.findByName("implementation"))
isCanBeResolved = true
}
}

fun resolveDependencies(configuration: Configuration, project: Project): Set<File>? {
return try {
configuration.resolve()
} catch (e: Exception) {
println("Could not resolve dependencies for project ${project.name}.")
null
}
}

fun inspectClassFilesInJar(file: File, project: Project) {
project.zipTree(file).visit {
if (name.endsWith(".class")) {
val majorVersion = extractMajorVersionFromClassFile(this.file)

if (majorVersion == 65) {
println("""
Warning: Class file in ${file.name} ${this.path}
uses Java class version $majorVersion (file version 65)
""".trimIndent())
}
}
}
}

fun extractMajorVersionFromClassFile(classFile: File): Int {
val classFileBytes = classFile.readBytes()
val byteBuffer = ByteBuffer.wrap(classFileBytes).order(ByteOrder.BIG_ENDIAN)

// Skip the first 4 bytes for the magic number, then read the next two for minor and another two for major
byteBuffer.position(4)
val minorVersion = byteBuffer.short.toInt()
return byteBuffer.short.toInt()
}

0 comments on commit 87d6060

Please sign in to comment.