Minor. remove import package usage from jdiEval.kt, jdiValues.kt and jdiTest.kt

This commit is contained in:
Stanislav Erokhin
2015-09-07 14:33:42 +03:00
parent fb14979051
commit 9dee696847
3 changed files with 66 additions and 48 deletions
+22 -19
View File
@@ -16,20 +16,23 @@
package org.jetbrains.eval4j.jdi package org.jetbrains.eval4j.jdi
import com.sun.jdi import com.sun.jdi.*
import com.sun.jdi.Value as jdi_Value
import com.sun.jdi.Type as jdi_Type
import com.sun.jdi.ClassNotLoadedException import com.sun.jdi.ClassNotLoadedException
import com.sun.jdi.Method import com.sun.jdi.Method
import com.sun.jdi.ObjectReference import com.sun.jdi.ObjectReference
import org.jetbrains.eval4j.* import org.jetbrains.eval4j.*
import org.jetbrains.eval4j.Value
import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.Type
val CLASS = Type.getType(javaClass<Class<*>>()) val CLASS = Type.getType(javaClass<Class<*>>())
val BOOTSTRAP_CLASS_DESCRIPTORS = setOf("Ljava/lang/String;", "Ljava/lang/ClassLoader;", "Ljava/lang/Class;") val BOOTSTRAP_CLASS_DESCRIPTORS = setOf("Ljava/lang/String;", "Ljava/lang/ClassLoader;", "Ljava/lang/Class;")
public class JDIEval( public class JDIEval(
private val vm: jdi.VirtualMachine, private val vm: VirtualMachine,
private val classLoader: jdi.ClassLoaderReference?, private val classLoader: ClassLoaderReference?,
private val thread: jdi.ThreadReference, private val thread: ThreadReference,
private val invokePolicy: Int private val invokePolicy: Int
) : Eval { ) : Eval {
@@ -103,8 +106,8 @@ public class JDIEval(
listOf(value)).boolean listOf(value)).boolean
} }
fun Type.asReferenceType(): jdi.ReferenceType = loadClass(this).jdiClass!!.reflectedType() fun Type.asReferenceType(): ReferenceType = loadClass(this).jdiClass!!.reflectedType()
fun Type.asArrayType(): jdi.ArrayType = asReferenceType() as jdi.ArrayType fun Type.asArrayType(): ArrayType = asReferenceType() as ArrayType
override fun newArray(arrayType: Type, size: Int): Value { override fun newArray(arrayType: Type, size: Int): Value {
val jdiArrayType = arrayType.asArrayType() val jdiArrayType = arrayType.asArrayType()
@@ -134,7 +137,7 @@ public class JDIEval(
return fillArray(arrayType.arrayElementType, dimensionSizes[0], dimensionSizes.drop(1)) return fillArray(arrayType.arrayElementType, dimensionSizes[0], dimensionSizes.drop(1))
} }
private fun Value.array() = jdiObj.checkNull() as jdi.ArrayReference private fun Value.array() = jdiObj.checkNull() as ArrayReference
override fun getArrayLength(array: Value): Value { override fun getArrayLength(array: Value): Value {
return int(array.array().length()) return int(array.array().length())
@@ -158,7 +161,7 @@ public class JDIEval(
} }
} }
private fun findField(fieldDesc: FieldDescription): jdi.Field { private fun findField(fieldDesc: FieldDescription): Field {
val _class = fieldDesc.ownerType.asReferenceType() val _class = fieldDesc.ownerType.asReferenceType()
val field = _class.fieldByName(fieldDesc.name) val field = _class.fieldByName(fieldDesc.name)
if (field == null) { if (field == null) {
@@ -167,7 +170,7 @@ public class JDIEval(
return field return field
} }
private fun findStaticField(fieldDesc: FieldDescription): jdi.Field { private fun findStaticField(fieldDesc: FieldDescription): Field {
val field = findField(fieldDesc) val field = findField(fieldDesc)
if (!field.isStatic()) { if (!field.isStatic()) {
throwBrokenCodeException(NoSuchFieldError("Field is not static: $fieldDesc")) throwBrokenCodeException(NoSuchFieldError("Field is not static: $fieldDesc"))
@@ -188,7 +191,7 @@ public class JDIEval(
} }
val _class = field.declaringType() val _class = field.declaringType()
if (_class !is jdi.ClassType) { if (_class !is ClassType) {
throwBrokenCodeException(NoSuchFieldError("Can't a field in a non-class: $field")) throwBrokenCodeException(NoSuchFieldError("Can't a field in a non-class: $field"))
} }
@@ -196,9 +199,9 @@ public class JDIEval(
mayThrow { _class.setValue(field, jdiValue) } mayThrow { _class.setValue(field, jdiValue) }
} }
private fun findMethod(methodDesc: MethodDescription, _class: jdi.ReferenceType = methodDesc.ownerType.asReferenceType()): jdi.Method { private fun findMethod(methodDesc: MethodDescription, _class: ReferenceType = methodDesc.ownerType.asReferenceType()): Method {
val method = when (_class) { val method = when (_class) {
is jdi.ClassType -> { is ClassType -> {
val m = _class.concreteMethodByName(methodDesc.name, methodDesc.desc) val m = _class.concreteMethodByName(methodDesc.name, methodDesc.desc)
if (m == null) listOf() else listOf(m) if (m == null) listOf() else listOf(m)
} }
@@ -216,7 +219,7 @@ public class JDIEval(
throwBrokenCodeException(NoSuchMethodError("Method is not static: $methodDesc")) throwBrokenCodeException(NoSuchMethodError("Method is not static: $methodDesc"))
} }
val _class = method.declaringType() val _class = method.declaringType()
if (_class !is jdi.ClassType) throwBrokenCodeException(NoSuchMethodError("Static method is a non-class type: $method")) if (_class !is ClassType) throwBrokenCodeException(NoSuchMethodError("Static method is a non-class type: $method"))
val args = mapArguments(arguments, method.safeArgumentTypes()) val args = mapArguments(arguments, method.safeArgumentTypes())
args.disableCollection() args.disableCollection()
@@ -274,7 +277,7 @@ public class JDIEval(
if (invokespecial && methodDesc.name == "<init>") { if (invokespecial && methodDesc.name == "<init>") {
// Constructor call // Constructor call
val ctor = findMethod(methodDesc) val ctor = findMethod(methodDesc)
val _class = (instance as NewObjectValue).asmType.asReferenceType() as jdi.ClassType val _class = (instance as NewObjectValue).asmType.asReferenceType() as ClassType
val args = mapArguments(arguments, ctor.safeArgumentTypes()) val args = mapArguments(arguments, ctor.safeArgumentTypes())
args.disableCollection() args.disableCollection()
val result = mayThrow { _class.newInstance(thread, ctor, args, invokePolicy) } val result = mayThrow { _class.newInstance(thread, ctor, args, invokePolicy) }
@@ -302,23 +305,23 @@ public class JDIEval(
} }
} }
private fun List<jdi.Value?>.disableCollection() { private fun List<jdi_Value?>.disableCollection() {
forEach { (it as? ObjectReference)?.disableCollection() } forEach { (it as? ObjectReference)?.disableCollection() }
} }
private fun List<jdi.Value?>.enableCollection() { private fun List<jdi_Value?>.enableCollection() {
forEach { (it as? ObjectReference)?.enableCollection() } forEach { (it as? ObjectReference)?.enableCollection() }
} }
private fun mapArguments(arguments: List<Value>, expecetedTypes: List<jdi.Type>): List<jdi.Value?> { private fun mapArguments(arguments: List<Value>, expecetedTypes: List<jdi_Type>): List<jdi_Value?> {
return arguments.zip(expecetedTypes).map { return arguments.zip(expecetedTypes).map {
val (arg, expectedType) = it val (arg, expectedType) = it
arg.asJdiValue(vm, expectedType.asType()) arg.asJdiValue(vm, expectedType.asType())
} }
} }
private fun jdi.Method.safeArgumentTypes(): List<jdi.Type> { private fun Method.safeArgumentTypes(): List<jdi_Type> {
try { try {
return argumentTypes() return argumentTypes()
} }
@@ -343,7 +346,7 @@ fun <T> mayThrow(f: () -> T): T {
try { try {
return f() return f()
} }
catch (e: jdi.InvocationException) { catch (e: InvocationException) {
throw ThrownFromEvaluatedCodeException(e.exception().asValue()) throw ThrownFromEvaluatedCodeException(e.exception().asValue())
} }
} }
@@ -16,12 +16,25 @@
package org.jetbrains.eval4j.jdi package org.jetbrains.eval4j.jdi
import com.sun.jdi import com.sun.jdi.ClassObjectReference
import com.sun.jdi.VirtualMachine
import org.jetbrains.eval4j.* import org.jetbrains.eval4j.*
import org.jetbrains.org.objectweb.asm.Opcodes.ACC_STATIC import org.jetbrains.org.objectweb.asm.Opcodes.ACC_STATIC
import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.tree.MethodNode import org.jetbrains.org.objectweb.asm.tree.MethodNode
import org.jetbrains.org.objectweb.asm.tree.analysis.Frame import org.jetbrains.org.objectweb.asm.tree.analysis.Frame
import com.sun.jdi.BooleanValue as jdi_BooleanValue
import com.sun.jdi.ByteValue as jdi_ByteValue
import com.sun.jdi.CharValue as jdi_CharValue
import com.sun.jdi.DoubleValue as jdi_DoubleValue
import com.sun.jdi.FloatValue as jdi_FloatValue
import com.sun.jdi.IntegerValue as jdi_IntegerValue
import com.sun.jdi.LongValue as jdi_LongValue
import com.sun.jdi.ObjectReference as jdi_ObjectReference
import com.sun.jdi.ShortValue as jdi_ShortValue
import com.sun.jdi.Type as jdi_Type
import com.sun.jdi.Value as jdi_Value
import com.sun.jdi.VoidValue as jdi_VoidValue
public fun makeInitialFrame(methodNode: MethodNode, arguments: List<Value>): Frame<Value> { public fun makeInitialFrame(methodNode: MethodNode, arguments: List<Value>): Frame<Value> {
val isStatic = (methodNode.access and ACC_STATIC) != 0 val isStatic = (methodNode.access and ACC_STATIC) != 0
@@ -51,39 +64,39 @@ public fun makeInitialFrame(methodNode: MethodNode, arguments: List<Value>): Fra
class JDIFailureException(message: String?, cause: Throwable? = null): RuntimeException(message, cause) class JDIFailureException(message: String?, cause: Throwable? = null): RuntimeException(message, cause)
public fun jdi.ObjectReference?.asValue(): ObjectValue { public fun jdi_ObjectReference?.asValue(): ObjectValue {
return when (this) { return when (this) {
null -> NULL_VALUE null -> NULL_VALUE
else -> ObjectValue(this, type().asType()) else -> ObjectValue(this, type().asType())
} }
} }
public fun jdi.Value?.asValue(): Value { public fun jdi_Value?.asValue(): Value {
return when (this) { return when (this) {
null -> NULL_VALUE null -> NULL_VALUE
is jdi.VoidValue -> VOID_VALUE is jdi_VoidValue -> VOID_VALUE
is jdi.BooleanValue -> IntValue(intValue(), Type.BOOLEAN_TYPE) is jdi_BooleanValue -> IntValue(intValue(), Type.BOOLEAN_TYPE)
is jdi.ByteValue -> IntValue(intValue(), Type.BYTE_TYPE) is jdi_ByteValue -> IntValue(intValue(), Type.BYTE_TYPE)
is jdi.ShortValue -> IntValue(intValue(), Type.SHORT_TYPE) is jdi_ShortValue -> IntValue(intValue(), Type.SHORT_TYPE)
is jdi.CharValue -> IntValue(intValue(), Type.CHAR_TYPE) is jdi_CharValue -> IntValue(intValue(), Type.CHAR_TYPE)
is jdi.IntegerValue -> IntValue(intValue(), Type.INT_TYPE) is jdi_IntegerValue -> IntValue(intValue(), Type.INT_TYPE)
is jdi.LongValue -> LongValue(longValue()) is jdi_LongValue -> LongValue(longValue())
is jdi.FloatValue -> FloatValue(floatValue()) is jdi_FloatValue -> FloatValue(floatValue())
is jdi.DoubleValue -> DoubleValue(doubleValue()) is jdi_DoubleValue -> DoubleValue(doubleValue())
is jdi.ObjectReference -> this.asValue() is jdi_ObjectReference -> this.asValue()
else -> throw JDIFailureException("Unknown value: $this") else -> throw JDIFailureException("Unknown value: $this")
} }
} }
fun jdi.Type.asType(): Type = Type.getType(this.signature()) fun jdi_Type.asType(): Type = Type.getType(this.signature())
val Value.jdiObj: jdi.ObjectReference? val Value.jdiObj: jdi_ObjectReference?
get() = this.obj() as jdi.ObjectReference? get() = this.obj() as jdi_ObjectReference?
val Value.jdiClass: jdi.ClassObjectReference? val Value.jdiClass: ClassObjectReference?
get() = this.jdiObj as jdi.ClassObjectReference? get() = this.jdiObj as ClassObjectReference?
public fun Value.asJdiValue(vm: jdi.VirtualMachine, expectedType: Type): jdi.Value? { public fun Value.asJdiValue(vm: VirtualMachine, expectedType: Type): jdi_Value? {
return when (this) { return when (this) {
NULL_VALUE -> null NULL_VALUE -> null
VOID_VALUE -> vm.mirrorOfVoid() VOID_VALUE -> vm.mirrorOfVoid()
@@ -98,8 +111,8 @@ public fun Value.asJdiValue(vm: jdi.VirtualMachine, expectedType: Type): jdi.Val
is LongValue -> vm.mirrorOf(value) is LongValue -> vm.mirrorOf(value)
is FloatValue -> vm.mirrorOf(value) is FloatValue -> vm.mirrorOf(value)
is DoubleValue -> vm.mirrorOf(value) is DoubleValue -> vm.mirrorOf(value)
is ObjectValue -> value as jdi.ObjectReference is ObjectValue -> value as jdi_ObjectReference
is NewObjectValue -> this.obj() as jdi.ObjectReference is NewObjectValue -> this.obj() as jdi_ObjectReference
else -> throw JDIFailureException("Unknown value: $this") else -> throw JDIFailureException("Unknown value: $this")
} }
} }
@@ -17,7 +17,7 @@
package org.jetbrains.eval4j.jdi.test package org.jetbrains.eval4j.jdi.test
import org.jetbrains.eval4j.* import org.jetbrains.eval4j.*
import com.sun.jdi import com.sun.jdi.*
import junit.framework.TestSuite import junit.framework.TestSuite
import org.jetbrains.eval4j.test.buildTestSuite import org.jetbrains.eval4j.test.buildTestSuite
import junit.framework.TestCase import junit.framework.TestCase
@@ -30,11 +30,13 @@ import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.eval4j.test.getTestName import org.jetbrains.eval4j.test.getTestName
import com.sun.jdi.ObjectReference import com.sun.jdi.ObjectReference
import com.sun.jdi.event.BreakpointEvent
import com.sun.jdi.event.ClassPrepareEvent
val DEBUGEE_CLASS = javaClass<Debugee>() val DEBUGEE_CLASS = javaClass<Debugee>()
fun suite(): TestSuite { fun suite(): TestSuite {
val connectors = jdi.Bootstrap.virtualMachineManager().launchingConnectors() val connectors = Bootstrap.virtualMachineManager().launchingConnectors()
val connector = connectors[0] val connector = connectors[0]
println("Using connector $connector") println("Using connector $connector")
@@ -54,8 +56,8 @@ fun suite(): TestSuite {
req.enable() req.enable()
val latch = CountDownLatch(1) val latch = CountDownLatch(1)
var classLoader : jdi.ClassLoaderReference? = null var classLoader : ClassLoaderReference? = null
var thread : jdi.ThreadReference? = null var thread : ThreadReference? = null
Thread { Thread {
val eventQueue = vm.eventQueue() val eventQueue = vm.eventQueue()
@@ -63,7 +65,7 @@ fun suite(): TestSuite {
val eventSet = eventQueue.remove() val eventSet = eventQueue.remove()
for (event in eventSet.eventIterator()) { for (event in eventSet.eventIterator()) {
when (event) { when (event) {
is jdi.event.ClassPrepareEvent -> { is ClassPrepareEvent -> {
val _class = event.referenceType()!! val _class = event.referenceType()!!
if (_class.name() == debugeeName) { if (_class.name() == debugeeName) {
for (l in _class.allLineLocations()) { for (l in _class.allLineLocations()) {
@@ -78,7 +80,7 @@ fun suite(): TestSuite {
} }
} }
} }
is jdi.event.BreakpointEvent -> { is BreakpointEvent -> {
println("Suspended at: " + event.location()) println("Suspended at: " + event.location())
thread = event.thread() thread = event.thread()
@@ -122,7 +124,7 @@ fun suite(): TestSuite {
eval eval
) )
fun jdi.ObjectReference?.callToString(): String? { fun ObjectReference?.callToString(): String? {
if (this == null) return "null" if (this == null) return "null"
return (eval.invokeMethod( return (eval.invokeMethod(
this.asValue(), this.asValue(),
@@ -132,7 +134,7 @@ fun suite(): TestSuite {
"()Ljava/lang/String;", "()Ljava/lang/String;",
false false
), ),
listOf()).jdiObj as jdi.StringReference).value() listOf()).jdiObj as StringReference).value()
} }