public static Dictionary<string, string> RetrieveAll(JniEnvironment environment)
        {
            Dictionary<string, string> properties = new Dictionary<string,string>();

            JniLocalFrame frame = environment.CreateLocalFrame(1);
            try
            {
                JniClass systemClazz = JniClass.FindClass(environment, "java/lang/System");
                string[] keyNames = GetJavaPropertyNames(environment, systemClazz);
                JniMethod getPropMid = systemClazz.GetStaticMethodId(environment, "getProperty", "(Ljava/lang/String;)Ljava/lang/String;");

                foreach (string keyName in keyNames)
                {
                    JValue[] paramArray = new JValue[1];
                    paramArray[0].AssignStringValue(environment, keyName);
                    try
                    {
                        string val = getPropMid.CallStaticStringMethod(environment, paramArray);
                        properties.Add(keyName, val);
                    }
                    finally
                    {
                        paramArray[0].Free(environment);
                    }
                }
            }
            finally
            {
                frame.SafePop();
            }

            return properties;
        }
Beispiel #2
0
 public static void CheckException(JniEnvironment environment)
 {
     if (JniThrowable.ExceptionCheck(environment))
     {
         ThrowCurrentException(environment);
     }
 }
        internal static void Init(JniEnvironment environment)
        {
            if (JavaVM.IsLogTypeEnabled(JavaVMLogTypes.RedirectStdOut | JavaVMLogTypes.RedirectStdErr))
            {
                JniLocalFrame frame = environment.CreateLocalFrame(1);
                try
                {
                    JniClass peer = JavaNativeMethodRegistrar.RegisterCounterpartAndReturnPeer(environment,
                        typeof(JavaStdStreamRedirector));

                    if (JavaVM.IsLogTypeEnabled(JavaVMLogTypes.RedirectStdOut))
                    {
                        HookStream(environment, peer, JavaVMLogTypes.RedirectStdOut, "redirectStdOut");
                    }

                    if (JavaVM.IsLogTypeEnabled(JavaVMLogTypes.RedirectStdErr))
                    {
                        HookStream(environment, peer, JavaVMLogTypes.RedirectStdErr, "redirectStdErr");
                    }
                }
                finally
                {
                    frame.SafePop();
                }
            }
        }
Beispiel #4
0
 internal JniThreadMonitor(JniEnvironment env, JniObject objectHandle)
 {
     JniObject.VerifyInputJniObject(objectHandle);
     mEnvironment = env;
     mObjectHandle = objectHandle;
     Enter();
 }
        public static void LogBannerProperties(JniEnvironment environment)
        {
            Dictionary<string, string> properties = RetrieveAll(environment);

            JavaVM.Log("Current Java System Properties");

            string[] logPropNames = {
                "java.version",
                "java.vendor",
                "java.home",
                "java.vm.version",
                "java.vm.vendor",
                "java.vm.name",
                "java.class.version",
                "java.class.path",
                "java.library.path",
                "os.name",
                "os.arch",
                "os.version",
                "user.name",
                };

            foreach (string logPropName in logPropNames)
            {
                JavaVM.Log(logPropName + " = " + properties[logPropName]);
            }
        }
Beispiel #6
0
 public static string ExtractStringFromHandle(JniEnvironment environment, IntPtr handle)
 {
     VerifyEnvironment(environment);
     unsafe
     {
         int length = GetStringLengthCore(environment, handle);
         char* p = JniEnvironment.Functions.GetStringCritical(environment.Handle, handle, null);
         if (p == null)
         {
             JniThrowable.ThrowCurrentException(environment);
         }
         try
         {
             // IMPORTANT - do not change code between GetStringCritical/ReleaseStringCritical.
             // See JNI documentation.
             return new String(p, 0, length);
         }
         finally
         {
             JniEnvironment.Functions.ReleaseStringCritical(environment.Handle, handle, p);
             // EXCEPTION CHECK: Do nothing, this method safe to call while exception is pending, does
             // not raise additional exceptions.
         }
     }
 }
