From fb44e66e6096f4384308d1bc20958353ba5e39f5 Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Thu, 31 Jul 2025 19:32:11 +0800 Subject: [PATCH] accounts: using reflect.TypeFor --- accounts/abi/abifuzzer_test.go | 2 +- accounts/abi/reflect.go | 24 ++++++++++++------------ accounts/abi/reflect_test.go | 4 ++-- accounts/abi/type.go | 16 ++++++++-------- accounts/keystore/keystore.go | 2 +- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/accounts/abi/abifuzzer_test.go b/accounts/abi/abifuzzer_test.go index dbf6ab6c54..bd27001059 100644 --- a/accounts/abi/abifuzzer_test.go +++ b/accounts/abi/abifuzzer_test.go @@ -79,7 +79,7 @@ func unpackPack(abi ABI, method string, input []byte) ([]interface{}, bool) { func packUnpack(abi ABI, method string, input *[]interface{}) bool { if packed, err := abi.Pack(method, input); err == nil { - outptr := reflect.New(reflect.TypeOf(input)) + outptr := reflect.New(reflect.TypeFor[*[]interface{}]()) err := abi.UnpackIntoInterface(outptr.Interface(), method, packed) if err != nil { panic(err) diff --git a/accounts/abi/reflect.go b/accounts/abi/reflect.go index 729ca93c54..38d8028d7f 100644 --- a/accounts/abi/reflect.go +++ b/accounts/abi/reflect.go @@ -53,7 +53,7 @@ func ConvertType(in interface{}, proto interface{}) interface{} { // indirect recursively dereferences the value until it either gets the value // or finds a big.Int func indirect(v reflect.Value) reflect.Value { - if v.Kind() == reflect.Ptr && v.Elem().Type() != reflect.TypeOf(big.Int{}) { + if v.Kind() == reflect.Ptr && v.Elem().Type() != reflect.TypeFor[big.Int]() { return indirect(v.Elem()) } return v @@ -65,32 +65,32 @@ func reflectIntType(unsigned bool, size int) reflect.Type { if unsigned { switch size { case 8: - return reflect.TypeOf(uint8(0)) + return reflect.TypeFor[uint8]() case 16: - return reflect.TypeOf(uint16(0)) + return reflect.TypeFor[uint16]() case 32: - return reflect.TypeOf(uint32(0)) + return reflect.TypeFor[uint32]() case 64: - return reflect.TypeOf(uint64(0)) + return reflect.TypeFor[uint64]() } } switch size { case 8: - return reflect.TypeOf(int8(0)) + return reflect.TypeFor[int8]() case 16: - return reflect.TypeOf(int16(0)) + return reflect.TypeFor[int16]() case 32: - return reflect.TypeOf(int32(0)) + return reflect.TypeFor[int32]() case 64: - return reflect.TypeOf(int64(0)) + return reflect.TypeFor[int64]() } - return reflect.TypeOf(&big.Int{}) + return reflect.TypeFor[*big.Int]() } // mustArrayToByteSlice creates a new byte slice with the exact same size as value // and copies the bytes in value to the new slice. func mustArrayToByteSlice(value reflect.Value) reflect.Value { - slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len()) + slice := reflect.MakeSlice(reflect.TypeFor[[]byte](), value.Len(), value.Len()) reflect.Copy(slice, value) return slice } @@ -104,7 +104,7 @@ func set(dst, src reflect.Value) error { switch { case dstType.Kind() == reflect.Interface && dst.Elem().IsValid() && (dst.Elem().Type().Kind() == reflect.Ptr || dst.Elem().CanSet()): return set(dst.Elem(), src) - case dstType.Kind() == reflect.Ptr && dstType.Elem() != reflect.TypeOf(big.Int{}): + case dstType.Kind() == reflect.Ptr && dstType.Elem() != reflect.TypeFor[big.Int](): return set(dst.Elem(), src) case srcType.AssignableTo(dstType) && dst.CanSet(): dst.Set(src) diff --git a/accounts/abi/reflect_test.go b/accounts/abi/reflect_test.go index 577fa6ca71..f5e509c52f 100644 --- a/accounts/abi/reflect_test.go +++ b/accounts/abi/reflect_test.go @@ -204,12 +204,12 @@ func TestConvertType(t *testing.T) { var fields []reflect.StructField fields = append(fields, reflect.StructField{ Name: "X", - Type: reflect.TypeOf(new(big.Int)), + Type: reflect.TypeFor[*big.Int](), Tag: "json:\"" + "x" + "\"", }) fields = append(fields, reflect.StructField{ Name: "Y", - Type: reflect.TypeOf(new(big.Int)), + Type: reflect.TypeFor[*big.Int](), Tag: "json:\"" + "y" + "\"", }) val := reflect.New(reflect.StructOf(fields)) diff --git a/accounts/abi/type.go b/accounts/abi/type.go index e59456f15a..3538bd992c 100644 --- a/accounts/abi/type.go +++ b/accounts/abi/type.go @@ -238,9 +238,9 @@ func (t Type) GetType() reflect.Type { case UintTy: return reflectIntType(true, t.Size) case BoolTy: - return reflect.TypeOf(false) + return reflect.TypeFor[bool]() case StringTy: - return reflect.TypeOf("") + return reflect.TypeFor[string]() case SliceTy: return reflect.SliceOf(t.Elem.GetType()) case ArrayTy: @@ -248,19 +248,19 @@ func (t Type) GetType() reflect.Type { case TupleTy: return t.TupleType case AddressTy: - return reflect.TypeOf(common.Address{}) + return reflect.TypeFor[common.Address]() case FixedBytesTy: - return reflect.ArrayOf(t.Size, reflect.TypeOf(byte(0))) + return reflect.ArrayOf(t.Size, reflect.TypeFor[byte]()) case BytesTy: - return reflect.SliceOf(reflect.TypeOf(byte(0))) + return reflect.SliceOf(reflect.TypeFor[byte]()) case HashTy: // hashtype currently not used - return reflect.ArrayOf(32, reflect.TypeOf(byte(0))) + return reflect.ArrayOf(32, reflect.TypeFor[byte]()) case FixedPointTy: // fixedpoint type currently not used - return reflect.ArrayOf(32, reflect.TypeOf(byte(0))) + return reflect.ArrayOf(32, reflect.TypeFor[byte]()) case FunctionTy: - return reflect.ArrayOf(24, reflect.TypeOf(byte(0))) + return reflect.ArrayOf(24, reflect.TypeFor[byte]()) default: panic("Invalid type") } diff --git a/accounts/keystore/keystore.go b/accounts/keystore/keystore.go index 3e85b0433b..fefba026ae 100644 --- a/accounts/keystore/keystore.go +++ b/accounts/keystore/keystore.go @@ -50,7 +50,7 @@ var ( ) // KeyStoreType is the reflect type of a keystore backend. -var KeyStoreType = reflect.TypeOf(&KeyStore{}) +var KeyStoreType = reflect.TypeFor[*KeyStore]() // KeyStoreScheme is the protocol scheme prefixing account and wallet URLs. const KeyStoreScheme = "keystore"