From 3e26f205b39e2b65291764084fb3835674da1b8c Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Sun, 13 Oct 2013 11:53:07 +0400 Subject: [PATCH] Static method calls supported --- annotations/jdi/com/sun/jdi/annotations.xml | 6 ++++++ src/org/jetbrains/eval4j/jdi/jdiEval.kt | 24 ++++++++++++++++++++- src/org/jetbrains/eval4j/jdi/jdiValues.kt | 23 +++++++++++++++++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/annotations/jdi/com/sun/jdi/annotations.xml b/annotations/jdi/com/sun/jdi/annotations.xml index baac6359e80..67939f35ef4 100644 --- a/annotations/jdi/com/sun/jdi/annotations.xml +++ b/annotations/jdi/com/sun/jdi/annotations.xml @@ -5,12 +5,18 @@ + + + + + + diff --git a/src/org/jetbrains/eval4j/jdi/jdiEval.kt b/src/org/jetbrains/eval4j/jdi/jdiEval.kt index 73a39dcb563..727bf7f5c30 100644 --- a/src/org/jetbrains/eval4j/jdi/jdiEval.kt +++ b/src/org/jetbrains/eval4j/jdi/jdiEval.kt @@ -97,8 +97,30 @@ class JDIEval( _class.setValue(field, newValue.asJdiValue(vm)) } + private fun findMethod(methodDesc: MethodDescription): jdi.Method { + val _class = loadClass(methodDesc.ownerType).jdiClass!!.reflectedType() + val method = when (_class) { + is jdi.ClassType -> { + val m = _class.concreteMethodByName(methodDesc.name, methodDesc.desc) + if (m == null) listOf() else listOf(m) + } + else -> _class.methodsByName(methodDesc.name, methodDesc.desc) + } + if (method.isEmpty()) { + throwException(NoSuchMethodError("Method not found: $methodDesc")) + } + return method[0] + } + override fun invokeStaticMethod(methodDesc: MethodDescription, arguments: List): Value { - throw UnsupportedOperationException() + val method = findMethod(methodDesc) + if (!method.isStatic()) { + throwException(NoSuchMethodError("Method is not static: $methodDesc")) + } + val _class = method.declaringType() + if (_class !is jdi.ClassType) throwException(NoSuchMethodError("Static method is a non-class type: $method")) + + return _class.invokeMethod(thread, method, arguments.map {v -> v.asJdiValue(vm)}, 0).asValue() } override fun getField(instance: Value, fieldDesc: FieldDescription): Value { diff --git a/src/org/jetbrains/eval4j/jdi/jdiValues.kt b/src/org/jetbrains/eval4j/jdi/jdiValues.kt index f622516be18..73d7fe55a38 100644 --- a/src/org/jetbrains/eval4j/jdi/jdiValues.kt +++ b/src/org/jetbrains/eval4j/jdi/jdiValues.kt @@ -55,4 +55,25 @@ val Value.jdiObj: jdi.ObjectReference? get() = this.obj as jdi.ObjectReference? val Value.jdiClass: jdi.ClassObjectReference? - get() = this.jdiObj as jdi.ClassObjectReference? \ No newline at end of file + get() = this.jdiObj as jdi.ClassObjectReference? + +fun Value.asJdiValue(vm: jdi.VirtualMachine): jdi.Value? { + return when (this) { + NULL_VALUE -> null + VOID_VALUE -> vm.mirrorOfVoid() + is IntValue -> when (asmType) { + Type.BOOLEAN_TYPE -> vm.mirrorOf(boolean) + Type.BYTE_TYPE -> vm.mirrorOf(int.toByte()) + Type.SHORT_TYPE -> vm.mirrorOf(int.toShort()) + Type.CHAR_TYPE -> vm.mirrorOf(int.toChar()) + Type.INT_TYPE -> vm.mirrorOf(int) + else -> throw JDIFailureException("Unknown value type: $this") + } + is LongValue -> vm.mirrorOf(value) + is FloatValue -> vm.mirrorOf(value) + is DoubleValue -> vm.mirrorOf(value) + is ObjectValue -> value as jdi.ObjectReference + is NewObjectValue -> throw JDIFailureException("Illegal value: $this") + else -> throw JDIFailureException("Unknown value: $this") + } +} \ No newline at end of file