Final Access Modifier Kotlin
See original GitHub issueHi,
First of very nice work. Nice UI very simple to go through. The alternatives are not even on par with this lib. This project could use some more recognition imho.
My question/bug: I don’t know if Recaf was only targeted for applications written in pure Java or not. However I’m facing an issue when trying to edit my access modifier of a Kotlin written .class
Consider the following Kotlin class
class Main {
fun main(args: Array<String>) {
println("Hello world from Kotlin")
}
}
Since every class in Kotlin is final
by default this Main
class is too.
Recaf successfully reports this aswell.
However upon removing the final
modifier, saving it and trying to extend it
class SomeExtendedClass : Main() {
fun someOtherMethod() {
println("Some Other Message")
}
}
Throws upon compilation:
SomeExtendedClass.kt:1:28: error: this type is final, so it cannot be inherited from class SomeExtendedClass : Main()
However the equivalent Java code does seem to do the trick.
final class Main {
public static void main(String[] args) {
System.out.println("Hello this is a Java world!");
}
}
So first of, is it possible to edit the byte code from a Kotlin class?
And if so how can I remove a final
modifier and make it open/public
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
No problem. I have tried your solution.
The
@Metadata
annotation is in the Visible annotations menu. However it is complete gibberish.I tried deleting it and exporting it. Whilst also setting the access to public
The JD-GUI decompiler now successfully says this class is a
public class
however in Android Studio the class is still final. I think deleting is not the solution. I probably need to edit the@Metadata
annotation to tell it this class should beopen
Edit I invalidated caches and restarted Android Studio. The class is public now.
Thanks a lot!
Kotlin uses an annotation called Metadata to store some extra information within the compiled class, and it’s used by the Kotlin compiler to determine a compiled Kotlin class’s actual modifier. Therefore, only change the modifiers specified in JVMS is not enough, you have to modify the data held in Metadata annotation too.
Here’s the difference between a
final
class’s metadata and aopen
class’s:According to the javadoc,
d1
is written in a custom binary-like format, so it’s impractical to edit that directly using a text field. But after some Googling, I found out there’s an official library for reading and modifying the Kotlin metadata, so maybe it can help with this problem.