summaryrefslogtreecommitdiffstats
path: root/runtime/class_linker-inl.h
blob: 0436435e650ee74c28f5cd8daf425ca27f147fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ART_RUNTIME_CLASS_LINKER_INL_H_
#define ART_RUNTIME_CLASS_LINKER_INL_H_

#include "class_linker.h"
#include "mirror/art_field.h"
#include "mirror/class_loader.h"
#include "mirror/dex_cache.h"
#include "mirror/iftable.h"
#include "mirror/object_array.h"
#include "sirt_ref.h"

namespace art {

inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx,
                                                  const mirror::ArtMethod* referrer) {
  mirror::String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
  if (UNLIKELY(resolved_string == NULL)) {
    mirror::Class* declaring_class = referrer->GetDeclaringClass();
    SirtRef<mirror::DexCache> dex_cache(Thread::Current(), declaring_class->GetDexCache());
    const DexFile& dex_file = *dex_cache->GetDexFile();
    resolved_string = ResolveString(dex_file, string_idx, dex_cache);
  }
  return resolved_string;
}

inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx,
                                               const mirror::ArtMethod* referrer) {
  mirror::Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
  if (UNLIKELY(resolved_type == NULL)) {
    mirror::Class* declaring_class = referrer->GetDeclaringClass();
    Thread* self = Thread::Current();
    SirtRef<mirror::DexCache> dex_cache(self, declaring_class->GetDexCache());
    SirtRef<mirror::ClassLoader> class_loader(self, declaring_class->GetClassLoader());
    const DexFile& dex_file = *dex_cache->GetDexFile();
    resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
  }
  return resolved_type;
}

inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, const mirror::ArtField* referrer) {
  mirror::Class* declaring_class = referrer->GetDeclaringClass();
  mirror::DexCache* dex_cache_ptr = declaring_class->GetDexCache();
  mirror::Class* resolved_type = dex_cache_ptr->GetResolvedType(type_idx);
  if (UNLIKELY(resolved_type == NULL)) {
    Thread* self = Thread::Current();
    SirtRef<mirror::DexCache> dex_cache(self, dex_cache_ptr);
    SirtRef<mirror::ClassLoader> class_loader(self, declaring_class->GetClassLoader());
    const DexFile& dex_file = *dex_cache->GetDexFile();
    resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
  }
  return resolved_type;
}

inline mirror::ArtMethod* ClassLinker::ResolveMethod(uint32_t method_idx,
                                                     const mirror::ArtMethod* referrer,
                                                     InvokeType type) {
  mirror::ArtMethod* resolved_method =
      referrer->GetDexCacheResolvedMethods()->Get(method_idx);
  if (UNLIKELY(resolved_method == NULL || resolved_method->IsRuntimeMethod())) {
    mirror::Class* declaring_class = referrer->GetDeclaringClass();
    Thread* self = Thread::Current();
    SirtRef<mirror::DexCache> dex_cache(self, declaring_class->GetDexCache());
    SirtRef<mirror::ClassLoader> class_loader(self, declaring_class->GetClassLoader());
    const DexFile& dex_file = *dex_cache->GetDexFile();
    resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, referrer, type);
  }
  return resolved_method;
}

inline mirror::ArtField* ClassLinker::ResolveField(uint32_t field_idx,
                                                   const mirror::ArtMethod* referrer,
                                                   bool is_static) {
  mirror::Class* declaring_class = referrer->GetDeclaringClass();
  mirror::ArtField* resolved_field =
      declaring_class->GetDexCache()->GetResolvedField(field_idx);
  if (UNLIKELY(resolved_field == NULL)) {
    Thread* self = Thread::Current();
    SirtRef<mirror::DexCache>  dex_cache(self, declaring_class->GetDexCache());
    SirtRef<mirror::ClassLoader> class_loader(self, declaring_class->GetClassLoader());
    const DexFile& dex_file = *dex_cache->GetDexFile();
    resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
  }
  return resolved_field;
}

template <class T>
inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) {
  return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
}

inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self,
                                                                        size_t length) {
  return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
}

inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self,
                                                                          size_t length) {
  return mirror::ObjectArray<mirror::String>::Alloc(self, GetClassRoot(kJavaLangStringArrayClass),
                                                    length);
}

inline mirror::ObjectArray<mirror::ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self,
                                                                                size_t length) {
  return mirror::ObjectArray<mirror::ArtMethod>::Alloc(self,
      GetClassRoot(kJavaLangReflectArtMethodArrayClass), length);
}

inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
  return down_cast<mirror::IfTable*>(
      mirror::IfTable::Alloc(self, GetClassRoot(kObjectArrayClass), ifcount * mirror::IfTable::kMax));
}

inline mirror::ObjectArray<mirror::ArtField>* ClassLinker::AllocArtFieldArray(Thread* self,
                                                                              size_t length) {
  return mirror::ObjectArray<mirror::ArtField>::Alloc(self,
                                                      GetClassRoot(kJavaLangReflectArtFieldArrayClass),
                                                      length);
}

inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root)
    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
  DCHECK(class_roots_ != NULL);
  mirror::Class* klass = class_roots_->Get(class_root);
  DCHECK(klass != NULL);
  return klass;
}

}  // namespace art

#endif  // ART_RUNTIME_CLASS_LINKER_INL_H_