예제 #1
0
        private static JavaType TypeByJniName(JniWrapper vm, string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            switch (name[0])
            {
            case 'Z': return(PrimitiveType.Boolean(vm));

            case 'B': return(PrimitiveType.Byte(vm));

            case 'C': return(PrimitiveType.Char(vm));

            case 'S': return(PrimitiveType.Short(vm));

            case 'I': return(PrimitiveType.Int(vm));

            case 'J': return(PrimitiveType.Long(vm));

            case 'F': return(PrimitiveType.Float(vm));

            case 'D': return(PrimitiveType.Double(vm));

            case 'L': return(new JavaClass(vm, name.TrimStart('L').TrimEnd(';').Replace('/', '.')));

            case '[': return(new ArrayType(TypeByJniName(vm, name.Substring(1))));

            default:
                throw new InvalidOperationException("Can't decipher this JNI type name: " + name);
            }
        }
예제 #2
0
        public static JavaType FromReflectedType(JniWrapper vm, IntPtr reflectedType)
        {
            string name = new JClassClass(vm).GetName(reflectedType);

            if (name.StartsWith("["))
            {
                return(TypeByJniName(vm, name));
            }
            PrimitiveType primitiveType = PrimitiveType.FromString(vm, name);

            if (primitiveType != null)
            {
                return(primitiveType);
            }
            return(new JavaClass(vm, IntPtr.Zero, reflectedType, name));
        }
예제 #3
0
 public JavaObject(JniWrapper vm, IntPtr objectPointer, JavaClass javaClass)
 {
     _vm            = vm;
     _objectPointer = objectPointer;
     _methodClass   = new JMethodClass(_vm);
     _classClass    = new JClassClass(_vm);
     _objectClass   = new JObjectClass(_vm);
     if (javaClass == null && _objectPointer != IntPtr.Zero)
     {
         _class = new JavaClass(vm, jniClass: vm.GetObjectClass(_objectPointer), reflectedClass: _objectClass.GetClass(_objectPointer), name: null);
     }
     else
     {
         _class = javaClass;
     }
 }
예제 #4
0
 public static ParameterInfo[] GetParameterInfo(JniWrapper vm, object[] args)
 {
     ParameterInfo[] ret = new ParameterInfo[args.Length];
     for (int i = 0; i < args.Length; i++)
     {
         if (args[i] == null)
         {
             ret[i] = new ParameterInfo(new JValue(IntPtr.Zero), new NullType());
         }
         else
         {
             JavaObjectFactory objectFactory = GetJavaType(vm, args[i].GetType(), args[i]);
             ret[i] = new ParameterInfo(objectFactory.Create(args[i]), objectFactory.JavaType);
         }
     }
     return(ret);
 }
예제 #5
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));
            }
예제 #6
0
 public JValue Value(JniWrapper vm, bool box)
 {
     if (!box)
     {
         return(_value);
     }
     else
     {
         ArrayType asArray;
         if ((asArray = _type as ArrayType) != null)
         {
             JavaArray arrayObject = new JavaArray(vm, _value.ToIntPtr(), asArray);
             return(arrayObject.Box());
         }
         else
         {
             PrimitiveType primitiveType = (PrimitiveType)_type;
             return(primitiveType.GetBoxClass().Box(_value, primitiveType));
         }
     }
 }
예제 #7
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();
 }
예제 #8
0
        public static PrimitiveType FromString(JniWrapper vm, string name)
        {
            switch (name)
            {
            case "byte": return(new PrimitiveType(vm, PrimitiveTypeKind.Byte));

            case "short": return(new PrimitiveType(vm, PrimitiveTypeKind.Short));

            case "int": return(new PrimitiveType(vm, PrimitiveTypeKind.Int));

            case "long": return(new PrimitiveType(vm, PrimitiveTypeKind.Long));

            case "char": return(new PrimitiveType(vm, PrimitiveTypeKind.Char));

            case "float": return(new PrimitiveType(vm, PrimitiveTypeKind.Float));

            case "double": return(new PrimitiveType(vm, PrimitiveTypeKind.Double));

            case "boolean": return(new PrimitiveType(vm, PrimitiveTypeKind.Boolean));

            default: return(null);
            }
        }
예제 #9
0
 public static JavaObjectFactory Char(JniWrapper vm)
 {
     return(new JavaObjectFactory(o => new JValue((char)o), PrimitiveType.Char(vm)));
 }
예제 #10
0
 public static JavaObjectFactory Double(JniWrapper vm)
 {
     return(new JavaObjectFactory(o => new JValue((double)o), PrimitiveType.Double(vm)));
 }
예제 #11
0
 public static JavaObjectFactory Float(JniWrapper vm)
 {
     return(new JavaObjectFactory(o => new JValue((float)o), PrimitiveType.Float(vm)));
 }
예제 #12
0
 public static JavaObjectFactory Long(JniWrapper vm)
 {
     return(new JavaObjectFactory(o => new JValue((long)o), PrimitiveType.Long(vm)));
 }
예제 #13
0
 public static PrimitiveType Boolean(JniWrapper vm)
 {
     return(new PrimitiveType(vm, PrimitiveTypeKind.Boolean));
 }
예제 #14
0
 public static PrimitiveType Int(JniWrapper vm)
 {
     return(new PrimitiveType(vm, PrimitiveTypeKind.Int));
 }
