Skip to content

Commit

Permalink
Fix issues with java file without any constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinan Kozak committed Oct 2, 2020
1 parent a30b477 commit 28058ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,26 @@ internal class NetworkLayerClassJsonDetector : RetrofitReturnTypeDetector() {
.mapNotNull { it.containingClass }
.toSet()

val constructorParamsWithJson: MutableList<String> = mutableListOf()
val checkedFields = allFields
.filterNot(::hasJsonNameAnnotation)
.map { it.name }
.toMutableList()

if (checkedFields.isNotEmpty()) {
val constructorParamsWithJson: MutableList<String> = mutableListOf()

val constructorParameter: List<JvmParameter> = classes
.map { it.constructors.first().parameters }
.fold(mutableListOf(), { acc, arrayOfJvmParameters -> acc.apply { addAll(arrayOfJvmParameters) } })
val constructorParameter: List<JvmParameter> = classes
.mapNotNull { it.constructors.firstOrNull()?.parameters }
.fold(mutableListOf(), { acc, arrayOfJvmParameters -> acc.apply { addAll(arrayOfJvmParameters) } })

constructorParameter.forEach { parameter ->
val name = parameter.name
if (name != null && parameter.annotations.any { annotation -> annotation.qualifiedName?.endsWith("Json") == true }) {
constructorParamsWithJson.add(name)
constructorParameter.forEach { parameter ->
val name = parameter.name
if (name != null && parameter.annotations.any { annotation -> annotation.qualifiedName?.endsWith("Json") == true }) {
constructorParamsWithJson.add(name)
}
}
checkedFields -= constructorParamsWithJson
}

val checkedFields = allFields
.filterNot(::hasJsonNameAnnotation)
.map { it.name } - constructorParamsWithJson

if (checkedFields.isNotEmpty()) {
context.report(
issue = ISSUE_NETWORK_LAYER_CLASS_JSON_RULE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,6 @@ internal class NetworkLayerClassJsonDetectorTest {
class Dto {
@Json(name = "a") final int totalResults;
@Json(name = "b") final int totalNewResults;
Dto(int totalResults, int totalNewResults) {
this.totalResult = totalResults;
this.totalNewResults = totalNewResults;
}
}
""".trimIndent()
)
Expand Down

0 comments on commit 28058ea

Please sign in to comment.