Ejemplo n.º 1
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)
}
Ejemplo n.º 2
0
func RegisterNative(className string, method string, paramTypes []string, returnType string, fn interface{}) {
	f := func(t string) types.Typed {
		switch t {
		case "void":
			return types.Basic(types.VoidKind)
		case "int":
			return types.Basic(types.IntKind)
		case "long":
			return types.Basic(types.LongKind)
		case "float":
			return types.Basic(types.FloatKind)
		case "double":
			return types.Basic(types.DoubleKind)
		case "boolean":
			return types.Basic(types.BoolKind)
		default:
			return types.Class{types.NewName(t)}
		}
		return types.Basic(types.VoidKind)
	}

	var msig types.MethodSignature
	for _, v := range paramTypes {
		msig.Params = append(msig.Params, f(v))
	}
	msig.Return = f(returnType)

	GetEnv().RegisterNative(className, method, msig, fn)
}
Ejemplo n.º 3
0
/*
	returns a new *Object of the class named by 'klass' (Wrapper around NewInstance(types.NewName(...)))
*/
func (self *Environment) NewInstanceStr(klass string, params ...interface{}) (obj *Object, err error) {
	class, err := self.GetClass(types.NewName(klass))
	if err != nil {
		return
	}
	return self.NewInstance(class, params...)
}
Ejemplo n.º 4
0
/*
	returns the (potentially cached) types.Name of the class.
*/
func (self *Class) GetName(env *Environment) (name types.Name, err error) {
	//log.Printf("Name(miss)")
	var cstr string
	cstr, _, err = self.CallString(env, false, "getName")
	if err == nil {
		name = types.NewName(cstr)
	}
	return
}
Ejemplo n.º 5
0
func CallStaticObj(class string, method string, retType interface{}, args ...interface{}) (*gojvm.Object, error) {
	var t types.Typed
	switch v := retType.(type) {
	case string:
		t = types.Class{types.NewName(v)}
	case types.Typed:
		t = v
	default:
		panic("Callable.callObj unknown retType type")
	}

	env := GetEnv()
	cls, err := env.GetClass(types.NewName(class))
	if err != nil {
		return nil, err
	}

	return cls.CallObj(env, true, method, t, args...)
}
Ejemplo n.º 6
0
func CallStaticIntArray(class string, method string, args ...interface{}) ([]int, error) {
	env := GetEnv()

	cls, err := env.GetClass(types.NewName(class))
	if err != nil {
		return nil, err
	}

	return cls.CallIntArray(env, true, method, args...)
}
Ejemplo n.º 7
0
func GetFieldStaticBoolean(class string, fieldName string) (bool, error) {
	env := GetEnv()

	cls, err := env.GetClass(types.NewName(class))
	if err != nil {
		return false, err
	}

	return cls.GetBooleanField(env, true, fieldName)
}
Ejemplo n.º 8
0
func CallStaticDouble(class string, method string, args ...interface{}) (float64, error) {
	env := GetEnv()

	cls, err := env.GetClass(types.NewName(class))
	if err != nil {
		return 0, err
	}

	return cls.CallDouble(env, true, method, args...)
}
Ejemplo n.º 9
0
func GetFieldStaticDouble(class string, fieldName string) (float64, error) {
	env := GetEnv()

	cls, err := env.GetClass(types.NewName(class))
	if err != nil {
		return 0, err
	}

	return cls.GetDoubleField(env, true, fieldName)
}
Ejemplo n.º 10
0
func CallStaticBoolean(class string, method string, args ...interface{}) (bool, error) {
	env := GetEnv()

	cls, err := env.GetClass(types.NewName(class))
	if err != nil {
		return false, err
	}

	return cls.CallBoolean(env, true, method, args...)
}
Ejemplo n.º 11
0
func GetFieldStaticInt(class string, fieldName string) (int, error) {
	env := GetEnv()

	cls, err := env.GetClass(types.NewName(class))
	if err != nil {
		return 0, err
	}

	return cls.GetIntField(env, true, fieldName)
}
Ejemplo n.º 12
0
func CallStaticVoid(class string, method string, args ...interface{}) error {
	env := GetEnv()

	cls, err := env.GetClass(types.NewName(class))
	if err != nil {
		return err
	}

	return cls.CallVoid(env, true, method, args...)
}
Ejemplo n.º 13
0
func GetFieldStaticObj(class string, fieldName string, retType interface{}) (*gojvm.Object, error) {
	env := GetEnv()

	var t types.Typed
	switch v := retType.(type) {
	case string:
		t = types.Class{types.NewName(v)}
	case types.Typed:
		t = v
	default:
		panic("Callable.callObj unknown retType type")
	}

	cls, err := env.GetClass(types.NewName(class))
	if err != nil {
		return nil, err
	}

	return cls.GetObjField(env, true, fieldName, t)
}
Ejemplo n.º 14
0
func CallStaticObjArray(class string, method string, retType interface{}, args ...interface{}) (*gojvm.Object, error) {
	var t types.Typed
	switch v := retType.(type) {
	case string:
		t = types.Array{types.Class{types.NewName(v)}}
	case types.Typed:
		t = v
	default:
		panic("Callable.callObj unknown retType type")
	}
	return CallStaticObj(class, method, t, args...)
}
Ejemplo n.º 15
0
func (c *Callable) CallObjArray(method string, retType interface{}, args ...interface{}) (*gojvm.Object, error) {
	var t types.Typed
	switch v := retType.(type) {
	case string:
		t = types.Array{types.Class{types.NewName(v)}}
	case types.Typed:
		t = v
	default:
		panic("Callable.callObj unknown retType type")
	}
	return c.Obj.CallObj(GetEnv(), false, method, t, args...)
}
Ejemplo n.º 16
0
func (c *Callable) SetFieldObj(fieldName string, retType interface{}, val interface{}) error {
	var t types.Typed
	switch v := retType.(type) {
	case string:
		t = types.Class{types.NewName(v)}
	case types.Typed:
		t = v
	default:
		panic("Callable.callObj unknown retType type")
	}

	return c.Obj.SetObjField(GetEnv(), false, fieldName, t, val.(CallableContainer).getCallable().Obj)
}
Ejemplo n.º 17
0
func (c *Callable) GetFieldObjArray(fieldName string, retType interface{}) (*gojvm.Object, error) {
	var t types.Typed
	switch v := retType.(type) {
	case string:
		t = types.Array{types.Class{types.NewName(v)}}
	case types.Typed:
		t = v
	default:
		panic("Callable.callObj unknown retType type")
	}

	return c.Obj.GetObjField(GetEnv(), false, fieldName, t)
}
Ejemplo n.º 18
0
func (self *Environment) RegisterNative(className string, method string, sig types.MethodSignature, fptr interface{}) error {
	class, err := self.GetClass(types.NewName(className))
	if err != nil {
		return err
	}
	cname := C.CString(method)
	defer C.free(unsafe.Pointer(cname))

	csig := C.CString(sig.String())
	defer C.free(unsafe.Pointer(csig))
	C.envRegisterNative(self.env, class.class, cname, csig, fptr.(unsafe.Pointer))

	return nil
}
Ejemplo n.º 19
0
/*
	Returns the (potentially cached) name of the ObjectClass of the
	named object.
*/
func (self *Object) Name(env *Environment) (name types.Name, err error) {
	clsObj, err := self.CallObj(env, false, "getClass", types.Class{ClassClass})
	if err != nil {
		return
	}

	nameStr, _, err := clsObj.CallString(env, false, "getName")
	if err != nil {
		return
	}
	return types.NewName(nameStr), nil
	/*
		var c *Class
		c, err = clsObj.ObjectClass(env)
		if err == nil {
			defer env.DeleteLocalClassRef(c)
			name, err = c.GetName(env)
		} else {
			log.Printf("Couldn't get object class!")
		}
		return
	*/
}
Ejemplo n.º 20
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
}
Ejemplo n.º 21
0
// Wrapper around GetClass(types.NewName(...))
func (self *Environment) GetClassStr(klass string) (c *Class, err error) {
	class := types.NewName(klass)
	return self.GetClass(class)
}
Ejemplo n.º 22
0
func ObjectArray(objects []*gojvm.Object, name string) *gojvm.ObjectArray {
	return &gojvm.ObjectArray{objects, types.NewName(name)}
}
Ejemplo n.º 23
0
func CastObject(obj *gojvm.Object, name string) *gojvm.CastObject {
	return &gojvm.CastObject{obj, types.NewName(name)}
}