Beispiel #7
0
 public static bool ExceptionCheck(JniEnvironment environment)
 {
     byte result = JniEnvironment.Functions.ExceptionCheck(environment.Handle);
     // EXCEPTION CHECK: Do nothing, this method safe to call while exception is pending, does
     // not raise additional exceptions.
     return JBooleanConverter.From(result);
 }
 private static void HookStream(JniEnvironment environment, JniClass nativeClazz, 
     JavaVMLogTypes hook, string javaHookMethod)
 {
     JniMethod outMid = nativeClazz.GetStaticMethodId(environment, javaHookMethod, "(I)V");
     JValue[] paramArray = new JValue[1];
     paramArray[0].AssignIntValue((int)hook);
     outMid.CallStaticVoidMethod(environment, paramArray);
 }
Beispiel #9
0
 public static JniLongArray PackLongArray(JniEnvironment environment, long[] sourceArray)
 {
     VerifyEnvironment(environment);
     if (sourceArray == null) { return null; }
     JniLongArray arrayHandle = NewLongArray(environment, sourceArray.Length);
     if (sourceArray.Length > 0)
     {
         arrayHandle.PackPrimitiveArray<long>(environment, sourceArray);
     }
     return arrayHandle;
 }
Beispiel #10
0
 public static JniIntArray PackIntArray(JniEnvironment environment, int[] sourceArray)
 {
     VerifyEnvironment(environment);
     if (sourceArray == null) { return null; }
     JniIntArray arrayHandle = NewIntArray(environment, sourceArray.Length);
     if (sourceArray.Length > 0)
     {
         arrayHandle.PackPrimitiveArray<int>(environment, sourceArray);
     }
     return arrayHandle;
 }
Beispiel #11
0
 public static JniCharArray PackCharArray(JniEnvironment environment, char[] sourceArray)
 {
     VerifyEnvironment(environment);
     if (sourceArray == null) { return null; }
     JniCharArray arrayHandle = NewCharArray(environment, sourceArray.Length);
     if (sourceArray.Length > 0)
     {
         arrayHandle.PackPrimitiveArray<char>(environment, sourceArray);
     }
     return arrayHandle;
 }
Beispiel #12
0
 public static JniByteArray PackByteArray(JniEnvironment environment, byte[] sourceArray)
 {
     VerifyEnvironment(environment);
     if (sourceArray == null) { return null; }
     JniByteArray arrayHandle = NewByteArray(environment, sourceArray.Length);
     if (sourceArray.Length > 0)
     {
         arrayHandle.PackPrimitiveArray<byte>(environment, sourceArray);
     }
     return arrayHandle;
 }
Beispiel #13
0
 internal JniClass(JniEnvironment environment, IntPtr handle, JniObjectLifecycle lifecycle)
     : base(environment, handle, lifecycle)
 {
     // TODO - make this instance global???
     if (!mStaticsInitialized)
     {
         mStaticsInitialized = true;
         JniClass localClassClass = this.GetObjectClass(environment);
         // NOTE: no need to NewGlobalRef class handle for java.lang.Class, will not be unloaded
         mGetNameMid = localClassClass.GetMethodId(environment, "getName", "()Ljava/lang/String;");
     }
 }
Beispiel #14
0
 public JniObject NewObjectCore(JniEnvironment environment, JValue[] arguments, JniConcreteObjectType concreteType)
 {
     VerifyEnvironment(environment);
     // NOTE: OK if "JValue[] arguments" is null
     IntPtr result = JniEnvironment.Functions.NewObjectA(environment.Handle, this.DeclaringClass.Handle, this.Handle, arguments);
     if (IntPtr.Zero == result)
     {
         JniThrowable.ThrowCurrentException(environment); // null result means an exception occurred
     }
     // EXCEPTION CHECK: Do nothing, already checked for exception above.
     return JniObject.CreateObjectOfType(environment, result, concreteType);
 }
