diff --git a/src/main/kotlin/astminer/common/model/FunctionInfoModel.kt b/src/main/kotlin/astminer/common/model/FunctionInfoModel.kt index 2b40034f..cacdd94c 100644 --- a/src/main/kotlin/astminer/common/model/FunctionInfoModel.kt +++ b/src/main/kotlin/astminer/common/model/FunctionInfoModel.kt @@ -22,11 +22,11 @@ interface FunctionInfo { get() = notImplemented("root") val filePath: String get() = notImplemented("filePath") - val annotations: List + val annotations: List? get() = notImplemented("annotations") - val modifiers: List + val modifiers: List? get() = notImplemented("modifiers") - val parameters: List + val parameters: List? get() = notImplemented("parameters") val returnType: String? get() = notImplemented("returnType") diff --git a/src/main/kotlin/astminer/filters/FunctionFilters.kt b/src/main/kotlin/astminer/filters/FunctionFilters.kt index 15591f34..d449131d 100644 --- a/src/main/kotlin/astminer/filters/FunctionFilters.kt +++ b/src/main/kotlin/astminer/filters/FunctionFilters.kt @@ -9,16 +9,20 @@ import astminer.common.splitToSubtokens * Filter that excludes functions that have at least one of modifiers from the [excludeModifiers] list. */ class ModifierFilter(private val excludeModifiers: List) : FunctionFilter { - override fun validate(functionInfo: FunctionInfo): Boolean = - !excludeModifiers.any { modifier -> modifier in functionInfo.modifiers } + override fun validate(functionInfo: FunctionInfo): Boolean { + val functionModifiers = checkNotNull(functionInfo.modifiers) { "Modifiers weren't properly parsed" } + return functionModifiers.none { modifier -> modifier in excludeModifiers } + } } /** - * Filter that excludes functions that have at least one annotations from the [excludeAnnotations] list. + * Filter that excludes functions that have at least one of annotations from the [excludeAnnotations] list. */ class AnnotationFilter(private val excludeAnnotations: List) : FunctionFilter { - override fun validate(functionInfo: FunctionInfo): Boolean = - !excludeAnnotations.any { annotation -> annotation in functionInfo.annotations } + override fun validate(functionInfo: FunctionInfo): Boolean { + val functionAnnotations = checkNotNull(functionInfo.annotations) { "Annotations weren't properly parsed" } + return functionAnnotations.none { annotation -> annotation in excludeAnnotations } + } } /** diff --git a/src/main/kotlin/astminer/parse/antlr/java/AntlrJavaFunctionInfo.kt b/src/main/kotlin/astminer/parse/antlr/java/AntlrJavaFunctionInfo.kt index d6ac9ec6..1a79687d 100644 --- a/src/main/kotlin/astminer/parse/antlr/java/AntlrJavaFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/antlr/java/AntlrJavaFunctionInfo.kt @@ -3,13 +3,21 @@ package astminer.parse.antlr.java import astminer.common.model.* import astminer.parse.antlr.* import astminer.parse.findEnclosingElementBy +import mu.KotlinLogging + +private val logger = KotlinLogging.logger("Antlr-Java-function-info") class AntlrJavaFunctionInfo(override val root: AntlrNode, override val filePath: String) : FunctionInfo { override val nameNode: AntlrNode? = collectNameNode() - override val parameters: List = collectParameters() override val returnType: String? = collectReturnType() override val enclosingElement: EnclosingElement? = collectEnclosingClass() + override val parameters: List? = + try { collectParameters() } catch (e: IllegalStateException) { + logger.warn { e.message } + null + } + private fun collectNameNode(): AntlrNode? = root.getChildOfType(METHOD_NAME_NODE) private fun collectReturnType(): String? { diff --git a/src/main/kotlin/astminer/parse/antlr/javascript/AntlrJavaScriptElementInfo.kt b/src/main/kotlin/astminer/parse/antlr/javascript/AntlrJavaScriptElementInfo.kt index 33589567..d5c12555 100644 --- a/src/main/kotlin/astminer/parse/antlr/javascript/AntlrJavaScriptElementInfo.kt +++ b/src/main/kotlin/astminer/parse/antlr/javascript/AntlrJavaScriptElementInfo.kt @@ -3,6 +3,9 @@ package astminer.parse.antlr.javascript import astminer.common.model.* import astminer.parse.antlr.* import astminer.parse.findEnclosingElementBy +import mu.KotlinLogging + +private val logger = KotlinLogging.logger("Antlr-Javascript-function-info") /** Base class for describing JavaScript methods, functions or arrow functions. @@ -76,9 +79,14 @@ abstract class AntlrJavaScriptElementInfo(override val root: AntlrNode, override class JavaScriptArrowInfo(root: AntlrNode, filePath: String) : AntlrJavaScriptElementInfo(root, filePath) { override val enclosingElement: EnclosingElement? = collectEnclosingElement() - override val parameters: List = collectParameters() override val nameNode: AntlrNode? = root.getChildOfType(ARROW_NAME_NODE) + override val parameters: List? = + try { collectParameters() } catch (e: IllegalStateException) { + logger.warn { e.message } + null + } + override fun getParametersRoot(): AntlrNode? { val parameterRoot = root.getChildOfType(ARROW_PARAMETER_NODE) return parameterRoot?.getChildOfType(ARROW_PARAMETER_INNER_NODE) ?: parameterRoot @@ -94,8 +102,12 @@ class JavaScriptArrowInfo(root: AntlrNode, filePath: String) : AntlrJavaScriptEl class JavaScriptMethodInfo(root: AntlrNode, filePath: String) : AntlrJavaScriptElementInfo(root, filePath) { override val enclosingElement: EnclosingElement? = collectEnclosingElement() - override val parameters: List = collectParameters() override val nameNode: AntlrNode? = collectNameNode() + override val parameters: List? = + try { collectParameters() } catch (e: IllegalStateException) { + logger.warn { e.message } + null + } private fun collectNameNode(): AntlrNode? { val methodNameParent = root.children.firstOrNull { @@ -119,8 +131,12 @@ class JavaScriptMethodInfo(root: AntlrNode, filePath: String) : AntlrJavaScriptE class JavaScriptFunctionInfo(root: AntlrNode, filePath: String) : AntlrJavaScriptElementInfo(root, filePath) { override val enclosingElement: EnclosingElement? = collectEnclosingElement() - override val parameters: List = collectParameters() override val nameNode: AntlrNode? = root.getChildOfType(FUNCTION_NAME_NODE) + override val parameters: List? = + try { collectParameters() } catch (e: IllegalStateException) { + logger.warn { e.message } + null + } override fun getParametersRoot(): AntlrNode? = root.getChildOfType(FUNCTION_PARAMETER_NODE) diff --git a/src/main/kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionInfo.kt b/src/main/kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionInfo.kt index a2bb3cf2..e7642cd2 100644 --- a/src/main/kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionInfo.kt @@ -13,9 +13,12 @@ private val logger = KotlinLogging.logger("ANTLR-PHP-function-info") class ANTLRPHPFunctionInfo(override val root: AntlrNode, override val filePath: String) : FunctionInfo { override val returnType = getElementType(root) override val nameNode: AntlrNode? = root.getChildOfType(FUNCTION_NAME) - - override val parameters: List = collectParameters() override val enclosingElement: EnclosingElement? = collectEnclosingElement() + override val parameters: List? = + try { collectParameters() } catch (e: IllegalStateException) { + logger.warn { e.message } + null + } private fun collectParameters(): List { // Parameters in this grammar have following structure (children order may be wrong): diff --git a/src/main/kotlin/astminer/parse/antlr/python/AntlrPythonFunctionInfo.kt b/src/main/kotlin/astminer/parse/antlr/python/AntlrPythonFunctionInfo.kt index 53ee37c4..bfbb5600 100644 --- a/src/main/kotlin/astminer/parse/antlr/python/AntlrPythonFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/antlr/python/AntlrPythonFunctionInfo.kt @@ -3,11 +3,18 @@ package astminer.parse.antlr.python import astminer.common.model.* import astminer.parse.antlr.* import astminer.parse.findEnclosingElementBy +import mu.KotlinLogging + +private val logger = KotlinLogging.logger("Antlr-python-function-info") class AntlrPythonFunctionInfo(override val root: AntlrNode, override val filePath: String) : FunctionInfo { override val nameNode: AntlrNode? = collectNameNode() - override val parameters: List = collectParameters() override val enclosingElement: EnclosingElement? = collectEnclosingElement() + override val parameters: List? = + try { collectParameters() } catch (e: IllegalStateException) { + logger.warn { e.message } + null + } private fun collectNameNode(): AntlrNode? = root.getChildOfType(FUNCTION_NAME_NODE) diff --git a/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt b/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt index 5bcc6b06..711293b7 100644 --- a/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt @@ -6,13 +6,20 @@ import astminer.common.model.FunctionInfo import astminer.common.model.FunctionInfoParameter import astminer.parse.findEnclosingElementBy import astminer.parse.fuzzy.FuzzyNode +import mu.KotlinLogging + +private val logger = KotlinLogging.logger("Fuzzyparser-Cpp-function-info") class FuzzyCppFunctionInfo(override val root: FuzzyNode, override val filePath: String) : FunctionInfo { override val returnType: String? = collectReturnType() override val enclosingElement: EnclosingElement? = collectEnclosingClass() - override val parameters: List = collectParameters() override val nameNode: FuzzyNode? = collectNameNode() + override val parameters: List? = + try { collectParameters() } catch (e: IllegalStateException) { + logger.warn { e.message } + null + } private fun collectNameNode(): FuzzyNode? = root.getChildOfType(METHOD_NAME_NODE) as? FuzzyNode diff --git a/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt b/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt index a6af57f7..ecf58f88 100644 --- a/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt @@ -5,6 +5,9 @@ import astminer.common.model.EnclosingElementType import astminer.common.model.FunctionInfo import astminer.common.model.FunctionInfoParameter import astminer.parse.gumtree.GumTreeNode +import mu.KotlinLogging + +private val logger = KotlinLogging.logger("Gumtree-Java-function-info") class GumTreeJavaFunctionInfo( override val root: GumTreeNode, @@ -12,9 +15,13 @@ class GumTreeJavaFunctionInfo( ) : FunctionInfo { override val nameNode: GumTreeNode? = root.getChildOfType(TypeLabels.simpleName) - override val parameters: List = collectParameters() override val returnType: String? = root.getElementType() override val enclosingElement: EnclosingElement? = collectEnclosingClass() + override val parameters: List? = + try { collectParameters() } catch (e: IllegalStateException) { + logger.warn { e.message } + null + } override val modifiers: List = root.children.filter { it.typeLabel == "Modifier" }.map { it.originalToken } override val annotations: List = root diff --git a/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt b/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt index d297a3ab..f873851a 100644 --- a/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt @@ -6,6 +6,9 @@ import astminer.common.model.FunctionInfo import astminer.common.model.FunctionInfoParameter import astminer.parse.findEnclosingElementBy import astminer.parse.gumtree.GumTreeNode +import mu.KotlinLogging + +private val logger = KotlinLogging.logger("Gumtree-Java-function-info") class GumTreePythonFunctionInfo( override val root: GumTreeNode, @@ -13,9 +16,13 @@ class GumTreePythonFunctionInfo( ) : FunctionInfo { override val nameNode: GumTreeNode = root - override val parameters: List = collectParameters() override val enclosingElement: EnclosingElement? = collectEnclosingClass() override val returnType: String? = getElementType(root)?.typeLabel + override val parameters: List? = + try { collectParameters() } catch (e: IllegalStateException) { + logger.warn { e.message } + null + } private fun getElementType(node: GumTreeNode): GumTreeNode? { if (node.typeLabel == TypeLabels.arg) { diff --git a/src/test/kotlin/astminer/parse/antlr/java/JavaFunctionSplitterTest.kt b/src/test/kotlin/astminer/parse/antlr/java/JavaFunctionSplitterTest.kt index d1fea436..73822194 100644 --- a/src/test/kotlin/astminer/parse/antlr/java/JavaFunctionSplitterTest.kt +++ b/src/test/kotlin/astminer/parse/antlr/java/JavaFunctionSplitterTest.kt @@ -70,29 +70,29 @@ class JavaFunctionSplitterTest { fun testNoParameters() { val methodNoParameters = functionInfos.find { it.name == "functionWithNoParameters" } assertNotNull(methodNoParameters) - assertEquals(0, methodNoParameters.parameters.size) + assertEquals(0, methodNoParameters.parameters?.size) } @Test fun testOneParameter() { val methodOneParameter = functionInfos.find { it.name == "functionWithOneParameter" } assertNotNull(methodOneParameter) - assertEquals(1, methodOneParameter.parameters.size) - val parameter = methodOneParameter.parameters[0] - assertEquals("p1", parameter.name) - assertEquals("int", parameter.type) + assertEquals(1, methodOneParameter.parameters?.size) + val parameter = methodOneParameter.parameters?.get(0) + assertEquals("p1", parameter?.name) + assertEquals("int", parameter?.type) } @Test fun testThreeParameters() { val methodThreeParameters = functionInfos.find { it.name == "functionWithThreeParameters" } assertNotNull(methodThreeParameters) - assertEquals(3, methodThreeParameters.parameters.size) + assertEquals(3, methodThreeParameters.parameters?.size) val methodTypes = listOf("Class", "String[][]", "int[]") for (i in 0 until 3) { - val parameter = methodThreeParameters.parameters[i] - assertEquals("p${i + 1}", parameter.name) - assertEquals(methodTypes[i], parameter.type) + val parameter = methodThreeParameters.parameters?.get(i) + assertEquals("p${i + 1}", parameter?.name) + assertEquals(methodTypes[i], parameter?.type) } } @@ -100,10 +100,10 @@ class JavaFunctionSplitterTest { fun testWeirdArrayParameter() { val methodWeirdArrayParameter = functionInfos.find { it.name == "functionWithStrangeArrayParameter" } assertNotNull(methodWeirdArrayParameter) - assertEquals(1, methodWeirdArrayParameter.parameters.size) - val weirdParameter = methodWeirdArrayParameter.parameters[0] - assertEquals(weirdParameter.name, "arr[]") - assertEquals(weirdParameter.type, "int") + assertEquals(1, methodWeirdArrayParameter.parameters?.size) + val weirdParameter = methodWeirdArrayParameter.parameters?.get(0) + assertEquals(weirdParameter?.name, "arr[]") + assertEquals(weirdParameter?.type, "int") } companion object { diff --git a/src/test/kotlin/astminer/parse/antlr/javascript/JavaScriptFunctionSplitterTest.kt b/src/test/kotlin/astminer/parse/antlr/javascript/JavaScriptFunctionSplitterTest.kt index 4e32386d..977013fb 100644 --- a/src/test/kotlin/astminer/parse/antlr/javascript/JavaScriptFunctionSplitterTest.kt +++ b/src/test/kotlin/astminer/parse/antlr/javascript/JavaScriptFunctionSplitterTest.kt @@ -40,7 +40,7 @@ class JavaScriptFunctionSplitterTest { fun FunctionInfo.getJsonInfo(): String { return "info : {" + "name : $name, " + - "args : ${parameters.joinToString(", ") { it.name }}, " + + "args : ${parameters?.joinToString(", ") { it.name }}, " + "enclosing element : ${enclosingElement?.type?.getEnclosingElementType()}, " + "enclosing element name : ${enclosingElement?.name}" + "}" diff --git a/src/test/kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionSplitterTest.kt b/src/test/kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionSplitterTest.kt index 27c6286b..3be0583d 100644 --- a/src/test/kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionSplitterTest.kt +++ b/src/test/kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionSplitterTest.kt @@ -40,7 +40,7 @@ internal class ANTLRPHPFunctionSplitterTest { fun FunctionInfo.getJsonInfo(): String = listOf( "info : {", "name: $name, ", - "args: ${parameters.joinToString(", ") { + "args: ${parameters?.joinToString(", ") { listOfNotNull(it.type, it.name).joinToString(" ") }}, ", "enclosing element: ${enclosingElement?.type?.getEnclosingElementType()}, ", diff --git a/src/test/kotlin/astminer/parse/antlr/python/PythonFunctionSplitterTest.kt b/src/test/kotlin/astminer/parse/antlr/python/PythonFunctionSplitterTest.kt index 9efdac5a..0e8bc21e 100644 --- a/src/test/kotlin/astminer/parse/antlr/python/PythonFunctionSplitterTest.kt +++ b/src/test/kotlin/astminer/parse/antlr/python/PythonFunctionSplitterTest.kt @@ -53,44 +53,44 @@ class PythonFunctionSplitterTest { fun testNoParameters() { val functionNoParameters = functionInfos.find { it.name == "function_with_no_parameters" } assertNotNull(functionNoParameters) - assertEquals(0, functionNoParameters.parameters.size) + assertEquals(0, functionNoParameters.parameters?.size) } @Test fun testOneParameter() { val functionOneParameter = functionInfos.find { it.name == "function_with_one_parameter" } assertNotNull(functionOneParameter) - assertEquals(1, functionOneParameter.parameters.size) - val parameter = functionOneParameter.parameters[0] - assertEquals("p1", parameter.name) + assertEquals(1, functionOneParameter.parameters?.size) + val parameter = functionOneParameter.parameters?.get(0) + assertEquals("p1", parameter?.name) } @Test fun testOneTypedParameter() { val functionOneTypedParameter = functionInfos.find { it.name == "function_with_one_typed_parameter" } assertNotNull(functionOneTypedParameter) - assertEquals(1, functionOneTypedParameter.parameters.size) - val parameter = functionOneTypedParameter.parameters[0] - assertEquals("p1", parameter.name) - assertEquals("int", parameter.type) + assertEquals(1, functionOneTypedParameter.parameters?.size) + val parameter = functionOneTypedParameter.parameters?.get(0) + assertEquals("p1", parameter?.name) + assertEquals("int", parameter?.type) } @Test fun functionWithComplexParameter() { val functionOneTypedParameter = functionInfos.find { it.name == "function_with_complex_parameter" } assertNotNull(functionOneTypedParameter) - assertEquals(1, functionOneTypedParameter.parameters.size) - val parameter = functionOneTypedParameter.parameters[0] - assertEquals("p1", parameter.name) - assertEquals("List[int]", parameter.type) + assertEquals(1, functionOneTypedParameter.parameters?.size) + val parameter = functionOneTypedParameter.parameters?.get(0) + assertEquals("p1", parameter?.name) + assertEquals("List[int]", parameter?.type) } @Test fun testThreeParameters() { val functionThreeParameters = functionInfos.find { it.name == "function_with_three_parameters" } assertNotNull(functionThreeParameters) - assertEquals(3, functionThreeParameters.parameters.size) - val parameters = functionThreeParameters.parameters + assertEquals(3, functionThreeParameters.parameters?.size) + val parameters = functionThreeParameters.parameters!! assertEquals("p1", parameters[0].name) assertEquals("p2", parameters[1].name) @@ -103,19 +103,19 @@ class PythonFunctionSplitterTest { fun testParameterInClass() { val functionOneParameter = functionInfos.find { it.name == "fun_with_parameter_in_class" } assertNotNull(functionOneParameter) - assertEquals(2, functionOneParameter.parameters.size) - val parameter = functionOneParameter.parameters[1] - assertEquals("p1", parameter.name) + assertEquals(2, functionOneParameter.parameters?.size) + val parameter = functionOneParameter.parameters?.get(1) + assertEquals("p1", parameter?.name) } @Test fun testTypedParameterInClass() { val functionOneTypedParameter = functionInfos.find { it.name == "fun_with_typed_parameter_in_class" } assertNotNull(functionOneTypedParameter) - assertEquals(2, functionOneTypedParameter.parameters.size) - val parameter = functionOneTypedParameter.parameters[1] - assertEquals("p1", parameter.name) - assertEquals("int", parameter.type) + assertEquals(2, functionOneTypedParameter.parameters?.size) + val parameter = functionOneTypedParameter.parameters?.get(1) + assertEquals("p1", parameter?.name) + assertEquals("int", parameter?.type) } @Test diff --git a/src/test/kotlin/astminer/parse/cpp/FuzzyMethodSplitterTest.kt b/src/test/kotlin/astminer/parse/cpp/FuzzyMethodSplitterTest.kt index ddb5ad63..3ff65833 100644 --- a/src/test/kotlin/astminer/parse/cpp/FuzzyMethodSplitterTest.kt +++ b/src/test/kotlin/astminer/parse/cpp/FuzzyMethodSplitterTest.kt @@ -85,28 +85,28 @@ class FuzzyMethodSplitterTest { fun testNoParameters() { val methodNoParameters = methodInfos.find { it.name == "functionWithNoParameters" } assertNotNull(methodNoParameters) - assertEquals(0, methodNoParameters.parameters.size) + assertEquals(0, methodNoParameters.parameters?.size) } @Test fun testOneParameter() { val methodOneParameter = methodInfos.find { it.name == "functionWithOneParameter" } assertNotNull(methodOneParameter) - assertEquals(1, methodOneParameter.parameters.size) - val parameter = methodOneParameter.parameters[0] - assertEquals("p1", parameter.name) - assertEquals("int", parameter.type) + assertEquals(1, methodOneParameter.parameters?.size) + val parameter = methodOneParameter.parameters?.get(0) + assertEquals("p1", parameter?.name) + assertEquals("int", parameter?.type) } @Test fun testThreeParameters() { val methodThreeParameters = methodInfos.find { it.name == "functionWithThreeParameters" } assertNotNull(methodThreeParameters) - assertEquals(3, methodThreeParameters.parameters.size) + assertEquals(3, methodThreeParameters.parameters?.size) for (i in 0 until 3) { - val parameter = methodThreeParameters.parameters[i] - assertEquals("p${i + 1}", parameter.name) - assertEquals("int", parameter.type) + val parameter = methodThreeParameters.parameters?.get(i) + assertEquals("p${i + 1}", parameter?.name) + assertEquals("int", parameter?.type) } } diff --git a/src/test/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionSplitterTest.kt b/src/test/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionSplitterTest.kt index 1cf8cb4c..2dfab373 100644 --- a/src/test/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionSplitterTest.kt +++ b/src/test/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionSplitterTest.kt @@ -22,8 +22,8 @@ class GumTreeJavaFunctionSplitterTest { assertEquals("fun", name) assertEquals("void", returnType) assertEquals("SingleFunction", enclosingElement?.name) - assertEquals(listOf("args", "param"), parameters.map { it.name }) - assertEquals(listOf("String[]", "int"), parameters.map { it.type }) + assertEquals(listOf("args", "param"), parameters?.map { it.name }) + assertEquals(listOf("String[]", "int"), parameters?.map { it.type }) } } @@ -36,8 +36,8 @@ class GumTreeJavaFunctionSplitterTest { assertEquals("main", name) assertEquals("void", returnType) assertEquals("InnerClass", enclosingElement?.name) - assertEquals(listOf("args"), parameters.map { it.name }) - assertEquals(listOf("String[]"), parameters.map { it.type }) + assertEquals(listOf("args"), parameters?.map { it.name }) + assertEquals(listOf("String[]"), parameters?.map { it.type }) } } @@ -50,15 +50,15 @@ class GumTreeJavaFunctionSplitterTest { assertEquals("main", name) assertEquals("void", returnType) assertEquals("InnerClass", enclosingElement?.name) - assertEquals(listOf("args"), parameters.map { it.name }) - assertEquals(listOf("String[]"), parameters.map { it.type }) + assertEquals(listOf("args"), parameters?.map { it.name }) + assertEquals(listOf("String[]"), parameters?.map { it.type }) } with(functionInfos.last()) { assertEquals("fun", name) assertEquals("void", returnType) assertEquals("SingleMethodInnerClass", enclosingElement?.name) - assertEquals(listOf("args", "param"), parameters.map { it.name }) - assertEquals(listOf("String[]", "int"), parameters.map { it.type }) + assertEquals(listOf("args", "param"), parameters?.map { it.name }) + assertEquals(listOf("String[]", "int"), parameters?.map { it.type }) } } @@ -71,8 +71,8 @@ class GumTreeJavaFunctionSplitterTest { assertEquals("fun", name) assertEquals("int", returnType) assertEquals("SingleFunction", enclosingElement?.name) - assertEquals(listOf("args", "param"), parameters.map { it.name }) - assertEquals(listOf("int", "SingleFunction"), parameters.map { it.type }) + assertEquals(listOf("args", "param"), parameters?.map { it.name }) + assertEquals(listOf("int", "SingleFunction"), parameters?.map { it.type }) } } diff --git a/src/test/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionSplitterTest.kt b/src/test/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionSplitterTest.kt index 910acd93..124926af 100644 --- a/src/test/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionSplitterTest.kt +++ b/src/test/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionSplitterTest.kt @@ -54,9 +54,9 @@ class GumTreePythonFunctionSplitterTest { with(functionInfo) { assertEquals("complex_args_full_typed", name) assertEquals(null, returnType) - assertEquals(1, parameters.size) - assertEquals(listOf("node"), parameters.map { it.name }.toList()) - assertEquals(listOf("JsonNodeType"), parameters.map { it.type }.toList()) + assertEquals(1, parameters?.size) + assertEquals(listOf("node"), parameters?.map { it.name }?.toList()) + assertEquals(listOf("JsonNodeType"), parameters?.map { it.type }?.toList()) } } @@ -68,9 +68,9 @@ class GumTreePythonFunctionSplitterTest { with(functionInfo) { assertEquals("func_dif_args_typed_return", name) assertEquals("Constant-int", returnType) - assertEquals(6, parameters.size) - assertEquals(listOf("a", "b", "c", "d", "e", "f"), parameters.map { it.name }.toList()) - assertEquals(emptyList(), parameters.mapNotNull { it.type }.toList()) + assertEquals(6, parameters?.size) + assertEquals(listOf("a", "b", "c", "d", "e", "f"), parameters?.map { it.name }?.toList()) + assertEquals(emptyList(), parameters?.mapNotNull { it.type }?.toList()) } } @@ -83,9 +83,9 @@ class GumTreePythonFunctionSplitterTest { assertEquals("foo_typed", name) assertEquals("A", enclosingElement?.name) assertEquals(null, returnType) - assertEquals(3, parameters.size) - assertEquals(listOf("self", "x", "y"), parameters.map { it.name }.toList()) - assertEquals(listOf(null, "int", "int"), parameters.map { it.type }.toList()) + assertEquals(3, parameters?.size) + assertEquals(listOf("self", "x", "y"), parameters?.map { it.name }?.toList()) + assertEquals(listOf(null, "int", "int"), parameters?.map { it.type }?.toList()) } } @@ -98,9 +98,9 @@ class GumTreePythonFunctionSplitterTest { assertEquals("bar_typed", name) assertEquals("C", enclosingElement?.name) assertEquals(null, returnType) - assertEquals(2, parameters.size) - assertEquals(listOf("self", "x"), parameters.map { it.name }.toList()) - assertEquals(listOf(null, "int"), parameters.map { it.type }.toList()) + assertEquals(2, parameters?.size) + assertEquals(listOf("self", "x"), parameters?.map { it.name }?.toList()) + assertEquals(listOf(null, "int"), parameters?.map { it.type }?.toList()) } } @@ -114,9 +114,9 @@ class GumTreePythonFunctionSplitterTest { assertEquals("AsyncFunctionDef", root.typeLabel) assertEquals(null, enclosingElement?.name) assertEquals("Constant-int", returnType) - assertEquals(4, parameters.size) - assertEquals(listOf("event", "x", "args", "kwargs"), parameters.map { it.name }.toList()) - assertEquals(listOf("str", "int", null, null), parameters.map { it.type }.toList()) + assertEquals(4, parameters?.size) + assertEquals(listOf("event", "x", "args", "kwargs"), parameters?.map { it.name }?.toList()) + assertEquals(listOf("str", "int", null, null), parameters?.map { it.type }?.toList()) } } @@ -136,12 +136,12 @@ class GumTreePythonFunctionSplitterTest { ?.getChildOfType("Constant-str") ?.originalToken ) - assertEquals(4, parameters.size) + assertEquals(4, parameters?.size) assertEquals( listOf("gh", "original_issue", "branch", "backport_pr_number"), - parameters.map { it.name }.toList() + parameters?.map { it.name }?.toList() ) - assertEquals(listOf(null, null, null, null), parameters.map { it.type }.toList()) + assertEquals(listOf(null, null, null, null), parameters?.map { it.type }?.toList()) } } @@ -155,9 +155,9 @@ class GumTreePythonFunctionSplitterTest { assertEquals("foo_1", functionInfo.root.parent?.wrappedNode?.parent?.label) assertEquals(null, enclosingElement?.name) assertEquals("Constant-NoneType", returnType) - assertEquals(1, parameters.size) - assertEquals(listOf("c"), parameters.map { it.name }.toList()) - assertEquals(listOf(null), parameters.map { it.type }.toList()) + assertEquals(1, parameters?.size) + assertEquals(listOf("c"), parameters?.map { it.name }?.toList()) + assertEquals(listOf(null), parameters?.map { it.type }?.toList()) } } @@ -171,9 +171,9 @@ class GumTreePythonFunctionSplitterTest { assertEquals("bar_1", functionInfo.root.parent?.wrappedNode?.parent?.label) assertEquals(null, enclosingElement?.name) assertEquals("Constant-int", returnType) - assertEquals(2, parameters.size) - assertEquals(listOf("d", "e"), parameters.map { it.name }.toList()) - assertEquals(listOf("int", "int"), parameters.map { it.type }.toList()) + assertEquals(2, parameters?.size) + assertEquals(listOf("d", "e"), parameters?.map { it.name }?.toList()) + assertEquals(listOf("int", "int"), parameters?.map { it.type }?.toList()) } } }