blob: 9f2ee23327417979474c630d85e199b1ea141628 [file] [log] [blame]
Mathieu Chartier0d896bd2018-05-25 00:20:27 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "dex/class_accessor-inl.h"
18
19#include "base/common_art_test.h"
20
21namespace art {
22
23class ClassAccessorTest : public CommonArtTest {};
24
25TEST_F(ClassAccessorTest, TestVisiting) {
26 std::vector<std::unique_ptr<const DexFile>> dex_files(
27 OpenDexFiles(GetLibCoreDexFileNames()[0].c_str()));
28 ASSERT_GT(dex_files.size(), 0u);
29 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
30 uint32_t class_def_idx = 0u;
31 ASSERT_GT(dex_file->NumClassDefs(), 0u);
32 for (ClassAccessor accessor : dex_file->GetClasses()) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080033 const dex::ClassDef& class_def = dex_file->GetClassDef(accessor.GetClassDefIndex());
Mathieu Chartier0d896bd2018-05-25 00:20:27 -070034 EXPECT_EQ(accessor.GetDescriptor(), dex_file->StringByTypeIdx(class_def.class_idx_));
Mathieu Chartier18e26872018-06-04 17:19:02 -070035 EXPECT_EQ(class_def_idx, accessor.GetClassDefIndex());
Mathieu Chartier0d896bd2018-05-25 00:20:27 -070036 ++class_def_idx;
37 // Check iterators against visitors.
38 auto methods = accessor.GetMethods();
39 auto fields = accessor.GetFields();
40 auto method_it = methods.begin();
41 auto field_it = fields.begin();
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070042 auto instance_fields = accessor.GetInstanceFields();
43 auto instance_field_it = instance_fields.begin();
Mathieu Chartier0d896bd2018-05-25 00:20:27 -070044 accessor.VisitFieldsAndMethods(
45 // Static fields.
46 [&](const ClassAccessor::Field& field) {
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070047 EXPECT_TRUE(field.IsStatic());
48 EXPECT_TRUE(field_it->IsStatic());
Mathieu Chartier0d896bd2018-05-25 00:20:27 -070049 EXPECT_EQ(field.GetIndex(), field_it->GetIndex());
50 EXPECT_EQ(field.GetAccessFlags(), field_it->GetAccessFlags());
51 ++field_it;
52 },
53 // Instance fields.
54 [&](const ClassAccessor::Field& field) {
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070055 EXPECT_FALSE(field.IsStatic());
56 EXPECT_FALSE(field_it->IsStatic());
Mathieu Chartier0d896bd2018-05-25 00:20:27 -070057 EXPECT_EQ(field.GetIndex(), field_it->GetIndex());
58 EXPECT_EQ(field.GetAccessFlags(), field_it->GetAccessFlags());
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070059 EXPECT_EQ(field.GetIndex(), instance_field_it->GetIndex());
60 EXPECT_EQ(field.GetAccessFlags(), instance_field_it->GetAccessFlags());
Mathieu Chartier0d896bd2018-05-25 00:20:27 -070061 ++field_it;
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070062 ++instance_field_it;
Mathieu Chartier0d896bd2018-05-25 00:20:27 -070063 },
64 // Direct methods.
65 [&](const ClassAccessor::Method& method) {
66 EXPECT_TRUE(method.IsStaticOrDirect());
67 EXPECT_EQ(method.IsStaticOrDirect(), method_it->IsStaticOrDirect());
68 EXPECT_EQ(method.GetIndex(), method_it->GetIndex());
69 EXPECT_EQ(method.GetAccessFlags(), method_it->GetAccessFlags());
70 EXPECT_EQ(method.GetCodeItem(), method_it->GetCodeItem());
71 ++method_it;
72 },
73 // Virtual methods.
74 [&](const ClassAccessor::Method& method) {
75 EXPECT_FALSE(method.IsStaticOrDirect());
76 EXPECT_EQ(method.IsStaticOrDirect(), method_it->IsStaticOrDirect());
77 EXPECT_EQ(method.GetIndex(), method_it->GetIndex());
78 EXPECT_EQ(method.GetAccessFlags(), method_it->GetAccessFlags());
79 EXPECT_EQ(method.GetCodeItem(), method_it->GetCodeItem());
80 ++method_it;
81 });
82 ASSERT_TRUE(field_it == fields.end());
83 ASSERT_TRUE(method_it == methods.end());
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070084 ASSERT_TRUE(instance_field_it == instance_fields.end());
Mathieu Chartier0d896bd2018-05-25 00:20:27 -070085 }
86 EXPECT_EQ(class_def_idx, dex_file->NumClassDefs());
87 }
88}
89
90} // namespace art