Beispiel #15
0
 public double GetDoubleField(
     JniEnvironment environment,
     JniObject jniObject)
 {
     VerifyReturnType(JniType.Double);
     VerifyEnvironment(environment);
     VerifyNotStatic();
     VerifyInputJniObject(jniObject);
     double result = JniEnvironment.Functions.GetDoubleField(environment.Handle, jniObject.Handle, this.Handle);
     JniThrowable.CheckException(environment);
     return result;
 }
Beispiel #16
0
 public bool GetBooleanField(
     JniEnvironment environment,
     JniObject jniObject)
 {
     VerifyReturnType(JniType.Boolean);
     VerifyEnvironment(environment);
     VerifyNotStatic();
     VerifyInputJniObject(jniObject);
     byte result = JniEnvironment.Functions.GetBooleanField(environment.Handle, jniObject.Handle, this.Handle);
     JniThrowable.CheckException(environment);
     return JBooleanConverter.From(result);
 }
Beispiel #17
0
 public byte[] GetByteArrayRegion(
     JniEnvironment environment,
     int start,
     int length
     )
 {
     VerifyEnvironment(environment);
     byte[] result = new byte[length];
     JniEnvironment.Functions.GetByteArrayRegion(environment.Handle, this.Handle, start, length, result);
     JniThrowable.CheckException(environment);
     return result;
 }
Beispiel #18
0
 public JniEnvironment FromHandle(IntPtr environmentHandle)
 {
     lock (mMapEnvironments)
     {
         JniEnvironment env = FromHandleCore(environmentHandle);
         if (env == null)
         {
             env = new JniEnvironment(false, environmentHandle, CurrentUnmanagedThreadId);
             AddEnvironment(env);
         }
         return env;
     }
 }
Beispiel #19
0
 public static bool IsAssignableFrom(
     JniEnvironment environment,
     JniClass subclass,
     JniClass superclass
     )
 {
     VerifyEnvironment(environment);
     VerifyInputJniObject(subclass);
     VerifyInputJniObject(superclass);
     byte result = JniEnvironment.Functions.IsAssignableFrom(environment.Handle, subclass.Handle, superclass.Handle);
     JniThrowable.CheckException(environment);
     return JBooleanConverter.From(result);
 }
Beispiel #20
0
 public static JniBooleanArray NewBooleanArray(
     JniEnvironment environment,
     int length)
 {
     VerifyEnvironment(environment);
     IntPtr result = JniEnvironment.Functions.NewBooleanArray(environment.Handle, length);
     if (IntPtr.Zero == result)
     {
         JniThrowable.ThrowCurrentException(environment); // null result means an exception occurred
     }
     // EXCEPTION CHECK: Do nothing, already checked for exception above.
     return new JniBooleanArray(environment, result, JniObjectLifecycle.Local);
 }
 internal static void RegisterCounterpart(JniEnvironment environment, Type counterpartType)
 {
     JniLocalFrame frame = environment.CreateLocalFrame(1);
     try
     {
         JniClass peer = GetJavaPeer(environment, counterpartType);
         RegisterCounterpart(environment, peer, counterpartType);
     }
     finally
     {
         frame.SafePop();
     }
 }
Beispiel #22
0
 public static void CheckErrorCode(JniEnvironment environment, int code)
 {
     if (code == (int)JniErrorCode.Ok)
     {
         return;
     }
     if (environment == null)
     {
         throw new JniException("TODO");
     }
     // Some error codes may be accompanied with Java Exceptions
     JniThrowable.CheckException(environment);
     HandleBadErrorCode(code);
 }
Beispiel #23
0
 public static JniClass FindClass(
     JniEnvironment environment,
     string name
     )
 {
     VerifyEnvironment(environment);
     IntPtr result = JniEnvironment.Functions.FindClass(environment.Handle, name);
     if (IntPtr.Zero == result)
     {
         JniThrowable.ThrowCurrentException(environment); // null result means an exception occurred
     }
     // EXCEPTION CHECK: Do nothing, already checked for exception above.
     return new JniClass(environment, result, JniObjectLifecycle.Local);
 }
 internal static JniClass GetJavaPeer(JniEnvironment environment, Type counterpartType)
 {
     string peerClassName = FindJavaPeerClassName(counterpartType);
     JniLocalFrame frame = environment.CreateLocalFrame(1);
     try
     {
         JniClass peer = JniClass.FindClass(environment, JniClass.ToJniClassName(peerClassName));
         return frame.Pop(peer) as JniClass;
     }
     finally
     {
         frame.SafePop();
     }
 }
