Skip to content

Commit 53b8305

Browse files
Getting method parameter names (#210)
1 parent 07a5c8b commit 53b8305

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

jacodb-core/src/main/kotlin/org/jacodb/impl/fs/ByteCodeConverter.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private fun MethodNode.asMethodInfo(): MethodInfo {
114114
parametersInfo = List(params.size) { index ->
115115
ParameterInfo(
116116
index = index,
117-
name = parameters?.get(index)?.name,
117+
name = argumentName(index),
118118
access = parameters?.get(index)?.access ?: Opcodes.ACC_PUBLIC,
119119
type = params[index],
120120
annotations = visibleParameterAnnotations?.get(index)?.asAnnotationInfos(true).orEmpty()
@@ -124,6 +124,17 @@ private fun MethodNode.asMethodInfo(): MethodInfo {
124124
)
125125
}
126126

127+
private fun MethodNode.argumentName(argIndex :Int): String? {
128+
localVariables?.let {
129+
(argIndex + 1 - (access and Opcodes.ACC_STATIC).countOneBits()).run {
130+
if (it.size > this) {
131+
return ArrayList(it).sortedBy(LocalVariableNode::index)[this].name
132+
}
133+
}
134+
}
135+
return parameters?.get(argIndex)?.name
136+
}
137+
127138
private fun FieldNode.asFieldInfo() = FieldInfo(
128139
name = name,
129140
signature = signature,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2022 UnitTestBot contributors (utbot.org)
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jacodb.testing
18+
19+
import kotlinx.coroutines.runBlocking
20+
import org.jacodb.api.JcMethod
21+
import org.jacodb.api.JcParameter
22+
import org.jacodb.api.ext.findClass
23+
import org.jacodb.api.ext.methods
24+
import org.jacodb.impl.fs.asClassInfo
25+
import org.jacodb.impl.types.ParameterInfo
26+
import org.junit.jupiter.api.Assertions
27+
import org.junit.jupiter.api.Test
28+
import java.nio.file.Files
29+
30+
class ParameterNamesTest : BaseTest() {
31+
companion object : WithDB()
32+
33+
private val target = Files.createTempDirectory("jcdb-temp")
34+
35+
@Test
36+
fun checkParameterName() {
37+
val clazz = cp.findClass("GenericsApi")
38+
runBlocking {
39+
cp.db.load(target.toFile())
40+
}
41+
val method = clazz.methods.firstOrNull { jcMethod -> jcMethod.name == "call" }
42+
Assertions.assertNotNull(method)
43+
Assertions.assertNull(method?.parameters?.get(0)?.name)
44+
Assertions.assertEquals("arg", method?.parameterNames?.get(0))
45+
}
46+
47+
private val JcMethod.parameterNames: List<String?>
48+
get() {
49+
return enclosingClass.asmNode()
50+
.asClassInfo(enclosingClass.bytecode()).methods.find { info -> info.name == name && info.desc == description }
51+
?.parametersInfo?.map(ParameterInfo::name)
52+
?: parameters.map(JcParameter::name)
53+
}
54+
}

0 commit comments

Comments
 (0)