Skip to content

Commit

Permalink
Merge pull request #203 from JetBrains-Research/filter_patch
Browse files Browse the repository at this point in the history
Fix blank functions filtering when using antlr with java
  • Loading branch information
SpirinEgor authored Feb 15, 2022
2 parents 7afcc92 + 2c849f5 commit 7288d01
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ class AntlrJavaFunctionInfo(override val root: AntlrNode, override val filePath:

override val body: AntlrNode? = root.children.find { it.typeLabel == METHOD_BODY_NODE }

override fun isBlank() = body == null || body.children.size <= 2
override fun isBlank(): Boolean {
if (body == null) return true
val block = body.getChildOfType(METHOD_BLOCK_NODE) ?: return true
return block.children.size <= 2
}

private fun collectNameNode(): AntlrNode? = root.getChildOfType(IDENTIFIER_NODE)

Expand Down Expand Up @@ -87,6 +91,7 @@ class AntlrJavaFunctionInfo(override val root: AntlrNode, override val filePath:
private const val ANNOTATION_NAME = "qualifiedName"
private const val IDENTIFIER_NODE = "IDENTIFIER"
private const val METHOD_BODY_NODE = "methodBody"
private const val METHOD_BLOCK_NODE = "block"

private const val CLASS_DECLARATION_NODE = "classDeclaration"
private const val ENUM_DECLARATION_NODE = "enumDeclaration"
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/astminer/pipeline/Pipeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class Pipeline(private val config: PipelineConfig) {
if (config.compressBeforeSaving) { results.toStructurallyNormalized() } else { results }
}
synchronized(this) {
storage.store(labeledResults)
metaStorage?.store(labeledResults)
storage.store(labeledResults, holdoutType)
metaStorage?.store(labeledResults, holdoutType)
}
progressBar.step()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import astminer.common.model.FunctionInfo
import astminer.parse.antlr.AntlrNode
import org.junit.Test
import java.io.File
import kotlin.test.BeforeTest
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import kotlin.test.*

class JavaFunctionSplitterTest {

Expand All @@ -30,48 +27,55 @@ class JavaFunctionSplitterTest {
val methodVoid = functionInfos.find { it.name == "functionReturningVoid" }
assertNotNull(methodVoid)
assertEquals("void", methodVoid.returnType)
assertTrue(methodVoid.isBlank())
}

@Test
fun testReturnInt() {
val methodInt = functionInfos.find { it.name == "functionReturningInt" }
assertNotNull(methodInt)
assertEquals("int", methodInt.returnType)
assertFalse(methodInt.isBlank())
}

@Test
fun testReturnStrings() {
val methodStrings = functionInfos.find { it.name == "functionReturningStrings" }
assertNotNull(methodStrings)
assertEquals("String[]", methodStrings.returnType)
assertFalse(methodStrings.isBlank())
}

@Test
fun testReturnClass() {
val methodClass = functionInfos.find { it.name == "functionReturningClass" }
assertNotNull(methodClass)
assertEquals("Class1", methodClass.returnType)
assertFalse(methodClass.isBlank())
}

@Test
fun testFunctionInClass() {
val methodClass = functionInfos.find { it.name == "functionInClass1" }
assertNotNull(methodClass)
assertEquals("Class1", methodClass.enclosingElement?.name)
assertTrue(methodClass.isBlank())
}

@Test
fun testFunctionInNestedClass() {
val methodClass = functionInfos.find { it.name == "functionInClass2" }
assertNotNull(methodClass)
assertEquals("Class2", methodClass.enclosingElement?.name)
assertTrue(methodClass.isBlank())
}

@Test
fun testNoParameters() {
val methodNoParameters = functionInfos.find { it.name == "functionWithNoParameters" }
assertNotNull(methodNoParameters)
assertEquals(0, methodNoParameters.parameters?.size)
assertTrue(methodNoParameters.isBlank())
}

@Test
Expand All @@ -82,6 +86,7 @@ class JavaFunctionSplitterTest {
val parameter = methodOneParameter.parameters?.get(0)
assertEquals("p1", parameter?.name)
assertEquals("int", parameter?.type)
assertTrue(methodOneParameter.isBlank())
}

@Test
Expand All @@ -95,6 +100,7 @@ class JavaFunctionSplitterTest {
assertEquals("p${i + 1}", parameter?.name)
assertEquals(methodTypes[i], parameter?.type)
}
assertTrue(methodThreeParameters.isBlank())
}

@Test
Expand All @@ -105,6 +111,7 @@ class JavaFunctionSplitterTest {
val weirdParameter = methodWeirdArrayParameter.parameters?.get(0)
assertEquals(weirdParameter?.name, "arr[]")
assertEquals(weirdParameter?.type, "int")
assertTrue(methodWeirdArrayParameter.isBlank())
}

@Test
Expand All @@ -114,6 +121,7 @@ class JavaFunctionSplitterTest {
val modifiers = methodWithOneModifier.modifiers
assertNotNull(modifiers)
assertEquals("abstract", modifiers.first())
assertTrue(methodWithOneModifier.isBlank())
}

@Test
Expand All @@ -123,6 +131,7 @@ class JavaFunctionSplitterTest {
val modifiers = methodWithMultipleModifiers.modifiers
assertNotNull(modifiers)
assertEquals(setOf("static", "public", "final"), modifiers.toSet())
assertFalse(methodWithMultipleModifiers.isBlank())
}

@Test
Expand All @@ -132,6 +141,7 @@ class JavaFunctionSplitterTest {
val annotations = methodWithOneAnnotation.annotations
assertNotNull(annotations)
assertEquals(setOf("Deprecated"), annotations.toSet())
assertTrue(methodWithOneAnnotation.isBlank())
}

@Test
Expand All @@ -141,6 +151,7 @@ class JavaFunctionSplitterTest {
val annotations = methodWithOneAnnotation.annotations
assertNotNull(annotations)
assertEquals(setOf("Deprecated", "SuppressWarnings"), annotations.toSet())
assertTrue(methodWithOneAnnotation.isBlank())
}

@Test
Expand All @@ -153,6 +164,7 @@ class JavaFunctionSplitterTest {
assertNotNull(annotations)
assertEquals(setOf("public", "static"), modifiers.toSet())
assertEquals(setOf("Deprecated"), annotations.toSet())
assertTrue(methodWithModifierAndAnnotation.isBlank())
}

@Test
Expand Down

0 comments on commit 7288d01

Please sign in to comment.