Beispiel #25
0
 public static void ThrowNewToJavaVM(
     JniEnvironment environment,
     JniClass jniClass,
     string message
     )
 {
     VerifyInputJniObject(jniClass);
     int result = JniEnvironment.Functions.ThrowNew(environment.Handle, jniClass.Handle, message);
     if (0 != result)
     {
         JniException.CheckErrorCode(environment, result);
     }
     // EXCEPTION CHECK: Do nothing, already checked for exception above.
 }
Beispiel #26
0
 public bool CallBooleanMethod(
     JniEnvironment environment,
     JniObject jniObject,
     JValue[] arguments
     )
 {
     VerifyReturnType(JniType.Boolean);
     VerifyEnvironment(environment);
     VerifyNotStatic();
     VerifyInputJniObject(jniObject);
     // NOTE: OK if "JValue[] arguments" is null
     byte result = JniEnvironment.Functions.CallBooleanMethodA(environment.Handle, jniObject.Handle, this.Handle, arguments);
     JniThrowable.CheckException(environment);
     return JBooleanConverter.From(result);
 }
Beispiel #27
0
 public double CallDoubleMethod(
     JniEnvironment environment,
     JniObject jniObject,
     JValue[] arguments
     )
 {
     VerifyReturnType(JniType.Double);
     VerifyEnvironment(environment);
     VerifyNotStatic();
     VerifyInputJniObject(jniObject);
     // NOTE: OK if "JValue[] arguments" is null
     double result = JniEnvironment.Functions.CallDoubleMethodA(environment.Handle, jniObject.Handle, this.Handle, arguments);
     JniThrowable.CheckException(environment);
     return result;
 }
Beispiel #28
0
 public bool[] GetBooleanArrayRegion(
     JniEnvironment environment,
     int start,
     int length)
 {
     VerifyEnvironment(environment);
     byte[] result = new byte[length];
     JniEnvironment.Functions.GetBooleanArrayRegion(environment.Handle, this.Handle, start, length, result);
     JniThrowable.CheckException(environment);
     bool[] boolArray = new bool[result.Length];
     for (int i = 0; i < result.Length; i++)
     {
         boolArray[i] = JBooleanConverter.From(result[i]);
     }
     return boolArray;
 }
Beispiel #29
0
 public static JniObjectArray NewObjectArray(
     JniEnvironment environment,
     int length,
     JniClass jniClass,
     JniObject init
     )
 {
     VerifyEnvironment(environment);
     VerifyInputJniObject(jniClass);
     IntPtr safeInit = (init == null) ? IntPtr.Zero : init.Handle; // OK if initial value is null
     IntPtr result = JniEnvironment.Functions.NewObjectArray(environment.Handle, length, jniClass.Handle, safeInit);
     if (IntPtr.Zero == result)
     {
         JniThrowable.ThrowCurrentException(environment); // null result means an exception occurred
     }
     // EXCEPTION CHECK: Do nothing, already checked for exception above.
     return new JniObjectArray(environment, result, JniObjectLifecycle.Local, -1, JniType.Null);
 }
Beispiel #30
0
 public static string[] ExtractStringArray(JniEnvironment environment, JniObjectArray objectArray)
 {
     VerifyEnvironment(environment);
     VerifyInputJniObject(objectArray);
     int length = objectArray.GetArrayLength(environment);
     string[] output = new string[length];
     JniLocalFrame frame = environment.CreateLocalFrame(length);
     try
     {
         for (int i = 0; i < length; i++)
         {
             JniObject o = objectArray.GetObjectArrayElement(environment, i);
             output[i] = (o == null) ? null : o.ExtractStringValue(environment);
         }
     }
     finally
     {
         frame.SafePop();
     }
     return output;
 }