本文整理汇总了Golang中reflect.Type.MethodByName方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.MethodByName方法的具体用法?Golang Type.MethodByName怎么用?Golang Type.MethodByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Type
的用法示例。
在下文中一共展示了Type.MethodByName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: findEnterFunc
func (h *Handler) findEnterFunc(t reflect.Type) *reflect.Method {
method, ok := t.MethodByName(flaglyEnter)
if ok {
return &method
}
return nil
}
示例2: compileField
func compileField(typ reflect.Type, name string, args []parse.Node, final reflect.Type) (fn lookupFn, elem reflect.Type) {
if isNilType(typ) {
fn = compileFieldDynamic(name, args)
return
}
if m, exist := typ.MethodByName(name); exist {
fn = compileMethodCall(typ, m.Func, args, final)
elem = m.Type.Out(0)
return
}
switch typ.Kind() {
case reflect.Struct:
structField, found := typ.FieldByName(name)
if !found {
panic(fmt.Errorf("%s has no field %s", typ, name))
}
fn = func(s state, v reflect.Value, final interface{}) reflect.Value {
return v.FieldByIndex(structField.Index)
}
elem = structField.Type
return
case reflect.Map:
k := reflect.ValueOf(name)
fn = func(s state, v reflect.Value, final interface{}) reflect.Value {
return v.MapIndex(k)
}
elem = typ.Key()
return
}
panic(fmt.Errorf("struct or map expected, but got %s", typ))
}
示例3: CheckMethod
func CheckMethod(t *testing.T, typ reflect.Type, desc Method) {
method, found := typ.MethodByName(desc.Name)
if !found {
t.Fatalf("Le type %v ne définit pas de méthode '%s'", typ, desc.Name)
}
if method.Type.NumIn() != len(desc.InTypes)+1 {
t.Fatalf("La méthode %v.%s doit prendre %d paramètre(s)", typ, desc.Name, len(desc.InTypes))
}
if method.Type.NumOut() != len(desc.OutTypes) {
t.Fatalf("La méthode %v.%s doit renvoyer %d résultat(s)", typ, desc.Name, len(desc.OutTypes))
}
for i, inType := range desc.InTypes {
if method.Type.In(i+1) != inType {
t.Fatalf("La méthode %v.%s doit prendre un type %v en paramètre #%d", typ, desc.Name, inType, i+1)
}
}
for i, outType := range desc.OutTypes {
if method.Type.Out(i) != outType {
t.Fatalf("La méthode %v.%s doit renvoyer un type %v en résultat #%d", typ, desc.Name, outType, i+1)
}
}
}
示例4: addMethods
func (info *Info) addMethods(jt *Type, t reflect.Type) {
// Add any methods.
var vt reflect.Type
if t.Kind() != reflect.Interface && !isWithoutReceiver(t) {
t = reflect.PtrTo(t)
vt = t.Elem()
}
for i := 0; i < t.NumMethod(); i++ {
m := t.Method(i)
if m.PkgPath != "" {
continue
}
if t.Kind() != reflect.Interface {
m.Type = withoutReceiverType{m.Type}
}
jm := Method{
Name: m.Name,
Type: info.Ref(m.Type),
}
if vt != nil {
_, hasValueMethod := vt.MethodByName(m.Name)
jm.PtrReceiver = !hasValueMethod
}
if jt.Methods == nil {
jt.Methods = make(map[string]*Method)
}
jt.Methods[jm.Name] = &jm
}
}
示例5: dispose
func dispose(v uintptr, t r.Type) {
if m, ok := t.MethodByName("Dispose"); ok {
// && m.Func.Type().NumIn() == 2 {
f := m.Func
tv := r.NewAt(f.Type().In(1).Elem(),
unsafe.Pointer(v))
f.Call([]r.Value{r.New(t).Elem(), tv})
}
}
示例6: missingMethod
func missingMethod(iT, xT reflect.Type) (method string) {
numMethod := iT.NumMethod()
for i := 0; i < numMethod; i += 1 {
method = iT.Method(i).Name
if _, ok := xT.MethodByName(method); !ok {
return
}
}
return ""
}
示例7: getMapMethods
func getMapMethods(baseType reflect.Type) (get, set reflect.Type, err error) {
getAll, ok := baseType.MethodByName("GetAll")
if !ok {
return nil, nil, errors.Errorf("no method named GetAll")
}
setAll, ok := baseType.MethodByName("SetAll")
if !ok {
return nil, nil, errors.Errorf("no method named SetAll")
}
return getAll.Type, setAll.Type, nil
}
示例8: MethodsMissingFromType
func MethodsMissingFromType(inter, typ reflect.Type) []string {
missingMethods := make([]string, 0)
for n := 0; n < inter.NumMethod(); n++ {
_, present := typ.MethodByName(inter.Method(n).Name)
if !present {
fmt.Println(inter.Method(n).Name)
missingMethods = append(missingMethods, inter.Method(n).Name)
}
}
return missingMethods
}
示例9: enumSymbols
func enumSymbols(t reflect.Type) []string {
if _, ok := t.MethodByName("SymbolSet"); ok {
//an enum type that RDL generated. Create a dummy instance and call to get the symbol set
tmp := reflect.New(t)
vv := tmp.Elem()
vv.SetInt(1)
symbols := tmp.MethodByName("SymbolSet").Call([]reflect.Value{})[0].Interface()
return symbols.([]string)[1:]
}
return nil
}
示例10: findParameterGetMethod
func findParameterGetMethod(handlerParametersType reflect.Type, field reflect.Type) (reflect.Method, bool) {
var name []rune
switch field.Kind() {
case reflect.Ptr:
name = []rune(field.Elem().Kind().String())
default:
name = []rune(field.Kind().String())
}
name[0] = unicode.ToUpper(name[0])
methodName := "Get" + string(name)
return handlerParametersType.MethodByName(methodName)
}
示例11: methodValue
func methodValue(key string, typ reflect.Type) (mapFunc, reflect.Type, error) {
if m, ok := typ.MethodByName(key); ok {
if m.Type.NumIn() > 1 {
return nil, nil, fmt.Errorf("method %s on type %s has %d arguments, must have none", key, typ, m.Type.NumIn()-1)
}
if m.Type.NumOut() != 1 {
return nil, nil, fmt.Errorf("method %s on type %s returns %d values, must return one", key, typ, m.Type.NumOut())
}
return methodValueFunc(m), m.Type.Out(0), nil
}
return nil, nil, nil
}
示例12: getTypes
func getTypes(t reflect.Type, s supportingTypes, c *int) *Type {
n, p := t.String(), t.PkgPath()
if _, ok := t.MethodByName("MarshalJSON"); ok {
return custom(n, p)
}
switch t.Kind() {
case reflect.String:
return simple(String)
case reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return simple(Int)
case reflect.Float32, reflect.Float64:
return simple(Float)
case reflect.Bool:
return simple(Bool)
case reflect.Ptr:
return s.set(n, p, func() *Type {
return ptrOf(getTypes(t.Elem(), s, c))
})
case reflect.Array, reflect.Slice:
if t.Elem().Kind() == reflect.Uint8 {
return simple(Bytes)
}
return s.set(n, p, func() *Type {
return arrayOf(getTypes(t.Elem(), s, c))
})
case reflect.Struct:
if p == "" {
// gets an anonymous type name and fake the package
n = anonTypeName(c)
p = "github.com/shutej/go2flow/anonymous"
}
return s.set(n, p, func() *Type {
return structOf(getFields(t, s, c))
})
case reflect.Map:
if t.Key().Kind() != reflect.String {
log.Fatalf("unexpected map key type: %v", t.Key())
}
return s.set(n, p, func() *Type {
return mapOf(getTypes(t.Elem(), s, c))
})
}
log.Fatalf("unknown kind for type: %v", t)
return nil
}
示例13: createTypeDescribe
func createTypeDescribe(typ reflect.Type) *typeDescribe {
td := &typeDescribe{}
td.initMethod, td.hasInitMethod = typ.MethodByName("PostConstruct")
for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i)
tag := f.Tag.Get("inject")
if tag != "" {
td.fields = append(td.fields, f)
}
}
typeDescribes[typ.String()] = td
return td
}
示例14: go2jsonType
func go2jsonType(t reflect.Type, canAddr bool) *Type {
if _, ok := t.MethodByName("MarshalJSON"); ok {
return CustomType(t.String())
}
if canAddr {
if _, ok := reflect.PtrTo(t).MethodByName("MarshalJSON"); ok {
return CustomType(t.String())
}
}
switch t.Kind() {
case reflect.Bool:
return SimpleType(Bool)
case reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Float32, reflect.Float64:
return SimpleType(Number)
case reflect.String:
return SimpleType(String)
case reflect.Complex64, reflect.Complex128:
return CustomType("complex")
case reflect.Chan:
return CustomType("chan")
case reflect.Array, reflect.Slice:
return ArrayOf(go2jsonType(t.Elem(), canAddr || t.Kind() == reflect.Slice))
case reflect.Map:
if t.Key().Kind() != reflect.String {
// TODO better error recovery
panic("unexpected map key kind")
}
return MapOf(go2jsonType(t.Elem(), false))
case reflect.Ptr:
return NullableOf(go2jsonType(t.Elem(), true))
case reflect.Struct:
return ObjectOf(jsonFields(t, canAddr))
case reflect.UnsafePointer:
return CustomType("unsafe.Pointer")
case reflect.Interface:
return CustomType("any")
case reflect.Func:
return CustomType("func")
}
panic(fmt.Errorf("unknown kind for type %s", t))
}
示例15: callMethod
func (p *Page) callMethod(tpc reflect.Type, vpc reflect.Value, action string, rvr reflect.Value, rvw reflect.Value) []reflect.Value {
arv := []reflect.Value{}
if rm, ok := tpc.MethodByName(action); ok {
mt := rm.Type
switch mt.NumIn() {
case 2:
if mt.In(1) == rvr.Type() {
arv = vpc.MethodByName(action).Call([]reflect.Value{rvr})
} else {
arv = vpc.MethodByName(action).Call([]reflect.Value{rvw})
}
case 3:
arv = vpc.MethodByName(action).Call([]reflect.Value{rvw, rvr})
default:
arv = vpc.MethodByName(action).Call([]reflect.Value{})
}
}
return arv
}