Skip to content

Commit

Permalink
Solved some issue along with speed improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
masesdevelopers committed Sep 25, 2024
1 parent 0fa01f4 commit a9b79aa
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 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
37 changes: 29 additions & 8 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 @@ -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
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 a9b79aa

Please sign in to comment.