예제 #15
0
 public static JavaObjectFactory Array(JniWrapper vm, ArrayType arrayType)
 {
     return(new JavaObjectFactory(o => new JValue(((JavaArray)o).ArrayPointer), arrayType));
 }
예제 #16
0
 public static PrimitiveType Long(JniWrapper vm)
 {
     return(new PrimitiveType(vm, PrimitiveTypeKind.Long));
 }
예제 #17
0
 public static PrimitiveType Char(JniWrapper vm)
 {
     return(new PrimitiveType(vm, PrimitiveTypeKind.Char));
 }
예제 #18
0
 private DarkJava(string jvmDllPath, bool attemptVmReuse, IEnumerable <JavaOption> options)
 {
     _jniWrapper     = new JniWrapper(jvmDllPath: jvmDllPath, attemptVmReuse: attemptVmReuse, options: options ?? Enumerable.Empty <JavaOption>());
     _defaultPackage = new JavaPackage(_jniWrapper, "");
 }
예제 #19
0
 public static PrimitiveType Float(JniWrapper vm)
 {
     return(new PrimitiveType(vm, PrimitiveTypeKind.Float));
 }
예제 #20
0
 /// <summary>
 /// Constructs the wrapper given the fully qualified class name.
 /// </summary>
 /// <param name="vm"></param>
 /// <param name="name"></param>
 public JavaClass(JniWrapper vm, string name)
     : this(vm, jniClass : IntPtr.Zero, reflectedClass : IntPtr.Zero, name : name)
 {
 }
예제 #21
0
 public JavaPackage(JniWrapper vm, string name)
 {
     _vm   = vm;
     _name = name.Replace('.', '/');
 }
예제 #22
0
 public static JavaObjectFactory Boolean(JniWrapper vm)
 {
     return(new JavaObjectFactory(o => new JValue((bool)o), PrimitiveType.Boolean(vm)));
 }
예제 #23
0
 public static JavaObjectFactory Object(JniWrapper vm, JavaClass javaClass)
 {
     return(new JavaObjectFactory(o => new JValue(((JavaObject)o).Pointer), javaClass));
 }
예제 #24
0
 public static PrimitiveType Short(JniWrapper vm)
 {
     return(new PrimitiveType(vm, PrimitiveTypeKind.Short));
 }
예제 #25
0
 private static JavaObjectFactory GetJavaType(JniWrapper vm, Type dotNetType, object prototype)
 {
     if (dotNetType.IsArray)
     {
         return(JavaObjectFactory.Array(vm, dotNetType, (Array)prototype));
     }
     else if (dotNetType.IsAssignableFrom(typeof(string)))
     {
         return(JavaObjectFactory.String(vm));
     }
     else if (dotNetType.IsAssignableFrom(typeof(byte)))
     {
         return(JavaObjectFactory.Byte(vm));
     }
     else if (dotNetType.IsAssignableFrom(typeof(short)))
     {
         return(JavaObjectFactory.Short(vm));
     }
     else if (dotNetType.IsAssignableFrom(typeof(int)))
     {
         return(JavaObjectFactory.Int(vm));
     }
     else if (dotNetType.IsAssignableFrom(typeof(long)))
     {
         return(JavaObjectFactory.Long(vm));
     }
     else if (dotNetType.IsAssignableFrom(typeof(float)))
     {
         return(JavaObjectFactory.Float(vm));
     }
     else if (dotNetType.IsAssignableFrom(typeof(double)))
     {
         return(JavaObjectFactory.Double(vm));
     }
     else if (dotNetType.IsAssignableFrom(typeof(char)))
     {
         return(JavaObjectFactory.Char(vm));
     }
     else if (dotNetType.IsAssignableFrom(typeof(bool)))
     {
         return(JavaObjectFactory.Boolean(vm));
     }
     else if (dotNetType.IsAssignableFrom(typeof(JavaObject)))
     {
         if (prototype == null)
         {
             throw new InvalidOperationException("Can't infer the type of the Java object without an instance.");
         }
         return(JavaObjectFactory.Object(vm, ((JavaObject)prototype).Class));
     }
     else if (dotNetType.IsAssignableFrom(typeof(JavaArray)))
     {
         if (prototype == null)
         {
             throw new InvalidOperationException("Can't infer the type of the Java array without an instance.");
         }
         return(JavaObjectFactory.Array(vm, ((JavaArray)prototype).ArrayType));
     }
     else
     {
         throw new InvalidOperationException("Can't infer the Java type of " + dotNetType);
     }
 }
예제 #26
0
 public static JavaObjectFactory Byte(JniWrapper vm)
 {
     return(new JavaObjectFactory(o => new JValue((byte)o), PrimitiveType.Byte(vm)));
 }
예제 #27
0
 public static JavaObjectFactory Short(JniWrapper vm)
 {
     return(new JavaObjectFactory(o => new JValue((short)o), PrimitiveType.Short(vm)));
 }
예제 #28
0
 public static PrimitiveType Double(JniWrapper vm)
 {
     return(new PrimitiveType(vm, PrimitiveTypeKind.Double));
 }
예제 #29
0
 public JavaArray(JniWrapper vm, IntPtr arrayPtr, ArrayType arrayType)
 {
     _vm        = vm;
     _arrayPtr  = arrayPtr;
     _arrayType = arrayType;
 }
예제 #30
0
 public static JavaObjectFactory String(JniWrapper vm)
 {
     return(new JavaObjectFactory(o => vm.NewString((string)o), new JavaClass(vm, "java/lang/String")));
 }