@@ -831,6 +831,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
831
831
}
832
832
}
833
833
834
+ private val stringTpe = defn.StringClass .typeRef
834
835
private val booleanTpe = defn.BooleanClass .typeRef
835
836
private val byteTpe = defn.ByteClass .typeRef
836
837
private val shortTpe = defn.ShortClass .typeRef
@@ -842,20 +843,21 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
842
843
private val anyRefTpe = defn.AnyRefClass .typeRef
843
844
private val anyValTpe = defn.AnyValClass .typeRef
844
845
private val unitTpe = defn.UnitClass .typeRef
846
+ private val nullTpe = defn.NullClass .typeRef
845
847
private val anyTpe = defn.AnyClass .typeRef
848
+ private val wildcardBounds = TypeBounds (defn.NothingClass .typeRef, anyTpe)
849
+ private val optionOfWildcardTpe = defn.OptionClass .typeRef.appliedTo(wildcardBounds)
850
+ private val arrayOfWildcardTpe = defn.ArrayClass .typeRef.appliedTo(wildcardBounds)
846
851
private val arrayOfAnyTpe = defn.ArrayClass .typeRef.appliedTo(anyTpe)
847
- private val stringTpe = TypeRepr .of[String ]
848
- private val optionTpe = TypeRepr .of[Option [? ]]
849
- private val tupleTpe = TypeRepr .of[Tuple ]
850
- private val iterableTpe = TypeRepr .of[Iterable [? ]]
851
- private val iteratorTpe = TypeRepr .of[Iterator [? ]]
852
- private val arrayTpe = TypeRepr .of[Array [? ]]
852
+ private val iterableOfWildcardTpe = Symbol .requiredClass(" scala.collection.Iterable" ).typeRef.appliedTo(wildcardBounds)
853
+ private val iteratorOfWildcardTpe = Symbol .requiredClass(" scala.collection.Iterator" ).typeRef.appliedTo(wildcardBounds)
854
+ private val tupleTpe = Symbol .requiredClass(" scala.Tuple" ).typeRef
853
855
private val iArrayOfAnyRefTpe = TypeRepr .of[IArray [AnyRef ]]
854
- private val namedTpe = TypeRepr .of[ named]
855
- private val stringifiedTpe = TypeRepr .of[ stringified]
856
- private val transientTpe = TypeRepr .of[ transient]
857
- private val jsonKeyCodecTpe = TypeRepr .of[ JsonKeyCodec ]
858
- private val jsonValueCodecTpe = TypeRepr .of[ JsonValueCodec ]
856
+ private val namedTpe = Symbol .requiredClass( " com.github.plokhotnyuk.jsoniter_scala.macros. named" ).typeRef
857
+ private val stringifiedTpe = Symbol .requiredClass( " com.github.plokhotnyuk.jsoniter_scala.macros. stringified" ).typeRef
858
+ private val transientTpe = Symbol .requiredClass( " com.github.plokhotnyuk.jsoniter_scala.macros. transient" ).typeRef
859
+ private val jsonKeyCodecTpe = Symbol .requiredClass( " com.github.plokhotnyuk.jsoniter_scala.core. JsonKeyCodec" ).typeRef
860
+ private val jsonValueCodecTpe = Symbol .requiredClass( " com.github.plokhotnyuk.jsoniter_scala.core. JsonValueCodec" ).typeRef
859
861
private val newArray = Select (New (TypeIdent (defn.ArrayClass )), defn.ArrayClass .primaryConstructor)
860
862
private val newArrayOfAny = TypeApply (newArray, List (Inferred (anyTpe)))
861
863
private val fromIArrayMethod = Select .unique(Ref (Symbol .requiredModule(" scala.runtime.TupleXXL" )), " fromIArray" )
@@ -964,17 +966,17 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
964
966
else tpe.typeSymbol.companionModule
965
967
}
966
968
967
- private def isOption (tpe : TypeRepr , types : List [TypeRepr ]): Boolean = tpe <:< optionTpe &&
968
- (cfg.skipNestedOptionValues || ! types.headOption.exists(_ <:< optionTpe ))
969
+ private def isOption (tpe : TypeRepr , types : List [TypeRepr ]): Boolean = tpe <:< optionOfWildcardTpe &&
970
+ (cfg.skipNestedOptionValues || ! types.headOption.exists(_ <:< optionOfWildcardTpe ))
969
971
970
972
private def isNullable (tpe : TypeRepr ): Boolean = tpe match
971
973
case OrType (left, right) => isNullable(right) || isNullable(left)
972
- case _ => tpe =:= TypeRepr .of[ Null ]
974
+ case _ => tpe =:= nullTpe
973
975
974
976
private def isIArray (tpe : TypeRepr ): Boolean = tpe.typeSymbol.fullName == " scala.IArray$package$.IArray"
975
977
976
978
private def isCollection (tpe : TypeRepr ): Boolean =
977
- tpe <:< iterableTpe || tpe <:< iteratorTpe || tpe <:< arrayTpe || isIArray( tpe)
979
+ tpe <:< arrayOfWildcardTpe || isIArray( tpe) || tpe <:< iterableOfWildcardTpe || tpe <:< iteratorOfWildcardTpe
978
980
979
981
private def isJavaEnum (tpe : TypeRepr ): Boolean = tpe <:< TypeRepr .of[java.lang.Enum [? ]]
980
982
@@ -1274,7 +1276,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
1274
1276
case AnnotatedType (AppliedType (base, _), annot) => AnnotatedType (base.appliedTo(ctArgs), annot)
1275
1277
case _ => polyRes.appliedTo(ctArgs)
1276
1278
case other => fail(s " Primary constructor for ' ${tpe.show}' is not 'MethodType' or 'PolyType' but ' $other'' " )
1277
- } else if (sym.isTerm) Ref ( sym).tpe
1279
+ } else if (sym.isTerm) sym.termRef
1278
1280
else fail(" Only concrete (no free type parameters) Scala classes & objects are supported for ADT leaf classes. " +
1279
1281
s " Please consider using of them for ADT with base ' ${tpe.show}' or provide a custom implicitly accessible codec for the ADT base. " )
1280
1282
}
@@ -1746,7 +1748,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
1746
1748
val sym = Symbol .newMethod(Symbol .spliceOwner, s " f ${fieldIndexAccessors.size}" ,
1747
1749
MethodType (List (" i" ))(_ => intTpe :: Nil , _ => stringTpe))
1748
1750
DefDef (sym, params => {
1749
- val List ( List ( param)) = params
1751
+ val param = params.head.head
1750
1752
val cases = f.map {
1751
1753
var i = - 1
1752
1754
n =>
@@ -1763,14 +1765,14 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
1763
1765
val sym = Symbol .newMethod(Symbol .spliceOwner, s " q ${equalsMethods.size}" ,
1764
1766
MethodType (" x1" :: " x2" :: Nil )(_ => tpe :: tpe :: Nil , _ => booleanTpe))
1765
1767
DefDef (sym, params => {
1766
- val List (List ( x1, x2)) = params
1768
+ val List (x1, x2) = params.head
1767
1769
new Some (f(x1.asExpr.asInstanceOf [Expr [T ]], x2.asExpr.asInstanceOf [Expr [T ]]).asTerm.changeOwner(sym))
1768
1770
})
1769
1771
}).symbol), List (arg1.asTerm, arg2.asTerm)).asExpr.asInstanceOf [Expr [Boolean ]]
1770
1772
1771
1773
private def genArrayEquals [T : Type ](tpe : TypeRepr , x1t : Expr [T ], x2t : Expr [T ]): Expr [Boolean ] =
1772
1774
val tpe1 = typeArg1(tpe)
1773
- if (tpe1 <:< TypeRepr .of[ Array [ ? ]] ) {
1775
+ if (tpe1 <:< arrayOfWildcardTpe ) {
1774
1776
tpe1.asType match
1775
1777
case ' [t1] =>
1776
1778
val x1 = x1t.asInstanceOf [Expr [Array [t1]]]
@@ -1856,7 +1858,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
1856
1858
val ref = Ref (sym)
1857
1859
decodeMethodRefs.update(methodKey, ref)
1858
1860
decodeMethodDefs.addOne(DefDef (sym, params => {
1859
- val List (List ( in, default)) = params
1861
+ val List (in, default) = params.head
1860
1862
new Some (f(in.asExpr.asInstanceOf [Expr [JsonReader ]], default.asExpr.asInstanceOf [Expr [T ]]).asTerm.changeOwner(sym))
1861
1863
}))
1862
1864
ref
@@ -1870,7 +1872,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
1870
1872
val ref = Ref (sym)
1871
1873
encodeMethodRefs.update(methodKey, ref)
1872
1874
encodeMethodDefs.addOne(DefDef (sym, params => {
1873
- val List (List ( x, out)) = params
1875
+ val List (x, out) = params.head
1874
1876
new Some (f(out.asExpr.asInstanceOf [Expr [JsonWriter ]], x.asExpr.asInstanceOf [Expr [T ]]).asTerm.changeOwner(sym))
1875
1877
}))
1876
1878
ref
@@ -1905,7 +1907,11 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
1905
1907
val tpe1 = valueClassValueType(tpe)
1906
1908
tpe1.asType match { case ' [t1] => getClassInfo(tpe).genNew(List (List (genNullValue[t1](tpe1 :: types).asTerm))).asExpr }
1907
1909
} else if (isCollection(tpe)) {
1908
- if (tpe <:< TypeRepr .of[mutable.BitSet ]) ' { new mutable.BitSet }
1910
+ if (tpe <:< arrayOfWildcardTpe) withNullValueFor(tpe) {
1911
+ typeArg1(tpe).asType match { case ' [t1] => genNewArray[t1](Expr (0 )) }
1912
+ } else if (isIArray(tpe)) withNullValueFor(tpe) {
1913
+ typeArg1(tpe).asType match { case ' [t1] => ' { IArray .unsafeFromArray($ {genNewArray[t1](Expr (0 ))}) } }
1914
+ } else if (tpe <:< TypeRepr .of[mutable.BitSet ]) ' { new mutable.BitSet }
1909
1915
else if (tpe <:< TypeRepr .of[collection.BitSet ]) withNullValueFor(tpe)(' { immutable.BitSet .empty })
1910
1916
else if (tpe <:< TypeRepr .of[:: [? ]]) Literal (NullConstant ()).asExpr
1911
1917
else if (tpe <:< TypeRepr .of[List [? ]] || tpe.typeSymbol == TypeRepr .of[Seq [? ]].typeSymbol) ' { Nil }
@@ -1931,15 +1937,11 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
1931
1937
scalaMapEmpty(tpe, typeArg1(tpe), typeArg2(tpe)).asExpr
1932
1938
} else if (tpe <:< TypeRepr .of[collection.Map [? , ? ]]) {
1933
1939
scalaMapEmpty(tpe, typeArg1(tpe), typeArg2(tpe)).asExpr
1934
- } else if (tpe <:< TypeRepr .of[ Iterable [ ? ]] || tpe <:< TypeRepr .of[ Iterator [ ? ]] ) {
1940
+ } else if (tpe <:< iterableOfWildcardTpe || tpe <:< iteratorOfWildcardTpe ) {
1935
1941
scalaCollectionEmpty(tpe, typeArg1(tpe)).asExpr
1936
- } else if (tpe <:< TypeRepr .of[Array [? ]]) withNullValueFor(tpe) {
1937
- typeArg1(tpe).asType match { case ' [t1] => genNewArray[t1](Expr (0 )) }
1938
- } else if (isIArray(tpe)) withNullValueFor(tpe) {
1939
- typeArg1(tpe).asType match { case ' [t1] => ' { IArray .unsafeFromArray($ {genNewArray[t1](Expr (0 ))}) } }
1940
1942
} else ' { null .asInstanceOf [T ] }
1941
1943
} else if (isEnumOrModuleValue(tpe)) enumOrModuleValueRef(tpe).asExpr
1942
- else if (TypeRepr .of[ Null ] <:< tpe) Literal (NullConstant ()).asExpr
1944
+ else if (nullTpe <:< tpe) Literal (NullConstant ()).asExpr
1943
1945
else if (isOpaque(tpe) && ! isNamedTuple(tpe)) {
1944
1946
val sTpe = opaqueDealias(tpe)
1945
1947
sTpe.asType match { case ' [st] => ' { $ {genNullValue[st](sTpe :: types.tail)}.asInstanceOf [T ] } }
@@ -2382,7 +2384,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
2382
2384
val isColl = isCollection(tpe)
2383
2385
val methodKey = new DecoderMethodKey (tpe, isColl & isStringified, useDiscriminator)
2384
2386
if (isColl) {
2385
- if (tpe <:< TypeRepr .of[ Array [ ? ]] || tpe <:< TypeRepr .of[immutable.ArraySeq [? ]] || isIArray(tpe) ||
2387
+ if (tpe <:< arrayOfWildcardTpe || isIArray( tpe) || tpe <:< TypeRepr .of[immutable.ArraySeq [? ]] ||
2386
2388
tpe <:< TypeRepr .of[mutable.ArraySeq [? ]]) withDecoderFor(methodKey, default, in) { (in, default) =>
2387
2389
val tpe1 = typeArg1(tpe)
2388
2390
val types1 = tpe1 :: types
@@ -2672,7 +2674,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
2672
2674
else $emptyCollection
2673
2675
}.asInstanceOf [Expr [T & mutable.Growable [t1]]],
2674
2676
x => genReadValForGrowable(tpe1 :: types, isStringified, x, in), default, identity, in).asInstanceOf [Expr [T ]]
2675
- } else if (tpe <:< TypeRepr .of[ Iterable [ ? ]] || tpe <:< TypeRepr .of[ Iterator [ ? ]] ) withDecoderFor(methodKey, default, in) { (in, default) =>
2677
+ } else if (tpe <:< iterableOfWildcardTpe || tpe <:< iteratorOfWildcardTpe ) withDecoderFor(methodKey, default, in) { (in, default) =>
2676
2678
val tpe1 = typeArg1(tpe)
2677
2679
tpe1.asType match
2678
2680
case ' [t1] =>
@@ -2842,13 +2844,13 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
2842
2844
(fTpe.asType match { case ' [ft] =>
2843
2845
fDefault match {
2844
2846
case Some (d) =>
2845
- if (cfg.transientEmpty && fTpe <:< TypeRepr .of[ Iterable [ ? ]] ) ' {
2847
+ if (cfg.transientEmpty && fTpe <:< iterableOfWildcardTpe ) ' {
2846
2848
val v = $ {getter.asInstanceOf [Expr [ft & Iterable [? ]]]}
2847
2849
if (! v.isEmpty && v != $ {d.asExpr.asInstanceOf [Expr [ft]]}) {
2848
2850
$ {genWriteConstantKey(fieldInfo.mappedName, out)}
2849
2851
$ {genWriteVal(' v , allTypes, fieldInfo.isStringified, None , out)}
2850
2852
}
2851
- } else if (cfg.transientEmpty && fTpe <:< TypeRepr .of[ Iterator [ ? ]] ) ' {
2853
+ } else if (cfg.transientEmpty && fTpe <:< iteratorOfWildcardTpe ) ' {
2852
2854
val v = $ {getter.asInstanceOf [Expr [ft & Iterator [? ]]]}
2853
2855
if (v.hasNext && v != $ {d.asExpr.asInstanceOf [Expr [ft]]}) {
2854
2856
$ {genWriteConstantKey(fieldInfo.mappedName, out)}
@@ -2870,7 +2872,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
2870
2872
$ {genWriteConstantKey(fieldInfo.mappedName, out)}
2871
2873
$ {genWriteVal(' v , allTypes, fieldInfo.isStringified, None , out)}
2872
2874
}
2873
- } else if (fTpe <:< TypeRepr .of[ Array [ ? ]] ) {
2875
+ } else if (fTpe <:< arrayOfWildcardTpe ) {
2874
2876
def cond (v : Expr [Array [? ]])(using Quotes ): Expr [Boolean ] =
2875
2877
val da = d.asExpr.asInstanceOf [Expr [Array [? ]]]
2876
2878
if (cfg.transientEmpty) ' { $v.length != 0 && ! $ {withEqualsFor(fTpe, v, da)((x1, x2) => genArrayEquals(fTpe, x1, x2))} }
@@ -2907,13 +2909,13 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
2907
2909
}
2908
2910
}
2909
2911
case None =>
2910
- if (cfg.transientEmpty && fTpe <:< TypeRepr .of[ Iterable [ ? ]] ) ' {
2912
+ if (cfg.transientEmpty && fTpe <:< iterableOfWildcardTpe ) ' {
2911
2913
val v = $ {getter.asInstanceOf [Expr [ft & Iterable [? ]]]}
2912
2914
if (! v.isEmpty) {
2913
2915
$ {genWriteConstantKey(fieldInfo.mappedName, out)}
2914
2916
$ {genWriteVal(' v , allTypes, fieldInfo.isStringified, None , out)}
2915
2917
}
2916
- } else if (cfg.transientEmpty && fTpe <:< TypeRepr .of[ Iterator [ ? ]] ) ' {
2918
+ } else if (cfg.transientEmpty && fTpe <:< iteratorOfWildcardTpe ) ' {
2917
2919
val v = $ {getter.asInstanceOf [Expr [ft & Iterator [? ]]]}
2918
2920
if (v.hasNext) {
2919
2921
$ {genWriteConstantKey(fieldInfo.mappedName, out)}
@@ -2935,7 +2937,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
2935
2937
$ {genWriteConstantKey(fieldInfo.mappedName, out)}
2936
2938
$ {genWriteVal(' v , allTypes, fieldInfo.isStringified, None , out)}
2937
2939
}
2938
- } else if (cfg.transientEmpty && fTpe <:< TypeRepr .of[ Array [ ? ]] ) ' {
2940
+ } else if (cfg.transientEmpty && fTpe <:< arrayOfWildcardTpe ) ' {
2939
2941
val v = $ {getter.asInstanceOf [Expr [ft & Array [? ]]]}
2940
2942
if (v.length != 0 ) {
2941
2943
$ {genWriteConstantKey(fieldInfo.mappedName, out)}
@@ -3084,7 +3086,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
3084
3086
val isColl = isCollection(tpe)
3085
3087
val methodKey = new EncoderMethodKey (tpe, isColl & isStringified, optWriteDiscriminator.map(x => (x.fieldName, x.fieldValue)))
3086
3088
if (isColl) {
3087
- if (tpe <:< TypeRepr .of[ Array [ ? ]] || tpe <:< TypeRepr .of[immutable.ArraySeq [? ]] || isIArray(tpe) ||
3089
+ if (tpe <:< arrayOfWildcardTpe || isIArray( tpe) || tpe <:< TypeRepr .of[immutable.ArraySeq [? ]]||
3088
3090
tpe <:< TypeRepr .of[mutable.ArraySeq [? ]]) withEncoderFor(methodKey, m, out) { (out, x) =>
3089
3091
val tpe1 = typeArg1(tpe)
3090
3092
tpe1.asType match
@@ -3222,13 +3224,13 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
3222
3224
} else $tx.foreach(x => $ {genWriteVal(' x , types1, isStringified, None , out)})
3223
3225
$out.writeArrayEnd()
3224
3226
}
3225
- } else if (tpe <:< TypeRepr .of[ Iterable [ ? ]] ) withEncoderFor(methodKey, m, out) { (out, x) =>
3227
+ } else if (tpe <:< iterableOfWildcardTpe ) withEncoderFor(methodKey, m, out) { (out, x) =>
3226
3228
val tpe1 = typeArg1(tpe)
3227
3229
tpe1.asType match
3228
3230
case ' [t1] =>
3229
3231
genWriteArray(x.asInstanceOf [Expr [Iterable [t1]]],
3230
3232
(out, x1) => genWriteVal(x1, tpe1 :: types, isStringified, None , out), out)
3231
- } else if (tpe <:< TypeRepr .of[ Iterator [ ? ]] ) withEncoderFor(methodKey, m, out) { (out, x) =>
3233
+ } else if (tpe <:< iteratorOfWildcardTpe ) withEncoderFor(methodKey, m, out) { (out, x) =>
3232
3234
val tpe1 = typeArg1(tpe)
3233
3235
tpe1.asType match
3234
3236
case ' [t1] =>
0 commit comments