View Issue Details

IDProjectCategoryView StatusLast Update
0038955pas2jsrtlpublic2021-06-03 12:49
Reporterhenrique Assigned ToMichael Van Canneyt  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
PlatformPas2JsOSWindows 
Fixed in Versiontrunk 
Summary0038955: GetAttributes implementation.
DescriptionI did the implementation of GetAttributes in RTTI.
TagsNo tags attached.
Fixed in Revision1197
Attached Files

Activities

henrique

2021-06-02 13:58

reporter  

0001-Implementa-o-para-busca-de-atributos.patch (4,065 bytes)   
From 1bff507b0cf37da0c96a359374046b9d68526bea Mon Sep 17 00:00:00 2001
From: Henrique Gottardi Werlang <henriquewerlang@hotmail.com>
Date: Wed, 2 Jun 2021 10:56:58 -0300
Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=A3o=20para=20busca=20de=20a?=
 =?UTF-8?q?tributos.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 packages/rtl/rtti.pas | 61 ++++++++++++++++++++++++++++---------------
 1 file changed, 40 insertions(+), 21 deletions(-)

diff --git a/packages/rtl/rtti.pas b/packages/rtl/rtti.pas
index 609f7f66..451fd1c6 100644
--- a/packages/rtl/rtti.pas
+++ b/packages/rtl/rtti.pas
@@ -98,9 +98,18 @@ type
   { TRttiObject }
 
   TRttiObject = class abstract
+  private
+    FAttributesLoaded: Boolean;
+    FAttributes: TCustomAttributeArray;
+  protected
+    function LoadCustomAttributes: TCustomAttributeArray; virtual;
   public
+    destructor Destroy; override;
+
     //property Handle: Pointer read GetHandle;  not supported in pas2js
-    function GetAttributes: TCustomAttributeArray; virtual;
+    function GetAttributes: TCustomAttributeArray;
+
+    property Attributes: TCustomAttributeArray read GetAttributes;
   end;
 
   { TRttiNamedObject }
@@ -128,9 +137,9 @@ type
     function GetMemberTypeInfo: TTypeMember;
     function GetName: string; override;
     function GetVisibility: TMemberVisibility; virtual;
+    function LoadCustomAttributes: TCustomAttributeArray; override;
   public
     constructor Create(AParent: TRttiType; ATypeInfo: TTypeMember);
-    function GetAttributes: TCustomAttributeArray; override;
 
     property MemberTypeInfo: TTypeMember read GetMemberTypeInfo;
     property Visibility: TMemberVisibility read GetVisibility;
@@ -237,7 +246,6 @@ type
 
   TRttiType = class(TRttiNamedObject)
   private
-    FAttributes: TCustomAttributeArray;
     FTypeInfo: TTypeInfo;
     //FMethods: specialize TArray<TRttiMethod>;
     function GetAsInstance: TRttiInstanceType;
@@ -255,10 +263,9 @@ type
     function GetTypeKind: TTypeKind; virtual;
     //function GetTypeSize: integer; virtual;
     //function GetBaseType: TRttiType; virtual;
+    function LoadCustomAttributes: TCustomAttributeArray; override;
   public
     constructor Create(ATypeInfo : PTypeInfo);
-    destructor Destroy; override;
-    function GetAttributes: TCustomAttributeArray; override;
     function GetField(const AName: string): TRttiField; virtual;
     function GetFields: TRttiFieldArray; virtual;
     function GetMethods: TRttiMethodArray; virtual;
@@ -1409,9 +1416,32 @@ end;
 
 { TRttiObject }
 
+destructor TRttiObject.Destroy;
+var
+  Attribute: TCustomAttribute;
+begin
+  for Attribute in FAttributes do
+    Attribute.Free;
+
+  FAttributes := nil;
+
+  inherited Destroy;
+end;
+
+function TRttiObject.LoadCustomAttributes: TCustomAttributeArray;
+begin
+  Result := nil;
+end;
+
 function TRttiObject.GetAttributes: TCustomAttributeArray;
 begin
-  Result:=nil;
+  if not FAttributesLoaded then
+  begin
+    FAttributes := LoadCustomAttributes;
+    FAttributesLoaded := True;
+  end;
+
+  Result := FAttributes;
 end;
 
 { TRttiNamedObject }
@@ -1444,9 +1474,9 @@ begin
   FTypeInfo:=ATypeInfo;
 end;
 
-function TRttiMember.GetAttributes: TCustomAttributeArray;
+function TRttiMember.LoadCustomAttributes: TCustomAttributeArray;
 begin
-  Result:=inherited GetAttributes;
+  Result:=GetRTTIAttributes(FTypeInfo.Attributes);
 end;
 
 function TRttiMember.GetMemberTypeInfo: TTypeMember;
@@ -1721,20 +1751,9 @@ begin
   FTypeInfo:=TTypeInfo(ATypeInfo);
 end;
 
-destructor TRttiType.Destroy;
-var
-  o: TCustomAttribute;
-begin
-  for o in FAttributes do
-    o.Free;
-  FAttributes:=nil;
-  inherited Destroy;
-end;
-
-function TRttiType.GetAttributes: TCustomAttributeArray;
+function TRttiType.LoadCustomAttributes: TCustomAttributeArray;
 begin
-  FAttributes:=GetRTTIAttributes(FTypeInfo.Attributes);
-  Result:=FAttributes;
+  Result:=GetRTTIAttributes(Handle.Attributes);
 end;
 
 function TRttiType.GetDeclaredProperties: TRttiPropertyArray;
-- 
2.31.1.windows.1

Michael Van Canneyt

2021-06-03 12:49

administrator   ~0131138

Applied, thank you very much !

Issue History

Date Modified Username Field Change
2021-06-02 13:58 henrique New Issue
2021-06-02 13:58 henrique File Added: 0001-Implementa-o-para-busca-de-atributos.patch
2021-06-03 12:49 Michael Van Canneyt Assigned To => Michael Van Canneyt
2021-06-03 12:49 Michael Van Canneyt Status new => resolved
2021-06-03 12:49 Michael Van Canneyt Resolution open => fixed
2021-06-03 12:49 Michael Van Canneyt Fixed in Version => trunk
2021-06-03 12:49 Michael Van Canneyt Fixed in Revision => 1197
2021-06-03 12:49 Michael Van Canneyt Note Added: 0131138