View Issue Details

IDProjectCategoryView StatusLast Update
0037736pas2jsrtlpublic2020-09-12 10:04
Reporterhenrique Assigned ToMichael Van Canneyt  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
PlatformPas2JsOSWindows 
Fixed in Versiontrunk 
Summary0037736: Adjust getmethod implementation
DescriptionThe function that carried the methods was not taking into account the methods of the parent class.
TagsNo tags attached.
Fixed in Revision872
Attached Files

Activities

henrique

2020-09-11 21:13

reporter  

Methods.patch (4,341 bytes)   
From fb6f30e8abc4bcbaca89d62d36f33e9fe888a3e6 Mon Sep 17 00:00:00 2001
From: Henrique Gottardi Werlang <henriquewerlang@hotmail.com>
Date: Fri, 11 Sep 2020 11:29:12 -0300
Subject: [PATCH] Ajustado busca de metodos.

---
 packages/rtl/rtti.pas | 82 +++++++++++++++++++++++++++++++------------
 1 file changed, 60 insertions(+), 22 deletions(-)

diff --git a/packages/rtl/rtti.pas b/packages/rtl/rtti.pas
index a09ab3b..1a60bc0 100644
--- a/packages/rtl/rtti.pas
+++ b/packages/rtl/rtti.pas
@@ -236,6 +236,7 @@ type
 
   TRttiStructuredType = class abstract(TRttiType)
   protected
+    function GetAncestor: TRttiStructuredType; virtual; abstract;
     function GetDeclaredProperties: TRttiPropertyArray; override;
     function GetMethod(const aName: String): TRttiMethod; override;
     function GetMethods: TRttiMethodArray; override;
@@ -254,16 +255,22 @@ type
   private
     function GetClassTypeInfo: TTypeInfoClass;
     function GetMetaClassType: TClass;
+  protected
+    function GetAncestor: TRttiStructuredType; override;
   public
     constructor Create(ATypeInfo: PTypeInfo);
     property ClassTypeInfo: TTypeInfoClass read GetClassTypeInfo;
     property MetaClassType: TClass read GetMetaClassType;
   end;
 
+  { TRttiInterfaceType }
+
   TRttiInterfaceType = class(TRttiStructuredType)
   private
     function GetGUID: TGUID;
     function GetInterfaceTypeInfo: TTypeInfoInterface;
+  protected
+    function GetAncestor: TRttiStructuredType; override;
   public
     constructor Create(ATypeInfo: PTypeInfo);
 
@@ -666,49 +673,70 @@ end;
 
 function TRttiStructuredType.GetMethods: TRttiMethodArray;
 var
-  A: Integer;
+  A, MethodCount: Integer;
+
+  BaseClass: TRttiStructuredType;
 
 begin
+  BaseClass := Self;
+  MethodCount := 0;
+
+  while Assigned(BaseClass) do
+  begin
+    Inc(MethodCount, BaseClass.StructTypeInfo.MethodCount);
+
+    BaseClass := BaseClass.GetAncestor;
+  end;
+
   SetLength(Result, StructTypeInfo.MethodCount);
 
-  for A := 0 to Pred(StructTypeInfo.MethodCount) do
-    Result[A] := TRttiMethod.Create(Self, StructTypeInfo.GetMethod(A));
+  BaseClass := Self;
+
+  while Assigned(BaseClass) do
+  begin
+    for A := 0 to Pred(BaseClass.StructTypeInfo.MethodCount) do
+    begin
+      Dec(MethodCount);
+
+      Result[MethodCount] := TRttiMethod.Create(BaseClass, BaseClass.StructTypeInfo.GetMethod(A));
+    end;
+
+    BaseClass := BaseClass.GetAncestor;
+  end;
 end;
 
 function TRttiStructuredType.GetMethods(const aName: String): TRttiMethodArray;
 var
-  A: Integer;
+  Method: TRttiMethod;
 
-  Method: TTypeMemberMethod;
+  MethodCount: Integer;
 
 begin
-  SetLength(Result, StructTypeInfo.MethodCount);
+  MethodCount := 0;
 
-  for A := 0 to Pred(StructTypeInfo.MethodCount) do
-  begin
-    Method := StructTypeInfo.GetMethod(A);
+  for Method in GetMethods do
+    if aName = Method.Name then
+      Inc(MethodCount);
+
+  SetLength(Result, MethodCount);
 
+  for Method in GetMethods do
     if aName = Method.Name then
-      Result[A] := TRttiMethod.Create(Self, Method);
-  end;
+    begin
+      Dec(MethodCount);
+
+      Result[MethodCount] := Method;
+    end;
 end;
 
 function TRttiStructuredType.GetMethod(const aName: String): TRttiMethod;
 var
-  A: Integer;
-
-  Method: TTypeMemberMethod;
+  Method: TRttiMethod;
 
 begin
-  Result := nil;
-
-  for A := 0 to Pred(StructTypeInfo.MethodCount) do
-  begin
-    Method := StructTypeInfo.GetMethod(A);
-
+  for Method in GetMethods do
     if aName = Method.Name then
-      Exit(TRttiMethod.Create(Self, Method));
-  end;
+      Exit(Method);
 end;
 
 function TRttiStructuredType.GetProperty(const AName: string): TRttiProperty;
@@ -759,6 +787,11 @@ begin
   Result:=ClassTypeInfo.ClassType;
 end;
 
+function TRttiInstanceType.GetAncestor: TRttiStructuredType;
+begin
+  Result := GRttiContext.GetType(ClassTypeInfo.Ancestor) as TRttiStructuredType;
+end;
+
 constructor TRttiInstanceType.Create(ATypeInfo: PTypeInfo);
 begin
   if not (TTypeInfo(ATypeInfo) is TTypeInfoClass) then
@@ -790,6 +823,11 @@ begin
   Result := TTypeInfoInterface(FTypeInfo);
 end;
 
+function TRttiInterfaceType.GetAncestor: TRttiStructuredType;
+begin
+  Result := GRttiContext.GetType(InterfaceTypeInfo.Ancestor) as TRttiStructuredType;
+end;
+
 { TRTTIContext }
 
 class constructor TRTTIContext.Init;
-- 
2.28.0.windows.1

Methods.patch (4,341 bytes)   

Michael Van Canneyt

2020-09-12 10:04

administrator   ~0125501

Checked & Applied, thank you very much for the patch !

Issue History

Date Modified Username Field Change
2020-09-11 21:13 henrique New Issue
2020-09-11 21:13 henrique File Added: Methods.patch
2020-09-12 10:04 Michael Van Canneyt Assigned To => Michael Van Canneyt
2020-09-12 10:04 Michael Van Canneyt Status new => resolved
2020-09-12 10:04 Michael Van Canneyt Resolution open => fixed
2020-09-12 10:04 Michael Van Canneyt Fixed in Version => trunk
2020-09-12 10:04 Michael Van Canneyt Fixed in Revision => 872
2020-09-12 10:04 Michael Van Canneyt Note Added: 0125501