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

Fix blank functions filtering when using antlr with java #203

Merged
merged 2 commits into from
Feb 15, 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 @@ -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