Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | namespace art { |
| 22 | |
| 23 | class ClassAccessorTest : public CommonArtTest {}; |
| 24 | |
| 25 | TEST_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 Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 33 | const dex::ClassDef& class_def = dex_file->GetClassDef(accessor.GetClassDefIndex()); |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 34 | EXPECT_EQ(accessor.GetDescriptor(), dex_file->StringByTypeIdx(class_def.class_idx_)); |
Mathieu Chartier | 18e2687 | 2018-06-04 17:19:02 -0700 | [diff] [blame] | 35 | EXPECT_EQ(class_def_idx, accessor.GetClassDefIndex()); |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 36 | ++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 Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 42 | auto instance_fields = accessor.GetInstanceFields(); |
| 43 | auto instance_field_it = instance_fields.begin(); |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 44 | accessor.VisitFieldsAndMethods( |
| 45 | // Static fields. |
| 46 | [&](const ClassAccessor::Field& field) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 47 | EXPECT_TRUE(field.IsStatic()); |
| 48 | EXPECT_TRUE(field_it->IsStatic()); |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 49 | 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 Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 55 | EXPECT_FALSE(field.IsStatic()); |
| 56 | EXPECT_FALSE(field_it->IsStatic()); |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 57 | EXPECT_EQ(field.GetIndex(), field_it->GetIndex()); |
| 58 | EXPECT_EQ(field.GetAccessFlags(), field_it->GetAccessFlags()); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 59 | EXPECT_EQ(field.GetIndex(), instance_field_it->GetIndex()); |
| 60 | EXPECT_EQ(field.GetAccessFlags(), instance_field_it->GetAccessFlags()); |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 61 | ++field_it; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 62 | ++instance_field_it; |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 63 | }, |
| 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 Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 84 | ASSERT_TRUE(instance_field_it == instance_fields.end()); |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame] | 85 | } |
| 86 | EXPECT_EQ(class_def_idx, dex_file->NumClassDefs()); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | } // namespace art |