Skip to content

Commit

Permalink
Prebuild signatures and update search behavior, update startup with a…
Browse files Browse the repository at this point in the history
… better filter for JAR content
  • Loading branch information
masesdevelopers committed Sep 26, 2024
1 parent 8d3168a commit b8eb53b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 49 deletions.
33 changes: 29 additions & 4 deletions src/net/JNetReflector/InternalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,36 @@ public static string AddClassNameToSignature(this string methodSignature, string
return methodSignature;
}

public static string SignatureFromGenericString(this IReadOnlyDictionary<string, string> methodSignatures, Method entry)
public static string SignatureFromGenericString(this Method entry)
{
var filteredGenString = entry.GenericString.RemoveThrowsAndCleanupSignature();
string signature = null;
if (!methodSignatures.TryGetValue(filteredGenString, out signature))
string signature = SignatureFromGenericString(filteredGenString, entry.DeclaringClass);
if (signature == null)
{
ReflectionUtils.ReportTrace(ReflectionUtils.ReflectionTraceLevel.Critical, $"SignatureFromGenericString: signature not found for method {entry.Name} (Generic string: {entry.GenericString}) of class {entry.DeclaringClass.TypeName} using {filteredGenString} not found.");
}
return signature;
}

static string SignatureFromGenericString(string filteredGenString, Class checkClass)
{
if (checkClass == null || checkClass.TypeName == "java.lang.Object") return null;
var methodSignatures = JNetReflectorHelper.SignatureForClass(checkClass);
string signature = null;
if (methodSignatures.TryGetValue(filteredGenString, out signature))
{
return signature;
}
try
{
return SignatureFromGenericString(filteredGenString, checkClass.SuperClass);
}
catch
{
return null;
}
}

#endregion

#region ZipArchiveEntry extension
Expand Down Expand Up @@ -364,6 +383,12 @@ public static bool IsFolder(this ZipArchiveEntry entry)
return false;
}

public static bool IsClass(this ZipArchiveEntry entry)
{
if (entry.Name.Contains(SpecialNames.ClassExtension)) return true;
return false;
}

