Skip to content

Commit

Permalink
Starting feature analysis.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchallen committed May 10, 2021
1 parent a3d7196 commit 93ac568
Show file tree
Hide file tree
Showing 6 changed files with 474 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ allprojects {
}
subprojects {
group = "com.github.cs125-illinois.jeed"
version = "2021.5.0"
version = "2021.5.1"
tasks.withType<KotlinCompile> {
val javaVersion = JavaVersion.VERSION_1_8.toString()
sourceCompatibility = javaVersion
Expand Down
46 changes: 20 additions & 26 deletions core/src/main/kotlin/Complexity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.antlr.v4.runtime.tree.ParseTreeWalker
val basicComplexityTokens = listOf(JavaLexer.FOR, JavaLexer.WHILE, JavaLexer.DO, JavaLexer.THROW)
val complexityExpressionBOPs = listOf(JavaLexer.AND, JavaLexer.OR, JavaLexer.QUESTION)

interface ComplexityValue {
sealed interface ComplexityValue {
var complexity: Int
fun lookup(name: String): ComplexityValue
}
Expand All @@ -19,12 +19,12 @@ interface ComplexityValue {
class ClassComplexity(
name: String,
range: SourceRange,
methods: MutableMap<String, LocatedMethod> = mutableMapOf(),
classes: MutableMap<String, LocatedClass> = mutableMapOf(),
methods: MutableMap<String, LocatedClassOrMethod> = mutableMapOf(),
classes: MutableMap<String, LocatedClassOrMethod> = mutableMapOf(),
override var complexity: Int = 0,
val isRecord: Boolean = false,
val isInterface: Boolean = false
) : LocatedClass(name, range, classes, methods), ComplexityValue {
) : LocatedClassOrMethod(name, range, classes, methods), ComplexityValue {
override fun lookup(name: String): ComplexityValue {
check(name.isNotEmpty())
@Suppress("TooGenericExceptionCaught")
Expand All @@ -48,10 +48,10 @@ class ClassComplexity(
class MethodComplexity(
name: String,
range: SourceRange,
methods: MutableMap<String, LocatedMethod> = mutableMapOf(),
classes: MutableMap<String, LocatedClass> = mutableMapOf(),
methods: MutableMap<String, LocatedClassOrMethod> = mutableMapOf(),
classes: MutableMap<String, LocatedClassOrMethod> = mutableMapOf(),
override var complexity: Int = 1
) : LocatedMethod(name, range, classes, methods), ComplexityValue {
) : LocatedClassOrMethod(name, range, classes, methods), ComplexityValue {
override fun lookup(name: String): ComplexityValue {
check(name.isNotEmpty())
@Suppress("TooGenericExceptionCaught")
Expand All @@ -72,7 +72,7 @@ class MethodComplexity(
}

@Suppress("TooManyFunctions")
class ComplexityResult(val source: Source, entry: Map.Entry<String, String>) : JavaParserBaseListener() {
private class ComplexityListener(val source: Source, entry: Map.Entry<String, String>) : JavaParserBaseListener() {
private val name = entry.key
private var anonymousCounter = 0
private var lambdaCounter = 0
Expand Down Expand Up @@ -268,23 +268,19 @@ class ComplexityResult(val source: Source, entry: Map.Entry<String, String>) : J
}

override fun enterFormalParameter(ctx: JavaParser.FormalParameterContext) {
if (!insideLambda) {
assert(ctx.children.size >= 2)
currentMethodParameters?.add(ctx.children[ctx.children.lastIndex - 1].text)
}
assert(ctx.children.size >= 2)
currentMethodParameters?.add(ctx.children[ctx.children.lastIndex - 1].text)
}

override fun enterLastFormalParameter(ctx: JavaParser.LastFormalParameterContext) {
if (!insideLambda) {
assert(ctx.children.size >= 2)
val type = ctx.children[ctx.children.lastIndex - 1].text
if (type != "...") {
currentMethodParameters?.add(type)
} else {
@Suppress("MagicNumber")
assert(ctx.children.size > 3)
currentMethodParameters?.add("...${ctx.children[ctx.children.lastIndex - 2].text}")
}
assert(ctx.children.size >= 2)
val type = ctx.children[ctx.children.lastIndex - 1].text
if (type != "...") {
currentMethodParameters?.add(type)
} else {
@Suppress("MagicNumber")
assert(ctx.children.size > 3)
currentMethodParameters?.add("...${ctx.children[ctx.children.lastIndex - 2].text}")
}
}

Expand Down Expand Up @@ -404,9 +400,6 @@ class ComplexityResult(val source: Source, entry: Map.Entry<String, String>) : J
currentMethod.complexity++
}

// Ignore argument lists that are part of lambda expressions
private var insideLambda = false

private var insideSwitch = false
override fun enterSwitchBlockStatementGroup(ctx: JavaParser.SwitchBlockStatementGroupContext) {
insideSwitch = true
Expand Down Expand Up @@ -463,13 +456,14 @@ class ComplexityFailed(errors: List<SourceError>) : JeedError(errors) {

@Throws(ComplexityFailed::class)
fun Source.complexity(names: Set<String> = sources.keys.toSet()): ComplexityResults {
require(type == Source.FileType.JAVA) { "Can't compute complexity yet for Kotlin sources" }
try {
return ComplexityResults(
this,
sources.filter {
names.contains(it.key)
}.mapValues {
ComplexityResult(this, it).results
ComplexityListener(this, it).results
}
)
} catch (e: JeedParsingException) {
Expand Down
Loading

0 comments on commit 93ac568

Please sign in to comment.