From c9daf22377cc3ef42744e008d5c164e399bd64d8 Mon Sep 17 00:00:00 2001 From: "Artemii.Kononov" Date: Wed, 9 Nov 2022 12:00:02 +0300 Subject: [PATCH] [child-process] Setting Field.modifiers accessible fix. Moving child process debug switch to UtSettings. --- .../src/main/kotlin/org/utbot/common/Logging.kt | 11 ++++++++++- .../kotlin/org/utbot/common/ReflectionUtil.kt | 4 ++++ .../kotlin/org/utbot/framework/UtSettings.kt | 15 +++++++++++++++ .../kotlin/org/utbot/engine/ValueConstructor.kt | 4 ++-- .../framework/concrete/MockValueConstructor.kt | 4 ++-- .../kotlin/org/utbot/instrumentation/Settings.kt | 16 ---------------- .../process/ChildProcessRunner.kt | 2 +- 7 files changed, 34 insertions(+), 22 deletions(-) diff --git a/utbot-core/src/main/kotlin/org/utbot/common/Logging.kt b/utbot-core/src/main/kotlin/org/utbot/common/Logging.kt index fe94851557..381d7328b9 100644 --- a/utbot-core/src/main/kotlin/org/utbot/common/Logging.kt +++ b/utbot-core/src/main/kotlin/org/utbot/common/Logging.kt @@ -68,7 +68,7 @@ inline fun LoggerWithLogMethod.bracket( } } -inline fun KLogger.catch(block: () -> T): T? { +inline fun KLogger.catchException(block: () -> T): T? { return try { block() } catch (e: Throwable) { @@ -76,3 +76,12 @@ inline fun KLogger.catch(block: () -> T): T? { null } } + +inline fun KLogger.logException(block: () -> T): T { + return try { + block() + } catch (e: Throwable) { + this.error("Exception occurred", e) + throw e + } +} \ No newline at end of file diff --git a/utbot-core/src/main/kotlin/org/utbot/common/ReflectionUtil.kt b/utbot-core/src/main/kotlin/org/utbot/common/ReflectionUtil.kt index 0b55259a9b..5577e84a72 100644 --- a/utbot-core/src/main/kotlin/org/utbot/common/ReflectionUtil.kt +++ b/utbot-core/src/main/kotlin/org/utbot/common/ReflectionUtil.kt @@ -37,6 +37,10 @@ object Reflection { fun setModifiers(field: Field, modifiers: Int) { modifiersField.set(field, modifiers) } + + fun isModifiersAccessible(): Boolean { + return modifiersField.isAccessible + } } inline fun AccessibleObject.withAccessibility(block: () -> R): R { diff --git a/utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt b/utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt index b82cd8f879..2ac46750e2 100644 --- a/utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt +++ b/utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt @@ -291,6 +291,21 @@ object UtSettings : AbstractSettings( */ var runIdeaProcessWithDebug by getBooleanProperty(false) + /** + * If true, runs the child process with the ability to attach a debugger. + * + * To debug the child process, set the breakpoint in the childProcessRunner.start() line + * and in the child process's main function and run the main process. + * Then run the remote JVM debug configuration in IDEA. + * If you see the message in console about successful connection, then + * the debugger is attached successfully. + * Now you can put the breakpoints in the child process and debug + * both processes simultaneously. + * + * @see [org.utbot.instrumentation.process.ChildProcessRunner.cmds] + */ + var runChildProcessWithDebug by getBooleanProperty(false) + /** * Number of branch instructions using for clustering executions in the test minimization phase. */ diff --git a/utbot-framework/src/main/kotlin/org/utbot/engine/ValueConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/engine/ValueConstructor.kt index d044d0111e..3ecf330d53 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/engine/ValueConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/engine/ValueConstructor.kt @@ -1,5 +1,6 @@ package org.utbot.engine +import org.utbot.common.Reflection import org.utbot.common.invokeCatching import org.utbot.framework.plugin.api.ClassId import org.utbot.engine.util.lambda.CapturedArgument @@ -239,8 +240,7 @@ class ValueConstructor { try { declaredField.isAccessible = true - val modifiersField = Field::class.java.getDeclaredField("modifiers") - modifiersField.isAccessible = true + check(Reflection.isModifiersAccessible()) val target = mockTarget(fieldModel) { FieldMockTarget( diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/concrete/MockValueConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/concrete/MockValueConstructor.kt index b332124ac9..9b06437078 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/concrete/MockValueConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/concrete/MockValueConstructor.kt @@ -44,6 +44,7 @@ import kotlin.reflect.KClass import org.mockito.Mockito import org.mockito.stubbing.Answer import org.objectweb.asm.Type +import org.utbot.common.Reflection import org.utbot.engine.util.lambda.CapturedArgument import org.utbot.engine.util.lambda.constructLambda import org.utbot.engine.util.lambda.constructStaticLambda @@ -189,8 +190,7 @@ class MockValueConstructor( val accessible = declaredField.isAccessible declaredField.isAccessible = true - val modifiersField = Field::class.java.getDeclaredField("modifiers") - modifiersField.isAccessible = true + check(Reflection.isModifiersAccessible()) val target = mockTarget(fieldModel) { FieldMockTarget(fieldModel.classId.name, model.classId.name, UtConcreteValue(classInstance), fieldId.name) diff --git a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/Settings.kt b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/Settings.kt index ce290b11b6..045074a26f 100644 --- a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/Settings.kt +++ b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/Settings.kt @@ -14,21 +14,5 @@ object Settings { const val TRACE_ARRAY_SIZE: Int = 1 shl 20 - // TODO: maybe add this guide to confluence? - /** - * If true, runs the child process with the ability to attach a debugger. - * - * To debug the child process, set the breakpoint in the childProcessRunner.start() line - * and in the child process's main function and run the main process. - * Then run the remote JVM debug configuration in IDEA. - * If you see the message in console about successful connection, then - * the debugger is attached successfully. - * Now you can put the breakpoints in the child process and debug - * both processes simultaneously. - * - * @see [org.utbot.instrumentation.process.ChildProcessRunner.cmds] - */ - const val runChildProcessWithDebug = false - var defaultConcreteExecutorPoolSize = 10 } \ No newline at end of file diff --git a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/process/ChildProcessRunner.kt b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/process/ChildProcessRunner.kt index 0a6ac39fa1..1dafdef084 100644 --- a/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/process/ChildProcessRunner.kt +++ b/utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/process/ChildProcessRunner.kt @@ -20,7 +20,7 @@ class ChildProcessRunner { private val id = Random.nextLong() private var processSeqN = 0 private val cmds: List by lazy { - val debugCmd = listOfNotNull(DEBUG_RUN_CMD.takeIf { Settings.runChildProcessWithDebug }) + val debugCmd = listOfNotNull(DEBUG_RUN_CMD.takeIf { UtSettings.runChildProcessWithDebug }) val javaVersionSpecificArguments = OpenModulesContainer.javaVersionSpecificArguments val pathToJava = JdkInfoService.provide().path