Exemple #1
0
 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());
     }
 }
Exemple #2
0
            public static JavaObjectFactory Array(JniWrapper vm, Type dotNetType, Array prototype)
            {
                object innerPrototype = null;

                if (prototype != null && prototype.Length > 0)
                {
                    innerPrototype = prototype.GetValue(0);
                }
                JavaObjectFactory innerFactory  = GetJavaType(vm, dotNetType.GetElementType(), innerPrototype);
                ArrayType         javaArrayType = new ArrayType(innerFactory.JavaType);

                return(new JavaObjectFactory(o =>
                {
                    Array asArray = (Array)o;
                    PrimitiveType asPrimitive = javaArrayType.MemberType as PrimitiveType;
                    if (asPrimitive != null)
                    {
                        switch (asPrimitive.Kind)
                        {
                        case PrimitiveTypeKind.Boolean: return vm.NewBooleanArray((bool[])o);

                        case PrimitiveTypeKind.Byte: return vm.NewByteArray((byte[])o);

                        case PrimitiveTypeKind.Char: return vm.NewCharArray((char[])o);

                        case PrimitiveTypeKind.Double: return vm.NewDoubleArray((double[])o);

                        case PrimitiveTypeKind.Float: return vm.NewFloatArray((float[])o);

                        case PrimitiveTypeKind.Int: return vm.NewIntArray((int[])o);

                        case PrimitiveTypeKind.Long: return vm.NewLongArray((long[])o);

                        case PrimitiveTypeKind.Short: return vm.NewShortArray((short[])o);

                        default: throw new InvalidOperationException("Unknown primitive kind: " + asPrimitive.Kind);
                        }
                    }
                    else
                    {
                        IntPtr[] elements = asArray.Cast <object>().Select(innerFactory._factory).Select(v => v.ToIntPtr()).ToArray();
                        return vm.NewArray(vm.FindClass(innerFactory.JavaType.JniClassName), elements);
                    }
                }, javaArrayType));
            }