Skip to content

Commit

Permalink
Solved some issues along with speed improvements (#543)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
masesdevelopers and github-actions[bot] authored Sep 26, 2024
1 parent 0310900 commit 438fe3f
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class<?>> find() {
ComponentSupplier componentSupplier = ComponentContainer.getInstance();
PathHelper pathHelper = componentSupplier.getPathHelper();
Expand Down
66 changes: 66 additions & 0 deletions src/net/JNet/Generated/Javax/Swing/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,72 @@ public partial class Action : Javax.Swing.IAction
#endregion

#region Fields
/// <summary>
/// <see href="https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/Action.html#ACCELERATOR_KEY"/>
/// </summary>
public static Java.Lang.String ACCELERATOR_KEY { get { if (!_ACCELERATOR_KEYReady) { _ACCELERATOR_KEYContent = SGetField<Java.Lang.String>(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
/// <summary>
/// <see href="https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/Action.html#ACTION_COMMAND_KEY"/>
/// </summary>
public static Java.Lang.String ACTION_COMMAND_KEY { get { if (!_ACTION_COMMAND_KEYReady) { _ACTION_COMMAND_KEYContent = SGetField<Java.Lang.String>(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
/// <summary>
/// <see href="https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/Action.html#DEFAULT"/>
/// </summary>
public static Java.Lang.String DEFAULT { get { if (!_DEFAULTReady) { _DEFAULTContent = SGetField<Java.Lang.String>(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
/// <summary>
/// <see href="https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/Action.html#DISPLAYED_MNEMONIC_INDEX_KEY"/>
/// </summary>
public static Java.Lang.String DISPLAYED_MNEMONIC_INDEX_KEY { get { if (!_DISPLAYED_MNEMONIC_INDEX_KEYReady) { _DISPLAYED_MNEMONIC_INDEX_KEYContent = SGetField<Java.Lang.String>(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
/// <summary>
/// <see href="https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/Action.html#LARGE_ICON_KEY"/>
/// </summary>
public static Java.Lang.String LARGE_ICON_KEY { get { if (!_LARGE_ICON_KEYReady) { _LARGE_ICON_KEYContent = SGetField<Java.Lang.String>(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
/// <summary>
/// <see href="https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/Action.html#LONG_DESCRIPTION"/>
/// </summary>
public static Java.Lang.String LONG_DESCRIPTION { get { if (!_LONG_DESCRIPTIONReady) { _LONG_DESCRIPTIONContent = SGetField<Java.Lang.String>(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
/// <summary>
/// <see href="https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/Action.html#MNEMONIC_KEY"/>
/// </summary>
public static Java.Lang.String MNEMONIC_KEY { get { if (!_MNEMONIC_KEYReady) { _MNEMONIC_KEYContent = SGetField<Java.Lang.String>(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
/// <summary>
/// <see href="https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/Action.html#NAME"/>
/// </summary>
public static Java.Lang.String NAME { get { if (!_NAMEReady) { _NAMEContent = SGetField<Java.Lang.String>(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
/// <summary>
/// <see href="https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/Action.html#SELECTED_KEY"/>
/// </summary>
public static Java.Lang.String SELECTED_KEY { get { if (!_SELECTED_KEYReady) { _SELECTED_KEYContent = SGetField<Java.Lang.String>(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
/// <summary>
/// <see href="https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/Action.html#SHORT_DESCRIPTION"/>
/// </summary>
public static Java.Lang.String SHORT_DESCRIPTION { get { if (!_SHORT_DESCRIPTIONReady) { _SHORT_DESCRIPTIONContent = SGetField<Java.Lang.String>(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
/// <summary>
/// <see href="https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/Action.html#SMALL_ICON"/>
/// </summary>
public static Java.Lang.String SMALL_ICON { get { if (!_SMALL_ICONReady) { _SMALL_ICONContent = SGetField<Java.Lang.String>(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

Expand Down
39 changes: 30 additions & 9 deletions src/net/JNetReflector/InternalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -2512,26 +2517,41 @@ 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
{
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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions src/net/JNetReflector/InternalMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,11 @@ static void PrepareSingleClass(this Class jClass, IReadOnlyDictionary<string, st
}
else
{
if (jClass.IsJVMClassWithCallbacks())
{
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);
if (!jClass.IsJVMClassWithCallbacks())
Expand Down
15 changes: 15 additions & 0 deletions src/net/JNetReflector/JNetReflectorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

using Java.Lang;
using Java.Lang.Reflect;
using Java.Util;
using MASES.JCOBridge.C2JBridge;
using MASES.JNetReflector;
Expand Down Expand Up @@ -51,6 +52,20 @@ public static bool EnableLogging
set { SExecute("setLoggingState", value); }
}

/// <summary>
/// Check if <see cref="Method"/> overrides or it is a concrete method
/// </summary>
/// <param name="entry">The <see cref="Method"/> to check</param>
/// <returns><see langword="true"/> if <see cref="Method"/> overrides or it is a concrete method</returns>
public static bool IsOverrideOrConcrete(Method entry) => SExecute<bool>("isOverrideOrConcrete", entry);

/// <summary>
/// Check if <see cref="Method"/> comes from one of the super interfaces
/// </summary>
/// <param name="entry">The <see cref="Method"/> to check</param>
/// <returns><see langword="true"/> if <see cref="Method"/> comes from super interface</returns>
public static bool IsFromSuperInterface(Method entry) => SExecute<bool>("isFromSuperInterface", entry);

public static IDictionary<Class, IReadOnlyDictionary<string, string>> ExtractJavaInfo(IEnumerable<Class> classes, string classPath, string javapFullPath = null)
{
const int argumentLengthLimit = 32699; // from MSDN
Expand Down

0 comments on commit 438fe3f

Please sign in to comment.