Browse Source

* Get method by address. Patch by Lipinast Lekrisov

Michaël Van Canneyt 7 months ago
parent
commit
072cb55315
2 changed files with 29 additions and 1 deletions
  1. 14 1
      packages/rtl-objpas/src/inc/rtti.pp
  2. 15 0
      packages/rtl-objpas/tests/tests.rtti.pas

+ 14 - 1
packages/rtl-objpas/src/inc/rtti.pp

@@ -394,6 +394,7 @@ type
     function GetMethods: TRttiMethodArray; virtual; overload;
     function GetMethods: TRttiMethodArray; virtual; overload;
     function GetMethods(const aName: string): TRttiMethodArray; overload; virtual;
     function GetMethods(const aName: string): TRttiMethodArray; overload; virtual;
     function GetMethod(const aName: String): TRttiMethod; virtual;
     function GetMethod(const aName: String): TRttiMethod; virtual;
+    function GetMethod(aCodeAddress: CodePointer): TRttiMethod; overload; virtual;
     function ToString : RTLString; override;
     function ToString : RTLString; override;
     property IsInstance: boolean read GetIsInstance;
     property IsInstance: boolean read GetIsInstance;
     property IsManaged: boolean read GetIsManaged;
     property IsManaged: boolean read GetIsManaged;
@@ -7507,7 +7508,7 @@ end;
 
 
 function TRttiType.GetMethod(const aName: String): TRttiMethod;
 function TRttiType.GetMethod(const aName: String): TRttiMethod;
 var
 var
-  methods: specialize TArray<TRttiMethod>;
+  methods: TRttiMethodArray;
   method: TRttiMethod;
   method: TRttiMethod;
 begin
 begin
   methods := GetMethods;
   methods := GetMethods;
@@ -7517,6 +7518,18 @@ begin
   Result := Nil;
   Result := Nil;
 end;
 end;
 
 
+function TRttiType.GetMethod(aCodeAddress: CodePointer): TRttiMethod;
+var
+  methods: TRttiMethodArray;
+  method: TRttiMethod;
+begin
+  methods := GetMethods;
+  for method in methods do
+    if method.CodeAddress = aCodeAddress then
+      Exit(method);
+  Result := Nil;
+end;
+
 function TRttiType.ToString: RTLString;
 function TRttiType.ToString: RTLString;
 begin
 begin
   Result:=Name;
   Result:=Name;

+ 15 - 0
packages/rtl-objpas/tests/tests.rtti.pas

@@ -109,6 +109,7 @@ type
     Procedure TestProperties;
     Procedure TestProperties;
     Procedure TestDeclaredMethods;
     Procedure TestDeclaredMethods;
     Procedure TestMethods;
     Procedure TestMethods;
+    Procedure TestMethodByAddress;
     Procedure TestMethodsInherited;
     Procedure TestMethodsInherited;
     Procedure TestPrivateFieldAttributes;
     Procedure TestPrivateFieldAttributes;
     Procedure TestProtectedFieldAttributes;
     Procedure TestProtectedFieldAttributes;
@@ -1822,6 +1823,20 @@ begin
   CheckMethod('Full declared',1, A[0],'PublicAdditionalMethod',mvPublic);
   CheckMethod('Full declared',1, A[0],'PublicAdditionalMethod',mvPublic);
 end;
 end;
 
 
+procedure TTestClassExtendedRTTI.TestMethodByAddress;
+
+var
+  Obj : TRttiObject;
+  RttiData : TRttiInstanceType absolute obj;
+  M1,M2 : TRttiMethod;
+begin
+  Obj:=FCtx.GetType(TAdditionalMethodClassRTTI.ClassInfo);
+  M1:=RttiData.GetMethod('PublicAdditionalMethod');
+  AssertNotNull('have method',m1);
+  M2:=RttiData.GetMethod(@TAdditionalMethodClassRTTI.PublicAdditionalMethod);
+  AssertSame('Correct method ',M1,M2);
+end;
+
 procedure TTestClassExtendedRTTI.TestMethodsInherited;
 procedure TTestClassExtendedRTTI.TestMethodsInherited;
 Var
 Var
   A : TRttiMethodArray;
   A : TRttiMethodArray;