!475 kotlin代码生成器,FieldWrapper增加友好提示

Merge pull request !475 from 落羽er/main
This commit is contained in:
Michael Yang 2024-07-18 05:15:38 +00:00 committed by Gitee
commit c4cdf8c2dc
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 11 additions and 5 deletions

View File

@ -27,7 +27,7 @@ import io.swagger.v3.oas.annotations.media.Schema
*/
#(table.buildTableAnnotation())
#end
#if(isBase)open #end class #(entityClassName) #if(withActiveRecord) : Model<#(entityClassName)>()#else#(table.buildKtExtends(isBase))#end {
open class #(entityClassName) #if(withActiveRecord) : Model<#(entityClassName)>()#else#(table.buildKtExtends(isBase))#end {
#for(column : table.columns)
#set(comment = javadocConfig.formatColumnComment(column.comment))
#if(isNotBlank(comment))
@ -45,7 +45,7 @@ import io.swagger.v3.oas.annotations.media.Schema
#if(withSwagger && swaggerVersion.getName() == "DOC")
@Schema(description = "#(column.comment)")
#end
#if(isBase)open #end var #(column.property): #(column.propertySimpleType)? = #if(isNotBlank(column.propertyDefaultValue)) = #(column.propertyDefaultValue)#else null#end
open var #(column.property): #(column.propertySimpleType)? = #if(isNotBlank(column.propertyDefaultValue)) = #(column.propertyDefaultValue)#else null#end
#end
}

View File

@ -94,9 +94,15 @@ public class FieldWrapper {
if (Collection.class.isAssignableFrom(fieldType)) {
Type genericType = field.getGenericType();
if (genericType instanceof ParameterizedType) {
Type actualTypeArgument = ((ParameterizedType) genericType).getActualTypeArguments()[0];
fieldWrapper.mappingType = (Class<?>) actualTypeArgument;
try{
//使用Kotlin时若Collection泛型类型为协变类型时 List<out E>并且传入的具体泛型类型为opengenericType的具体类型为WildcardType? extends ExampleClass该类型不可转为Class无具体类型使用instanceOf会抛出转换异常
//该错误目前只在以上情况中发现
if (genericType instanceof ParameterizedType) {
Type actualTypeArgument = ((ParameterizedType) genericType).getActualTypeArguments()[0];
fieldWrapper.mappingType = (Class<?>) actualTypeArgument;
}
}catch (ClassCastException e){
throw new UnsupportedOperationException(String.format("不支持使用[%s]作为集合类型。请使用MutableList",fieldType.getName()));
}
} else if (Map.class.isAssignableFrom(fieldType)) {
Type genericType = field.getGenericType();