blob: 0436435e650ee74c28f5cd8daf425ca27f147fa5 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
2 * Copyright (C) 2011 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_CLASS_LINKER_INL_H_
18#define ART_RUNTIME_CLASS_LINKER_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
20#include "class_linker.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "mirror/art_field.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070022#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/iftable.h"
25#include "mirror/object_array.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070026#include "sirt_ref.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027
28namespace art {
29
30inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -070031 const mirror::ArtMethod* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032 mirror::String* resolved_string = referrer->GetDexCacheStrings()->Get(string_idx);
33 if (UNLIKELY(resolved_string == NULL)) {
34 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -070035 SirtRef<mirror::DexCache> dex_cache(Thread::Current(), declaring_class->GetDexCache());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036 const DexFile& dex_file = *dex_cache->GetDexFile();
37 resolved_string = ResolveString(dex_file, string_idx, dex_cache);
38 }
39 return resolved_string;
40}
41
42inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx,
Brian Carlstromea46f952013-07-30 01:26:50 -070043 const mirror::ArtMethod* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044 mirror::Class* resolved_type = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
45 if (UNLIKELY(resolved_type == NULL)) {
46 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -070047 Thread* self = Thread::Current();
48 SirtRef<mirror::DexCache> dex_cache(self, declaring_class->GetDexCache());
49 SirtRef<mirror::ClassLoader> class_loader(self, declaring_class->GetClassLoader());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 const DexFile& dex_file = *dex_cache->GetDexFile();
51 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
52 }
53 return resolved_type;
54}
55
Brian Carlstromea46f952013-07-30 01:26:50 -070056inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, const mirror::ArtField* referrer) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -070058 mirror::DexCache* dex_cache_ptr = declaring_class->GetDexCache();
59 mirror::Class* resolved_type = dex_cache_ptr->GetResolvedType(type_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 if (UNLIKELY(resolved_type == NULL)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070061 Thread* self = Thread::Current();
62 SirtRef<mirror::DexCache> dex_cache(self, dex_cache_ptr);
63 SirtRef<mirror::ClassLoader> class_loader(self, declaring_class->GetClassLoader());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 const DexFile& dex_file = *dex_cache->GetDexFile();
65 resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader);
66 }
67 return resolved_type;
68}
69
Brian Carlstromea46f952013-07-30 01:26:50 -070070inline mirror::ArtMethod* ClassLinker::ResolveMethod(uint32_t method_idx,
71 const mirror::ArtMethod* referrer,
72 InvokeType type) {
73 mirror::ArtMethod* resolved_method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 referrer->GetDexCacheResolvedMethods()->Get(method_idx);
75 if (UNLIKELY(resolved_method == NULL || resolved_method->IsRuntimeMethod())) {
76 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -070077 Thread* self = Thread::Current();
78 SirtRef<mirror::DexCache> dex_cache(self, declaring_class->GetDexCache());
79 SirtRef<mirror::ClassLoader> class_loader(self, declaring_class->GetClassLoader());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 const DexFile& dex_file = *dex_cache->GetDexFile();
81 resolved_method = ResolveMethod(dex_file, method_idx, dex_cache, class_loader, referrer, type);
82 }
83 return resolved_method;
84}
85
Brian Carlstromea46f952013-07-30 01:26:50 -070086inline mirror::ArtField* ClassLinker::ResolveField(uint32_t field_idx,
87 const mirror::ArtMethod* referrer,
88 bool is_static) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070089 mirror::Class* declaring_class = referrer->GetDeclaringClass();
Brian Carlstromea46f952013-07-30 01:26:50 -070090 mirror::ArtField* resolved_field =
Mathieu Chartier590fee92013-09-13 13:46:47 -070091 declaring_class->GetDexCache()->GetResolvedField(field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 if (UNLIKELY(resolved_field == NULL)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070093 Thread* self = Thread::Current();
94 SirtRef<mirror::DexCache> dex_cache(self, declaring_class->GetDexCache());
95 SirtRef<mirror::ClassLoader> class_loader(self, declaring_class->GetClassLoader());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 const DexFile& dex_file = *dex_cache->GetDexFile();
97 resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static);
98 }
99 return resolved_field;
100}
101
102template <class T>
103inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) {
104 return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length);
105}
106
107inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self,
108 size_t length) {
109 return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length);
110}
111
112inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self,
113 size_t length) {
114 return mirror::ObjectArray<mirror::String>::Alloc(self, GetClassRoot(kJavaLangStringArrayClass),
115 length);
116}
117
Brian Carlstromea46f952013-07-30 01:26:50 -0700118inline mirror::ObjectArray<mirror::ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self,
119 size_t length) {
120 return mirror::ObjectArray<mirror::ArtMethod>::Alloc(self,
121 GetClassRoot(kJavaLangReflectArtMethodArrayClass), length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122}
123
124inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) {
125 return down_cast<mirror::IfTable*>(
126 mirror::IfTable::Alloc(self, GetClassRoot(kObjectArrayClass), ifcount * mirror::IfTable::kMax));
127}
128
Brian Carlstromea46f952013-07-30 01:26:50 -0700129inline mirror::ObjectArray<mirror::ArtField>* ClassLinker::AllocArtFieldArray(Thread* self,
130 size_t length) {
131 return mirror::ObjectArray<mirror::ArtField>::Alloc(self,
132 GetClassRoot(kJavaLangReflectArtFieldArrayClass),
133 length);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134}
135
136inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root)
137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
138 DCHECK(class_roots_ != NULL);
139 mirror::Class* klass = class_roots_->Get(class_root);
140 DCHECK(klass != NULL);
141 return klass;
142}
143
144} // namespace art
145
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700146#endif // ART_RUNTIME_CLASS_LINKER_INL_H_