From a6f8cf9ca83a23b6e738e57335d93f889ff38ef2 Mon Sep 17 00:00:00 2001 From: Zarina Kurbatova Date: Thu, 6 Oct 2022 00:17:22 +0300 Subject: [PATCH 1/2] Wrap method reference into @link tag only if the invoked method is not private --- .../comment/CustomJavaDocCommentBuilder.kt | 3 +- .../summary/comment/SimpleCommentBuilder.kt | 40 +++++++++++++++---- .../SymbolicExecutionClusterCommentBuilder.kt | 6 ++- .../comment/SimpleCommentBuilderTest.kt | 4 +- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/utbot-summary/src/main/kotlin/org/utbot/summary/comment/CustomJavaDocCommentBuilder.kt b/utbot-summary/src/main/kotlin/org/utbot/summary/comment/CustomJavaDocCommentBuilder.kt index db6a0249f9..04e412a3c7 100644 --- a/utbot-summary/src/main/kotlin/org/utbot/summary/comment/CustomJavaDocCommentBuilder.kt +++ b/utbot-summary/src/main/kotlin/org/utbot/summary/comment/CustomJavaDocCommentBuilder.kt @@ -30,7 +30,8 @@ class CustomJavaDocCommentBuilder( val methodReference = getMethodReference( currentMethod.declaringClass.name, currentMethod.name, - currentMethod.parameterTypes + currentMethod.parameterTypes, + false ) val classReference = getClassReference(currentMethod.declaringClass.javaStyleName) diff --git a/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt b/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt index d085a5d3f0..8d86ea777e 100644 --- a/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt +++ b/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt @@ -194,7 +194,10 @@ open class SimpleCommentBuilder( sentenceInvoke.squashStmtText() if (!sentenceInvoke.isEmpty()) { sentenceBlock.invokeSentenceBlock = - Pair(getMethodReference(className, methodName, methodParameterTypes), sentenceInvoke) + Pair( + getMethodReference(className, methodName, methodParameterTypes, invokeSootMethod.isPrivate), + sentenceInvoke + ) createNextBlock = true invokeRegistered = true } @@ -302,7 +305,15 @@ open class SimpleCommentBuilder( val className = stmt.invokeExpr.methodRef.declaringClass.name val methodName = stmt.invokeExpr.method.name val methodParameterTypes = stmt.invokeExpr.method.parameterTypes - addTextInvoke(sentenceBlock, className, methodName, methodParameterTypes, frequency) + val isPrivate = stmt.invokeExpr.method.isPrivate + addTextInvoke( + sentenceBlock, + className, + methodName, + methodParameterTypes, + isPrivate, + frequency + ) } } @@ -314,13 +325,14 @@ open class SimpleCommentBuilder( className: String, methodName: String, methodParameterTypes: List, + isPrivate: Boolean, frequency: Int ) { if (!shouldSkipInvoke(methodName)) sentenceBlock.stmtTexts.add( StmtDescription( StmtType.Invoke, - getMethodReference(className, methodName, methodParameterTypes), + getMethodReference(className, methodName, methodParameterTypes, isPrivate), frequency ) ) @@ -345,21 +357,33 @@ open class SimpleCommentBuilder( } /** - * Returns a reference to the invoked method. + * Returns a reference to the invoked method. IDE can't resolve references to private methods in comments, + * so we add @link tag only if the invoked method is not private. * * It looks like {@link packageName.className#methodName(type1, type2)}. * * In case when an enclosing class in nested, we need to replace '$' with '.' * to render the reference. */ - fun getMethodReference(className: String, methodName: String, methodParameterTypes: List): String { + fun getMethodReference( + className: String, + methodName: String, + methodParameterTypes: List, + isPrivate: Boolean + ): String { val prettyClassName: String = className.replace("$", ".") - return if (methodParameterTypes.isEmpty()) { - "{@link $prettyClassName#$methodName()}" + val text = if (methodParameterTypes.isEmpty()) { + "$prettyClassName#$methodName()" } else { val methodParametersAsString = methodParameterTypes.joinToString(",") - "{@link $prettyClassName#$methodName($methodParametersAsString)}" + "$prettyClassName#$methodName($methodParametersAsString)" + } + + return if (isPrivate) { + text + } else { + "{@link $text}" } } diff --git a/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SymbolicExecutionClusterCommentBuilder.kt b/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SymbolicExecutionClusterCommentBuilder.kt index 97663ff8b3..76f760d66f 100644 --- a/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SymbolicExecutionClusterCommentBuilder.kt +++ b/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SymbolicExecutionClusterCommentBuilder.kt @@ -90,12 +90,16 @@ class SymbolicExecutionClusterCommentBuilder( val className = invokeSootMethod.declaringClass.name val methodName = invokeSootMethod.name val methodParameterTypes = invokeSootMethod.parameterTypes + val isPrivate = invokeSootMethod.isPrivate val sentenceInvoke = SimpleSentenceBlock(stringTemplates = StringsTemplatesPlural()) buildSentenceBlock(invoke, sentenceInvoke, invokeSootMethod) sentenceInvoke.squashStmtText() if (!sentenceInvoke.isEmpty()) { sentenceBlock.invokeSentenceBlock = - Pair(getMethodReference(className, methodName, methodParameterTypes), sentenceInvoke) + Pair( + getMethodReference(className, methodName, methodParameterTypes, isPrivate), + sentenceInvoke + ) createNextBlock = true invokeRegistered = true } diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt index ba1d82e02b..738adb39be 100644 --- a/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt @@ -67,7 +67,7 @@ class SimpleCommentBuilderTest { @Test fun `builds inline link for method`() { val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst) - val methodReference = commentBuilder.getMethodReference("org.utbot.ClassName", "methodName", listOf()) + val methodReference = commentBuilder.getMethodReference("org.utbot.ClassName", "methodName", listOf(), false) val expectedMethodReference = "{@link org.utbot.ClassName#methodName()}" assertEquals(methodReference, expectedMethodReference) } @@ -76,7 +76,7 @@ class SimpleCommentBuilderTest { fun `builds inline link for method in nested class`() { val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst) val methodReference = - commentBuilder.getMethodReference("org.utbot.ClassName\$NestedClassName", "methodName", listOf()) + commentBuilder.getMethodReference("org.utbot.ClassName\$NestedClassName", "methodName", listOf(), false) val expectedMethodReference = "{@link org.utbot.ClassName.NestedClassName#methodName()}" assertEquals(methodReference, expectedMethodReference) } From 6c627d416c2ed9b15ec4b790cc6c25f84beae6c9 Mon Sep 17 00:00:00 2001 From: Zarina Kurbatova Date: Thu, 6 Oct 2022 00:58:10 +0300 Subject: [PATCH 2/2] Fix tests --- .../kotlin/examples/inner/SummaryInnerCallsTest.kt | 10 +++++----- .../test/kotlin/examples/ternary/SummaryTernaryTest.kt | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/utbot-summary-tests/src/test/kotlin/examples/inner/SummaryInnerCallsTest.kt b/utbot-summary-tests/src/test/kotlin/examples/inner/SummaryInnerCallsTest.kt index cc596ceca0..d264d2c3f6 100644 --- a/utbot-summary-tests/src/test/kotlin/examples/inner/SummaryInnerCallsTest.kt +++ b/utbot-summary-tests/src/test/kotlin/examples/inner/SummaryInnerCallsTest.kt @@ -136,7 +136,7 @@ class SummaryInnerCallsTest : SummaryTestCaseGeneratorTest( "throws IllegalArgumentException in: return binarySearch.leftBinSearch(array, key);\n" val summary7 = "Test calls {@link org.utbot.examples.algorithms.BinarySearch#leftBinSearch(long[],long)},\n" + " there it invokes:\n" + - " {@link org.utbot.examples.algorithms.BinarySearch#isUnsorted(long[])} once\n" + + " org.utbot.examples.algorithms.BinarySearch#isUnsorted(long[]) once\n" + " triggers recursion of leftBinSearch once, \n" + "Test throws NullPointerException in: return binarySearch.leftBinSearch(array, key);\n" @@ -378,8 +378,8 @@ class SummaryInnerCallsTest : SummaryTestCaseGeneratorTest( " (fst < 100): False,\n" + " (snd < 100): False\n" + " invokes:\n" + - " {@link org.utbot.examples.invokes.InvokeExample#half(int)} once,\n" + - " {@link org.utbot.examples.invokes.InvokeExample#mult(int,int)} once\n" + + " org.utbot.examples.invokes.InvokeExample#half(int) once,\n" + + " org.utbot.examples.invokes.InvokeExample#mult(int,int) once\n" + " returns from: return mult(x, y);\n" + " \n" + "Test then returns from: return invokeExample.simpleFormula(f, s);\n" @@ -625,8 +625,8 @@ class SummaryInnerCallsTest : SummaryTestCaseGeneratorTest( " (fst < 100): False,\n" + " (snd < 100): False\n" + " invokes:\n" + - " {@link org.utbot.examples.invokes.InvokeExample#half(int)} once,\n" + - " {@link org.utbot.examples.invokes.InvokeExample#mult(int,int)} once\n" + + " org.utbot.examples.invokes.InvokeExample#half(int) once,\n" + + " org.utbot.examples.invokes.InvokeExample#mult(int,int) once\n" + " returns from: return mult(x, y);\n" + " \n" + " Test later returns from: return invokeExample.simpleFormula(f, s);\n" + diff --git a/utbot-summary-tests/src/test/kotlin/examples/ternary/SummaryTernaryTest.kt b/utbot-summary-tests/src/test/kotlin/examples/ternary/SummaryTernaryTest.kt index b7c031075a..cf8c456096 100644 --- a/utbot-summary-tests/src/test/kotlin/examples/ternary/SummaryTernaryTest.kt +++ b/utbot-summary-tests/src/test/kotlin/examples/ternary/SummaryTernaryTest.kt @@ -475,12 +475,12 @@ class SummaryTernaryTest : SummaryTestCaseGeneratorTest( val summary1 = "Test executes conditions:\n" + " (num1 > num2): True\n" + "invokes:\n" + - " {@link org.utbot.examples.ternary.Ternary#intFunc1()} once\n" + + " org.utbot.examples.ternary.Ternary#intFunc1() once\n" + "returns from: return num1 > num2 ? intFunc1() : intFunc2();\n" val summary2 = "Test executes conditions:\n" + " (num1 > num2): False\n" + "invokes:\n" + - " {@link org.utbot.examples.ternary.Ternary#intFunc2()} once\n" + + " org.utbot.examples.ternary.Ternary#intFunc2() once\n" + "returns from: return num1 > num2 ? intFunc1() : intFunc2();\n" val methodName1 = "testIntFunc_Num1GreaterThanNum2"