Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap method reference into @link tag only if the invoked method is not private #1106

Merged
merged 2 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
)
}
}

Expand All @@ -314,13 +325,14 @@ open class SimpleCommentBuilder(
className: String,
methodName: String,
methodParameterTypes: List<Type>,
isPrivate: Boolean,
frequency: Int
) {
if (!shouldSkipInvoke(methodName))
sentenceBlock.stmtTexts.add(
StmtDescription(
StmtType.Invoke,
getMethodReference(className, methodName, methodParameterTypes),
getMethodReference(className, methodName, methodParameterTypes, isPrivate),
frequency
)
)
Expand All @@ -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<Type>): String {
fun getMethodReference(
className: String,
methodName: String,
methodParameterTypes: List<Type>,
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}"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down