ConstructorInfo GetPeerConstructor(JniObjectReference instance, Type fallbackType)
            {
                var klass       = JniEnvironment.Types.GetObjectClass(instance);
                var jniTypeName = JniEnvironment.Types.GetJniTypeNameFromClass(klass);

                Type type = null;

                while (jniTypeName != null)
                {
                    type = Runtime.TypeManager.GetType(JniTypeSignature.Parse(jniTypeName));

                    if (type != null)
                    {
                        var ctor = GetActivationConstructor(type);

                        if (ctor != null)
                        {
                            JniObjectReference.Dispose(ref klass);
                            return(ctor);
                        }
                    }

                    var super = JniEnvironment.Types.GetSuperclass(klass);
                    jniTypeName = super.IsValid
                                                ? JniEnvironment.Types.GetJniTypeNameFromClass(super)
                                                : null;

                    JniObjectReference.Dispose(ref klass, JniObjectReferenceOptions.CopyAndDispose);
                    klass = super;
                }
                JniObjectReference.Dispose(ref klass, JniObjectReferenceOptions.CopyAndDispose);

                return(GetActivationConstructor(fallbackType));
            }
 public virtual IEnumerable <Type> GetTypes(JniTypeSignature typeSignature)
 {
     if (typeSignature.SimpleReference == null)
     {
         return(EmptyTypeArray);
     }
     return(CreateGetTypesEnumerator(typeSignature));
 }
Ejemplo n.º 3
0
 static JniTypeSignature GetCachedTypeSignature(ref JniTypeSignature field, string signature, int arrayRank = 0, bool keyword = false)
 {
     if (!field.IsValid)
     {
         field = new JniTypeSignature(signature, arrayRank, keyword);
     }
     return(field);
 }
Ejemplo n.º 4
0
            internal Type GetRuntimeType(JniObjectReference reference)
            {
                JniTypeSignature signature;

                if (!JniTypeSignature.TryParse(JniEnvironment.Types.GetJniTypeNameFromInstance(reference), out signature))
                {
                    return(null);
                }
                return(Runtime.TypeManager.GetType(signature));
            }
Ejemplo n.º 5
0
        static JniObjectReference _NewArray(int length)
        {
            var info = JniEnvironment.Runtime.TypeManager.GetTypeSignature(typeof(T));

            if (info.SimpleReference == null)
            {
                info = new JniTypeSignature("java/lang/Object", info.ArrayRank);
            }
            if (info.IsKeyword && info.ArrayRank == 0)
            {
                info = info.GetPrimitivePeerTypeSignature();
            }
            using (var t = new JniType(info.Name)) {
                return(JniEnvironment.Arrays.NewObjectArray(length, t.PeerReference, new JniObjectReference()));
            }
        }
Ejemplo n.º 6
0
            IEnumerable <Type> CreateGetTypesEnumerator(JniTypeSignature typeSignature)
            {
                foreach (var type in GetTypesForSimpleReference(typeSignature.SimpleReference))
                {
                    if (typeSignature.ArrayRank == 0)
                    {
                        yield return(type);

                        continue;
                    }
#if !XA_INTEGRATION
                    if (typeSignature.ArrayRank > 0)
                    {
                        var rank      = typeSignature.ArrayRank;
                        var arrayType = type;
                        if (typeSignature.IsKeyword)
                        {
                            arrayType = typeof(JavaPrimitiveArray <>).MakeGenericType(arrayType);
                            rank--;
                        }
                        while (rank-- > 0)
                        {
                            arrayType = typeof(JavaObjectArray <>).MakeGenericType(arrayType);
                        }
                        yield return(arrayType);
                    }
#endif  // !XA_INTEGRATION
                    if (typeSignature.ArrayRank > 0)
                    {
                        var rank      = typeSignature.ArrayRank;
                        var arrayType = type;
                        while (rank-- > 0)
                        {
                            arrayType = arrayType.MakeArrayType();
                        }
                        yield return(arrayType);
                    }
                }
            }
