Skip to content

Commit

Permalink
Merge pull request #3 from izhangzhihao/izhangzhihao-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
izhangzhihao authored Nov 10, 2023
2 parents c47f37f + ffa07e6 commit b234544
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This plugin allows removing all Kotlin @Metadata / @DebugMetadata / @SourceDebug
```kotlin
plugins {
kotlin("jvm")
id("io.github.izhangzhihao.unmeta") version "1.0.1"
id("io.github.izhangzhihao.unmeta") version "1.0.2"
}

unmeta {
Expand Down Expand Up @@ -43,7 +43,7 @@ gradle.taskGraph.whenReady {
```kotlin
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.8.21'
id "io.github.izhangzhihao.unmeta" version "1.0.1"
id "io.github.izhangzhihao.unmeta" version "1.0.2"
}

unmeta {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.izhangzhihao.unmeta.example

/**
* a tail-recursive function with keyword tailrec
*/
tailrec fun factorial(n: Long, a: Long = 1): Long {
return if (n == 1L) a
else factorial(n - 1, n * a)
}
2 changes: 1 addition & 1 deletion example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
kotlin("jvm")
id("io.github.izhangzhihao.unmeta") version "1.0.1"
id("io.github.izhangzhihao.unmeta") version "1.0.2"
}

unmeta {
Expand Down
2 changes: 1 addition & 1 deletion plugin-build/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ID=io.github.izhangzhihao.unmeta
VERSION=1.0.1
VERSION=1.0.2
GROUP=io.github.izhangzhihao
DISPLAY_NAME=Unmeta Kotlin classes plugin
DESCRIPTION=Remove @Metadata and @DebugMetadata annotation from your Kotlin classes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.izhangzhihao.unmeta

import org.gradle.api.logging.Logger
import org.objectweb.asm.AnnotationVisitor
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.Opcodes

class UnmetaClassVisitor(private val path: String, cv: ClassVisitor, private val logger: Logger) :
ClassVisitor(Opcodes.ASM9, cv), Opcodes {

var modified = false

override fun visitAnnotation(desc: String?, visible: Boolean): AnnotationVisitor? {
return when (desc) {
"Lkotlin/Metadata;" -> {
logger.debug("Removed @Metadata annotation from $path")
modified = true
null
}
"Lkotlin/coroutines/jvm/internal/DebugMetadata;" -> {
logger.debug("Removed @DebugMetadata annotation from $path")
modified = true
null
}
"Lkotlin/jvm/internal/SourceDebugExtension;" -> {
logger.debug("Removed @SourceDebugExtension annotation from $path")
modified = true
null
}
else -> {
super.visitAnnotation(desc, visible)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.izhangzhihao.unmeta

import org.gradle.api.Project
import org.gradle.api.provider.Property
import javax.inject.Inject

open class UnmetaExtension @Inject constructor(project: Project) {
private val objects = project.objects

val enable: Property<Boolean> = objects.property(Boolean::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.izhangzhihao.unmeta

import org.gradle.api.Plugin
import org.gradle.api.Project

class UnmetaPlugin : Plugin<Project> {

private lateinit var extension: UnmetaExtension

override fun apply(project: Project) {
extension = project.extensions.create("unmeta", UnmetaExtension::class.java, project)
val unmetaTaskTask = project.tasks.create("unmeta", UnmetaTask::class.java)
unmetaTaskTask.enable.set(extension.enable)
project.tasks.getByName("compileKotlin").finalizedBy(unmetaTaskTask)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.github.izhangzhihao.unmeta

import org.gradle.api.DefaultTask
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassWriter
import java.io.File

abstract class UnmetaTask : DefaultTask() {

init {
description = "Drop @Metadata & @DebugMetadata from kotlin classes"
group = "build"
}

@get:Input
@get:Option(option = "enable", description = "is unmeta enabled")
@get:Optional
abstract val enable: Property<Boolean>

@TaskAction
fun unmetaAction() {
if (!enable.get()) {
logger.warn("unmeta is disabled")
} else {
logger.info("Start drop @Metadata & @DebugMetadata from kotlin classes")
project.buildDir.listFiles()
?.forEach { file ->
if (file.isDirectory) {
dropMetadata(file)
}
}
}
}

private fun dropMetadata(file: File) {
file.walk()
.filter { it.path.contains("classes") && it.path.endsWith(".class") }
.forEach {
if (it.isFile) {
val sourceClassBytes = it.readBytes()
val classReader = ClassReader(sourceClassBytes)
val classWriter = ClassWriter(classReader, ClassWriter.COMPUTE_MAXS)
val unmetaClassVisitor = UnmetaClassVisitor(it.absolutePath, classWriter, logger)
classReader.accept(unmetaClassVisitor, ClassReader.SKIP_DEBUG)
if (unmetaClassVisitor.modified) {
it.writeBytes(classWriter.toByteArray())
}
}
}
}
}
2 changes: 1 addition & 1 deletion plugin-build/plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

dependencies {
implementation("org.ow2.asm:asm:9.4")
implementation("org.ow2.asm:asm:9.6")
implementation(gradleApi())

testImplementation(libs.junit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.Opcodes

class UnmetaClassVisitor(private val path: String, cv: ClassVisitor, private val logger: Logger) :
ClassVisitor(Opcodes.ASM7, cv), Opcodes {
ClassVisitor(Opcodes.ASM9, cv), Opcodes {

var modified = false

Expand Down

0 comments on commit b234544

Please sign in to comment.