From 438fe3f0dba47416bba15e151d5cb469ededdcbc Mon Sep 17 00:00:00 2001 From: MASES Public Developers Team <94312179+masesdevelopers@users.noreply.github.com> Date: Thu, 26 Sep 2024 17:25:43 +0200 Subject: [PATCH] Solved some issues along with speed improvements (#543) * Solved some issue along with speed improvements * Added fields and operators on listeners generation * Fix missing reflection of classes listed in ClassesWithCallbacks Co-authored-by: masesdevelopers <94312179+masesdevelopers@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../jnet/developed/JNetReflectorHelper.java | 30 +++++++++ src/net/JNet/Generated/Javax/Swing/Action.cs | 66 +++++++++++++++++++ src/net/JNetReflector/InternalExtensions.cs | 39 ++++++++--- src/net/JNetReflector/InternalMethods.cs | 5 ++ src/net/JNetReflector/JNetReflectorHelper.cs | 15 +++++ 5 files changed, 146 insertions(+), 9 deletions(-) diff --git a/src/jvm/jnet/src/main/java/org/mases/jnet/developed/JNetReflectorHelper.java b/src/jvm/jnet/src/main/java/org/mases/jnet/developed/JNetReflectorHelper.java index 76a7e24c20..bfd1912182 100644 --- a/src/jvm/jnet/src/main/java/org/mases/jnet/developed/JNetReflectorHelper.java +++ b/src/jvm/jnet/src/main/java/org/mases/jnet/developed/JNetReflectorHelper.java @@ -42,6 +42,36 @@ public static void setLoggingState(boolean value) { } } + public static boolean isOverrideOrConcrete(java.lang.reflect.Method entry) { + try { + java.lang.Class superClass = entry.getDeclaringClass().getSuperclass(); + if (superClass == null) return false; + if (superClass.getTypeName().equals("java.lang.Object")) return false; + java.lang.reflect.Method method = superClass.getMethod(entry.getName(), entry.getParameterTypes()); + return true; + } + catch (java.lang.NoSuchMethodException e) { + return false; + } + } + + public static boolean isFromSuperInterface(java.lang.reflect.Method entry) { + for( Class interfaceToCheck : entry.getDeclaringClass().getInterfaces()) { + try { + java.lang.reflect.Method method = interfaceToCheck.getMethod(entry.getName(), entry.getParameterTypes()); + if (!method.getReturnType().equals(entry.getReturnType())) + { + return true; + } + } + catch (java.lang.NoSuchMethodException e) { + + } + } + + return false; + } + public static Collection> find() { ComponentSupplier componentSupplier = ComponentContainer.getInstance(); PathHelper pathHelper = componentSupplier.getPathHelper(); diff --git a/src/net/JNet/Generated/Javax/Swing/Action.cs b/src/net/JNet/Generated/Javax/Swing/Action.cs index 7d6250a03c..3ef4eb8cdc 100644 --- a/src/net/JNet/Generated/Javax/Swing/Action.cs +++ b/src/net/JNet/Generated/Javax/Swing/Action.cs @@ -165,6 +165,72 @@ public partial class Action : Javax.Swing.IAction #endregion #region Fields + /// + /// + /// + public static Java.Lang.String ACCELERATOR_KEY { get { if (!_ACCELERATOR_KEYReady) { _ACCELERATOR_KEYContent = SGetField(LocalBridgeClazz, "ACCELERATOR_KEY"); _ACCELERATOR_KEYReady = true; } return _ACCELERATOR_KEYContent; } } + private static Java.Lang.String _ACCELERATOR_KEYContent = default; + private static bool _ACCELERATOR_KEYReady = false; // this is used because in case of generics + /// + /// + /// + public static Java.Lang.String ACTION_COMMAND_KEY { get { if (!_ACTION_COMMAND_KEYReady) { _ACTION_COMMAND_KEYContent = SGetField(LocalBridgeClazz, "ACTION_COMMAND_KEY"); _ACTION_COMMAND_KEYReady = true; } return _ACTION_COMMAND_KEYContent; } } + private static Java.Lang.String _ACTION_COMMAND_KEYContent = default; + private static bool _ACTION_COMMAND_KEYReady = false; // this is used because in case of generics + /// + /// + /// + public static Java.Lang.String DEFAULT { get { if (!_DEFAULTReady) { _DEFAULTContent = SGetField(LocalBridgeClazz, "DEFAULT"); _DEFAULTReady = true; } return _DEFAULTContent; } } + private static Java.Lang.String _DEFAULTContent = default; + private static bool _DEFAULTReady = false; // this is used because in case of generics + /// + /// + /// + public static Java.Lang.String DISPLAYED_MNEMONIC_INDEX_KEY { get { if (!_DISPLAYED_MNEMONIC_INDEX_KEYReady) { _DISPLAYED_MNEMONIC_INDEX_KEYContent = SGetField(LocalBridgeClazz, "DISPLAYED_MNEMONIC_INDEX_KEY"); _DISPLAYED_MNEMONIC_INDEX_KEYReady = true; } return _DISPLAYED_MNEMONIC_INDEX_KEYContent; } } + private static Java.Lang.String _DISPLAYED_MNEMONIC_INDEX_KEYContent = default; + private static bool _DISPLAYED_MNEMONIC_INDEX_KEYReady = false; // this is used because in case of generics + /// + /// + /// + public static Java.Lang.String LARGE_ICON_KEY { get { if (!_LARGE_ICON_KEYReady) { _LARGE_ICON_KEYContent = SGetField(LocalBridgeClazz, "LARGE_ICON_KEY"); _LARGE_ICON_KEYReady = true; } return _LARGE_ICON_KEYContent; } } + private static Java.Lang.String _LARGE_ICON_KEYContent = default; + private static bool _LARGE_ICON_KEYReady = false; // this is used because in case of generics + /// + /// + /// + public static Java.Lang.String LONG_DESCRIPTION { get { if (!_LONG_DESCRIPTIONReady) { _LONG_DESCRIPTIONContent = SGetField(LocalBridgeClazz, "LONG_DESCRIPTION"); _LONG_DESCRIPTIONReady = true; } return _LONG_DESCRIPTIONContent; } } + private static Java.Lang.String _LONG_DESCRIPTIONContent = default; + private static bool _LONG_DESCRIPTIONReady = false; // this is used because in case of generics + /// + /// + /// + public static Java.Lang.String MNEMONIC_KEY { get { if (!_MNEMONIC_KEYReady) { _MNEMONIC_KEYContent = SGetField(LocalBridgeClazz, "MNEMONIC_KEY"); _MNEMONIC_KEYReady = true; } return _MNEMONIC_KEYContent; } } + private static Java.Lang.String _MNEMONIC_KEYContent = default; + private static bool _MNEMONIC_KEYReady = false; // this is used because in case of generics + /// + /// + /// + public static Java.Lang.String NAME { get { if (!_NAMEReady) { _NAMEContent = SGetField(LocalBridgeClazz, "NAME"); _NAMEReady = true; } return _NAMEContent; } } + private static Java.Lang.String _NAMEContent = default; + private static bool _NAMEReady = false; // this is used because in case of generics + /// + /// + /// + public static Java.Lang.String SELECTED_KEY { get { if (!_SELECTED_KEYReady) { _SELECTED_KEYContent = SGetField(LocalBridgeClazz, "SELECTED_KEY"); _SELECTED_KEYReady = true; } return _SELECTED_KEYContent; } } + private static Java.Lang.String _SELECTED_KEYContent = default; + private static bool _SELECTED_KEYReady = false; // this is used because in case of generics + /// + /// + /// + public static Java.Lang.String SHORT_DESCRIPTION { get { if (!_SHORT_DESCRIPTIONReady) { _SHORT_DESCRIPTIONContent = SGetField(LocalBridgeClazz, "SHORT_DESCRIPTION"); _SHORT_DESCRIPTIONReady = true; } return _SHORT_DESCRIPTIONContent; } } + private static Java.Lang.String _SHORT_DESCRIPTIONContent = default; + private static bool _SHORT_DESCRIPTIONReady = false; // this is used because in case of generics + /// + /// + /// + public static Java.Lang.String SMALL_ICON { get { if (!_SMALL_ICONReady) { _SMALL_ICONContent = SGetField(LocalBridgeClazz, "SMALL_ICON"); _SMALL_ICONReady = true; } return _SMALL_ICONContent; } } + private static Java.Lang.String _SMALL_ICONContent = default; + private static bool _SMALL_ICONReady = false; // this is used because in case of generics #endregion diff --git a/src/net/JNetReflector/InternalExtensions.cs b/src/net/JNetReflector/InternalExtensions.cs index 1e8623108c..058959606b 100644 --- a/src/net/JNetReflector/InternalExtensions.cs +++ b/src/net/JNetReflector/InternalExtensions.cs @@ -29,6 +29,8 @@ using System.Reflection; using System.Reflection.Emit; using System.Text.RegularExpressions; +using Org.Mases.Jnet; +using System.Diagnostics; namespace MASES.JNetReflector { @@ -1100,7 +1102,7 @@ public static bool IsJVMListenerClass(this Class entry) { if (entry.TypeName.IsJVMListenerClass()) { - if (!entry.IsInterface && entry.Interfaces.Length == 0) // if a class and there aren't interfaces it is not a listener + if (!entry.IsJVMClassWithCallbacks() && !entry.IsInterface && entry.Interfaces.Length == 0) // if a class and there aren't interfaces it is not a listener { return false; } @@ -2341,18 +2343,20 @@ public static bool IsProperty(this Method entry) public static bool ToBeCallback(this Method entry, Class classDefinition, bool forListener) { + string entryName = entry.Name; + string classDefinitionTypeName = classDefinition.TypeName; bool match = forListener; if (JNetReflectorCore.ClassesWithCallbacks != null) { foreach (var elem in JNetReflectorCore.ClassesWithCallbacks) { - if (elem.ClassName == classDefinition.TypeName) + if (elem.ClassName == classDefinitionTypeName) { // now on check only filters match = false; foreach (var pattern in elem.Patterns) { - match |= Regex.IsMatch(entry.Name, pattern); + match |= Regex.IsMatch(entryName, pattern); } break; } @@ -2498,8 +2502,9 @@ public static bool IsObjectArrayReturnType(this Method entry, bool usedInGeneric public static bool IsOverrideOrConcrete(this Method entry) { - // to be optimized: very time consuming method if (entry == null) throw new ArgumentNullException(nameof(entry)); +#if TEST + // to be optimized: very time consuming method try { var superClass = entry.DeclaringClass.SuperClass; @@ -2512,12 +2517,21 @@ public static bool IsOverrideOrConcrete(this Method entry) { return false; } +#else + return JNetReflectorHelper.IsOverrideOrConcrete(entry); +#endif } public static bool IsFromSuperInterface(this Method entry) { - // to be optimized: very time consuming method if (entry == null) throw new ArgumentNullException(nameof(entry)); +#if TEST + Stopwatch sw1 = Stopwatch.StartNew(); + var jvmResult = JNetReflectorHelper.IsFromSuperInterface(entry); // in JVM the speed is 1000 times higher + sw1.Stop(); + // to be optimized: very time consuming method + Stopwatch sw2 = Stopwatch.StartNew(); + bool netResult = false; foreach (var interfaceToCheck in entry.DeclaringClass.Interfaces) { try @@ -2525,13 +2539,19 @@ public static bool IsFromSuperInterface(this Method entry) Method method = interfaceToCheck.GetMethod(entry.Name, entry.ParameterTypes); if (!method.ReturnType.Equals(entry.ReturnType)) { - return true; + netResult = true; + break; } } catch (NoSuchMethodException) { } } + sw2.Stop(); + if (jvmResult != netResult) throw new InvalidOperationException("Results of IsFromSuperInterface are different"); - return false; + return netResult; +#else + return JNetReflectorHelper.IsFromSuperInterface(entry); +#endif } public static bool MustBeAvoided(this Method entry, bool usedInGenerics) @@ -2581,7 +2601,7 @@ public static string JavadocHrefUrl(this Method entry, bool camel) return string.Format(AllPackageClasses.DocTemplate(_CurrentJavadocBaseUrl), JavadocUrl(entry, camel).Replace(SpecialNames.BeginGenericDeclaration, "%3C").Replace(SpecialNames.EndGenericDeclaration, "%3E")); } - #endregion +#endregion #region Field extension @@ -2763,7 +2783,8 @@ public static bool IsObjectType(this Parameter entry, bool camel) public static bool IsJVMException(this Parameter entry) { - if (entry == null || entry.ParameterizedType == null || entry.ParameterizedType.TypeName == null) return false; + if (entry == null || entry.ParameterizedType == null || entry.ParameterizedType.TypeName == null + || (entry.Type != null && entry.Type.IsPrimitive == true)) return false; var cName = entry.ParameterizedType.TypeName; cName = cName.Contains(SpecialNames.BeginGenericDeclaration) ? cName.Substring(0, cName.IndexOf(SpecialNames.BeginGenericDeclaration)) : cName; var cEntry = cName.JVMClass(); diff --git a/src/net/JNetReflector/InternalMethods.cs b/src/net/JNetReflector/InternalMethods.cs index 48a8a42835..fc945a34ac 100644 --- a/src/net/JNetReflector/InternalMethods.cs +++ b/src/net/JNetReflector/InternalMethods.cs @@ -753,6 +753,11 @@ static void PrepareSingleClass(this Class jClass, IReadOnlyDictionary + /// Check if overrides or it is a concrete method + /// + /// The to check + /// if overrides or it is a concrete method + public static bool IsOverrideOrConcrete(Method entry) => SExecute("isOverrideOrConcrete", entry); + + /// + /// Check if comes from one of the super interfaces + /// + /// The to check + /// if comes from super interface + public static bool IsFromSuperInterface(Method entry) => SExecute("isFromSuperInterface", entry); + public static IDictionary> ExtractJavaInfo(IEnumerable classes, string classPath, string javapFullPath = null) { const int argumentLengthLimit = 32699; // from MSDN