1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef LIBPANDAFILE_CLASS_DATA_ACCESSOR_H
17 #define LIBPANDAFILE_CLASS_DATA_ACCESSOR_H
18 
19 #include "file.h"
20 #include "file_items.h"
21 #include "field_data_accessor.h"
22 #include "method_data_accessor.h"
23 
24 namespace panda::panda_file {
25 
26 // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions)
27 class ClassDataAccessor {
28 public:
29     ClassDataAccessor(const File &panda_file, File::EntityId class_id);
30 
31     ~ClassDataAccessor() = default;
32 
GetSuperClassId() const33     File::EntityId GetSuperClassId() const
34     {
35         return File::EntityId(super_class_off_);
36     }
37 
IsInterface() const38     bool IsInterface() const
39     {
40         return (access_flags_ & ACC_INTERFACE) != 0;
41     }
42 
IsAnnotation() const43     bool IsAnnotation() const
44     {
45         return (access_flags_ & ACC_ANNOTATION) != 0;
46     }
47 
GetAccessFlags() const48     uint32_t GetAccessFlags() const
49     {
50         return access_flags_;
51     }
52 
GetFieldsNumber() const53     uint32_t GetFieldsNumber() const
54     {
55         return num_fields_;
56     }
57 
GetMethodsNumber() const58     uint32_t GetMethodsNumber() const
59     {
60         return num_methods_;
61     }
62 
GetIfacesNumber() const63     uint32_t GetIfacesNumber() const
64     {
65         return num_ifaces_;
66     }
67 
68     uint32_t GetAnnotationsNumber();
69 
70     uint32_t GetRuntimeAnnotationsNumber();
71 
72     File::EntityId GetInterfaceId(size_t idx) const;
73 
74     template <class Callback>
75     void EnumerateInterfaces(const Callback &cb);
76 
77     template <class Callback>
78     void EnumerateRuntimeAnnotations(const Callback &cb);
79 
80     template <class Callback>
81     void EnumerateAnnotations(const Callback &cb);
82 
83     template <class Callback>
84     void EnumerateTypeAnnotations(const Callback &cb);
85 
86     template <class Callback>
87     void EnumerateRuntimeTypeAnnotations(const Callback &cb);
88 
89     template <class Callback>
90     bool EnumerateRuntimeAnnotationsWithEarlyStop(const Callback &cb);
91 
92     template <class Callback>
93     bool EnumerateAnnotationsWithEarlyStop(const Callback &cb);
94 
95     std::optional<SourceLang> GetSourceLang();
96 
97     std::optional<File::EntityId> GetSourceFileId();
98 
99     template <class Callback>
100     void EnumerateFields(const Callback &cb);
101 
102     template <class Callback>
103     void EnumerateMethods(const Callback &cb);
104 
GetSize()105     size_t GetSize()
106     {
107         if (size_ == 0) {
108             SkipMethods();
109         }
110 
111         return size_;
112     }
113 
GetPandaFile() const114     const File &GetPandaFile() const
115     {
116         return panda_file_;
117     }
118 
GetClassId() const119     File::EntityId GetClassId() const
120     {
121         return class_id_;
122     }
123 
GetDescriptor() const124     const uint8_t *GetDescriptor() const
125     {
126         return name_.data;
127     }
128 
GetName() const129     const File::StringData GetName() const
130     {
131         return name_;
132     }
133 
134 private:
135     void SkipSourceLang();
136 
137     void SkipRuntimeAnnotations();
138 
139     void SkipAnnotations();
140 
141     void SkipSourceFile();
142 
143     void SkipFields();
144 
145     void SkipMethods();
146 
147     void SkipRuntimeTypeAnnotations();
148 
149     void SkipTypeAnnotations();
150 
151     const File &panda_file_;
152     File::EntityId class_id_;
153 
154     File::StringData name_;
155     uint32_t super_class_off_;
156     uint32_t access_flags_;
157     uint32_t num_fields_;
158     uint32_t num_methods_;
159     uint32_t num_ifaces_;
160 
161     Span<const uint8_t> ifaces_offsets_sp_ {nullptr, nullptr};
162     Span<const uint8_t> source_lang_sp_ {nullptr, nullptr};
163     Span<const uint8_t> runtime_annotations_sp_ {nullptr, nullptr};
164     Span<const uint8_t> annotations_sp_ {nullptr, nullptr};
165     Span<const uint8_t> runtime_type_annotation_sp_ {nullptr, nullptr};
166     Span<const uint8_t> type_annotation_sp_ {nullptr, nullptr};
167     Span<const uint8_t> source_file_sp_ {nullptr, nullptr};
168     Span<const uint8_t> fields_sp_ {nullptr, nullptr};
169     Span<const uint8_t> methods_sp_ {nullptr, nullptr};
170 
171     size_t size_;
172 };
173 
174 }  // namespace panda::panda_file
175 
176 #endif  // LIBPANDAFILE_CLASS_DATA_ACCESSOR_H
177