From 5a8482ccb01fe3c8497c28b2ec353882d8648cf1 Mon Sep 17 00:00:00 2001 From: illided Date: Mon, 2 Aug 2021 22:31:22 +0300 Subject: [PATCH 1/6] parameters list properties now is nullable --- .../common/model/FunctionInfoModel.kt | 6 +-- .../astminer/filters/FunctionFilters.kt | 14 ++++-- .../antlr/java/JavaFunctionSplitterTest.kt | 26 +++++----- .../JavaScriptFunctionSplitterTest.kt | 2 +- .../antlr/php/ANTLRPHPFunctionSplitterTest.kt | 2 +- .../python/PythonFunctionSplitterTest.kt | 42 ++++++++-------- .../parse/cpp/FuzzyMethodSplitterTest.kt | 18 +++---- .../java/GumTreeJavaFunctionSplitterTest.kt | 20 ++++---- .../GumTreePythonFunctionSplitterTest.kt | 48 +++++++++---------- 9 files changed, 91 insertions(+), 87 deletions(-) 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..db95d100 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 { + return functionInfo.modifiers?.let {modifiers -> excludeModifiers.intersect(modifiers).isEmpty() } + ?: throw IllegalStateException("Modifiers wasn't properly parsed") + } } /** - * 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 { + return functionInfo.annotations?.let { annotations -> excludeAnnotations.intersect(annotations).isEmpty() } + ?: throw IllegalStateException("Annotations was not properly parsed") + } } /** 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()) } } } From ca966bd967d9e3c53d75c7650765fc6b69b9c52d Mon Sep 17 00:00:00 2001 From: illided Date: Tue, 3 Aug 2021 12:50:47 +0300 Subject: [PATCH 2/6] code style fixes --- src/main/kotlin/astminer/filters/FunctionFilters.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/astminer/filters/FunctionFilters.kt b/src/main/kotlin/astminer/filters/FunctionFilters.kt index db95d100..d449131d 100644 --- a/src/main/kotlin/astminer/filters/FunctionFilters.kt +++ b/src/main/kotlin/astminer/filters/FunctionFilters.kt @@ -10,8 +10,8 @@ import astminer.common.splitToSubtokens */ class ModifierFilter(private val excludeModifiers: List) : FunctionFilter { override fun validate(functionInfo: FunctionInfo): Boolean { - return functionInfo.modifiers?.let {modifiers -> excludeModifiers.intersect(modifiers).isEmpty() } - ?: throw IllegalStateException("Modifiers wasn't properly parsed") + val functionModifiers = checkNotNull(functionInfo.modifiers) { "Modifiers weren't properly parsed" } + return functionModifiers.none { modifier -> modifier in excludeModifiers } } } @@ -20,8 +20,8 @@ class ModifierFilter(private val excludeModifiers: List) : FunctionFilte */ class AnnotationFilter(private val excludeAnnotations: List) : FunctionFilter { override fun validate(functionInfo: FunctionInfo): Boolean { - return functionInfo.annotations?.let { annotations -> excludeAnnotations.intersect(annotations).isEmpty() } - ?: throw IllegalStateException("Annotations was not properly parsed") + val functionAnnotations = checkNotNull(functionInfo.annotations) { "Annotations weren't properly parsed" } + return functionAnnotations.none { annotation -> annotation in excludeAnnotations } } } From 4a9755a113deef60a310c3d80e3553155665b702 Mon Sep 17 00:00:00 2001 From: illided Date: Tue, 3 Aug 2021 13:17:29 +0300 Subject: [PATCH 3/6] antlr parameters function properties now null on failed parse --- .../parse/antlr/java/AntlrJavaFunctionInfo.kt | 10 ++++++++- .../javascript/AntlrJavaScriptElementInfo.kt | 22 ++++++++++++++++--- .../parse/antlr/php/ANTLRPHPFunctionInfo.kt | 7 ++++-- .../antlr/python/AntlrPythonFunctionInfo.kt | 9 +++++++- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/astminer/parse/antlr/java/AntlrJavaFunctionInfo.kt b/src/main/kotlin/astminer/parse/antlr/java/AntlrJavaFunctionInfo.kt index d6ac9ec6..3a21e5f1 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 + +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..5ac35813 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 + +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..484ace50 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) { + astminer.parse.antlr.javascript.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..3c409ea4 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 + +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) From e71b170bc90e25d0773b20eaf38ba042ef0b0454 Mon Sep 17 00:00:00 2001 From: illided Date: Tue, 3 Aug 2021 14:54:41 +0300 Subject: [PATCH 4/6] same update on other parsers --- .../astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt | 8 +++++++- .../parse/gumtree/java/GumTreeJavaFunctionInfo.kt | 8 +++++++- .../parse/gumtree/python/GumTreePythonFunctionInfo.kt | 8 +++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt b/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt index 5bcc6b06..daf78a86 100644 --- a/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt @@ -4,6 +4,7 @@ import astminer.common.model.EnclosingElement import astminer.common.model.EnclosingElementType import astminer.common.model.FunctionInfo import astminer.common.model.FunctionInfoParameter +import astminer.parse.antlr.javascript.logger import astminer.parse.findEnclosingElementBy import astminer.parse.fuzzy.FuzzyNode @@ -11,8 +12,13 @@ class FuzzyCppFunctionInfo(override val root: FuzzyNode, override val filePath: 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..2e4c749a 100644 --- a/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt @@ -4,6 +4,7 @@ import astminer.common.model.EnclosingElement import astminer.common.model.EnclosingElementType import astminer.common.model.FunctionInfo import astminer.common.model.FunctionInfoParameter +import astminer.parse.antlr.javascript.logger import astminer.parse.gumtree.GumTreeNode class GumTreeJavaFunctionInfo( @@ -12,9 +13,14 @@ 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..e40fb0d3 100644 --- a/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt @@ -4,6 +4,7 @@ import astminer.common.model.EnclosingElement import astminer.common.model.EnclosingElementType import astminer.common.model.FunctionInfo import astminer.common.model.FunctionInfoParameter +import astminer.parse.antlr.javascript.logger import astminer.parse.findEnclosingElementBy import astminer.parse.gumtree.GumTreeNode @@ -13,9 +14,14 @@ 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) { From 7dee65050dc2e894acab9e28c9c75c12639ba901 Mon Sep 17 00:00:00 2001 From: illided Date: Tue, 3 Aug 2021 14:58:06 +0300 Subject: [PATCH 5/6] proper loggers added --- .../kotlin/astminer/parse/antlr/java/AntlrJavaFunctionInfo.kt | 2 +- .../parse/antlr/javascript/AntlrJavaScriptElementInfo.kt | 2 +- .../kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionInfo.kt | 2 +- .../astminer/parse/antlr/python/AntlrPythonFunctionInfo.kt | 2 +- .../kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt | 4 +++- .../astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt | 4 +++- .../parse/gumtree/python/GumTreePythonFunctionInfo.kt | 4 +++- 7 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/astminer/parse/antlr/java/AntlrJavaFunctionInfo.kt b/src/main/kotlin/astminer/parse/antlr/java/AntlrJavaFunctionInfo.kt index 3a21e5f1..1a79687d 100644 --- a/src/main/kotlin/astminer/parse/antlr/java/AntlrJavaFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/antlr/java/AntlrJavaFunctionInfo.kt @@ -5,7 +5,7 @@ import astminer.parse.antlr.* import astminer.parse.findEnclosingElementBy import mu.KotlinLogging -val logger = KotlinLogging.logger("Antlr-Java-function-info") +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() diff --git a/src/main/kotlin/astminer/parse/antlr/javascript/AntlrJavaScriptElementInfo.kt b/src/main/kotlin/astminer/parse/antlr/javascript/AntlrJavaScriptElementInfo.kt index 5ac35813..d5c12555 100644 --- a/src/main/kotlin/astminer/parse/antlr/javascript/AntlrJavaScriptElementInfo.kt +++ b/src/main/kotlin/astminer/parse/antlr/javascript/AntlrJavaScriptElementInfo.kt @@ -5,7 +5,7 @@ import astminer.parse.antlr.* import astminer.parse.findEnclosingElementBy import mu.KotlinLogging -val logger = KotlinLogging.logger("Antlr-Javascript-function-info") +private val logger = KotlinLogging.logger("Antlr-Javascript-function-info") /** Base class for describing JavaScript methods, functions or arrow functions. diff --git a/src/main/kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionInfo.kt b/src/main/kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionInfo.kt index 484ace50..e7642cd2 100644 --- a/src/main/kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/antlr/php/ANTLRPHPFunctionInfo.kt @@ -16,7 +16,7 @@ class ANTLRPHPFunctionInfo(override val root: AntlrNode, override val filePath: override val enclosingElement: EnclosingElement? = collectEnclosingElement() override val parameters: List? = try { collectParameters() } catch (e: IllegalStateException) { - astminer.parse.antlr.javascript.logger.warn { e.message } + logger.warn { e.message } null } diff --git a/src/main/kotlin/astminer/parse/antlr/python/AntlrPythonFunctionInfo.kt b/src/main/kotlin/astminer/parse/antlr/python/AntlrPythonFunctionInfo.kt index 3c409ea4..bfbb5600 100644 --- a/src/main/kotlin/astminer/parse/antlr/python/AntlrPythonFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/antlr/python/AntlrPythonFunctionInfo.kt @@ -5,7 +5,7 @@ import astminer.parse.antlr.* import astminer.parse.findEnclosingElementBy import mu.KotlinLogging -val logger = KotlinLogging.logger("Antlr-python-function-info") +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() diff --git a/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt b/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt index daf78a86..341ad923 100644 --- a/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt @@ -4,9 +4,11 @@ import astminer.common.model.EnclosingElement import astminer.common.model.EnclosingElementType import astminer.common.model.FunctionInfo import astminer.common.model.FunctionInfoParameter -import astminer.parse.antlr.javascript.logger 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 { diff --git a/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt b/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt index 2e4c749a..c2dccf45 100644 --- a/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt @@ -4,8 +4,10 @@ import astminer.common.model.EnclosingElement import astminer.common.model.EnclosingElementType import astminer.common.model.FunctionInfo import astminer.common.model.FunctionInfoParameter -import astminer.parse.antlr.javascript.logger import astminer.parse.gumtree.GumTreeNode +import mu.KotlinLogging + +private val logger = KotlinLogging.logger("Gumtree-Java-function-info") class GumTreeJavaFunctionInfo( override val root: GumTreeNode, diff --git a/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt b/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt index e40fb0d3..b6aa8411 100644 --- a/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt @@ -4,9 +4,11 @@ import astminer.common.model.EnclosingElement import astminer.common.model.EnclosingElementType import astminer.common.model.FunctionInfo import astminer.common.model.FunctionInfoParameter -import astminer.parse.antlr.javascript.logger 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, From a8124bd1569d83a06a07fc14fbde6d5eca04fbd6 Mon Sep 17 00:00:00 2001 From: illided Date: Tue, 3 Aug 2021 15:00:43 +0300 Subject: [PATCH 6/6] code style fixes --- .../kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt | 3 +-- .../astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt | 3 +-- .../astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt b/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt index 341ad923..711293b7 100644 --- a/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/fuzzy/cpp/FuzzyCppFunctionInfo.kt @@ -16,8 +16,7 @@ class FuzzyCppFunctionInfo(override val root: FuzzyNode, override val filePath: override val enclosingElement: EnclosingElement? = collectEnclosingClass() override val nameNode: FuzzyNode? = collectNameNode() override val parameters: List? = - try { collectParameters() } - catch (e: IllegalStateException) { + try { collectParameters() } catch (e: IllegalStateException) { logger.warn { e.message } null } diff --git a/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt b/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt index c2dccf45..ecf58f88 100644 --- a/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/gumtree/java/GumTreeJavaFunctionInfo.kt @@ -18,8 +18,7 @@ class GumTreeJavaFunctionInfo( override val returnType: String? = root.getElementType() override val enclosingElement: EnclosingElement? = collectEnclosingClass() override val parameters: List? = - try { collectParameters() } - catch (e: IllegalStateException) { + try { collectParameters() } catch (e: IllegalStateException) { logger.warn { e.message } null } diff --git a/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt b/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt index b6aa8411..f873851a 100644 --- a/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt +++ b/src/main/kotlin/astminer/parse/gumtree/python/GumTreePythonFunctionInfo.kt @@ -19,8 +19,7 @@ class GumTreePythonFunctionInfo( override val enclosingElement: EnclosingElement? = collectEnclosingClass() override val returnType: String? = getElementType(root)?.typeLabel override val parameters: List? = - try { collectParameters() } - catch (e: IllegalStateException) { + try { collectParameters() } catch (e: IllegalStateException) { logger.warn { e.message } null }