示例#1
0
func size(env *gojvm.Environment, obj *gojvm.Object) (len int, err error) {
	len, err = obj.CallInt(env, false, "size")
	if err != nil {
		return
	}
	return
}
示例#2
0
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)
}
示例#3
0
func (g *JavaToGoInetAddress) Convert(obj *gojvm.Object) (err error) {
	v, _, err := obj.CallString(g.env, false, "getHostAddress")
	if err != nil {
		return
	}
	*g.strPtr = v
	return
}
示例#4
0
func (g *JavaToGoDate) Convert(obj *gojvm.Object) (err error) {
	ms, err := obj.CallLong(g.env, false, "getTime")
	if err != nil {
		return
	}
	*g.timePtr = time.Unix(ms/1000, (ms%1000)*1000000)
	return
}
示例#5
0
func (g *JavaToGoBoolean) Convert(obj *gojvm.Object) (err error) {
	v, err := obj.CallBool(g.env, false, "booleanValue")
	if err != nil {
		return
	}
	*g.boolPtr = v
	return
}
示例#6
0
func (g *JavaToGoDouble) Convert(obj *gojvm.Object) (err error) {
	v, err := obj.CallDouble(g.env, false, "floatValue")
	if err != nil {
		return
	}
	*g.floatPtr = v
	return
}
示例#7
0
func (g *JavaToGoInt) Convert(obj *gojvm.Object) (err error) {
	v, err := obj.CallInt(g.env, false, "intValue")
	if err != nil {
		return
	}
	*g.intPtr = v
	return
}
示例#8
0
func (g *JavaToGoLong) Convert(obj *gojvm.Object) (err error) {
	v, err := obj.CallLong(g.env, false, "longValue")
	if err != nil {
		return
	}
	*g.int64Ptr = v
	return
}
示例#9
0
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
}
示例#10
0
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
}
示例#11
0
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
}
示例#12
0
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
}