Skip to content

Commit

Permalink
Check throwable and String. I am unable to verify those with test som…
Browse files Browse the repository at this point in the history
…ehow.
  • Loading branch information
kozaxinan committed Sep 26, 2024
1 parent 8548aaf commit 378c87e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import com.android.tools.lint.detector.api.Issue
import com.android.tools.lint.detector.api.JavaContext
import com.android.tools.lint.detector.api.Scope
import com.android.tools.lint.detector.api.Severity
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiField
import com.intellij.psi.PsiModifier
import org.jetbrains.uast.UClass
import org.jetbrains.uast.UField
import org.jetbrains.uast.getContainingUClass
import org.jetbrains.uast.toUElementOfType

/**
Expand Down Expand Up @@ -50,10 +52,15 @@ internal class ImmutableDataClassDetector : Detector(), UastScanner {
fields: List<UField>,
evaluator: JavaEvaluator,
) {
val problematicFields = fields.filter { field: UField ->
!field.hasModifierProperty(PsiModifier.FINAL) ||
field.isTypeMutable(evaluator)
}
val problematicFields = fields
.filterNot { field: UField ->
hasThrowableSuperClass(field.getContainingUClass())
}
.filter { field: UField ->
!field
.hasModifierProperty(PsiModifier.FINAL) ||
field.isTypeMutable(evaluator)
}

if (problematicFields.isNotEmpty()) {
val message = problematicFields.joinToString(separator = "\n") { field ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kozaxinan.android.checks

import com.android.tools.lint.client.api.JavaEvaluator
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiMember
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
import org.jetbrains.kotlin.asJava.elements.KtLightMember
Expand Down Expand Up @@ -115,3 +116,18 @@ fun String?.matchesAnyOf(patterns: Sequence<Regex>): Boolean {
}
return false
}

fun hasThrowableSuperClass(uClass: UClass?): Boolean {
var superClass: PsiClass? = uClass?.javaPsi?.superClass
while (superClass != null) {
if (superClass.qualifiedName == "java.lang.Throwable") {
return true
}
superClass = superClass.superClass
}
return false
}

fun isStringClass(uClass: UClass): Boolean {
return uClass.qualifiedName == "java.lang.String"
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,17 @@ internal abstract class RetrofitReturnTypeDetector : Detector(), UastScanner {

internal fun findAllInnerFields(
typeRef: PsiClassType,
visitedTypes: MutableSet<PsiClassType> = mutableSetOf()
visitedTypes: MutableSet<PsiClassType> = mutableSetOf(),
): Set<UField> {
val actualReturnType = findGenericClassType(typeRef)
val typeClass = actualReturnType
val typeClass: UClass = actualReturnType
.resolve()
.toUElement() as? UClass
?: return emptySet()

if (hasThrowableSuperClass(typeClass) || isStringClass(typeClass)) {
return setOf()
}

if (visitedTypes.contains(actualReturnType)) {
return setOf()
Expand All @@ -137,6 +140,7 @@ internal abstract class RetrofitReturnTypeDetector : Detector(), UastScanner {

return innerFields +
innerFields
.asSequence()
.filterNot { it.isStatic }
.map { it.type }
.filterIsInstance<PsiClassType>()
Expand Down

0 comments on commit 378c87e

Please sign in to comment.