Skip to content

Commit

Permalink
Added generation of side-by-side method to return direct listener
Browse files Browse the repository at this point in the history
  • Loading branch information
masesdevelopers committed May 14, 2024
1 parent 9f707f8 commit 938d567
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 24 deletions.
6 changes: 6 additions & 0 deletions src/net/JNetReflector/InternalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2313,6 +2313,12 @@ public static bool IsReturnTypeAnException(this Method entry)
return entry.ReturnType.IsJVMException();
}

public static bool IsReturnTypeAListener(this Method entry)
{
if (entry == null) throw new ArgumentNullException(nameof(entry));
return entry.ReturnType.IsJVMListenerClass();
}

public static string ReturnType(this Method entry, IList<string> genArguments, IList<KeyValuePair<string, string>> genClauses, string prefix, bool usedInGenerics, bool camel)
{
if (entry == null) throw new ArgumentNullException(nameof(entry));
Expand Down
119 changes: 95 additions & 24 deletions src/net/JNetReflector/InternalMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1709,55 +1709,107 @@ static string AnalyzeMethods(this Class classDefinition, IReadOnlyDictionary<str
executionParamsString = string.Format(AllPackageClasses.ClassStub.MethodStub.SINGLE_ARRAY_EXECUTION_FORMAT, parameters[0].Name);
}

bool isListenerReturnType = method.IsReturnTypeAListener();
bool isVoidMethod = method.IsVoid();
bool isReturnTypeException = method.IsReturnTypeAnException();
string executionStub = string.Empty;
string executionStubDirect = string.Empty;
if (forListener && method.IsDefault) methodNameOrigin += SpecialNames.DefaultMethodSuffix;
if (isReturnTypeException)
{
var execFormat = method.IsStatic() ? AllPackageClasses.ClassStub.MethodStub.STATIC_EXECUTION_FORMAT_EXCEPTION : AllPackageClasses.ClassStub.MethodStub.EXECUTION_FORMAT_EXCEPTION;
executionStub = string.Format(execFormat, execStub,
methodNameOrigin,
string.IsNullOrWhiteSpace(signature) ? string.Empty : $", \"{signature}\"",
executionParamsString.Length == 0 ? string.Empty : ", " + executionParamsString,
returnType);
executionStub = string.Format(execFormat,
execStub,
methodNameOrigin,
string.IsNullOrWhiteSpace(signature) ? string.Empty : $", \"{signature}\"",
executionParamsString.Length == 0 ? string.Empty : ", " + executionParamsString,
returnType);
}
else
{
var execFormat = method.IsStatic() ? AllPackageClasses.ClassStub.MethodStub.STATIC_EXECUTION_FORMAT : AllPackageClasses.ClassStub.MethodStub.EXECUTION_FORMAT;
executionStub = string.Format(execFormat, method.IsVoid() ? string.Empty : "return ",
execStub,
isVoidMethod || method.IsObjectReturnType(isGeneric, JNetReflectorCore.UseCamel) ? string.Empty : $"<{returnType}>",
methodNameOrigin,
string.IsNullOrWhiteSpace(signature) ? string.Empty : $", \"{signature}\"",
executionParamsString.Length == 0 ? string.Empty : ", " + executionParamsString);
executionStub = string.Format(execFormat,
method.IsVoid() ? string.Empty : "return ",
execStub,
isVoidMethod || method.IsObjectReturnType(isGeneric, JNetReflectorCore.UseCamel) ? string.Empty : $"<{returnType}>",
methodNameOrigin,
string.IsNullOrWhiteSpace(signature) ? string.Empty : $", \"{signature}\"",
executionParamsString.Length == 0 ? string.Empty : ", " + executionParamsString);
if (isListenerReturnType)
{
string directNewClass = returnType;
var indexOfGenerics = returnType.IndexOf('<');
if (indexOfGenerics > 0)
{
directNewClass = returnType.Insert(indexOfGenerics, SpecialNames.DirectMethodSuffix);
}
else
{
directNewClass = returnType + SpecialNames.DirectMethodSuffix;
}

executionStubDirect = string.Format(execFormat,
method.IsVoid() ? string.Empty : "return ",
execStub,
isVoidMethod || method.IsObjectReturnType(isGeneric, JNetReflectorCore.UseCamel) ? string.Empty : $"<{directNewClass}, {returnType}>",
methodNameOrigin,
string.IsNullOrWhiteSpace(signature) ? string.Empty : $", \"{signature}\"",
executionParamsString.Length == 0 ? string.Empty : ", " + executionParamsString);
}
}

if (hasVarArg)
{
string executionStubWithVarArg = string.Empty;
string executionStubWithVarArgDirect = string.Empty;
if (isReturnTypeException)
{
var execFormat = method.IsStatic() ? AllPackageClasses.ClassStub.MethodStub.STATIC_EXECUTION_FORMAT_EXCEPTION : AllPackageClasses.ClassStub.MethodStub.EXECUTION_FORMAT_EXCEPTION;
executionStubWithVarArg = string.Format(execFormat, execStub,
methodNameOrigin,
string.IsNullOrWhiteSpace(signature) ? string.Empty : $", \"{signature}\"",
(executionParamsString.Length == 0 ? string.Empty : ", ")
+ executionParamsString + ", " + varArg.Name(),
returnType);
executionStubWithVarArg = string.Format(execFormat,
execStub,
methodNameOrigin,
string.IsNullOrWhiteSpace(signature) ? string.Empty : $", \"{signature}\"",
(executionParamsString.Length == 0 ? string.Empty : ", ")
+ executionParamsString + ", " + varArg.Name(),
returnType);
}
else
{
var execFormat = method.IsStatic() ? AllPackageClasses.ClassStub.MethodStub.STATIC_EXECUTION_FORMAT : AllPackageClasses.ClassStub.MethodStub.EXECUTION_FORMAT;
executionStubWithVarArg = string.Format(execFormat, isVoidMethod ? string.Empty : "return ",
execStub,
isVoidMethod || method.IsObjectReturnType(isGeneric, JNetReflectorCore.UseCamel) ? string.Empty : $"<{returnType}>",
methodNameOrigin,
string.IsNullOrWhiteSpace(signature) ? string.Empty : $", \"{signature}\"",
(executionParamsString.Length == 0 ? string.Empty : ", ")
+ executionParamsString + ", " + varArg.Name());
executionStubWithVarArg = string.Format(execFormat,
isVoidMethod ? string.Empty : "return ",
execStub,
isVoidMethod || method.IsObjectReturnType(isGeneric, JNetReflectorCore.UseCamel) ? string.Empty : $"<{returnType}>",
methodNameOrigin,
string.IsNullOrWhiteSpace(signature) ? string.Empty : $", \"{signature}\"",
(executionParamsString.Length == 0 ? string.Empty : ", ")
+ executionParamsString + ", " + varArg.Name());

if (isListenerReturnType)
{
string directNewClass = returnType;
var indexOfGenerics = returnType.IndexOf('<');
if (indexOfGenerics > 0)
{
directNewClass = returnType.Insert(indexOfGenerics, SpecialNames.DirectMethodSuffix);
}
else
{
directNewClass = returnType + SpecialNames.DirectMethodSuffix;
}

executionStubWithVarArgDirect = string.Format(execFormat,
isVoidMethod ? string.Empty : "return ",
execStub,
isVoidMethod || method.IsObjectReturnType(isGeneric, JNetReflectorCore.UseCamel) ? string.Empty : $"<{directNewClass}, {returnType}>",
methodNameOrigin,
string.IsNullOrWhiteSpace(signature) ? string.Empty : $", \"{signature}\"",
(executionParamsString.Length == 0 ? string.Empty : ", ")
+ executionParamsString + ", " + varArg.Name());
}
}
executionStub = $"if ({varArg.Name()}.Length == 0) {executionStub} else {executionStubWithVarArg}";
executionStubDirect = $"if ({varArg.Name()}.Length == 0) {executionStubDirect} else {executionStubWithVarArgDirect}";
}

ReportTrace(ReflectionTraceLevel.Debug, "Preparing method {0}", genString);
Expand Down Expand Up @@ -1926,6 +1978,25 @@ static string AnalyzeMethods(this Class classDefinition, IReadOnlyDictionary<str
.Replace(AllPackageClasses.ClassStub.MethodStub.HELP, method.JavadocHrefUrl(JNetReflectorCore.UseCamel));

subClassBlock.AppendLine(singleMethod);

if (isListenerReturnType)
{
singleMethod = template.Replace(AllPackageClasses.ClassStub.MethodStub.DECORATION, jDecoration.ToString())
.Replace(AllPackageClasses.ClassStub.MethodStub.LISTENER_HANDLER_EXECUTION, listenerHandlerType)
.Replace(AllPackageClasses.ClassStub.MethodStub.LISTENER_HANDLER_NAME, baseHandlerName)
.Replace(AllPackageClasses.ClassStub.MethodStub.MODIFIER, modifier)
.Replace(AllPackageClasses.ClassStub.MethodStub.RETURNTYPE, returnType)
.Replace(AllPackageClasses.ClassStub.MethodStub.NAME, methodName + SpecialNames.DirectMethodSuffix)
.Replace(AllPackageClasses.ClassStub.MethodStub.PARAMETERS, paramsString)
.Replace(AllPackageClasses.ClassStub.MethodStub.WHERECLAUSES, genericClauses.ConvertClauses(isGeneric))
.Replace(AllPackageClasses.ClassStub.MethodStub.EXECUTION, executionStubDirect)
.Replace(AllPackageClasses.ClassStub.MethodStub.LISTENER_EXECUTION_TYPE, executionPropertyParams)
.Replace(AllPackageClasses.ClassStub.MethodStub.LISTENER_FIRST_PARAMETER, CLRListenerEventArgsType)
.Replace(AllPackageClasses.ClassStub.MethodStub.LISTENER_EXECUTION, listenerExecutionParamsString)
.Replace(AllPackageClasses.ClassStub.MethodStub.HELP, method.JavadocHrefUrl(JNetReflectorCore.UseCamel));

subClassBlock.AppendLine(singleMethod);
}
}

var returnStr = subClassBlock.ToString();
Expand Down

0 comments on commit 938d567

Please sign in to comment.