public static bool IsJVMNestedClass(this ZipArchiveEntry entry)
{
if (entry.Length != 0
Expand Down Expand Up @@ -1632,7 +1657,7 @@ public static bool IsSpecialClass(this Class entry)
return true;
}

string className = entry.JVMNestedClassName(entry.JVMNestingLevels(), null, true, false);
string className = entry.JVMNestedClassName(entry.JVMNestingLevels(), null, true, false, true);

if (entry.IsJVMNestedClass()
&& SpecialNames.SpecialNumberedNames.Any((o) => className.StartsWith(o))) return true;
Expand Down
98 changes: 59 additions & 39 deletions src/net/JNetReflector/InternalMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public static void AnalyzeJar(string pathToJar, string javadocUrl, int javadocVe
ReportTrace(ReflectionTraceLevel.Debug, "Entry {0}", entry.ToString());
if (entry.IsSpecialFolder()) continue; // do not reflect this folders
if (entry.IsFolder()) continue; // do not reflect this folders
if (!entry.IsClass()) continue; // reflect only Java classes, this speed up next line avoiding too many excpetions

var jClass = entry.JVMClass();
if (jClass != null && (jClass.IsJVMClass() || jClass.IsJVMNestedClass()))
Expand Down Expand Up @@ -248,6 +249,25 @@ public static void AnalyzeNamespace(IDictionary<string, IDictionary<string, IDic

static void AnalyzeItems(this IDictionary<string, IDictionary<string, IDictionary<string, Class>>> items)
{
if (JNetReflectorCore.PreferMethodWithSignature)
{
try
{
ParallelOptions po = new ParallelOptions();
Console.CancelKeyPress += Console_CancelKeyPress;
CancellationTokenSource = new CancellationTokenSource();
CancellationTokenSource.Token.ThrowIfCancellationRequested();
po.CancellationToken = CancellationTokenSource.Token;
po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
Parallel.ForEach(items, po, ExtractSignatureParallel);
}
finally
{
CancellationTokenSource = null;
Console.CancelKeyPress -= Console_CancelKeyPress;
}
}

if (!JNetReflectorCore.AvoidParallelBuild)
{
try
Expand Down Expand Up @@ -292,33 +312,43 @@ static void AnalyzeSubItemsParallel(KeyValuePair<string, IDictionary<string, IDi
}
catch (System.Exception e)
{
ReportTrace(ReflectionTraceLevel.Error, "Error in parallel for package {0}: {1}", items.Key, e.Message);
ReportTrace(ReflectionTraceLevel.Error, "Error in AnalyzeSubItemsParallel for package {0}: {1}", items.Key, e.Message);
throw;
}
}

static void AnalyzeSubItems(string package, IDictionary<string, IDictionary<string, Class>> items, string jarOrModuleName)
static void ExtractSignatureParallel(KeyValuePair<string, IDictionary<string, IDictionary<string, Class>>> items)
{
var allPackageClasses = Template.GetTemplate(Template.AllPackageClassesTemplate);

ReportTrace(ReflectionTraceLevel.Info, "Starting analysis for package {0}", package);
StringBuilder sb = new StringBuilder();

IDictionary<Class, IReadOnlyDictionary<string, string>> signaturesForClasses = null;
if (JNetReflectorCore.PreferMethodWithSignature)
try
{
List<Class> classes = new List<Class>();
foreach (var entry in items)
foreach (var entry in items.Value)
{
classes.AddRange(entry.Value.Values);
}

signaturesForClasses = JNetReflectorHelper.ExtractJavaInfo(classes, JNetReflectorCore.CurrentClassPath, JNetReflectorCore.JavaPLocationPath);
JNetReflectorHelper.ExtractJavaInfo(classes, JNetReflectorCore.CurrentClassPath, JNetReflectorCore.JavaPLocationPath);
}
catch (OperationCanceledException)
{
throw;
}
catch (System.Exception e)
{
ReportTrace(ReflectionTraceLevel.Error, "Error in ExtractSignatureParallel for package {0}: {1}", items.Key, e.Message);
throw;
}
}

static void AnalyzeSubItems(string package, IDictionary<string, IDictionary<string, Class>> items, string jarOrModuleName)
{
var allPackageClasses = Template.GetTemplate(Template.AllPackageClassesTemplate);

ReportTrace(ReflectionTraceLevel.Info, "Starting analysis for package {0}", package);
StringBuilder sb = new StringBuilder();

foreach (var entry in items)
{
var classContent = AnalyzeClasses(jarOrModuleName, entry.Value.Values, signaturesForClasses);
var classContent = AnalyzeClasses(jarOrModuleName, entry.Value.Values);
if (!string.IsNullOrEmpty(classContent))
{
sb.AppendLine(classContent);
Expand Down Expand Up @@ -364,7 +394,7 @@ static void AnalyzeSubItems(string package, IDictionary<string, IDictionary<stri
}
}

static string AnalyzeClasses(string jarOrModuleName, IEnumerable<Class> classDefinitions, IDictionary<Class, IReadOnlyDictionary<string, string>> signaturesForClasses)
static string AnalyzeClasses(string jarOrModuleName, IEnumerable<Class> classDefinitions)
{
ReportTrace(ReflectionTraceLevel.Info, "******************* AnalyzeClass {0} *******************", jarOrModuleName);

Expand Down Expand Up @@ -441,12 +471,10 @@ static string AnalyzeClasses(string jarOrModuleName, IEnumerable<Class> classDef
string subInterfaceStr;
AnalyzeNestedClasses(nestedClasses, classDefinitions, out subClassStr, out nestedBlock, out subInterfaceStr);

IReadOnlyDictionary<string, string> signatures = null;
signaturesForClasses?.TryGetValue(jClass, out signatures);
string classBlock;
string singleClassStr;
string singleInterfaceStr;
jClass.PrepareSingleClass(signatures, classDefinitions, false, out classBlock, out singleClassStr, out singleInterfaceStr);
jClass.PrepareSingleClass(classDefinitions, false, out classBlock, out singleClassStr, out singleInterfaceStr);

singleInterfaceStr = singleInterfaceStr.Replace(AllPackageClasses.ClassStub.NESTED_INTERFACES, string.IsNullOrWhiteSpace(subInterfaceStr) ? string.Empty : subInterfaceStr);
singleInterfaceStr = singleInterfaceStr.AddTabLevel(1);
Expand Down Expand Up @@ -475,7 +503,7 @@ static string AnalyzeClasses(string jarOrModuleName, IEnumerable<Class> classDef
string singleInterfaceGenericStr;
if (!JNetReflectorCore.AvoidCSharpGenericDefinition && jClass.IsJVMGenericClass() && !jClass.IsClassToAvoidInGenerics())
{
jClass.PrepareSingleClass(signatures, classDefinitions, true, out classGenericBlock, out singleClassGenericStr, out singleInterfaceGenericStr);
jClass.PrepareSingleClass(classDefinitions, true, out classGenericBlock, out singleClassGenericStr, out singleInterfaceGenericStr);
singleClassGenericStr = singleClassGenericStr.Replace(AllPackageClasses.ClassStub.NESTED_CLASSES, string.Empty);
singleInterfaceGenericStr = singleInterfaceGenericStr.Replace(AllPackageClasses.ClassStub.NESTED_INTERFACES, string.Empty);
classGenericBlock = classGenericBlock.AddTabLevel(1);
Expand Down Expand Up @@ -526,12 +554,6 @@ static void AnalyzeNestedClasses(IEnumerable<Class> nestedClasses, IEnumerable<C
StringBuilder subClassAutonoumous = new StringBuilder();
StringBuilder subInterfaceAutonoumous = new StringBuilder();

IDictionary<Class, IReadOnlyDictionary<string, string>> signaturesForNestedClasses = null;
if (JNetReflectorCore.PreferMethodWithSignature)
{
signaturesForNestedClasses = JNetReflectorHelper.ExtractJavaInfo(nestedClasses, JNetReflectorCore.CurrentClassPath, JNetReflectorCore.JavaPLocationPath);
}

foreach (var entry in nestedClasses)
{
string innerNestedClassBlock = string.Empty;
Expand All @@ -550,9 +572,7 @@ static void AnalyzeNestedClasses(IEnumerable<Class> nestedClasses, IEnumerable<C
string nestedClassBlock;
string singleNestedClassStr;
string singleNestedInterfaceStr;
IReadOnlyDictionary<string, string> signatures = null;
signaturesForNestedClasses?.TryGetValue(entry, out signatures);
entry.PrepareSingleClass(signatures, classDefinitions, false, out nestedClassBlock, out singleNestedClassStr, out singleNestedInterfaceStr);
entry.PrepareSingleClass(classDefinitions, false, out nestedClassBlock, out singleNestedClassStr, out singleNestedInterfaceStr);
nestedClassBlock = nestedClassBlock.Replace(AllPackageClasses.ClassStub.NESTED_CLASSES, string.IsNullOrWhiteSpace(innerNestedClassBlock) ? string.Empty : innerNestedClassBlock);
singleNestedClassStr = singleNestedClassStr.Replace(AllPackageClasses.ClassStub.NESTED_CLASSES, string.IsNullOrWhiteSpace(innerSingleNestedClassStr) ? string.Empty : innerSingleNestedClassStr);
singleNestedInterfaceStr = singleNestedInterfaceStr.Replace(AllPackageClasses.ClassStub.NESTED_INTERFACES, string.IsNullOrWhiteSpace(innerSingleNestedInterfaceStr) ? string.Empty : innerSingleNestedInterfaceStr);
Expand Down Expand Up @@ -593,7 +613,7 @@ static void AnalyzeNestedClasses(IEnumerable<Class> nestedClasses, IEnumerable<C
{
AnalyzeNestedClasses(nesting, classDefinitions.Concat(nesting), out innerNestedClassBlock, out innerSingleNestedClassStr, out innerSingleNestedInterfaceStr);
}
entry.PrepareSingleClass(signatures, classDefinitions, true, out nestedClassBlock, out singleNestedClassStr, out singleNestedInterfaceStr);
entry.PrepareSingleClass(classDefinitions, true, out nestedClassBlock, out singleNestedClassStr, out singleNestedInterfaceStr);
nestedClassBlock = nestedClassBlock.Replace(AllPackageClasses.ClassStub.NESTED_CLASSES, string.IsNullOrWhiteSpace(innerNestedClassBlock) ? string.Empty : innerNestedClassBlock);
singleNestedClassStr = singleNestedClassStr.Replace(AllPackageClasses.ClassStub.NESTED_CLASSES, string.IsNullOrWhiteSpace(innerSingleNestedClassStr) ? string.Empty : innerSingleNestedClassStr);
singleNestedInterfaceStr = singleNestedInterfaceStr.Replace(AllPackageClasses.ClassStub.NESTED_INTERFACES, string.IsNullOrWhiteSpace(innerSingleNestedInterfaceStr) ? string.Empty : innerSingleNestedInterfaceStr);
Expand Down Expand Up @@ -626,7 +646,7 @@ static void AnalyzeNestedClasses(IEnumerable<Class> nestedClasses, IEnumerable<C
singleInterfaceNestedBlock = subInterfaceAutonoumous.ToString();
}

static void PrepareSingleClass(this Class jClass, IReadOnlyDictionary<string, string> signatures, IEnumerable<Class> classDefinitions, bool isGeneric, out string allPackagesClassBlock, out string singleClassStr, out string singleInterfaceStr)
static void PrepareSingleClass(this Class jClass, IEnumerable<Class> classDefinitions, bool isGeneric, out string allPackagesClassBlock, out string singleClassStr, out string singleInterfaceStr)
{
if (jClass.IsJNetInternalOrManuallyDeveloped())
{
Expand Down Expand Up @@ -748,8 +768,8 @@ static void PrepareSingleClass(this Class jClass, IReadOnlyDictionary<string, st
constructorClassBlock = jClass.AnalyzeConstructors(classDefinitions, isGeneric, true).AddTabLevel(1);
operatorClassBlock = jClass.AnalyzeOperators(classDefinitions, isGeneric, true).AddTabLevel(1);
fieldClassBlock = jClass.AnalyzeFields(classDefinitions, isGeneric).AddTabLevel(1);
staticMethodClassBlock = jClass.AnalyzeMethods(signatures, classDefinitions, methodPrefilter, isGeneric, false, jClassIsListener, false, true).AddTabLevel(1);
methodClassBlock = jClass.AnalyzeMethods(signatures, classDefinitions, methodPrefilter, isGeneric, false, jClassIsListener, false, false).AddTabLevel(1);
staticMethodClassBlock = jClass.AnalyzeMethods(classDefinitions, methodPrefilter, isGeneric, false, jClassIsListener, false, true).AddTabLevel(1);
methodClassBlock = jClass.AnalyzeMethods(classDefinitions, methodPrefilter, isGeneric, false, jClassIsListener, false, false).AddTabLevel(1);
}
else
{
Expand All @@ -758,16 +778,16 @@ static void PrepareSingleClass(this Class jClass, IReadOnlyDictionary<string, st
operatorClassBlock = jClass.AnalyzeOperators(classDefinitions, isGeneric, true).AddTabLevel(1);
}
fieldClassBlock = jClass.AnalyzeFields(classDefinitions, isGeneric).AddTabLevel(1);
staticMethodClassBlock = jClass.AnalyzeMethods(signatures, classDefinitions, methodPrefilter, isGeneric, false, jClassIsListener, false, true).AddTabLevel(1);
methodClassBlock = jClass.AnalyzeMethods(signatures, classDefinitions, methodPrefilter, isGeneric, false, jClassIsListener, false, false).AddTabLevel(1);
staticMethodClassBlock = jClass.AnalyzeMethods(classDefinitions, methodPrefilter, isGeneric, false, jClassIsListener, false, true).AddTabLevel(1);
methodClassBlock = jClass.AnalyzeMethods(classDefinitions, methodPrefilter, isGeneric, false, jClassIsListener, false, false).AddTabLevel(1);
if (!jClass.IsJVMClassWithCallbacks())
{
methodClassBlockDirect = jClass.AnalyzeMethods(signatures, classDefinitions, methodPrefilter, isGeneric, false, false, true, false).AddTabLevel(1);
methodClassBlockDirect = jClass.AnalyzeMethods(classDefinitions, methodPrefilter, isGeneric, false, false, true, false).AddTabLevel(1);
}
}
if (!JNetReflectorCore.DisableInterfaceMethodGeneration && createInterfaceData)
{
methodInterfaceBlock = jClass.AnalyzeMethods(signatures, classDefinitions, methodPrefilter, isGeneric, true, jClassIsListener, false, false).AddTabLevel(1);
methodInterfaceBlock = jClass.AnalyzeMethods(classDefinitions, methodPrefilter, isGeneric, true, jClassIsListener, false, false).AddTabLevel(1);
}
}

Expand Down Expand Up @@ -1243,7 +1263,7 @@ static IList<Method> PrefilterMethods(this Class classDefinition, out bool isMai
return prefilteredMethods;
}

static string AnalyzeMethods(this Class classDefinition, IReadOnlyDictionary<string, string> methodsToSignature, IEnumerable<Class> classDefinitions, IList<Method> prefilteredMethods, bool isGeneric, bool forInterface, bool forListener, bool isDirectListener, bool staticMethods)
static string AnalyzeMethods(this Class classDefinition, IEnumerable<Class> classDefinitions, IList<Method> prefilteredMethods, bool isGeneric, bool forInterface, bool forListener, bool isDirectListener, bool staticMethods)
{
ReportTrace(ReflectionTraceLevel.Info, "******************* Analyze Methods of {0} with static {1} *******************", classDefinition.GenericString, staticMethods);

Expand Down Expand Up @@ -1423,7 +1443,7 @@ static string AnalyzeMethods(this Class classDefinition, IReadOnlyDictionary<str
StringBuilder executionStub = new StringBuilder();
if (getMethod != null)
{
string getSignature = methodsToSignature?.SignatureFromGenericString(getMethod);
string getSignature = getMethod.SignatureFromGenericString();
string execStub = getMethod.IsStatic() ? AllPackageClasses.ClassStub.MethodStub.STATIC_EXECUTE : AllPackageClasses.ClassStub.MethodStub.INSTANCE_EXECUTE;
if (!string.IsNullOrWhiteSpace(getSignature))
{
Expand All @@ -1448,7 +1468,7 @@ static string AnalyzeMethods(this Class classDefinition, IReadOnlyDictionary<str

if (setMethod != null)
{
string setSignature = methodsToSignature?.SignatureFromGenericString(setMethod);
string setSignature = setMethod.SignatureFromGenericString();
if (JNetReflectorCore.ReflectDeprecated) isSetDeprecated = setMethod.IsDeprecated();
string setExecStub = setMethod.IsStatic() ? AllPackageClasses.ClassStub.PropertyStub.STATIC_SET_EXECUTION_FORMAT : AllPackageClasses.ClassStub.PropertyStub.SET_EXECUTION_FORMAT;
var methodToCall = setMethod.IsStatic() ? AllPackageClasses.ClassStub.MethodStub.STATIC_EXECUTE : AllPackageClasses.ClassStub.MethodStub.INSTANCE_EXECUTE;
Expand Down Expand Up @@ -1509,7 +1529,7 @@ static string AnalyzeMethods(this Class classDefinition, IReadOnlyDictionary<str
var method = item.Value;
bool implementMethodAsListener = isDirectListener ? forListener : method.ToBeCallback(classDefinition, forListener);
var genString = method.GenericString;
string signature = methodsToSignature?.SignatureFromGenericString(method);
string signature = method.SignatureFromGenericString();
var paramCount = method.ParameterCount;
var methodNameOrigin = method.Name;
var eventHandlerName = methodNameOrigin;
Expand Down
Loading

0 comments on commit b8eb53b

Please sign in to comment.