26
26
import com .alibaba .fastjson .serializer .SerializerFeature ;
27
27
import com .alibaba .fastjson2 .JSONFactory ;
28
28
import com .alibaba .fastjson2 .reader .ObjectReader ;
29
+ import com .alibaba .fastjson2 .util .KotlinUtils ;
29
30
30
31
import java .io .InputStream ;
31
32
import java .io .Reader ;
@@ -47,17 +48,6 @@ public class TypeUtils {
47
48
public static boolean compatibleWithJavaBean ;
48
49
public static boolean compatibleWithFieldName ;
49
50
50
- private static volatile Class kotlin_metadata ;
51
- private static volatile boolean kotlin_metadata_error ;
52
- private static volatile boolean kotlin_class_klass_error ;
53
- private static volatile Constructor kotlin_kclass_constructor ;
54
- private static volatile Method kotlin_kclass_getConstructors ;
55
- private static volatile Method kotlin_kfunction_getParameters ;
56
- private static volatile Method kotlin_kparameter_getName ;
57
- private static volatile boolean kotlin_error ;
58
- private static volatile Map <Class , String []> kotlinIgnores ;
59
- private static volatile boolean kotlinIgnores_error ;
60
-
61
51
private static boolean transientClassInited ;
62
52
private static Class <? extends Annotation > transientClass ;
63
53
@@ -1123,138 +1113,23 @@ static void setAccessible(AccessibleObject obj) {
1123
1113
}
1124
1114
1125
1115
public static boolean isKotlin (Class clazz ) {
1126
- if (kotlin_metadata == null && !kotlin_metadata_error ) {
1127
- try {
1128
- kotlin_metadata = Class .forName ("kotlin.Metadata" );
1129
- } catch (Throwable e ) {
1130
- kotlin_metadata_error = true ;
1131
- }
1132
- }
1133
- return kotlin_metadata != null && clazz .isAnnotationPresent (kotlin_metadata );
1116
+ return KotlinUtils .isKotlin (clazz );
1134
1117
}
1135
1118
1136
1119
public static Constructor getKotlinConstructor (Constructor [] constructors ) {
1137
1120
return getKotlinConstructor (constructors , null );
1138
1121
}
1139
1122
1140
1123
public static Constructor getKotlinConstructor (Constructor [] constructors , String [] paramNames ) {
1141
- Constructor creatorConstructor = null ;
1142
- for (Constructor <?> constructor : constructors ) {
1143
- Class <?>[] parameterTypes = constructor .getParameterTypes ();
1144
- if (paramNames != null && parameterTypes .length != paramNames .length ) {
1145
- continue ;
1146
- }
1147
- // String equals to Class will always return false !
1148
- if (parameterTypes .length > 0 && "kotlin.jvm.internal.DefaultConstructorMarker" .equals (parameterTypes [parameterTypes .length - 1 ].getName ())) {
1149
- continue ;
1150
- }
1151
- if (creatorConstructor != null && creatorConstructor .getParameterTypes ().length >= parameterTypes .length ) {
1152
- continue ;
1153
- }
1154
- creatorConstructor = constructor ;
1155
- }
1156
- return creatorConstructor ;
1124
+ return KotlinUtils .getKotlinConstructor (constructors , paramNames );
1157
1125
}
1158
1126
1159
1127
public static String [] getKoltinConstructorParameters (Class clazz ) {
1160
- if (kotlin_kclass_constructor == null && !kotlin_class_klass_error ) {
1161
- try {
1162
- Class class_kotlin_kclass = Class .forName ("kotlin.reflect.jvm.internal.KClassImpl" );
1163
- kotlin_kclass_constructor = class_kotlin_kclass .getConstructor (Class .class );
1164
- } catch (Throwable e ) {
1165
- kotlin_class_klass_error = true ;
1166
- }
1167
- }
1168
- if (kotlin_kclass_constructor == null ) {
1169
- return null ;
1170
- }
1171
-
1172
- if (kotlin_kclass_getConstructors == null && !kotlin_class_klass_error ) {
1173
- try {
1174
- Class class_kotlin_kclass = Class .forName ("kotlin.reflect.jvm.internal.KClassImpl" );
1175
- kotlin_kclass_getConstructors = class_kotlin_kclass .getMethod ("getConstructors" );
1176
- } catch (Throwable e ) {
1177
- kotlin_class_klass_error = true ;
1178
- }
1179
- }
1180
-
1181
- if (kotlin_kfunction_getParameters == null && !kotlin_class_klass_error ) {
1182
- try {
1183
- Class class_kotlin_kfunction = Class .forName ("kotlin.reflect.KFunction" );
1184
- kotlin_kfunction_getParameters = class_kotlin_kfunction .getMethod ("getParameters" );
1185
- } catch (Throwable e ) {
1186
- kotlin_class_klass_error = true ;
1187
- }
1188
- }
1189
-
1190
- if (kotlin_kparameter_getName == null && !kotlin_class_klass_error ) {
1191
- try {
1192
- Class class_kotlinn_kparameter = Class .forName ("kotlin.reflect.KParameter" );
1193
- kotlin_kparameter_getName = class_kotlinn_kparameter .getMethod ("getName" );
1194
- } catch (Throwable e ) {
1195
- kotlin_class_klass_error = true ;
1196
- }
1197
- }
1198
-
1199
- if (kotlin_error ) {
1200
- return null ;
1201
- }
1202
-
1203
- try {
1204
- Object constructor = null ;
1205
- Object kclassImpl = kotlin_kclass_constructor .newInstance (clazz );
1206
- Iterable it = (Iterable ) kotlin_kclass_getConstructors .invoke (kclassImpl );
1207
- for (Iterator iterator = it .iterator (); iterator .hasNext ();) {
1208
- Object item = iterator .next ();
1209
- List parameters = (List ) kotlin_kfunction_getParameters .invoke (item );
1210
- if (constructor != null && parameters .size () == 0 ) {
1211
- continue ;
1212
- }
1213
- constructor = item ;
1214
- }
1215
-
1216
- if (constructor == null ) {
1217
- return null ;
1218
- }
1219
-
1220
- List parameters = (List ) kotlin_kfunction_getParameters .invoke (constructor );
1221
- String [] names = new String [parameters .size ()];
1222
- for (int i = 0 ; i < parameters .size (); i ++) {
1223
- Object param = parameters .get (i );
1224
- names [i ] = (String ) kotlin_kparameter_getName .invoke (param );
1225
- }
1226
- return names ;
1227
- } catch (Throwable e ) {
1228
- e .printStackTrace ();
1229
- kotlin_error = true ;
1230
- }
1231
- return null ;
1128
+ return KotlinUtils .getKoltinConstructorParameters (clazz );
1232
1129
}
1233
1130
1234
1131
static boolean isKotlinIgnore (Class clazz , String methodName ) {
1235
- if (kotlinIgnores == null && !kotlinIgnores_error ) {
1236
- try {
1237
- Map <Class , String []> map = new HashMap <>();
1238
- Class charRangeClass = Class .forName ("kotlin.ranges.CharRange" );
1239
- map .put (charRangeClass , new String []{"getEndInclusive" , "isEmpty" });
1240
- Class intRangeClass = Class .forName ("kotlin.ranges.IntRange" );
1241
- map .put (intRangeClass , new String []{"getEndInclusive" , "isEmpty" });
1242
- Class longRangeClass = Class .forName ("kotlin.ranges.LongRange" );
1243
- map .put (longRangeClass , new String []{"getEndInclusive" , "isEmpty" });
1244
- Class floatRangeClass = Class .forName ("kotlin.ranges.ClosedFloatRange" );
1245
- map .put (floatRangeClass , new String []{"getEndInclusive" , "isEmpty" });
1246
- Class doubleRangeClass = Class .forName ("kotlin.ranges.ClosedDoubleRange" );
1247
- map .put (doubleRangeClass , new String []{"getEndInclusive" , "isEmpty" });
1248
- kotlinIgnores = map ;
1249
- } catch (Throwable error ) {
1250
- kotlinIgnores_error = true ;
1251
- }
1252
- }
1253
- if (kotlinIgnores == null ) {
1254
- return false ;
1255
- }
1256
- String [] ignores = kotlinIgnores .get (clazz );
1257
- return ignores != null && Arrays .binarySearch (ignores , methodName ) >= 0 ;
1132
+ return KotlinUtils .isKotlinIgnore (clazz , methodName );
1258
1133
}
1259
1134
1260
1135
private static boolean isJSONTypeIgnore (Class <?> clazz , String propertyName ) {
0 commit comments