本文整理汇总了Golang中reflect.Type.Method方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.Method方法的具体用法?Golang Type.Method怎么用?Golang Type.Method使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Type
的用法示例。
在下文中一共展示了Type.Method方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: addImportsForInterfaceMethods
// Add imports for each of the methods of the interface, but not the interface
// itself.
func addImportsForInterfaceMethods(imports importMap, it reflect.Type) {
// Handle each method.
for i := 0; i < it.NumMethod(); i++ {
m := it.Method(i)
addImportsForType(imports, m.Type)
}
}
示例2: newControllerInfo
func newControllerInfo(namespace string, t reflect.Type, defaultAction string) *controllerInfo {
typeName := t.String()
if strings.HasPrefix(typeName, "*") {
panic(errInvalidCtrlType(typeName))
}
numMethod := t.NumMethod()
if numMethod < 1 {
panic(errCtrlNoAction(typeName))
}
methods := make([]string, 0, numMethod)
for i := 0; i < numMethod; i++ {
methodInfo := t.Method(i)
numIn := methodInfo.Type.NumIn()
numOut := methodInfo.Type.NumOut()
if numIn != 1 || numOut != 1 {
continue
}
methodName := methodInfo.Name
methods = append(methods, methodName)
}
if len(methods) < 1 {
panic(errCtrlNoAction(typeName))
}
actions := make(map[string]string, len(methods))
for _, m := range methods {
actions[strings.ToLower(m)] = m
}
return &controllerInfo{
NsName: namespace,
CtrlName: getControllerName(t),
CtrlType: t,
Actions: actions,
DefaultAction: defaultAction,
}
}
示例3: createEventHandlersForType
func createEventHandlersForType(sourceType reflect.Type) handlersMap {
handlers := make(handlersMap)
// Loop through all the methods of the source
methodCount := sourceType.NumMethod()
for i := 0; i < methodCount; i++ {
method := sourceType.Method(i)
// Only match methods that satisfy prefix
if strings.HasPrefix(method.Name, methodHandlerPrefix) {
// Handling methods are defined in code by:
// func (source *MySource) HandleMyEvent(e MyEvent).
// When getting the type of this methods by reflection the signature
// is as following:
// func HandleMyEvent(source *MySource, e MyEvent).
if method.Type.NumIn() == 2 {
eventType := method.Type.In(1)
handler := createEventHandler(method)
handlers[eventType] = handler
}
}
}
return handlers
}
示例4: deepFieldsImpl
func deepFieldsImpl(ifaceType reflect.Type) []string {
fields := []string{}
if ifaceType.Kind() != reflect.Ptr && ifaceType.Kind() != reflect.Struct {
return fields
}
methods := ifaceType.NumMethod()
for i := 0; i < methods; i++ {
var v reflect.Method
v = ifaceType.Method(i)
fields = append(fields, v.Name)
}
if ifaceType.Kind() == reflect.Ptr {
return fields
}
elements := ifaceType.NumField()
for i := 0; i < elements; i++ {
var v reflect.StructField
v = ifaceType.Field(i)
fields = append(fields, v.Name)
}
return fields
}
示例5: 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
}
}
示例6: findRPCMethods
func (s *Service) findRPCMethods(typ reflect.Type) {
for i := 0; i < typ.NumMethod(); i++ {
m := typ.Method(i)
// Don't register callbacks
if m.Name == "Started" || m.Name == "Stopped" || m.Name == "Registered" || m.Name == "Unregistered" {
continue
}
// Only register exported methods
if m.PkgPath != "" {
continue
}
if m.Type.NumOut() != 1 && m.Type.NumOut() != 2 {
continue
}
s.methods[m.Name] = m.Func
//s.Log.Println("Registered RPC Method: " + m.Name)
s.Log.Item(RegisteredMethod{
Method: m.Name,
})
}
}
示例7: generatemethodsEx
func generatemethodsEx(t reflect.Type, ignorefunc func(name string) bool, callobject string, name func(t reflect.Type, m reflect.Method) string) (methods string) {
t2 := t
if t.Kind() == reflect.Ptr {
t2 = t.Elem()
}
for i := 0; i < t.NumMethod(); i++ {
var (
m = t.Method(i)
reason string
)
if m.Name[0] != strings.ToUpper(m.Name[:1])[0] {
reason = "unexported"
goto skip
}
if ignorefunc != nil && ignorefunc(m.Name) {
reason = "in skip list"
goto skip
}
if m, err := generatemethod(m, t2, callobject, name(t2, m)); err != nil {
reason = err.Error()
goto skip
} else {
methods += m
}
continue
skip:
fmt.Printf("Skipping method %s.%s: %s\n", t2, m.Name, reason)
}
return
}
示例8: suitableMethods
// suitableMethods returns suitable Rpc methods of typ, it will report
// error using log if reportErr is true.
func suitableMethods(typ reflect.Type, reportErr bool) map[string]*methodType {
methods := make(map[string]*methodType)
OUTER:
for m := 0; m < typ.NumMethod(); m++ {
method := typ.Method(m)
mtype := method.Type
mname := method.Name
// Method must be exported.
if method.PkgPath != "" {
continue
}
// Method needs at least one ins: receiver
if mtype.NumIn() < 1 {
if reportErr {
log.Println("method", mname, "has wrong number of ins:", mtype.NumIn())
}
continue
}
// check if all args are exported or built in types
argTypes := make([]reflect.Type, mtype.NumIn()-1)
for i := 1; i < mtype.NumIn(); i++ { // first is the receiver which we don't need
if !isExportedOrBuiltinType(mtype.In(i)) {
if reportErr {
log.Printf("%s argument %d not an exported type - %v\n", mname, i, mtype.In(i))
}
continue
}
argTypes[i-1] = mtype.In(i)
}
// check if all return values are exported or built in types
var hasError bool
outTypes := make([]reflect.Type, mtype.NumOut())
for i := 0; i < mtype.NumOut(); i++ {
if mtype.Out(i) == typeOfError {
if hasError { // verify if there is only a single error returned
if reportErr {
log.Printf("%s returns multiple errors\n", mname)
continue OUTER
}
}
hasError = true
outTypes[i] = mtype.Out(i)
} else if isExportedOrBuiltinType(mtype.Out(i)) {
outTypes[i] = mtype.Out(i)
continue
} else {
if reportErr {
log.Printf("Returned argument #%d for %s is of invalid type %v\n", i, mname, mtype.Out(i))
}
continue OUTER
}
}
methods[mname] = &methodType{method: method, ArgTypes: argTypes, ReplyTypes: outTypes, CanRetErr: hasError}
}
return methods
}
示例9: methods
func methods(t reflect.Type) []string {
res := []string{}
for i := 0; i < t.NumMethod(); i++ {
res = append(res, t.Method(i).Name)
}
return res
}
示例10: Methods
func (c *structCache) Methods(typ reflect.Type) map[string]int {
c.methodsl.RLock()
indxs, ok := c.methods[typ]
c.methodsl.RUnlock()
if ok {
return indxs
}
num := typ.NumMethod()
indxs = make(map[string]int, num)
for i := 0; i < num; i++ {
m := typ.Method(i)
if m.Type.NumIn() > 1 {
continue
}
if m.Type.NumOut() != 1 {
continue
}
indxs[m.Name] = m.Index
}
c.methodsl.Lock()
c.methods[typ] = indxs
c.methodsl.Unlock()
return indxs
}
示例11: methodByName
// TODO: delete when reflect's own MethodByName is released.
func methodByName(typ reflect.Type, name string) (reflect.Method, bool) {
for i := 0; i < typ.NumMethod(); i++ {
if typ.Method(i).Name == name {
return typ.Method(i), true
}
}
return reflect.Method{}, false
}
示例12: buildMethodData
func buildMethodData(t reflect.Type) map[string]reflect.Value {
data := make(map[string]reflect.Value)
for i, l := 0, t.NumMethod(); i < l; i++ {
m := t.Method(i)
data[strings.ToLower(m.Name)] = m.Func
}
return data
}
示例13: getMethodsForType
func (e *Exchange) getMethodsForType(t reflect.Type) []string {
r := []string{}
for i := 0; i < t.NumMethod(); i++ {
r = append(r, t.Method(i).Name)
}
return r
}
示例14: suitableMethods
// 合适的方法返回对应的Rpc方法,如果reportError设置为true的话,它会使用log来报告error。
func suitableMethods(typ reflect.Type, reportErr bool) map[string]*methodType {
methods := make(map[string]*methodType)
for m := 0; m < typ.NumMethod(); m++ {
method := typ.Method(m)
mtype := method.Type
mname := method.Name
// Method must be exported.
if method.PkgPath != "" {
continue
}
// Method needs three ins: receiver, *args, *reply.
if mtype.NumIn() != 3 {
if reportErr {
log.Println("method", mname, "has wrong number of ins:", mtype.NumIn())
}
continue
}
// First arg need not be a pointer.
argType := mtype.In(1)
if !isExportedOrBuiltinType(argType) {
if reportErr {
log.Println(mname, "argument type not exported:", argType)
}
continue
}
// Second arg must be a pointer.
replyType := mtype.In(2)
if replyType.Kind() != reflect.Ptr {
if reportErr {
log.Println("method", mname, "reply type not a pointer:", replyType)
}
continue
}
// Reply type must be exported.
if !isExportedOrBuiltinType(replyType) {
if reportErr {
log.Println("method", mname, "reply type not exported:", replyType)
}
continue
}
// Method needs one out.
if mtype.NumOut() != 1 {
if reportErr {
log.Println("method", mname, "has wrong number of outs:", mtype.NumOut())
}
continue
}
// The return type of the method must be error.
if returnType := mtype.Out(0); returnType != typeOfError {
if reportErr {
log.Println("method", mname, "returns", returnType.String(), "not error")
}
continue
}
methods[mname] = &methodType{method: method, ArgType: argType, ReplyType: replyType}
}
return methods
}
示例15: FindMethod
func FindMethod(recvType reflect.Type, funcVal reflect.Value) *reflect.Method {
for i := 0; i < recvType.NumMethod(); i++ {
method := recvType.Method(i)
if method.Func.Pointer() == funcVal.Pointer() {
return &method
}
}
return nil
}