Пример #1
0
 /// <summary>
 /// Calls the main static method of a given Java class.
 /// NOTE: this method DOES NOT return.  It calls System.Environment.Exit.
 /// If the Java program calls System.exit, this method will also not return.  
 /// The .NET program should end with the same error code as the Java program.
 /// </summary>
 /// <param name="mainClassName">The JNI class name containg the method</param>
 /// <param name="arguments">The arguments to pass to the main method.</param>
 public static void RunMain(string mainClassName, string[] arguments)
 {
     JniEnvironment env = JavaVM.SafeFindEnvironment();
     JniLocalFrame frame = env.CreateLocalFrame(10);
     try
     {
         JniClass mainClass = JniClass.FindClass(env, mainClassName);
         JniMethod method = mainClass.GetStaticMethodId(env, "main", "([Ljava/lang/String;)V");
         JniObjectArray javaArguments = JniString.PackStringArray(env, arguments);
         JValue[] methodArgs = new JValue[1];
         methodArgs[0].AssignObjectValue(javaArguments);
         method.CallStaticVoidMethod(env, methodArgs);
     }
     catch (JniThrowableException ex)
     {
         System.Console.Out.WriteLine(ex.Throwable.ExtractStackTrace(env));
         System.Environment.Exit(1);
         throw;
     }
     catch (Exception ex)
     {
         System.Console.Out.WriteLine(JniThrowable.ExtractDotNetExceptionMessage(ex));
         System.Environment.Exit(1);
         throw;
     }
     finally
     {
         frame.SafePop();
     }
     System.Environment.Exit(0);
 }
Пример #2
0
        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;
        }
Пример #3
0
 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);
 }
Пример #4
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);
 }
Пример #5
0
 public char CallCharMethod(
     JniEnvironment environment,
     JniObject jniObject,
     JValue[] arguments
     )
 {
     VerifyReturnType(JniType.Char);
     VerifyEnvironment(environment);
     VerifyNotStatic();
     VerifyInputJniObject(jniObject);
     // NOTE: OK if "JValue[] arguments" is null
     char result = JniEnvironment.Functions.CallCharMethodA(environment.Handle, jniObject.Handle, this.Handle, arguments);
     JniThrowable.CheckException(environment);
     return result;
 }
Пример #6
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);
 }
Пример #7
0
 public void Test()
 {
     JniEnvironment env = JavaVM.FindEnvironment();
     string[] startArgs = { "one", "two", "three", "chicken" };
     JniLocalFrame frame = env.CreateLocalFrame(3);
     try
     {
         JniObjectArray array = JniString.PackStringArray(env, startArgs);
         JniClass mainClazz = JniClass.FindClass(env, "csjni/test/TestMain");
         JniMethod mid = mainClazz.GetStaticMethodId(env, "main", "([Ljava/lang/String;)V");
         JValue[] paramArray = new JValue[1];
         paramArray[0].AssignObjectValue(array);
         mid.CallStaticVoidMethod(env, paramArray);
     }
     finally
     {
         frame.SafePop();
     }
 }
