Skip to content

Commit

Permalink
fix #449 修复解析器声明了泛型,但onParse方法返回具体的实体类,编译报错问题
Browse files Browse the repository at this point in the history
  • Loading branch information
liujingxing committed Jun 30, 2023
1 parent dc3f6fa commit e75eeef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public abstract class BaseRxHttp : ITag, CallFactory {
toObservable(wrapResponseParser<T>(type))

public fun <T> toObservableResponse(type: Class<T>): ObservableCall<T> =
toObservableResponse(type as Type)
toObservableResponse<T>(type as Type)

public fun <T> toObservableResponseList(type: Class<T>): ObservableCall<List<T>> {
val typeList = List::class.parameterizedBy(type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,13 @@ private fun TypeElement.getToObservableXxxFun(
.build()
.apply { methodList.add(this) }

//注意,这里获取泛型边界跟ksp不一样,这里会自动过滤Object类型,即使手动声明了
//泛型数量等于1 且没有为泛型指定边界(Object类型边界除外),才去生成Parser注解里wrappers字段对应的toObservableXxx方法
if (typeCount == 1 && typeVariableNames.first().bounds.isEmpty()) {
/**
* 生成Parser注解里wrappers字段对应的toObservableXxx方法,如满足以下3个条件
* 1、泛型数量为1
* 2、泛型没有边界(kapt会自动过滤Object类型, 即使手动声明了)
* 3、解析器onParse方法返回泛型
*/
if (typeCount == 1 && typeVariableNames.first().bounds.isEmpty() && onParserFunReturnType is TypeVariableName) {
val toObservableXxxFunList = methodSpec
.getToObservableXxxWrapFun(parserAlias, onParserFunReturnType, typeMap)
methodList.addAll(toObservableXxxFunList)
Expand Down Expand Up @@ -286,27 +290,25 @@ private fun MethodSpec.getToObservableXxxWrapFun(
val methodName = "toObservable$parserAlias${simpleName}"

//3、toObservableXxx方法体
val funBody = CodeBlock.builder()
val methodBody = CodeBlock.builder()
val paramNames = parameterSpecs.joinToStringIndexed(", ") { index, it ->
if (index < typeCount) {
//Class类型参数,需要进行再次包装,最后再取参数名
val variableName = "${it.name}$simpleName"
//格式:Type tTypeList = ParameterizedTypeImpl.get(List.class, tType);
val expression = "\$T $variableName = \$T.get($simpleName.class, ${it.name})"
funBody.addStatement(expression, J_TYPE, parameterizedType)
methodBody.addStatement(expression, J_TYPE, parameterizedType)
variableName
} else it.name
}
val returnStatement = "return ${methodSpec.name}($paramNames)"
funBody.addStatement(returnStatement)

methodBody.addStatement("return ${methodSpec.name}($paramNames)")
//4、生成toObservableXxx方法
MethodSpec.methodBuilder(methodName)
.addModifiers(Modifier.PUBLIC)
.addTypeVariables(typeVariableNames)
.addParameters(parameterSpecs)
.varargs(methodSpec.varargs)
.addCode(funBody.build()) //方法里面的表达式
.addCode(methodBody.build()) //方法里面的表达式
.returns(toObservableXxxFunReturnType)
.build()
.apply { methodList.add(this) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,13 @@ private fun KSClassDeclaration.getToObservableXxxFun(

if (typeCount > 0 && isDependenceRxJava()) {
val paramNames = classParameterSpecs.toParamNames(typeCount)
val typeOfs = typeVariableNames.getTypeVariableString()

//生成Class类型参数的toObservableXxx方法
val funSpec = FunSpec.builder(funName)
.addTypeVariables(typeVariableNames)
.addParameters(classParameterSpecs)
.addStatement("return $funName($paramNames)") //方法里面的表达式
.addStatement("return $funName$typeOfs($paramNames)") //方法里面的表达式
.returns(toObservableXxxFunReturnType)
.build()
.apply { funList.add(this) }
Expand All @@ -201,9 +202,13 @@ private fun KSClassDeclaration.getToObservableXxxFun(
val name = typeName.toString()
name != "kotlin.Any" && name != "kotlin.Any?"
}

//泛型数量等于1 且没有为泛型指定边界(Any类型边界除外),才去生成Parser注解里wrappers字段对应的toObservableXxx方法
if (typeCount == 1 && nonAnyBounds.isEmpty()) {
/**
* 生成Parser注解里wrappers字段对应的toObservableXxx方法,如满足以下3个条件
* 1、泛型数量为1
* 2、泛型没有边界(Any类型边界除外)
* 3、解析器onParse方法返回泛型
*/
if (typeCount == 1 && nonAnyBounds.isEmpty() && onParserFunReturnType is TypeVariableName) {
val toObservableXxxFunList = funSpec
.getToObservableXxxWrapFun(parserAlias, onParserFunReturnType, typeMap)
funList.addAll(toObservableXxxFunList)
Expand Down Expand Up @@ -269,9 +274,7 @@ private fun FunSpec.getToObservableXxxWrapFun(
variableName
} else if (it.isVararg()) "*${it.name}" else it.name
}
val returnStatement = "return ${funSpec.name}($paramNames)"
funBody.addStatement(returnStatement)

funBody.addStatement("return ${funSpec.name}($paramNames)")
//4、生成toObservableXxx方法
FunSpec.builder(funName)
.addTypeVariables(typeVariableNames)
Expand Down

0 comments on commit e75eeef

Please sign in to comment.