Ejemplo n.º 1
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));
            }
Ejemplo n.º 2
0
        public bool TryGetClass(GetMemberBinder binder, out object result)
        {
            string qualifiedName = GetQualifiedName(binder.Name);

            // Try to find the class with this name and fail if not found. I really tried to find anyway
            // in java to check for class existence without exceptions but couldn't find any.
            try
            {
                IntPtr classPtr = _vm.FindClass(qualifiedName);
                result = new JavaClass(_vm, classPtr, IntPtr.Zero, qualifiedName);
                return(true);
            }
            catch (JavaException)
            {
                result = null;
                return(false);
            }
        }
Ejemplo n.º 3
0
 internal JavaClass(JniWrapper vm, IntPtr jniClass, IntPtr reflectedClass, string name)
 {
     _vm               = vm;
     _classClass       = new JClassClass(vm);
     _methodClass      = new JMethodClass(vm);
     _fieldClass       = new JFieldClass(vm);
     _constructorClass = new JConstructorClass(vm);
     _classLoaderClass = new JClassLoaderClass(vm);
     _jniClass         = jniClass;
     _reflectedClass   = reflectedClass;
     if (_reflectedClass == IntPtr.Zero)
     {
         if (name == null)
         {
             throw new ArgumentNullException("I can't infer the name from just the JNI class (I should be able to, but I have no idea how).");
         }
         _reflectedClass = _classClass.ForName(name.Replace('/', '.'), true, _classLoaderClass.GetSystemClassLoader());
         if (_reflectedClass == IntPtr.Zero)
         {
             ThrowClassNotFoundException(name);
         }
     }
     if (name == null)
     {
         name = _classClass.GetName(_reflectedClass);
     }
     if (_jniClass == IntPtr.Zero)
     {
         _jniClass = vm.FindClass(name.Replace('.', '/'));
         if (_jniClass == IntPtr.Zero)
         {
             ThrowClassNotFoundException(name);
         }
     }
     _name            = name.Replace('/', '.');
     _allMethods      = _classClass.GetMethods(_reflectedClass).ToList().AsReadOnly();
     _allConstructors = _classClass.GetConstructors(_reflectedClass).ToList().AsReadOnly();
 }