private JavaArray CreateArray(Array dotNetArray) { bool[] asBool; byte[] asByte; char[] asChar; double[] asDouble; float[] asFloat; int[] asInt; long[] asLong; short[] asShort; JavaObject[] asObject; if ((asBool = dotNetArray as bool[]) != null) { return(new JavaArray(_jniWrapper, _jniWrapper.NewBooleanArray(asBool), new ArrayType(PrimitiveType.Boolean(_jniWrapper)))); } else if ((asByte = dotNetArray as byte[]) != null) { return(new JavaArray(_jniWrapper, _jniWrapper.NewByteArray(asByte), new ArrayType(PrimitiveType.Byte(_jniWrapper)))); } else if ((asChar = dotNetArray as char[]) != null) { return(new JavaArray(_jniWrapper, _jniWrapper.NewCharArray(asChar), new ArrayType(PrimitiveType.Char(_jniWrapper)))); } else if ((asDouble = dotNetArray as double[]) != null) { return(new JavaArray(_jniWrapper, _jniWrapper.NewDoubleArray(asDouble), new ArrayType(PrimitiveType.Double(_jniWrapper)))); } else if ((asFloat = dotNetArray as float[]) != null) { return(new JavaArray(_jniWrapper, _jniWrapper.NewFloatArray(asFloat), new ArrayType(PrimitiveType.Float(_jniWrapper)))); } else if ((asInt = dotNetArray as int[]) != null) { return(new JavaArray(_jniWrapper, _jniWrapper.NewIntArray(asInt), new ArrayType(PrimitiveType.Int(_jniWrapper)))); } else if ((asLong = dotNetArray as long[]) != null) { return(new JavaArray(_jniWrapper, _jniWrapper.NewLongArray(asLong), new ArrayType(PrimitiveType.Long(_jniWrapper)))); } else if ((asShort = dotNetArray as short[]) != null) { return(new JavaArray(_jniWrapper, _jniWrapper.NewShortArray(asShort), new ArrayType(PrimitiveType.Short(_jniWrapper)))); } else if ((asObject = dotNetArray as JavaObject[]) != null) { JavaObject prototype = asObject.FirstOrDefault(o => o != null); if (prototype == null) { throw new InvalidOperationException("Can't construct a Java array without at least one non-null object in there."); } return(new JavaArray(_jniWrapper, prototype.Class.NewArray(asObject.Select(o => o.Pointer).ToArray()), new ArrayType(prototype.Class))); } else { throw new InvalidOperationException("Can't construct a Java array from the given array of the given type: " + dotNetArray.GetType()); } }
public override bool TryGetMember(GetMemberBinder binder, out object result) { if (binder.Name == "class") { result = new JavaObject(_vm, _reflectedClass, new JavaClass(_vm, "java/lang/Class")); return(true); } Tuple <IntPtr, JavaClass> field = GetField(binder.Name); if (field != null) { IntPtr value = _vm.GetStaticObjectField(_jniClass, field.Item1); result = value == IntPtr.Zero ? null : new JavaObject(_vm, value, field.Item2); return(true); } return(base.TryGetMember(binder, out result)); }
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { int index = (int)indexes[0]; PrimitiveType memberAsPrimitive; ArrayType memberAsArray; if ((memberAsPrimitive = _arrayType.MemberType as PrimitiveType) != null) { switch (memberAsPrimitive.Kind) { case PrimitiveTypeKind.Boolean: result = _vm.GetBooleanArray(_arrayPtr)[index]; break; case PrimitiveTypeKind.Byte: result = _vm.GetByteArray(_arrayPtr)[index]; break; case PrimitiveTypeKind.Char: result = _vm.GetCharArray(_arrayPtr)[index]; break; case PrimitiveTypeKind.Double: result = _vm.GetDoubleArray(_arrayPtr)[index]; break; case PrimitiveTypeKind.Float: result = _vm.GetFloatArray(_arrayPtr)[index]; break; case PrimitiveTypeKind.Int: result = _vm.GetIntArray(_arrayPtr)[index]; break; case PrimitiveTypeKind.Long: result = _vm.GetLongArray(_arrayPtr)[index]; break; case PrimitiveTypeKind.Short: result = _vm.GetShortArray(_arrayPtr)[index]; break; default: throw new InvalidOperationException("Unknown primitive kind: " + memberAsPrimitive.Kind); } return(true); } else if ((memberAsArray = _arrayType.MemberType as ArrayType) != null) { result = new JavaArray(_vm, _vm.GetArray(_arrayPtr)[index], memberAsArray); return(true); } else { IntPtr objectPointer = _vm.GetArray(_arrayPtr)[index]; // I'll intentionally pass the actual type as null if we have a non-null object, so we can discover // the precise type instead of just assuming it's of the base class type. result = new JavaObject(_vm, objectPointer, objectPointer == IntPtr.Zero ? (JavaClass)_arrayType.MemberType : null); return(true); } }
private object InvokeMethod(IntPtr method, ParameterInfo[] parameterInfo, bool[] needBoxing) { object result; IntPtr methodReturn = _methodClass.GetReturnType(method); IntPtr jniMethodPtr = _vm.FromReflectedMethod(method); string returnTypeName = _classClass.GetName(methodReturn); JValue[] parameters = parameterInfo.Select((p, i) => p.Value(_vm, needBoxing[i])).ToArray(); PrimitiveType returnTypeAsPrimitive = PrimitiveType.FromString(_vm, returnTypeName); if (methodReturn == IntPtr.Zero || returnTypeName == "void") { result = null; _vm.CallVoidMethod(_objectPointer, jniMethodPtr, parameters); } else if (returnTypeAsPrimitive != null) { switch (returnTypeAsPrimitive.Kind) { case PrimitiveTypeKind.Boolean: result = _vm.CallBooleanMethod(_objectPointer, jniMethodPtr, parameters); break; case PrimitiveTypeKind.Byte: result = _vm.CallByteMethod(_objectPointer, jniMethodPtr, parameters); break; case PrimitiveTypeKind.Char: result = _vm.CallCharMethod(_objectPointer, jniMethodPtr, parameters); break; case PrimitiveTypeKind.Double: result = _vm.CallDoubleMethod(_objectPointer, jniMethodPtr, parameters); break; case PrimitiveTypeKind.Float: result = _vm.CallFloatMethod(_objectPointer, jniMethodPtr, parameters); break; case PrimitiveTypeKind.Int: result = _vm.CallIntMethod(_objectPointer, jniMethodPtr, parameters); break; case PrimitiveTypeKind.Long: result = _vm.CallLongMethod(_objectPointer, jniMethodPtr, parameters); break; case PrimitiveTypeKind.Short: result = _vm.CallShortMethod(_objectPointer, jniMethodPtr, parameters); break; default: throw new InvalidOperationException("Unknown primitive type: " + returnTypeAsPrimitive.Kind); } } else { IntPtr methodResult = _vm.CallObjectMethod(_objectPointer, jniMethodPtr, parameters); if (methodResult != IntPtr.Zero) { if (returnTypeName.StartsWith("[")) { result = new JavaArray(_vm, methodResult, (ArrayType)JavaType.FromReflectedType(_vm, methodReturn)); } else { result = new JavaObject(_vm, methodResult, null); } } else { result = null; } } return(result); }