Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Launch behavior to better support more cases #451

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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