Example #1
0
func (self *Environment) getMethod(t interface{}, static bool, mname string, rType types.Typed, params ...interface{}) (jval C.jvalue, meth *Method, args argList, objList []*Object, err error) {
	switch v := t.(type) {
	case *Object:
		//print("getObjMethod\t",mname, "\t",rType.TypeString(),"\n")
		jval = C.objValue(v.object)
		meth, args, objList, err = self.getObjectMethod(v, static, mname, rType, params...)
	case *Class:
		//			print("getClassMethod\t",mname, "\t",rType.TypeString(),"\n")
		jval = C.objValue(v.class)
		meth, args, objList, err = self.getClassMethod(v, static, mname, rType, params...)
	default:
		panic("getMethod called on unknown type")
	}
	return
}
Example #2
0
func (self *Environment) getField(t interface{}, static bool, mname string, rType types.Typed) (jval C.jvalue, field *Field, err error) {
	if debug {
		log.Printf("getfield: %s - %v", mname, t)
	}
	switch v := t.(type) {
	case *Object:
		jval = C.objValue(v.object)
		field, err = self.getObjectField(v, static, mname, rType)
		/*
			case *Object:
				//print("getObjMethod\t",mname, "\t",rType.TypeString(),"\n")
				jval = C.objValue(v.object)
				meth, args, objList, err = self.getObjectMethod(v, static, mname, rType, params...)
		*/
	case *Class:
		jval = C.objValue(v.class)
		field, err = self.getClassField(v, static, mname, rType)
	default:
		panic("getField called on unknown type")
	}

	return
}
Example #3
0
//	TODO(refcounting): any constructed objects will be leaked on call return,
//	as nothing cleans up proxy objects.  I'm also torn on how to differentiate
//	the objects made here  and those coming in from other references.
//
//	Refcounting attempt 1, objects _we_ construct will be returned in the objStack,
//	otherwise refcounts of 'pass-through' java natives are untouched by the call to newArgList.
//
//	On error, the stack has already been blown (and will be empty).
func newArgList(ctx *Environment, params ...interface{}) (alp argList, objStack []*Object, err error) {
	alp = make(argList, 0)
	defer func() {
		if err != nil {
			blowStack(ctx, objStack)
			objStack = []*Object{}
		}
	}()
	for i, param := range params {
		var ok C.int
		switch v := param.(type) {
		case int:
			alp = append(alp, C.intValue(C.jint(v)))
		case int64:
			alp = append(alp, C.longValue(C.jlong(v)))
		case C.jstring:
			alp = append(alp, C.objValue(v))
		case C.jboolean:
			alp = append(alp, C.boolValue(v))
		case C.jint:
			alp = append(alp, C.intValue(v))
		case C.jobject:
			alp = append(alp, C.objValue(v))
		case *Object:
			alp = append(alp, C.objValue(v.object))
		case *Class:
			alp = append(alp, C.objValue(v.class))
		case C.jvalue:
			alp = append(alp, v)
		case string:
			var str *Object
			str, err = ctx.NewStringObject(v)
			if err == nil {
				objStack = append(objStack, str)
				alp = append(alp, C.objValue(str.object))
			}
		case []string:
			var klass *Class
			var obj *Object
			klass, err = ctx.GetClassStr("java/lang/String")
			// classes via this channel are cached and globally referenced by gojvm, not stacked.
			if err == nil {
				obj, err = ctx.newObjectArray(len(v), klass, nil)
			}
			if err == nil {
				objStack = append(objStack, obj)
				for i, s := range v {
					var str *Object
					str, err = ctx.NewStringObject(s)
					if err == nil {
						// I'm assuming stuffing the array adds a reference inside the JVM.
						defer ctx.DeleteLocalRef(str)
						ctx.setObjectArrayElement(obj, i, str)
						if ctx.ExceptionCheck() {
							err = ctx.ExceptionOccurred()
						}

					}
					if err != nil {
						break
					}
				}
			}
			if err == nil {
				alp = append(alp, C.objValue(obj.object))
			}
		case []byte:
			var obj *Object
			obj, err = ctx.newByteObject(v)
			if err == nil {
				alp = append(alp, C.objValue(obj.object))
			}
			objStack = append(objStack, obj)
		case bool:
			val := C.jboolean(C.JNI_FALSE)
			if v {
				val = C.JNI_TRUE
			}
			alp = append(alp, C.boolValue(val))
		default:
			err = errors.New(fmt.Sprintf("Unknown type: %T/%s", v, v))
		}
		if ok != 0 {
			err = errors.New("Couldn't parse arg #" + strconv.Itoa(i+1))
		}
		if err != nil {
			break
		}
	}
	return
}