Fixed a minor bug in IClassFile implementation

This commit is contained in:
Akash Yadav
2022-08-27 22:08:59 +05:30
parent dfabf9a0e5
commit 02a54c928e
2 changed files with 18 additions and 4 deletions
@@ -19,6 +19,7 @@ package com.itsaky.androidide.classfile.internal
import com.itsaky.androidide.classfile.constants.IConstant
import com.itsaky.androidide.classfile.IClassFile
import com.itsaky.androidide.classfile.constants.ClassConstant
import com.itsaky.androidide.classfile.constants.Utf8Constant
/**
@@ -54,19 +55,29 @@ internal class ClassFile : IClassFile {
override fun getName(): String {
val entry = constantPool[thisClass]
if (entry !is Utf8Constant) {
if (entry !is ClassConstant) {
throw IllegalStateException("Invalid constant at index: '$thisClass'")
}
return entry.bytes.decodeToString()
val nameEntry = constantPool[entry.nameIndex]
if (nameEntry !is Utf8Constant) {
throw IllegalStateException("Invalid constant at index: '$thisClass'")
}
return nameEntry.bytes.decodeToString()
}
override fun getSuperClassName(): String {
val entry = constantPool[superClass]
if (entry !is Utf8Constant) {
if (entry !is ClassConstant) {
throw IllegalStateException("Invalid constant at index: '$superClass'")
}
return entry.bytes.decodeToString()
val nameEntry = constantPool[entry.nameIndex]
if (nameEntry !is Utf8Constant) {
throw IllegalStateException("Invalid constant at index: '$superClass'")
}
return nameEntry.bytes.decodeToString()
}
}
@@ -42,6 +42,9 @@ class ClassFileReaderTest {
assertThat(file.majorVersion).isEqualTo(55)
assertThat(file.minorVersion).isEqualTo(0)
assertThat(file.getName()).isEqualTo("io/github/rosemoe/sora/widget/CodeEditor")
assertThat(file.getSuperClassName()).isEqualTo("android/view/View")
assertThat(file.accessFlags and ClassFileConstants.ACC_PUBLIC).isNotEqualTo(0)
assertThat(file.accessFlags and ClassFileConstants.ACC_SUPER).isNotEqualTo(0)