Ejemplo n.º 7
0
            public JniTypeSignature GetTypeSignature(Type type)
            {
                AssertValid();

                if (type == null)
                {
                    throw new ArgumentNullException(nameof(type));
                }
                if (type.ContainsGenericParameters)
                {
                    throw new ArgumentException($"'{type}' contains a generic type definition. This is not supported.", nameof(type));
                }

                type = GetUnderlyingType(type, out int rank);

                JniTypeSignature signature = JniTypeSignature.Empty;

                if (GetBuiltInTypeSignature(type, ref signature))
                {
                    return(signature.AddArrayRank(rank));
                }
                if (GetBuiltInTypeArraySignature(type, ref signature))
                {
                    return(signature.AddArrayRank(rank));
                }

                var simpleRef = GetSimpleReference(type);

                if (simpleRef != null)
                {
                    return(new JniTypeSignature(simpleRef, rank, false));
                }

                var name = type.GetCustomAttribute <JniTypeSignatureAttribute> (inherit: false);

                if (name != null)
                {
                    return(new JniTypeSignature(name.SimpleReference, name.ArrayRank + rank, name.IsKeyword));
                }

                var isGeneric  = type.IsGenericType;
                var genericDef = isGeneric ? type.GetGenericTypeDefinition() : type;

                if (isGeneric)
                {
                    if (genericDef == typeof(JavaArray <>) || genericDef == typeof(JavaObjectArray <>))
                    {
                        var r = GetTypeSignature(type.GenericTypeArguments [0]);
                        return(r.AddArrayRank(rank + 1));
                    }
                }

                if (isGeneric)
                {
                    simpleRef = GetSimpleReference(genericDef);
                    if (simpleRef != null)
                    {
                        return(new JniTypeSignature(simpleRef, rank, false));
                    }
                }

                return(default);
Ejemplo n.º 8
0
            public Type    GetType(JniTypeSignature typeSignature)
            {
                AssertValid();

                return(GetTypes(typeSignature).FirstOrDefault());
            }
 public Type    GetType(JniTypeSignature typeSignature)
 {
     return(GetTypes(typeSignature).FirstOrDefault());
 }
Ejemplo n.º 10
0
        static bool GetBuiltInTypeSignature(Type type, ref JniTypeSignature signature)
        {
            switch (Type.GetTypeCode(type))
            {
            case TypeCode.String:
                signature = GetCachedTypeSignature(ref __StringTypeSignature, "java/lang/String");
                return(true);

            case TypeCode.Boolean:
                signature = GetCachedTypeSignature(ref __BooleanTypeSignature, "Z", arrayRank: 0, keyword: true);
                return(true);

            case TypeCode.SByte:
                signature = GetCachedTypeSignature(ref __SByteTypeSignature, "B", arrayRank: 0, keyword: true);
                return(true);

            case TypeCode.Char:
                signature = GetCachedTypeSignature(ref __CharTypeSignature, "C", arrayRank: 0, keyword: true);
                return(true);

            case TypeCode.Int16:
                signature = GetCachedTypeSignature(ref __Int16TypeSignature, "S", arrayRank: 0, keyword: true);
                return(true);

            case TypeCode.Int32:
                signature = GetCachedTypeSignature(ref __Int32TypeSignature, "I", arrayRank: 0, keyword: true);
                return(true);

            case TypeCode.Int64:
                signature = GetCachedTypeSignature(ref __Int64TypeSignature, "J", arrayRank: 0, keyword: true);
                return(true);

            case TypeCode.Single:
                signature = GetCachedTypeSignature(ref __SingleTypeSignature, "F", arrayRank: 0, keyword: true);
                return(true);

            case TypeCode.Double:
                signature = GetCachedTypeSignature(ref __DoubleTypeSignature, "D", arrayRank: 0, keyword: true);
                return(true);

            case TypeCode.DateTime:
            case TypeCode.DBNull:
            case TypeCode.Decimal:
            case TypeCode.Empty:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                return(false);
            }

            if (type == typeof(void))
            {
                signature = GetCachedTypeSignature(ref __VoidTypeSignature, "V", arrayRank: 0, keyword: true);
                return(true);
            }

            if (!type.IsValueType)
            {
                return(false);
            }

            if (type == typeof(Boolean?))
            {
                signature = GetCachedTypeSignature(ref __BooleanNullableTypeSignature, "java/lang/Boolean");
                return(true);
            }
            if (type == typeof(SByte?))
            {
                signature = GetCachedTypeSignature(ref __SByteNullableTypeSignature, "java/lang/Byte");
                return(true);
            }
            if (type == typeof(Char?))
            {
                signature = GetCachedTypeSignature(ref __CharNullableTypeSignature, "java/lang/Character");
                return(true);
            }
            if (type == typeof(Int16?))
            {
                signature = GetCachedTypeSignature(ref __Int16NullableTypeSignature, "java/lang/Short");
                return(true);
            }
            if (type == typeof(Int32?))
            {
                signature = GetCachedTypeSignature(ref __Int32NullableTypeSignature, "java/lang/Integer");
                return(true);
            }
            if (type == typeof(Int64?))
            {
                signature = GetCachedTypeSignature(ref __Int64NullableTypeSignature, "java/lang/Long");
                return(true);
            }
            if (type == typeof(Single?))
            {
                signature = GetCachedTypeSignature(ref __SingleNullableTypeSignature, "java/lang/Float");
                return(true);
            }
            if (type == typeof(Double?))
            {
                signature = GetCachedTypeSignature(ref __DoubleNullableTypeSignature, "java/lang/Double");
                return(true);
            }

            return(false);
        }
            internal Type GetRuntimeType(JniObjectReference reference)
            {
                var signature = JniTypeSignature.Parse(JniEnvironment.Types.GetJniTypeNameFromInstance(reference));

                return(Runtime.TypeManager.GetType(signature));
            }