Skip to content

Commit

Permalink
Update Launch behavior to better support more cases (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
masesdevelopers authored Jun 13, 2024
1 parent e9eebbc commit 00f7fd8
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/net/JNet/JNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<RunAnalyzersDuringLiveAnalysis>False</RunAnalyzersDuringLiveAnalysis>
<RunAnalyzersDuringBuild>False</RunAnalyzersDuringBuild>
</PropertyGroup>
<PropertyGroup Condition="'$(GITHUB_SIMPLIFIED_GENERATION)' == 'true'">
<DefineConstants>$(DefineConstants);JNET_SIMPLIFIED_GENERATION</DefineConstants>
</PropertyGroup>
<ItemGroup Condition="True">
<jnetcore_jars Include="$(ProjectDir)\..\..\..\jars\*.*" />
</ItemGroup>
Expand Down
64 changes: 63 additions & 1 deletion src/net/JNet/JNetCoreBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,68 @@ public static dynamic New(string className, params object[] args)
return GlobalInstance.JVM.New(className, args);
}

#endregion
/// <summary>
/// Launch the <typeparamref name="TClass"/> class with the <paramref name="args"/> arguments
/// </summary>
/// <typeparam name="TClass">A type which is defined as Main-Class</typeparam>
/// <param name="args">The arguments of the main method</param>
public static new void Launch<TClass>(params string[] args)
where TClass : IJVMBridgeMain
{
Launch(typeof(TClass), args);
}

/// <summary>
/// Launch the <paramref name="type"/> with the <paramref name="args"/> arguments
/// </summary>
/// <param name="type">The <see cref="Type"/> extending <see cref="IJVMBridgeMain"/></param>
/// <param name="args">The arguments of the main method</param>
public static new void Launch(Type type, params string[] args)
{
if (type == null) throw new ArgumentNullException(nameof(type));

try
{
SetupJVMWrapper<T>.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
}
}
25 changes: 20 additions & 5 deletions src/net/JNetPSCore/JNetPSHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TClass>.CreateGlobalInstance));
cmdlet.WriteDebug("Invoked CreateGlobalInstance");
_instanceCreated = true;
try
{
try
{
_ = typeof(TClass).RunStaticMethodOn(typeof(SetupJVMWrapper<>), nameof(JNetCore<TClass>.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;
}
}
}
/// <summary>
/// Invokes <see cref="SetupJVMWrapper.Launch(Type, string[])"/> to start a Main-Class
/// Invokes <see cref="JNetCore{TClass}.Launch(Type, string[])"/> to start a Main-Class
/// </summary>
public static void Launch(Type type, params string[] args)
{
_ = typeof(TClass).RunStaticMethodOn(typeof(SetupJVMWrapper), nameof(SetupJVMWrapper.Launch), type, args);
_ = typeof(TClass).RunStaticMethodOn(typeof(JNetCore<TClass>), nameof(JNetCore<TClass>.Launch), type, args);
}
/// <summary>
/// Creates a new class instance
Expand Down

0 comments on commit 00f7fd8

Please sign in to comment.