Пример #8
0
        internal static string ExtractStackTrace(JniEnvironment environment, JniThrowable throwableObj)
        {
            JniLocalFrame frame = environment.CreateLocalFrame(10);
            try
            {
                // construct ByteArrayOutputStream
                JniClass baosClazz = JniClass.FindClass(environment, "java/io/ByteArrayOutputStream");
                JniConstructor baosCtorMid = baosClazz.GetConstructorId(environment, "()V");
                JniMethod baosFlushMid = baosClazz.GetMethodId(environment, "flush", "()V");
                JniMethod baosToStringMid = baosClazz.GetMethodId(environment, "toString", "()Ljava/lang/String;");
                JniObject baosObj = baosCtorMid.NewObject(environment, null);

                // construct PrintStream
                JniClass psClazz = JniClass.FindClass(environment, "java/io/PrintStream");
                JniConstructor psCtorMid = psClazz.GetConstructorId(environment, "(Ljava/io/OutputStream;)V");
                JValue[] ps_paramArray = new JValue[1];
                ps_paramArray[0].AssignObjectValue(baosObj);
                JniObject psObj = psCtorMid.NewObject(environment, ps_paramArray);

                // call printStackTrace
                JniClass throwableClazz = throwableObj.GetObjectClass(environment);
                JniMethod throwablePSTMid = throwableClazz.GetMethodId(environment, "printStackTrace", "(Ljava/io/PrintStream;)V");
                JValue[] t_paramArray = new JValue[1];
                t_paramArray[0].AssignObjectValue(psObj);
                throwablePSTMid.CallVoidMethod(environment, throwableObj, t_paramArray);

                // flush ByteArrayOutputStream & get string
                baosFlushMid.CallVoidMethod(environment, baosObj, null);
                string stackTrace = baosToStringMid.CallStringMethod(environment, baosObj, null);

                return stackTrace;
            }
            finally
            {
                frame.SafePop();
            }
        }
Пример #9
0
 public void CallVoidMethod(
     JniEnvironment environment,
     JniObject jniObject,
     JValue[] arguments
     )
 {
     VerifyReturnType(JniType.Void);
     VerifyEnvironment(environment);
     VerifyNotStatic();
     VerifyInputJniObject(jniObject);
     // NOTE: OK if "JValue[] arguments" is null
     JniEnvironment.Functions.CallVoidMethodA(environment.Handle, jniObject.Handle, this.Handle, arguments);
     JniThrowable.CheckException(environment);
 }
Пример #10
0
 public string CallStringMethod(
     JniEnvironment environment,
     JniObject jniObject,
     JValue[] arguments
     )
 {
     VerifyReturnType(JniType.String);
     VerifyEnvironment(environment);
     VerifyNotStatic();
     VerifyInputJniObject(jniObject);
     // NOTE: OK if "JValue[] arguments" is null
     IntPtr result = JniEnvironment.Functions.CallObjectMethodA(environment.Handle, jniObject.Handle, this.Handle, arguments);
     JniThrowable.CheckException(environment);
     return JniString.ExtractAndRelease(environment, result);
 }
Пример #11
0
 public void CallStaticVoidMethod(
     JniEnvironment environment,
     JValue[] arguments
     )
 {
     VerifyReturnType(JniType.Void);
     VerifyEnvironment(environment);
     VerifyStatic();
     // NOTE: OK if "JValue[] arguments" is null
     JniEnvironment.Functions.CallStaticVoidMethodA(environment.Handle, this.DeclaringClass.Handle, this.Handle, arguments);
     JniThrowable.CheckException(environment);
 }
Пример #12
0
 public short CallStaticShortMethod(
     JniEnvironment environment,
     JValue[] arguments
     )
 {
     VerifyReturnType(JniType.Short);
     VerifyEnvironment(environment);
     VerifyStatic();
     // NOTE: OK if "JValue[] arguments" is null
     short result = JniEnvironment.Functions.CallStaticShortMethodA(environment.Handle, this.DeclaringClass.Handle, this.Handle, arguments);
     JniThrowable.CheckException(environment);
     return result;
 }
Пример #13
0
 public JniObject CallStaticObjectMethod(
     JniEnvironment environment,
     JValue[] arguments
     )
 {
     VerifyObjectReturnType();
     VerifyEnvironment(environment);
     VerifyStatic();
     // NOTE: OK if "JValue[] arguments" is null
     IntPtr result = JniEnvironment.Functions.CallStaticObjectMethodA(environment.Handle, this.DeclaringClass.Handle, this.Handle, arguments);
     JniThrowable.CheckException(environment);
     return JniObject.CreateReturnObject(environment, result, this);
 }
Пример #14
0
 public JniObject NewThrowableObject(JniEnvironment environment, JValue[] arguments)
 {
     return NewObjectCore(environment, arguments, JniConcreteObjectType.Throwable);
 }