예제 #1
0
파일: runtime.go 프로젝트: skanjo/javabind
func (j *JavaToGoSet) Convert(obj *gojvm.Object) (err error) {
	iter, err := obj.CallObj(j.env, false, "iterator", types.Class{types.NewName("java.util.Iterator")})
	if err != nil {
		return
	}
	return j.JavaToGoIterator.Convert(iter)
}
예제 #2
0
파일: runtime.go 프로젝트: skanjo/javabind
func (j *JavaToGoMap_Entry) Convert(obj *gojvm.Object) (err error) {
	r_value := reflect.ValueOf(j.entry)

	if r_value.Type().Kind() != reflect.Ptr {
		return errors.New("JavaToGoMapEntry.Convert: dest not ptr")
	}

	r_struct := reflect.Indirect(r_value)
	if r_struct.Type().Kind() != reflect.Struct {
		return errors.New("JavaToGoMapEntry.Convert: dest ptr , does not point to struct")
	}

	keyObj, err := obj.CallObj(j.env, false, "getKey", types.Class{types.JavaLangObject})
	if err != nil {
		return
	}
	valObj, err := obj.CallObj(j.env, false, "getValue", types.Class{types.JavaLangObject})
	if err != nil {
		return
	}

	r_structKey := r_struct.FieldByName("Key")
	j.key.Dest(r_structKey.Addr().Interface())
	callable := &Callable{}
	j.value.Dest(callable)
	if err = j.key.Convert(keyObj); err != nil {
		return err
	}
	if err = j.value.Convert(valObj); err != nil {
		return err
	}

	r_structVal := r_struct.FieldByName("Value")
	r_structValNew := reflect.New(r_structVal.Type().Elem())
	reflect.Indirect(r_structValNew).FieldByName("Callable").Set(reflect.ValueOf(callable))
	r_structVal.Set(r_structValNew)

	if err = j.key.CleanUp(); err != nil {
		return err
	}
	if err = j.value.CleanUp(); err != nil {
		return err
	}

	return
}
예제 #3
0
파일: runtime.go 프로젝트: skanjo/javabind
func (j *JavaToGoList) Convert(obj *gojvm.Object) (err error) {
	r_value := reflect.ValueOf(j.list)

	if r_value.Type().Kind() != reflect.Ptr {
		return errors.New("JavaToGoList.Convert: dest not ptr")
	}

	r_slice := reflect.Indirect(r_value)
	if r_slice.Type().Kind() != reflect.Slice {
		return errors.New("JavaToGoList.Convert: dest ptr , does not point to slice")
	}

	len, err := size(j.env, obj)
	if err != nil {
		return
	}
	for i := 0; i < len; i++ {
		itemObj, err := obj.CallObj(j.env, false, "get", types.Class{types.JavaLangObject}, i)
		if err != nil {
			return err
		}

		r_newElem := reflect.Indirect(reflect.New(r_slice.Type().Elem()))
		if r_newElem.Type().Kind() == reflect.Ptr {
			r_elemVal := reflect.New(r_newElem.Type().Elem())
			r_newElem.Set(r_elemVal)
			c := &Callable{}
			reflect.Indirect(r_elemVal).FieldByName("Callable").Set(reflect.ValueOf(c))
			j.item.Dest(c)
		} else {
			j.item.Dest(r_newElem.Addr().Interface())
		}
		if err = j.item.Convert(itemObj); err != nil {
			return err
		}
		if err = j.item.CleanUp(); err != nil {
			return err
		}

		r_newSlice := reflect.Append(r_slice, r_newElem)
		r_slice.Set(r_newSlice)
	}

	return
}
예제 #4
0
파일: runtime.go 프로젝트: skanjo/javabind
func (j *JavaToGoIterator) Convert(obj *gojvm.Object) (err error) {
	r_value := reflect.ValueOf(j.list)

	if r_value.Type().Kind() != reflect.Ptr {
		return errors.New("JavaToGoList.Convert: dest not ptr")
	}

	r_slice := reflect.Indirect(r_value)
	if r_slice.Type().Kind() != reflect.Slice {
		return errors.New("JavaToGoList.Convert: dest ptr , does not point to slice")
	}

	for {
		hasNext, err := obj.CallBool(j.env, false, "hasNext")
		if err != nil {
			return err
		}
		if hasNext == false {
			break
		}

		next, err := obj.CallObj(j.env, false, "next", types.Class{types.JavaLangObject})
		if err != nil {
			return err
		}

		r_newElem := reflect.Indirect(reflect.New(r_slice.Type().Elem()))
		j.item.Dest(r_newElem.Addr().Interface())
		if err = j.item.Convert(next); err != nil {
			return err
		}
		if err = j.item.CleanUp(); err != nil {
			return err
		}

		r_newSlice := reflect.Append(r_slice, r_newElem)
		r_slice.Set(r_newSlice)
	}

	return
}
예제 #5
0
파일: runtime.go 프로젝트: skanjo/javabind
func (j *JavaToGoMap) Convert(obj *gojvm.Object) (err error) {
	r_value := reflect.ValueOf(j.mapval)

	if r_value.Type().Kind() != reflect.Ptr {
		return errors.New("JavaToGoMap.Convert: dest not ptr")
	}

	r_map := reflect.Indirect(r_value)
	if r_map.Type().Kind() != reflect.Map {
		return errors.New("JavaToGoMap.Convert: dest ptr , does not point to map")
	}

	if r_map.IsNil() {
		r_map.Set(reflect.MakeMap(r_map.Type()))
	}

	keySet, err := obj.CallObj(j.env, false, "keySet", types.Class{types.NewName("java/util/Set")})
	if err != nil {
		return
	}
	keyList, err := j.env.NewInstanceStr("java/util/ArrayList", &gojvm.CastObject{keySet, types.NewName("java/util/Collection")})
	if err != nil {
		return
	}

	len, err := size(j.env, obj)
	if err != nil {
		return
	}
	for i := 0; i < len; i++ {
		keyobj, err := keyList.CallObj(j.env, false, "get", types.Class{types.JavaLangObject}, i)
		if err != nil {
			return err
		}
		valobj, err := obj.CallObj(
			j.env,
			false,
			"get",
			types.Class{types.JavaLangObject},
			&gojvm.CastObject{keyobj, types.JavaLangObject},
		)
		if err != nil {
			return err
		}

		r_newKey := reflect.Indirect(reflect.New(r_map.Type().Key()))
		j.key.Dest(r_newKey.Addr().Interface())
		r_newVal := reflect.Indirect(reflect.New(r_map.Type().Elem()))

		if r_newVal.Type().Kind() == reflect.Ptr {
			r_elemVal := reflect.New(r_newVal.Type().Elem())
			r_newVal.Set(r_elemVal)
			c := &Callable{}
			reflect.Indirect(r_elemVal).FieldByName("Callable").Set(reflect.ValueOf(c))
			j.value.Dest(c)
		} else {
			j.value.Dest(r_newVal.Addr().Interface())
		}

		//		j.value.Dest(r_newVal.Addr().Interface())

		if err = j.key.Convert(keyobj); err != nil {
			return err
		}
		if err = j.value.Convert(valobj); err != nil {
			return err
		}

		if err = j.key.CleanUp(); err != nil {
			return err
		}
		if err = j.value.CleanUp(); err != nil {
			return err
		}

		r_map.SetMapIndex(r_newKey, r_newVal)
	}

	return
}