From d7389cb9479f148c9af5e7eb1c87683478266503 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Wed, 9 Nov 2022 12:56:16 +0800 Subject: [PATCH] feat: add first add sample #22 --- .../kotlinast/KotlinBasicIdentListenerTest.kt | 14 +++--- .../kotlinast/KotlinFullIdentListenerTest.kt | 8 ++-- .../spring/KotlinSpringCompatibleTest.kt | 10 ++-- .../scalaast/ScalaFullIdentListenerTest.kt | 12 ++--- .../expr/{TokenTree.kt => Expression.kt} | 46 +++++++++++++++---- .../chapi/domain/expr/ExpressionTest.kt | 10 +++- 6 files changed, 68 insertions(+), 32 deletions(-) rename chapi-domain/src/main/kotlin/chapi/domain/expr/{TokenTree.kt => Expression.kt} (56%) diff --git a/chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/KotlinBasicIdentListenerTest.kt b/chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/KotlinBasicIdentListenerTest.kt index 8ea362c4..f93085e2 100644 --- a/chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/KotlinBasicIdentListenerTest.kt +++ b/chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/KotlinBasicIdentListenerTest.kt @@ -127,11 +127,11 @@ package chapi.ast.kotlinast import hello.Human -class Person(val name: String, val age: Int = 0) : Human { - var height: Int = 0 - private var weight: Int = 0 +class Person(val name: String, val age: IntValue = 0) : Human { + var height: IntValue = 0 + private var weight: IntValue = 0 - fun setWeight(weight: Int) { + fun setWeight(weight: IntValue) { this.weight = weight } } @@ -146,19 +146,19 @@ class Person(val name: String, val age: Int = 0) : Human { assertEquals(Modifiers[0], "val") } codeContainer.DataStructures[0].Fields[1].run { - assertEquals(TypeType, "kotlin.Int") + assertEquals(TypeType, "kotlin.IntValue") assertEquals(TypeValue, "0") assertEquals(TypeKey, "age") assertEquals(Modifiers[0], "val") } codeContainer.DataStructures[0].Fields[2].run { - assertEquals(TypeType, "kotlin.Int") + assertEquals(TypeType, "kotlin.IntValue") assertEquals(TypeValue, "0") assertEquals(TypeKey, "height") assertEquals(Modifiers[0], "var") } codeContainer.DataStructures[0].Fields[3].run { - assertEquals(TypeType, "kotlin.Int") + assertEquals(TypeType, "kotlin.IntValue") assertEquals(TypeValue, "0") assertEquals(TypeKey, "weight") assertEquals(Modifiers[0], "private") diff --git a/chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/KotlinFullIdentListenerTest.kt b/chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/KotlinFullIdentListenerTest.kt index 075a40c1..882bf9e6 100644 --- a/chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/KotlinFullIdentListenerTest.kt +++ b/chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/KotlinFullIdentListenerTest.kt @@ -173,7 +173,7 @@ import org.springframework.web.client.RestTemplate @Component -class QualityGateClientImpl(@Value val baseUrl: String) : QualityGateClient { +class QualityGateClientImpl(@CustomValueType val baseUrl: String) : QualityGateClient { override fun getQualityGate(qualityGateName: String): CouplingQualityGate { RestTemplate().getForObject("/api/quality-gate-profile/mcc", CouplingQualityGate::class.java) } @@ -195,7 +195,7 @@ package chapi.ast.kotlinast import org.springframework.web.client.RestTemplate @Component -class QualityGateClientImpl(@Value("\${'$'}{client.host}") val baseUrl: String) : QualityGateClient { +class QualityGateClientImpl(@CustomValueType("\${'$'}{client.host}") val baseUrl: String) : QualityGateClient { override fun getQualityGate(qualityGateName: String): CouplingQualityGate { val couplingQualityGate = RestTemplate().getForObject(baseUrl + "/api/quality-gate-profile/${'$'}qualityGateName", CouplingQualityGate::class.java) return couplingQualityGate ?: CouplingQualityGate(null, qualityGateName, emptyList(), null, null) @@ -218,7 +218,7 @@ package com.thoughtworks.archguard.packages.domain import org.springframework.web.client.RestTemplate class PackageStore { - fun addEdge(a: String, b: String, num: Int) { + fun addEdge(a: String, b: String, num: IntValue) { val aId = getNodeId(a) val bId = getNodeId(b) packageEdges.add(PackageEdge(aId, bId, num)) @@ -240,7 +240,7 @@ package com.thoughtworks.archguard.packages.domain import org.springframework.web.client.RestTemplate class SizingRepositoryImpl(val jdbi: Jdbi) : SizingRepository { - override fun getClassSizingListAboveMethodCountThresholdCount(systemId: Long, threshold: Int): Long { + override fun getClassSizingListAboveMethodCountThresholdCount(systemId: Long, threshold: IntValue): Long { return jdbi.withHandle { val table = "select count(name) as count from code_class where system_id = :systemId and is_test=false and loc is not NULL " + "group by class_name " + diff --git a/chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/spring/KotlinSpringCompatibleTest.kt b/chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/spring/KotlinSpringCompatibleTest.kt index ccd2444f..85728930 100644 --- a/chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/spring/KotlinSpringCompatibleTest.kt +++ b/chapi-ast-kotlin/src/test/kotlin/chapi/ast/kotlinast/spring/KotlinSpringCompatibleTest.kt @@ -34,7 +34,7 @@ class ClassController(val service: ClassService) { fun getDependencies(@PathVariable("systemId") systemId: Long, @PathVariable("name") name: String, @RequestParam(value = "module", required = false, defaultValue = "") module: String, - @RequestParam("deep", required = false, defaultValue = "3") deep: Int): JClass { + @RequestParam("deep", required = false, defaultValue = "3") deep: IntValue): JClass { return service.getDependencies(systemId, module, name, deep) } @@ -42,9 +42,9 @@ class ClassController(val service: ClassService) { fun getInvokes(@PathVariable("systemId") systemId: Long, @PathVariable("name") name: String, @RequestParam(value = "module", required = false, defaultValue = "") module: String, - @RequestParam(value = "deep", required = false, defaultValue = "3") deep: Int, - @RequestParam(value = "callerDeep", required = false) callerDeep: Int?, - @RequestParam(value = "calleeDeep", required = false) calleeDeep: Int?, + @RequestParam(value = "deep", required = false, defaultValue = "3") deep: IntValue, + @RequestParam(value = "callerDeep", required = false) callerDeep: IntValue?, + @RequestParam(value = "calleeDeep", required = false) calleeDeep: IntValue?, @RequestParam(value = "needIncludeImpl", required = false, defaultValue = "true") needIncludeImpl: Boolean?): JClass { return service.findInvokes(systemId, module, name, callerDeep ?: deep, calleeDeep ?: deep, needIncludeImpl ?: true) @@ -54,7 +54,7 @@ class ClassController(val service: ClassService) { fun getMethodsCallees(@PathVariable("systemId") systemId: Long, @PathVariable("name") name: String, @RequestParam(value = "module", required = false, defaultValue = "") module: String, - @RequestParam(value = "deep", required = false, defaultValue = "3") deep: Int, + @RequestParam(value = "deep", required = false, defaultValue = "3") deep: IntValue, @RequestParam(value = "needParents", required = false, defaultValue = "true") needParents: Boolean, @RequestParam(value = "needIncludeImpl", required = false, defaultValue = "true") needIncludeImpl: Boolean): JClass { return service.findMethodsCallees(systemId, module, name, deep, needIncludeImpl, needParents) diff --git a/chapi-ast-scala/src/test/kotlin/chapi/ast/scalaast/ScalaFullIdentListenerTest.kt b/chapi-ast-scala/src/test/kotlin/chapi/ast/scalaast/ScalaFullIdentListenerTest.kt index 5c4d5f78..23a7b5ea 100644 --- a/chapi-ast-scala/src/test/kotlin/chapi/ast/scalaast/ScalaFullIdentListenerTest.kt +++ b/chapi-ast-scala/src/test/kotlin/chapi/ast/scalaast/ScalaFullIdentListenerTest.kt @@ -25,7 +25,7 @@ object HelloWorld { @Test internal fun shouldIdentClassName() { val code = """ -class Outer(i : Int) { +class Outer(i : IntValue) { def foo(x : Inner.type) = x.getI } """ @@ -39,7 +39,7 @@ class Outer(i : Int) { @Test internal fun shouldIdentClassParameters() { val code = """ -class Outer(i : Int) { +class Outer(i : IntValue) { def foo(x : Inner.type) = x.getI } """ @@ -47,16 +47,16 @@ class Outer(i : Int) { val container = ScalaAnalyser().analysis(code, "hello.scala") assertEquals(container.DataStructures.size, 1) assertEquals(container.DataStructures[0].Parameters.size, 1) - assertEquals(container.DataStructures[0].Parameters[0].TypeType, "Int") + assertEquals(container.DataStructures[0].Parameters[0].TypeType, "IntValue") assertEquals(container.DataStructures[0].Parameters[0].TypeValue, "i") } @Test internal fun shouldIdentOutClassInnerObjectName() { val code = """ -class Outer(i : Int) { +class Outer(i : IntValue) { object Inner { - def getI : Int = i + def getI : IntValue = i } def foo(x : Inner.type) = x.getI } @@ -89,7 +89,7 @@ trait Iterator[A] { def next(): A } -class IntIterator(to: Int) extends Iterator[Int] { +class IntIterator(to: IntValue) extends Iterator[IntValue] { private var current = 0 override def hasNext: Boolean = current < to } diff --git a/chapi-domain/src/main/kotlin/chapi/domain/expr/TokenTree.kt b/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt similarity index 56% rename from chapi-domain/src/main/kotlin/chapi/domain/expr/TokenTree.kt rename to chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt index d171f10a..6d46d4ac 100644 --- a/chapi-domain/src/main/kotlin/chapi/domain/expr/TokenTree.kt +++ b/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt @@ -13,18 +13,41 @@ import java.util.function.IntBinaryOperator // Postfix; // } sealed class Expression { - class BinOp(lhs: Expression, op: BinOpKind, rhs: Expression) : ExpressionNode - class UnOp(lhs: Expression, op: UnOpKind) : ExpressionNode - class Int(val value: Int?) : ExpressionNode + class BinOp(val lhs: ExpressionNode, val op: BinOpKind, val rhs: ExpressionNode) : ExpressionNode { + override fun toString(): String { + return "$lhs $op $rhs" + } + } + + class UnOp(lhs: ExpressionNode, op: UnOpKind) : ExpressionNode + class IntValue(val value: kotlin.Int) : ExpressionNode { + override fun toString() = value.toString() + } + + class CustomValueType(val value: ValueType) : ExpressionNode + } sealed class BinOpKind { - object Add : BinOpKind() // + - object Sub : BinOpKind() // - - object Mul : BinOpKind() // * - object Div : BinOpKind() // / - object Pow : BinOpKind() // ^ - object Eq : BinOpKind() // = + object Add : BinOpKind() { + override fun toString() = "+" + } + + object Sub : BinOpKind() { + override fun toString() = "-" + } + + object Mul : BinOpKind() { + override fun toString() = "*" + } + + object Div : BinOpKind() { + override fun toString() = "/" + } + + object Pow : BinOpKind() { + override fun toString() = "^" + } } sealed class UnOpKind { @@ -53,6 +76,11 @@ interface ExpressionNode { } +interface ValueType { + val name: String + val value: Any +} + interface Operator : ExpressionNode enum class Arithmetics : BinaryOperator, IntBinaryOperator { diff --git a/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt b/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt index 4b7080d7..5d5448da 100644 --- a/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt +++ b/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt @@ -1,10 +1,18 @@ package chapi.domain.expr import org.junit.jupiter.api.Test +import kotlin.test.assertEquals class ExpressionTest { @Test - fun shouldAddChildren() { + fun shouldPresentationAdd() { + // addOp(1, 2) + val binOp = Expression.BinOp( + lhs = Expression.IntValue(1), + op = BinOpKind.Add, + rhs = Expression.IntValue(2) + ) + assertEquals("1 + 2", binOp.toString()) } }