Пример #1
0
        public static T[] WrapJavaArrayObject <T>(JniLocalHandle handle)
        {
            if (JniHandle.IsNull(handle))
            {
                return(null);
            }

            JNIEnv env    = JNIEnv.ThreadEnv;
            var    length = env.GetArrayLength(handle);

            T[] ret = new T[length];

            Type type = typeof(T).GetElementType();

            if (type.IsPrimitive)
            {
                if (type == typeof(int) || type == typeof(uint))
                {
                    env.GetIntArrayRegion(handle, 0, length, ret as int[]);
                    return(ret);
                }
                if (type == typeof(long) || type == typeof(ulong))
                {
                    env.GetLongArrayRegion(handle, 0, length, ret as long[]);
                    return(ret);
                }
                if (type == typeof(bool))
                {
                    env.GetBooleanArrayRegion(handle, 0, length, ret as bool[]);
                    return(ret);
                }
                if (type == typeof(double))
                {
                    env.GetDoubleArrayRegion(handle, 0, length, ret as double[]);
                    return(ret);
                }
                if (type == typeof(byte) || type == typeof(sbyte))
                {
                    env.GetByteArrayRegion(handle, 0, length, ret as byte[]);
                    return(ret);
                }
                if (type == typeof(char))
                {
                    env.GetCharArrayRegion(handle, 0, length, ret as char[]);
                    return(ret);
                }
                if (type == typeof(short) || type == typeof(ushort))
                {
                    env.GetShortArrayRegion(handle, 0, length, ret as short[]);
                    return(ret);
                }
                if (type == typeof(float))
                {
                    env.GetFloatArrayRegion(handle, 0, length, ret as float[]);
                    return(ret);
                }
            }

            if (type.IsArray)
            {
                MethodInfo method = mWrapJavaArrayObject.MakeGenericMethod(type.GetElementType());
                for (int i = 0; i < length; i++)
                {
                    ret[i] = (T)method.Invoke(null, new object[] { env.GetObjectArrayElement(handle, i) });
                }
            }
            else if (type.IsInterface)
            {
                MethodInfo method = mWrapIJavaObject.MakeGenericMethod(type);
                for (int i = 0; i < length; i++)
                {
                    ret[i] = (T)method.Invoke(null, new object[] { env.GetObjectArrayElement(handle, i) });
                }
            }
            else if (type.IsSubclassOf(typeof(JavaException)))
            {
                JavaException[] exceptions = ret as JavaException[];
                for (int i = 0; i < length; i++)
                {
                    exceptions[i] = WrapJavaException(env.GetObjectArrayElement(handle, i));
                }
            }
            else if (type.IsSubclassOf(typeof(JavaObject)))
            {
                JavaObject[] objects = ret as JavaObject[];
                for (int i = 0; i < length; i++)
                {
                    objects[i] = WrapJavaObject(env.GetObjectArrayElement(handle, i));
                }
            }
            else
            {
                throw new InvalidOperationException("Trying to convert unknown type.");
            }

            return(ret);
        }