Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "field-inl.h" |
| 18 | |
| 19 | #include "dex_cache-inl.h" |
| 20 | #include "object_array-inl.h" |
| 21 | #include "object-inl.h" |
| 22 | |
| 23 | namespace art { |
| 24 | namespace mirror { |
| 25 | |
| 26 | GcRoot<Class> Field::static_class_; |
| 27 | GcRoot<Class> Field::array_class_; |
| 28 | |
| 29 | void Field::SetClass(Class* klass) { |
| 30 | CHECK(static_class_.IsNull()) << static_class_.Read() << " " << klass; |
| 31 | CHECK(klass != nullptr); |
| 32 | static_class_ = GcRoot<Class>(klass); |
| 33 | } |
| 34 | |
| 35 | void Field::ResetClass() { |
| 36 | CHECK(!static_class_.IsNull()); |
| 37 | static_class_ = GcRoot<Class>(nullptr); |
| 38 | } |
| 39 | |
| 40 | void Field::SetArrayClass(Class* klass) { |
| 41 | CHECK(array_class_.IsNull()) << array_class_.Read() << " " << klass; |
| 42 | CHECK(klass != nullptr); |
| 43 | array_class_ = GcRoot<Class>(klass); |
| 44 | } |
| 45 | |
| 46 | void Field::ResetArrayClass() { |
| 47 | CHECK(!array_class_.IsNull()); |
| 48 | array_class_ = GcRoot<Class>(nullptr); |
| 49 | } |
| 50 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 51 | void Field::VisitRoots(RootVisitor* visitor) { |
| 52 | static_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass)); |
| 53 | array_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | ArtField* Field::GetArtField() { |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame^] | 57 | mirror::Class* declaring_class = GetDeclaringClass(); |
| 58 | if (UNLIKELY(declaring_class->IsProxyClass())) { |
| 59 | DCHECK(IsStatic()); |
| 60 | DCHECK_EQ(declaring_class->NumStaticFields(), 2U); |
| 61 | // 0 == Class[] interfaces; 1 == Class[][] throws; |
| 62 | if (GetDexFieldIndex() == 0) { |
| 63 | return &declaring_class->GetSFields()[0]; |
| 64 | } else { |
| 65 | DCHECK_EQ(GetDexFieldIndex(), 1U); |
| 66 | return &declaring_class->GetSFields()[1]; |
| 67 | } |
| 68 | } |
| 69 | mirror::DexCache* const dex_cache = declaring_class->GetDexCache(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 70 | ArtField* const art_field = dex_cache->GetResolvedField(GetDexFieldIndex(), sizeof(void*)); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 71 | CHECK(art_field != nullptr); |
| 72 | return art_field; |
| 73 | } |
| 74 | |
| 75 | } // namespace mirror |
| 76 | } // namespace art |