Skip to content

Commit d4f99a9

Browse files
authored
Wrap method reference into @link tag only if the invoked method is not private (#1106)
* Wrap method reference into @link tag only if the invoked method is not private * Fix tests
1 parent 04ed20e commit d4f99a9

File tree

6 files changed

+48
-19
lines changed

6 files changed

+48
-19
lines changed

utbot-summary-tests/src/test/kotlin/examples/inner/SummaryInnerCallsTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class SummaryInnerCallsTest : SummaryTestCaseGeneratorTest(
136136
"throws IllegalArgumentException in: return binarySearch.leftBinSearch(array, key);\n"
137137
val summary7 = "Test calls {@link org.utbot.examples.algorithms.BinarySearch#leftBinSearch(long[],long)},\n" +
138138
" there it invokes:\n" +
139-
" {@link org.utbot.examples.algorithms.BinarySearch#isUnsorted(long[])} once\n" +
139+
" org.utbot.examples.algorithms.BinarySearch#isUnsorted(long[]) once\n" +
140140
" triggers recursion of leftBinSearch once, \n" +
141141
"Test throws NullPointerException in: return binarySearch.leftBinSearch(array, key);\n"
142142

@@ -378,8 +378,8 @@ class SummaryInnerCallsTest : SummaryTestCaseGeneratorTest(
378378
" (fst < 100): False,\n" +
379379
" (snd < 100): False\n" +
380380
" invokes:\n" +
381-
" {@link org.utbot.examples.invokes.InvokeExample#half(int)} once,\n" +
382-
" {@link org.utbot.examples.invokes.InvokeExample#mult(int,int)} once\n" +
381+
" org.utbot.examples.invokes.InvokeExample#half(int) once,\n" +
382+
" org.utbot.examples.invokes.InvokeExample#mult(int,int) once\n" +
383383
" returns from: return mult(x, y);\n" +
384384
" \n" +
385385
"Test then returns from: return invokeExample.simpleFormula(f, s);\n"
@@ -625,8 +625,8 @@ class SummaryInnerCallsTest : SummaryTestCaseGeneratorTest(
625625
" (fst < 100): False,\n" +
626626
" (snd < 100): False\n" +
627627
" invokes:\n" +
628-
" {@link org.utbot.examples.invokes.InvokeExample#half(int)} once,\n" +
629-
" {@link org.utbot.examples.invokes.InvokeExample#mult(int,int)} once\n" +
628+
" org.utbot.examples.invokes.InvokeExample#half(int) once,\n" +
629+
" org.utbot.examples.invokes.InvokeExample#mult(int,int) once\n" +
630630
" returns from: return mult(x, y);\n" +
631631
" \n" +
632632
" Test later returns from: return invokeExample.simpleFormula(f, s);\n" +

utbot-summary-tests/src/test/kotlin/examples/ternary/SummaryTernaryTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,12 @@ class SummaryTernaryTest : SummaryTestCaseGeneratorTest(
475475
val summary1 = "Test executes conditions:\n" +
476476
" (num1 > num2): True\n" +
477477
"invokes:\n" +
478-
" {@link org.utbot.examples.ternary.Ternary#intFunc1()} once\n" +
478+
" org.utbot.examples.ternary.Ternary#intFunc1() once\n" +
479479
"returns from: return num1 > num2 ? intFunc1() : intFunc2();\n"
480480
val summary2 = "Test executes conditions:\n" +
481481
" (num1 > num2): False\n" +
482482
"invokes:\n" +
483-
" {@link org.utbot.examples.ternary.Ternary#intFunc2()} once\n" +
483+
" org.utbot.examples.ternary.Ternary#intFunc2() once\n" +
484484
"returns from: return num1 > num2 ? intFunc1() : intFunc2();\n"
485485

486486
val methodName1 = "testIntFunc_Num1GreaterThanNum2"

utbot-summary/src/main/kotlin/org/utbot/summary/comment/CustomJavaDocCommentBuilder.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class CustomJavaDocCommentBuilder(
3030
val methodReference = getMethodReference(
3131
currentMethod.declaringClass.name,
3232
currentMethod.name,
33-
currentMethod.parameterTypes
33+
currentMethod.parameterTypes,
34+
false
3435
)
3536
val classReference = getClassReference(currentMethod.declaringClass.javaStyleName)
3637

utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ open class SimpleCommentBuilder(
194194
sentenceInvoke.squashStmtText()
195195
if (!sentenceInvoke.isEmpty()) {
196196
sentenceBlock.invokeSentenceBlock =
197-
Pair(getMethodReference(className, methodName, methodParameterTypes), sentenceInvoke)
197+
Pair(
198+
getMethodReference(className, methodName, methodParameterTypes, invokeSootMethod.isPrivate),
199+
sentenceInvoke
200+
)
198201
createNextBlock = true
199202
invokeRegistered = true
200203
}
@@ -302,7 +305,15 @@ open class SimpleCommentBuilder(
302305
val className = stmt.invokeExpr.methodRef.declaringClass.name
303306
val methodName = stmt.invokeExpr.method.name
304307
val methodParameterTypes = stmt.invokeExpr.method.parameterTypes
305-
addTextInvoke(sentenceBlock, className, methodName, methodParameterTypes, frequency)
308+
val isPrivate = stmt.invokeExpr.method.isPrivate
309+
addTextInvoke(
310+
sentenceBlock,
311+
className,
312+
methodName,
313+
methodParameterTypes,
314+
isPrivate,
315+
frequency
316+
)
306317
}
307318
}
308319

@@ -314,13 +325,14 @@ open class SimpleCommentBuilder(
314325
className: String,
315326
methodName: String,
316327
methodParameterTypes: List<Type>,
328+
isPrivate: Boolean,
317329
frequency: Int
318330
) {
319331
if (!shouldSkipInvoke(methodName))
320332
sentenceBlock.stmtTexts.add(
321333
StmtDescription(
322334
StmtType.Invoke,
323-
getMethodReference(className, methodName, methodParameterTypes),
335+
getMethodReference(className, methodName, methodParameterTypes, isPrivate),
324336
frequency
325337
)
326338
)
@@ -345,21 +357,33 @@ open class SimpleCommentBuilder(
345357
}
346358

347359
/**
348-
* Returns a reference to the invoked method.
360+
* Returns a reference to the invoked method. IDE can't resolve references to private methods in comments,
361+
* so we add @link tag only if the invoked method is not private.
349362
*
350363
* It looks like {@link packageName.className#methodName(type1, type2)}.
351364
*
352365
* In case when an enclosing class in nested, we need to replace '$' with '.'
353366
* to render the reference.
354367
*/
355-
fun getMethodReference(className: String, methodName: String, methodParameterTypes: List<Type>): String {
368+
fun getMethodReference(
369+
className: String,
370+
methodName: String,
371+
methodParameterTypes: List<Type>,
372+
isPrivate: Boolean
373+
): String {
356374
val prettyClassName: String = className.replace("$", ".")
357375

358-
return if (methodParameterTypes.isEmpty()) {
359-
"{@link $prettyClassName#$methodName()}"
376+
val text = if (methodParameterTypes.isEmpty()) {
377+
"$prettyClassName#$methodName()"
360378
} else {
361379
val methodParametersAsString = methodParameterTypes.joinToString(",")
362-
"{@link $prettyClassName#$methodName($methodParametersAsString)}"
380+
"$prettyClassName#$methodName($methodParametersAsString)"
381+
}
382+
383+
return if (isPrivate) {
384+
text
385+
} else {
386+
"{@link $text}"
363387
}
364388
}
365389

utbot-summary/src/main/kotlin/org/utbot/summary/comment/SymbolicExecutionClusterCommentBuilder.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,16 @@ class SymbolicExecutionClusterCommentBuilder(
9090
val className = invokeSootMethod.declaringClass.name
9191
val methodName = invokeSootMethod.name
9292
val methodParameterTypes = invokeSootMethod.parameterTypes
93+
val isPrivate = invokeSootMethod.isPrivate
9394
val sentenceInvoke = SimpleSentenceBlock(stringTemplates = StringsTemplatesPlural())
9495
buildSentenceBlock(invoke, sentenceInvoke, invokeSootMethod)
9596
sentenceInvoke.squashStmtText()
9697
if (!sentenceInvoke.isEmpty()) {
9798
sentenceBlock.invokeSentenceBlock =
98-
Pair(getMethodReference(className, methodName, methodParameterTypes), sentenceInvoke)
99+
Pair(
100+
getMethodReference(className, methodName, methodParameterTypes, isPrivate),
101+
sentenceInvoke
102+
)
99103
createNextBlock = true
100104
invokeRegistered = true
101105
}

utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class SimpleCommentBuilderTest {
6767
@Test
6868
fun `builds inline link for method`() {
6969
val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst)
70-
val methodReference = commentBuilder.getMethodReference("org.utbot.ClassName", "methodName", listOf())
70+
val methodReference = commentBuilder.getMethodReference("org.utbot.ClassName", "methodName", listOf(), false)
7171
val expectedMethodReference = "{@link org.utbot.ClassName#methodName()}"
7272
assertEquals(methodReference, expectedMethodReference)
7373
}
@@ -76,7 +76,7 @@ class SimpleCommentBuilderTest {
7676
fun `builds inline link for method in nested class`() {
7777
val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst)
7878
val methodReference =
79-
commentBuilder.getMethodReference("org.utbot.ClassName\$NestedClassName", "methodName", listOf())
79+
commentBuilder.getMethodReference("org.utbot.ClassName\$NestedClassName", "methodName", listOf(), false)
8080
val expectedMethodReference = "{@link org.utbot.ClassName.NestedClassName#methodName()}"
8181
assertEquals(methodReference, expectedMethodReference)
8282
}

0 commit comments

Comments
 (0)