diff --git a/src/net/JNet/JNet.csproj b/src/net/JNet/JNet.csproj index 680d2409a0..64c5393536 100644 --- a/src/net/JNet/JNet.csproj +++ b/src/net/JNet/JNet.csproj @@ -14,6 +14,9 @@ False False + + $(DefineConstants);JNET_SIMPLIFIED_GENERATION + diff --git a/src/net/JNet/JNetCoreBase.cs b/src/net/JNet/JNetCoreBase.cs index 7b56f13eb5..aa44553254 100644 --- a/src/net/JNet/JNetCoreBase.cs +++ b/src/net/JNet/JNetCoreBase.cs @@ -268,6 +268,68 @@ public static dynamic New(string className, params object[] args) return GlobalInstance.JVM.New(className, args); } - #endregion + /// + /// Launch the class with the arguments + /// + /// A type which is defined as Main-Class + /// The arguments of the main method + public static new void Launch(params string[] args) + where TClass : IJVMBridgeMain + { + Launch(typeof(TClass), args); + } + + /// + /// Launch the with the arguments + /// + /// The extending + /// The arguments of the main method + public static new void Launch(Type type, params string[] args) + { + if (type == null) throw new ArgumentNullException(nameof(type)); + + try + { + SetupJVMWrapper.Launch(type, args); + } + catch (ArgumentException) + { +#if !JNETREFLECTOR && !JNET_SIMPLIFIED_GENERATION + if (type.GetInterface(typeof(IJVMBridgeMain).Name) == null) throw; + var execType = type; + do + { + if (args.Length == 0) + { + System.Reflection.MethodInfo method = execType.GetMethods().FirstOrDefault(method => method.Name == "SExecute" & method.GetParameters().Length == 2 & method.IsGenericMethod == false); + if (method != null) + { + method.Invoke(null, new object[] { "main", new object[] { args } }); + return; + } + } + else + { + System.Reflection.MethodInfo method = execType.GetMethod("Main", new Type[] { typeof(Java.Lang.String[]) }); + if (method != null) + { + Java.Lang.String[] strings = new Java.Lang.String[args.Length]; + for (int i = 0; i < args.Length; i++) + { + strings[i] = args[i]; + } + method.Invoke(null, new object[] { strings }); + } + return; + } + execType = execType.BaseType; + } + while (execType != null && execType != typeof(object)); +#endif + } + throw new ArgumentException($"{type} does not define any IJVMBridgeMain type or interface", "type"); + } + +#endregion } } \ No newline at end of file diff --git a/src/net/JNetPSCore/JNetPSHelper.cs b/src/net/JNetPSCore/JNetPSHelper.cs index 527ba673e6..ca7e7dea25 100644 --- a/src/net/JNetPSCore/JNetPSHelper.cs +++ b/src/net/JNetPSCore/JNetPSHelper.cs @@ -469,17 +469,32 @@ public static void CreateGlobalInstance(System.Management.Automation.Cmdlet cmdl { if (_instanceCreated) { cmdlet.WriteWarning("A new CreateGlobalInstance requested, but it was previously requested."); return; } cmdlet.WriteDebug("Invoking CreateGlobalInstance"); - _ = typeof(TClass).RunStaticMethodOn(typeof(SetupJVMWrapper<>), nameof(JNetCore.CreateGlobalInstance)); - cmdlet.WriteDebug("Invoked CreateGlobalInstance"); - _instanceCreated = true; + try + { + try + { + _ = typeof(TClass).RunStaticMethodOn(typeof(SetupJVMWrapper<>), nameof(JNetCore.CreateGlobalInstance)); + } + catch (TargetInvocationException tie) { throw tie.InnerException; } + } + catch (Exception jbe) + { + cmdlet.WriteExtendedError(jbe); + cmdlet.WriteWarning("Something went wrong within CreateGlobalInstance, maybe the instance is unusable."); + } + finally + { + cmdlet.WriteDebug("Invoked CreateGlobalInstance"); + _instanceCreated = true; + } } } /// - /// Invokes to start a Main-Class + /// Invokes to start a Main-Class /// public static void Launch(Type type, params string[] args) { - _ = typeof(TClass).RunStaticMethodOn(typeof(SetupJVMWrapper), nameof(SetupJVMWrapper.Launch), type, args); + _ = typeof(TClass).RunStaticMethodOn(typeof(JNetCore), nameof(JNetCore.Launch), type, args); } /// /// Creates a new class instance