• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()33     File::EntityId GetSuperClassId() const
34     {
35         return File::EntityId(super_class_off_);
36     }
37 
IsInterface()38     bool IsInterface() const
39     {
40         return (access_flags_ & ACC_INTERFACE) != 0;
41     }
42 
GetAccessFlags()43     uint32_t GetAccessFlags() const
44     {
45         return access_flags_;
46     }
47 
GetFieldsNumber()48     uint32_t GetFieldsNumber() const
49     {
50         return num_fields_;
51     }
52 
GetMethodsNumber()53     uint32_t GetMethodsNumber() const
54     {
55         return num_methods_;
56     }
57 
GetIfacesNumber()58     uint32_t GetIfacesNumber() const
59     {
60         return num_ifaces_;
61     }
62 
63     uint32_t GetAnnotationsNumber();
64 
65     uint32_t GetRuntimeAnnotationsNumber();
66 
67     File::EntityId GetInterfaceId(size_t idx) const;
68 
69     template <class Callback>
70     void EnumerateInterfaces(const Callback &cb);
71 
72     template <class Callback>
73     void EnumerateRuntimeAnnotations(const Callback &cb);
74 
75     template <class Callback>
76     void EnumerateAnnotations(const Callback &cb);
77 
78     template <class Callback>
79     void EnumerateTypeAnnotations(const Callback &cb);
80 
81     template <class Callback>
82     void EnumerateRuntimeTypeAnnotations(const Callback &cb);
83 
84     template <class Callback>
85     bool EnumerateRuntimeAnnotationsWithEarlyStop(const Callback &cb);
86 
87     template <class Callback>
88     bool EnumerateAnnotationsWithEarlyStop(const Callback &cb);
89 
90     std::optional<SourceLang> GetSourceLang();
91 
92     std::optional<File::EntityId> GetSourceFileId();
93 
94     template <class Callback>
95     void EnumerateFields(const Callback &cb);
96 
97     template <class Callback>
98     void EnumerateMethods(const Callback &cb);
99 
GetSize()100     size_t GetSize()
101     {
102         if (size_ == 0) {
103             SkipMethods();
104         }
105 
106         return size_;
107     }
108 
GetPandaFile()109     const File &GetPandaFile() const
110     {
111         return panda_file_;
112     }
113 
GetClassId()114     File::EntityId GetClassId() const
115     {
116         return class_id_;
117     }
118 
GetDescriptor()119     const uint8_t *GetDescriptor() const
120     {
121         return name_.data;
122     }
123 
124 private:
125     void SkipSourceLang();
126 
127     void SkipRuntimeAnnotations();
128 
129     void SkipAnnotations();
130 
131     void SkipSourceFile();
132 
133     void SkipFields();
134 
135     void SkipMethods();
136 
137     void SkipRuntimeTypeAnnotations();
138 
139     void SkipTypeAnnotations();
140 
141     const File &panda_file_;
142     File::EntityId class_id_;
143 
144     File::StringData name_;
145     uint32_t super_class_off_;
146     uint32_t access_flags_;
147     uint32_t num_fields_;
148     uint32_t num_methods_;
149     uint32_t num_ifaces_;
150 
151     Span<const uint8_t> ifaces_offsets_sp_ {nullptr, nullptr};
152     Span<const uint8_t> source_lang_sp_ {nullptr, nullptr};
153     Span<const uint8_t> runtime_annotations_sp_ {nullptr, nullptr};
154     Span<const uint8_t> annotations_sp_ {nullptr, nullptr};
155     Span<const uint8_t> runtime_type_annotation_sp_ {nullptr, nullptr};
156     Span<const uint8_t> type_annotation_sp_ {nullptr, nullptr};
157     Span<const uint8_t> source_file_sp_ {nullptr, nullptr};
158     Span<const uint8_t> fields_sp_ {nullptr, nullptr};
159     Span<const uint8_t> methods_sp_ {nullptr, nullptr};
160 
161     size_t size_;
162 };
163 
164 }  // namespace panda::panda_file
165 
166 #endif  // LIBPANDAFILE_CLASS_DATA_ACCESSOR_H
167