Skip to content

Commit

Permalink
Change DebugUtil to allow Clazz instances
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjameshamilton authored and Oberon Swings committed Jan 20, 2023
1 parent 113a95f commit 5081fbe
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions base/src/main/kotlin/proguard/util/DebugUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package proguard.util

import proguard.analysis.cpa.jvm.cfa.JvmCfa
import proguard.classfile.ClassPool
import proguard.classfile.Clazz
import proguard.classfile.Method
import proguard.classfile.MethodSignature
import proguard.classfile.ProgramClass
import proguard.classfile.ProgramMethod
import proguard.classfile.attribute.Attribute
import proguard.classfile.attribute.visitor.AllAttributeVisitor
import proguard.classfile.attribute.visitor.AttributeNameFilter
import proguard.classfile.instruction.visitor.AllInstructionVisitor
import proguard.classfile.visitor.ClassPrinter
Expand All @@ -22,52 +24,52 @@ import java.io.StringWriter
object DebugUtil {

/**
* Get the bytecode of a particular [ProgramClass].
* Get the bytecode of a particular [Clazz].
*/
@JvmStatic
fun asString(clazz: ProgramClass): String {
fun asString(clazz: Clazz): String {
val sw = StringWriter()
val pw = PrintWriter(sw)
clazz.accept(ClassPrinter(pw))
return sw.toString()
}

/**
* Get the bytecode of a particular [ProgramMethod].
* Get the bytecode of a particular [Method].
*/
@JvmStatic
@JvmOverloads
fun asString(clazz: ProgramClass, method: ProgramMethod, verbose: Boolean = false): String {
fun asString(clazz: Clazz, method: Method, verbose: Boolean = false): String {
val sw = StringWriter()
val pw = PrintWriter(sw)
if (verbose) {
method.accept(clazz, ClassPrinter(pw))
} else {
method.attributesAccept(clazz, AttributeNameFilter(Attribute.CODE, AllInstructionVisitor(ClassPrinter(pw))))
method.accept(clazz, AllAttributeVisitor(AttributeNameFilter(Attribute.CODE, AllInstructionVisitor(ClassPrinter(pw)))))
}
return sw.toString()
}

/**
* Get the bytecode of a particular [ProgramMethod] from your [ClassPool].
* Get the bytecode of a particular [Method] from your [ClassPool].
*/
@JvmStatic
@JvmOverloads
fun asString(classPool: ClassPool, signature: MethodSignature, verbose: Boolean = false): String {
val clazz = classPool.getClass(signature.className)
check(clazz is ProgramClass) { "Program class " + clazz.name + " not found in class pool" }
check(clazz is Clazz) { "Class " + clazz.name + " not found in class pool" }
val method = clazz.findMethod(signature.method, signature.descriptor.toString())
check(method is ProgramMethod) { "Program method " + signature + " not found in class " + clazz.getName() }
check(method is Method) { "Method " + signature + " not found in class " + clazz.name }
return asString(clazz, method, verbose)
}

/**
* Get the bytecode of all methods in a [ProgramClass] from your [ClassPool].
* Get the bytecode of all methods in a [Class] from your [ClassPool].
*/
@JvmStatic
fun asString(classPool: ClassPool, className: String): String {
val clazz = classPool.getClass(className)
check(clazz is ProgramClass) { "Program class $className not found in class pool" }
check(clazz is Clazz) { "Class $className not found in class pool" }
return asString(clazz)
}

Expand Down

0 comments on commit 5081fbe

Please sign in to comment.