blob: 8fac609745ac73e7ad6ec475018f77033ccf1e1c [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
17#include "class.h"
18
19#include "abstract_method-inl.h"
20#include "class-inl.h"
21#include "class_linker.h"
22#include "class_loader.h"
23#include "dex_cache.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "field-inl.h"
26#include "gc/card_table-inl.h"
27#include "object-inl.h"
28#include "object_array-inl.h"
29#include "object_utils.h"
30#include "runtime.h"
31#include "sirt_ref.h"
32#include "thread.h"
33#include "throwable.h"
34#include "utils.h"
35#include "well_known_classes.h"
36
37namespace art {
38namespace mirror {
39
40Class* Class::java_lang_Class_ = NULL;
41
42void Class::SetClassClass(Class* java_lang_Class) {
43 CHECK(java_lang_Class_ == NULL) << java_lang_Class_ << " " << java_lang_Class;
44 CHECK(java_lang_Class != NULL);
45 java_lang_Class_ = java_lang_Class;
46}
47
48void Class::ResetClass() {
49 CHECK(java_lang_Class_ != NULL);
50 java_lang_Class_ = NULL;
51}
52
53void Class::SetStatus(Status new_status) {
54 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
55 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
56 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
57 if (new_status > kStatusResolved) {
58 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
59 }
60 if (new_status == kStatusError) {
61 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
62
63 // stash current exception
64 Thread* self = Thread::Current();
65 SirtRef<Throwable> exception(self, self->GetException());
66 CHECK(exception.get() != NULL);
67
68 // clear exception to call FindSystemClass
69 self->ClearException();
70 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
71 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
72 CHECK(!self->IsExceptionPending());
73
74 // only verification errors, not initialization problems, should set a verify error.
75 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
76 Class* exception_class = exception->GetClass();
77 if (!eiie_class->IsAssignableFrom(exception_class)) {
78 SetVerifyErrorClass(exception_class);
79 }
80
81 // restore exception
82 self->SetException(exception.get());
83 }
84 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
85}
86
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087void Class::SetDexCache(DexCache* new_dex_cache) {
88 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
89}
90
91Object* Class::AllocObject(Thread* self) {
92 DCHECK(!IsArrayClass()) << PrettyClass(this);
93 DCHECK(IsInstantiable()) << PrettyClass(this);
94 // TODO: decide whether we want this check. It currently fails during bootstrap.
95 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
96 DCHECK_GE(this->object_size_, sizeof(Object));
97 return Runtime::Current()->GetHeap()->AllocObject(self, this, this->object_size_);
98}
99
100void Class::SetClassSize(size_t new_class_size) {
101 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
102 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
103}
104
105// Return the class' name. The exact format is bizarre, but it's the specified behavior for
106// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
107// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
108// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
109String* Class::ComputeName() {
110 String* name = GetName();
111 if (name != NULL) {
112 return name;
113 }
114 std::string descriptor(ClassHelper(this).GetDescriptor());
115 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
116 // The descriptor indicates that this is the class for
117 // a primitive type; special-case the return value.
118 const char* c_name = NULL;
119 switch (descriptor[0]) {
120 case 'Z': c_name = "boolean"; break;
121 case 'B': c_name = "byte"; break;
122 case 'C': c_name = "char"; break;
123 case 'S': c_name = "short"; break;
124 case 'I': c_name = "int"; break;
125 case 'J': c_name = "long"; break;
126 case 'F': c_name = "float"; break;
127 case 'D': c_name = "double"; break;
128 case 'V': c_name = "void"; break;
129 default:
130 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
131 }
132 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
133 } else {
134 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
135 // components.
136 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
137 descriptor.erase(0, 1);
138 descriptor.erase(descriptor.size() - 1);
139 }
140 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
141 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
142 }
143 SetName(name);
144 return name;
145}
146
147void Class::DumpClass(std::ostream& os, int flags) const {
148 if ((flags & kDumpClassFullDetail) == 0) {
149 os << PrettyClass(this);
150 if ((flags & kDumpClassClassLoader) != 0) {
151 os << ' ' << GetClassLoader();
152 }
153 if ((flags & kDumpClassInitialized) != 0) {
154 os << ' ' << GetStatus();
155 }
156 os << "\n";
157 return;
158 }
159
160 Class* super = GetSuperClass();
161 ClassHelper kh(this);
162 os << "----- " << (IsInterface() ? "interface" : "class") << " "
163 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
164 os << " objectSize=" << SizeOf() << " "
165 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
166 os << StringPrintf(" access=0x%04x.%04x\n",
167 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
168 if (super != NULL) {
169 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
170 }
171 if (IsArrayClass()) {
172 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
173 }
174 if (kh.NumDirectInterfaces() > 0) {
175 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
176 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
177 Class* interface = kh.GetDirectInterface(i);
178 const ClassLoader* cl = interface->GetClassLoader();
179 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
180 }
181 }
182 os << " vtable (" << NumVirtualMethods() << " entries, "
183 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
184 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
185 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
186 }
187 os << " direct methods (" << NumDirectMethods() << " entries):\n";
188 for (size_t i = 0; i < NumDirectMethods(); ++i) {
189 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
190 }
191 if (NumStaticFields() > 0) {
192 os << " static fields (" << NumStaticFields() << " entries):\n";
193 if (IsResolved() || IsErroneous()) {
194 for (size_t i = 0; i < NumStaticFields(); ++i) {
195 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
196 }
197 } else {
198 os << " <not yet available>";
199 }
200 }
201 if (NumInstanceFields() > 0) {
202 os << " instance fields (" << NumInstanceFields() << " entries):\n";
203 if (IsResolved() || IsErroneous()) {
204 for (size_t i = 0; i < NumInstanceFields(); ++i) {
205 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
206 }
207 } else {
208 os << " <not yet available>";
209 }
210 }
211}
212
213void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
214 if (new_reference_offsets != CLASS_WALK_SUPER) {
215 // Sanity check that the number of bits set in the reference offset bitmap
216 // agrees with the number of references
217 size_t count = 0;
218 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
219 count += c->NumReferenceInstanceFieldsDuringLinking();
220 }
221 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
222 }
223 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
224 new_reference_offsets, false);
225}
226
227void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
228 if (new_reference_offsets != CLASS_WALK_SUPER) {
229 // Sanity check that the number of bits set in the reference offset bitmap
230 // agrees with the number of references
231 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
232 NumReferenceStaticFieldsDuringLinking());
233 }
234 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
235 new_reference_offsets, false);
236}
237
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
239 size_t i = 0;
240 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
241 ++i;
242 }
243 if (descriptor1.find('/', i) != StringPiece::npos ||
244 descriptor2.find('/', i) != StringPiece::npos) {
245 return false;
246 } else {
247 return true;
248 }
249}
250
251bool Class::IsInSamePackage(const Class* that) const {
252 const Class* klass1 = this;
253 const Class* klass2 = that;
254 if (klass1 == klass2) {
255 return true;
256 }
257 // Class loaders must match.
258 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
259 return false;
260 }
261 // Arrays are in the same package when their element classes are.
262 while (klass1->IsArrayClass()) {
263 klass1 = klass1->GetComponentType();
264 }
265 while (klass2->IsArrayClass()) {
266 klass2 = klass2->GetComponentType();
267 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700268 // trivial check again for array types
269 if (klass1 == klass2) {
270 return true;
271 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272 // Compare the package part of the descriptor string.
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700273 if (LIKELY(!klass1->IsProxyClass() && !klass2->IsProxyClass())) {
274 ClassHelper kh(klass1);
275 const DexFile* dex_file = &kh.GetDexFile();
276 const DexFile::TypeId* type_id = &dex_file->GetTypeId(klass1->GetDexTypeIndex());
277 const char* descriptor1 = dex_file->GetTypeDescriptor(*type_id);
278 kh.ChangeClass(klass2);
279 dex_file = &kh.GetDexFile();
280 type_id = &dex_file->GetTypeId(klass1->GetDexTypeIndex());
281 const char* descriptor2 = dex_file->GetTypeDescriptor(*type_id);
282 return IsInSamePackage(descriptor1, descriptor2);
283 } else {
284 ClassHelper kh(klass1);
285 std::string descriptor1(kh.GetDescriptor());
286 kh.ChangeClass(klass2);
287 std::string descriptor2(kh.GetDescriptor());
288 return IsInSamePackage(descriptor1, descriptor2);
289 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290}
291
292bool Class::IsClassClass() const {
293 Class* java_lang_Class = GetClass()->GetClass();
294 return this == java_lang_Class;
295}
296
297bool Class::IsStringClass() const {
298 return this == String::GetJavaLangString();
299}
300
301bool Class::IsThrowableClass() const {
302 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
303}
304
305bool Class::IsFieldClass() const {
306 Class* java_lang_Class = GetClass();
307 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->GetClass();
308 return this == java_lang_reflect_Field;
309
310}
311
312bool Class::IsMethodClass() const {
313 return (this == AbstractMethod::GetMethodClass()) ||
314 (this == AbstractMethod::GetConstructorClass());
315
316}
317
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318void Class::SetClassLoader(ClassLoader* new_class_loader) {
319 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
320}
321
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
323 // Check the current class before checking the interfaces.
324 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
325 if (method != NULL) {
326 return method;
327 }
328
329 int32_t iftable_count = GetIfTableCount();
330 IfTable* iftable = GetIfTable();
331 for (int32_t i = 0; i < iftable_count; i++) {
332 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
333 if (method != NULL) {
334 return method;
335 }
336 }
337 return NULL;
338}
339
340AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
341 // Check the current class before checking the interfaces.
342 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
343 if (method != NULL) {
344 return method;
345 }
346
347 int32_t iftable_count = GetIfTableCount();
348 IfTable* iftable = GetIfTable();
349 for (int32_t i = 0; i < iftable_count; i++) {
350 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
351 if (method != NULL) {
352 return method;
353 }
354 }
355 return NULL;
356}
357
358
359AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
360 MethodHelper mh;
361 for (size_t i = 0; i < NumDirectMethods(); ++i) {
362 AbstractMethod* method = GetDirectMethod(i);
363 mh.ChangeMethod(method);
364 if (name == mh.GetName() && signature == mh.GetSignature()) {
365 return method;
366 }
367 }
368 return NULL;
369}
370
371AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
372 if (GetDexCache() == dex_cache) {
373 for (size_t i = 0; i < NumDirectMethods(); ++i) {
374 AbstractMethod* method = GetDirectMethod(i);
375 if (method->GetDexMethodIndex() == dex_method_idx) {
376 return method;
377 }
378 }
379 }
380 return NULL;
381}
382
383AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
384 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
385 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
386 if (method != NULL) {
387 return method;
388 }
389 }
390 return NULL;
391}
392
393AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
394 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
395 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
396 if (method != NULL) {
397 return method;
398 }
399 }
400 return NULL;
401}
402
403AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
404 const StringPiece& signature) const {
405 MethodHelper mh;
406 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
407 AbstractMethod* method = GetVirtualMethod(i);
408 mh.ChangeMethod(method);
409 if (name == mh.GetName() && signature == mh.GetSignature()) {
410 return method;
411 }
412 }
413 return NULL;
414}
415
416AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
417 if (GetDexCache() == dex_cache) {
418 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
419 AbstractMethod* method = GetVirtualMethod(i);
420 if (method->GetDexMethodIndex() == dex_method_idx) {
421 return method;
422 }
423 }
424 }
425 return NULL;
426}
427
428AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
429 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
430 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
431 if (method != NULL) {
432 return method;
433 }
434 }
435 return NULL;
436}
437
438AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
439 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
440 AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
441 if (method != NULL) {
442 return method;
443 }
444 }
445 return NULL;
446}
447
448Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
449 // Is the field in this class?
450 // Interfaces are not relevant because they can't contain instance fields.
451 FieldHelper fh;
452 for (size_t i = 0; i < NumInstanceFields(); ++i) {
453 Field* f = GetInstanceField(i);
454 fh.ChangeField(f);
455 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
456 return f;
457 }
458 }
459 return NULL;
460}
461
462Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
463 if (GetDexCache() == dex_cache) {
464 for (size_t i = 0; i < NumInstanceFields(); ++i) {
465 Field* f = GetInstanceField(i);
466 if (f->GetDexFieldIndex() == dex_field_idx) {
467 return f;
468 }
469 }
470 }
471 return NULL;
472}
473
474Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
475 // Is the field in this class, or any of its superclasses?
476 // Interfaces are not relevant because they can't contain instance fields.
477 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
478 Field* f = c->FindDeclaredInstanceField(name, type);
479 if (f != NULL) {
480 return f;
481 }
482 }
483 return NULL;
484}
485
486Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
487 // Is the field in this class, or any of its superclasses?
488 // Interfaces are not relevant because they can't contain instance fields.
489 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
490 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
491 if (f != NULL) {
492 return f;
493 }
494 }
495 return NULL;
496}
497
498Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
499 DCHECK(type != NULL);
500 FieldHelper fh;
501 for (size_t i = 0; i < NumStaticFields(); ++i) {
502 Field* f = GetStaticField(i);
503 fh.ChangeField(f);
504 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
505 return f;
506 }
507 }
508 return NULL;
509}
510
511Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
512 if (dex_cache == GetDexCache()) {
513 for (size_t i = 0; i < NumStaticFields(); ++i) {
514 Field* f = GetStaticField(i);
515 if (f->GetDexFieldIndex() == dex_field_idx) {
516 return f;
517 }
518 }
519 }
520 return NULL;
521}
522
523Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
524 // Is the field in this class (or its interfaces), or any of its
525 // superclasses (or their interfaces)?
526 ClassHelper kh;
527 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
528 // Is the field in this class?
529 Field* f = k->FindDeclaredStaticField(name, type);
530 if (f != NULL) {
531 return f;
532 }
533 // Is this field in any of this class' interfaces?
534 kh.ChangeClass(k);
535 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
536 Class* interface = kh.GetDirectInterface(i);
537 f = interface->FindStaticField(name, type);
538 if (f != NULL) {
539 return f;
540 }
541 }
542 }
543 return NULL;
544}
545
546Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
547 ClassHelper kh;
548 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
549 // Is the field in this class?
550 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
551 if (f != NULL) {
552 return f;
553 }
554 // Is this field in any of this class' interfaces?
555 kh.ChangeClass(k);
556 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
557 Class* interface = kh.GetDirectInterface(i);
558 f = interface->FindStaticField(dex_cache, dex_field_idx);
559 if (f != NULL) {
560 return f;
561 }
562 }
563 }
564 return NULL;
565}
566
567Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
568 // Find a field using the JLS field resolution order
569 ClassHelper kh;
570 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
571 // Is the field in this class?
572 Field* f = k->FindDeclaredInstanceField(name, type);
573 if (f != NULL) {
574 return f;
575 }
576 f = k->FindDeclaredStaticField(name, type);
577 if (f != NULL) {
578 return f;
579 }
580 // Is this field in any of this class' interfaces?
581 kh.ChangeClass(k);
582 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
583 Class* interface = kh.GetDirectInterface(i);
584 f = interface->FindStaticField(name, type);
585 if (f != NULL) {
586 return f;
587 }
588 }
589 }
590 return NULL;
591}
592
593} // namespace mirror
594} // namespace art