More information when no containing field found
This commit is contained in:
@@ -39,6 +39,9 @@ import org.jetbrains.jet.renderer.DescriptorRenderer
|
|||||||
import org.jetbrains.jet.lang.resolve.calls.context.BasicCallResolutionContext
|
import org.jetbrains.jet.lang.resolve.calls.context.BasicCallResolutionContext
|
||||||
import org.jetbrains.jet.lang.psi.debugText.getDebugText
|
import org.jetbrains.jet.lang.psi.debugText.getDebugText
|
||||||
import org.jetbrains.jet.lang.resolve.calls.tasks.ResolutionCandidate
|
import org.jetbrains.jet.lang.resolve.calls.tasks.ResolutionCandidate
|
||||||
|
import java.lang.reflect.GenericDeclaration
|
||||||
|
import java.lang.reflect.Method
|
||||||
|
import java.lang.reflect.Constructor
|
||||||
|
|
||||||
class LazyOperationsLog(
|
class LazyOperationsLog(
|
||||||
val stringSanitizer: (String) -> String
|
val stringSanitizer: (String) -> String
|
||||||
@@ -109,7 +112,7 @@ class LazyOperationsLog(
|
|||||||
val data = record.data
|
val data = record.data
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
|
|
||||||
sb.append(data.field?.getName() ?: "<name not found>")
|
sb.append(data.field?.getName() ?: "in ${data.lambdaCreatedIn.getDeclarationName()}")
|
||||||
|
|
||||||
if (!data.arguments.isEmpty()) {
|
if (!data.arguments.isEmpty()) {
|
||||||
sb.append(data.arguments.map { render(it) }.join(", ", "(", ")"))
|
sb.append(data.arguments.map { render(it) }.join(", ", "(", ")"))
|
||||||
@@ -209,4 +212,13 @@ private fun Printer.indent(body: Printer.() -> Unit): Printer {
|
|||||||
body()
|
body()
|
||||||
popIndent()
|
popIndent()
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun GenericDeclaration?.getDeclarationName(): String? {
|
||||||
|
return when (this) {
|
||||||
|
is Class<*> -> getName().substringAfterLast(".")
|
||||||
|
is Method -> getDeclaringClass().getDeclarationName() + "::" + getName() + "()"
|
||||||
|
is Constructor<*> -> getDeclaringClass().getDeclarationName() + "::" + getName() + "()"
|
||||||
|
else -> "<no name>"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -58,12 +58,12 @@ public class LoggingStorageManager(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun computeCallerData(lambda: Any, wrapper: Any, arguments: List<Any?>, result: Any?): CallData {
|
private fun computeCallerData(lambda: Any, wrapper: Any, arguments: List<Any?>, result: Any?): CallData {
|
||||||
val jClass = lambda.javaClass
|
val lambdaClass = lambda.javaClass
|
||||||
|
|
||||||
val outerClass: Class<out Any?>? = jClass.getEnclosingClass()
|
val outerClass: Class<out Any?>? = lambdaClass.getEnclosingClass()
|
||||||
|
|
||||||
// fields named "this" or "this$0"
|
// fields named "this" or "this$0"
|
||||||
val referenceToOuter = jClass.getAllDeclaredFields().firstOrNull {
|
val referenceToOuter = lambdaClass.getAllDeclaredFields().firstOrNull {
|
||||||
field ->
|
field ->
|
||||||
field.getType() == outerClass && field.getName()!!.contains("this")
|
field.getType() == outerClass && field.getName()!!.contains("this")
|
||||||
}
|
}
|
||||||
@@ -71,6 +71,11 @@ public class LoggingStorageManager(
|
|||||||
|
|
||||||
val outerInstance = referenceToOuter?.get(lambda)
|
val outerInstance = referenceToOuter?.get(lambda)
|
||||||
|
|
||||||
|
fun Class<*>.findFunctionField(): Field? {
|
||||||
|
return this.getAllDeclaredFields().firstOrNull {
|
||||||
|
it.getType()?.getName()?.startsWith("kotlin.Function") ?: false
|
||||||
|
}
|
||||||
|
}
|
||||||
val containingField = if (outerInstance == null) null
|
val containingField = if (outerInstance == null) null
|
||||||
else outerClass?.getAllDeclaredFields()?.firstOrNull {
|
else outerClass?.getAllDeclaredFields()?.firstOrNull {
|
||||||
(field): Boolean ->
|
(field): Boolean ->
|
||||||
@@ -79,10 +84,7 @@ public class LoggingStorageManager(
|
|||||||
if (value == null) return@firstOrNull false
|
if (value == null) return@firstOrNull false
|
||||||
|
|
||||||
val valueClass = value.javaClass
|
val valueClass = value.javaClass
|
||||||
|
val functionField = valueClass.findFunctionField()
|
||||||
val functionField = valueClass.getAllDeclaredFields().firstOrNull {
|
|
||||||
it.getType()?.getName()?.startsWith("kotlin.Function") ?: false
|
|
||||||
}
|
|
||||||
if (functionField == null) return@firstOrNull false
|
if (functionField == null) return@firstOrNull false
|
||||||
|
|
||||||
functionField.setAccessible(true)
|
functionField.setAccessible(true)
|
||||||
@@ -90,11 +92,26 @@ public class LoggingStorageManager(
|
|||||||
functionValue == wrapper
|
functionValue == wrapper
|
||||||
}
|
}
|
||||||
|
|
||||||
val enclosingEntity = jClass.getEnclosingConstructor()
|
if (containingField == null) {
|
||||||
?: jClass.getEnclosingMethod()
|
val wrappedLambdaField = lambdaClass.findFunctionField()
|
||||||
?: jClass.getEnclosingClass()
|
if (wrappedLambdaField != null) {
|
||||||
|
wrappedLambdaField.setAccessible(true)
|
||||||
|
val wrappedLambda = wrappedLambdaField.get(lambda)
|
||||||
|
return CallData(outerInstance, null, enclosingEntity(wrappedLambda.javaClass), arguments, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return CallData(outerInstance, containingField, enclosingEntity as GenericDeclaration?, arguments, result)
|
val enclosingEntity = enclosingEntity(lambdaClass)
|
||||||
|
|
||||||
|
return CallData(outerInstance, containingField, enclosingEntity, arguments, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun enclosingEntity(_class: Class<Any>): GenericDeclaration? {
|
||||||
|
val result = _class.getEnclosingConstructor()
|
||||||
|
?: _class.getEnclosingMethod()
|
||||||
|
?: _class.getEnclosingClass()
|
||||||
|
|
||||||
|
return result as GenericDeclaration?
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Class<*>.getAllDeclaredFields(): List<Field> {
|
private fun Class<*>.getAllDeclaredFields(): List<Field> {
|
||||||
|
|||||||
Reference in New Issue
Block a user