blob: afab3141fe14f31739d941bee969756b3e518370 [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#ifndef ART_SRC_MIRROR_CLASS_H_
18#define ART_SRC_MIRROR_CLASS_H_
19
20#include "modifiers.h"
21#include "object.h"
22#include "primitive.h"
23
24/*
25 * A magic value for refOffsets. Ignore the bits and walk the super
26 * chain when this is the value.
27 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
28 * fields followed by 2 ref instance fields.]
29 */
30#define CLASS_WALK_SUPER ((unsigned int)(3))
31#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
32#define CLASS_OFFSET_ALIGNMENT 4
33#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
34/*
35 * Given an offset, return the bit number which would encode that offset.
36 * Local use only.
37 */
38#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
39 ((unsigned int)(byteOffset) / \
40 CLASS_OFFSET_ALIGNMENT)
41/*
42 * Is the given offset too large to be encoded?
43 */
44#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
45 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
46/*
47 * Return a single bit, encoding the offset.
48 * Undefined if the offset is too large, as defined above.
49 */
50#define CLASS_BIT_FROM_OFFSET(byteOffset) \
51 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
52/*
53 * Return an offset, given a bit number as returned from CLZ.
54 */
55#define CLASS_OFFSET_FROM_CLZ(rshift) \
56 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
57
58namespace art {
59
60struct ClassClassOffsets;
61struct ClassOffsets;
62class StringPiece;
63
64namespace mirror {
65
66class ClassLoader;
67class DexCache;
68class Field;
69class IfTable;
70
71// Type for the InitializedStaticStorage table. Currently the Class
72// provides the static storage. However, this might change to an Array
73// to improve image sharing, so we use this type to avoid assumptions
74// on the current storage.
75class MANAGED StaticStorageBase : public Object {
76};
77
78// C++ mirror of java.lang.Class
79class MANAGED Class : public StaticStorageBase {
80 public:
81 // Class Status
82 //
83 // kStatusNotReady: If a Class cannot be found in the class table by
84 // FindClass, it allocates an new one with AllocClass in the
85 // kStatusNotReady and calls LoadClass. Note if it does find a
86 // class, it may not be kStatusResolved and it will try to push it
87 // forward toward kStatusResolved.
88 //
89 // kStatusIdx: LoadClass populates with Class with information from
90 // the DexFile, moving the status to kStatusIdx, indicating that the
91 // Class value in super_class_ has not been populated. The new Class
92 // can then be inserted into the classes table.
93 //
94 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
95 // attempt to move a kStatusIdx class forward to kStatusLoaded by
96 // using ResolveClass to initialize the super_class_ and ensuring the
97 // interfaces are resolved.
98 //
99 // kStatusResolved: Still holding the lock on Class, the ClassLinker
100 // shows linking is complete and fields of the Class populated by making
101 // it kStatusResolved. Java allows circularities of the form where a super
102 // class has a field that is of the type of the sub class. We need to be able
103 // to fully resolve super classes while resolving types for fields.
104 //
105 // kStatusRetryVerificationAtRuntime: The verifier sets a class to
106 // this state if it encounters a soft failure at compile time. This
107 // often happens when there are unresolved classes in other dex
108 // files, and this status marks a class as needing to be verified
109 // again at runtime.
110 //
111 // TODO: Explain the other states
112 enum Status {
113 kStatusError = -1,
114 kStatusNotReady = 0,
115 kStatusIdx = 1, // Loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_.
116 kStatusLoaded = 2, // DEX idx values resolved.
117 kStatusResolved = 3, // Part of linking.
118 kStatusVerifying = 4, // In the process of being verified.
119 kStatusRetryVerificationAtRuntime = 5, // Compile time verification failed, retry at runtime.
120 kStatusVerifyingAtRuntime = 6, // Retrying verification at runtime.
121 kStatusVerified = 7, // Logically part of linking; done pre-init.
122 kStatusInitializing = 8, // Class init in progress.
123 kStatusInitialized = 9, // Ready to go.
124 };
125
126 Status GetStatus() const {
127 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
128 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
129 }
130
131 void SetStatus(Status new_status) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
132
133 // Returns true if the class has failed to link.
134 bool IsErroneous() const {
135 return GetStatus() == kStatusError;
136 }
137
138 // Returns true if the class has been loaded.
139 bool IsIdxLoaded() const {
140 return GetStatus() >= kStatusIdx;
141 }
142
143 // Returns true if the class has been loaded.
144 bool IsLoaded() const {
145 return GetStatus() >= kStatusLoaded;
146 }
147
148 // Returns true if the class has been linked.
149 bool IsResolved() const {
150 return GetStatus() >= kStatusResolved;
151 }
152
153 // Returns true if the class was compile-time verified.
154 bool IsCompileTimeVerified() const {
155 return GetStatus() >= kStatusRetryVerificationAtRuntime;
156 }
157
158 // Returns true if the class has been verified.
159 bool IsVerified() const {
160 return GetStatus() >= kStatusVerified;
161 }
162
163 // Returns true if the class is initializing.
164 bool IsInitializing() const {
165 return GetStatus() >= kStatusInitializing;
166 }
167
168 // Returns true if the class is initialized.
169 bool IsInitialized() const {
170 return GetStatus() == kStatusInitialized;
171 }
172
173 uint32_t GetAccessFlags() const;
174
175 void SetAccessFlags(uint32_t new_access_flags) {
176 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
177 }
178
179 // Returns true if the class is an interface.
180 bool IsInterface() const {
181 return (GetAccessFlags() & kAccInterface) != 0;
182 }
183
184 // Returns true if the class is declared public.
185 bool IsPublic() const {
186 return (GetAccessFlags() & kAccPublic) != 0;
187 }
188
189 // Returns true if the class is declared final.
190 bool IsFinal() const {
191 return (GetAccessFlags() & kAccFinal) != 0;
192 }
193
194 bool IsFinalizable() const {
195 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
196 }
197
198 void SetFinalizable() {
199 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
200 SetAccessFlags(flags | kAccClassIsFinalizable);
201 }
202
203 // Returns true if the class is abstract.
204 bool IsAbstract() const {
205 return (GetAccessFlags() & kAccAbstract) != 0;
206 }
207
208 // Returns true if the class is an annotation.
209 bool IsAnnotation() const {
210 return (GetAccessFlags() & kAccAnnotation) != 0;
211 }
212
213 // Returns true if the class is synthetic.
214 bool IsSynthetic() const {
215 return (GetAccessFlags() & kAccSynthetic) != 0;
216 }
217
218 bool IsReferenceClass() const {
219 return (GetAccessFlags() & kAccClassIsReference) != 0;
220 }
221
222 bool IsWeakReferenceClass() const {
223 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
224 }
225
226 bool IsSoftReferenceClass() const {
227 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
228 }
229
230 bool IsFinalizerReferenceClass() const {
231 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
232 }
233
234 bool IsPhantomReferenceClass() const {
235 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
236 }
237
238
239 String* GetName() const; // Returns the cached name.
240 void SetName(String* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); // Sets the cached name.
241 // Computes the name, then sets the cached value.
242 String* ComputeName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
243
244 bool IsProxyClass() const {
245 // Read access flags without using getter as whether something is a proxy can be check in
246 // any loaded state
247 // TODO: switch to a check if the super class is java.lang.reflect.Proxy?
248 uint32_t access_flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
249 return (access_flags & kAccClassIsProxy) != 0;
250 }
251
252 Primitive::Type GetPrimitiveType() const {
253 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
254 return static_cast<Primitive::Type>(
255 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
256 }
257
258 void SetPrimitiveType(Primitive::Type new_type) {
259 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
260 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
261 }
262
263 // Returns true if the class is a primitive type.
264 bool IsPrimitive() const {
265 return GetPrimitiveType() != Primitive::kPrimNot;
266 }
267
268 bool IsPrimitiveBoolean() const {
269 return GetPrimitiveType() == Primitive::kPrimBoolean;
270 }
271
272 bool IsPrimitiveByte() const {
273 return GetPrimitiveType() == Primitive::kPrimByte;
274 }
275
276 bool IsPrimitiveChar() const {
277 return GetPrimitiveType() == Primitive::kPrimChar;
278 }
279
280 bool IsPrimitiveShort() const {
281 return GetPrimitiveType() == Primitive::kPrimShort;
282 }
283
284 bool IsPrimitiveInt() const {
285 return GetPrimitiveType() == Primitive::kPrimInt;
286 }
287
288 bool IsPrimitiveLong() const {
289 return GetPrimitiveType() == Primitive::kPrimLong;
290 }
291
292 bool IsPrimitiveFloat() const {
293 return GetPrimitiveType() == Primitive::kPrimFloat;
294 }
295
296 bool IsPrimitiveDouble() const {
297 return GetPrimitiveType() == Primitive::kPrimDouble;
298 }
299
300 bool IsPrimitiveVoid() const {
301 return GetPrimitiveType() == Primitive::kPrimVoid;
302 }
303
304 bool IsPrimitiveArray() const {
305 return IsArrayClass() && GetComponentType()->IsPrimitive();
306 }
307
308 // Depth of class from java.lang.Object
309 size_t Depth() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
310 size_t depth = 0;
311 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
312 depth++;
313 }
314 return depth;
315 }
316
317 bool IsArrayClass() const {
318 return GetComponentType() != NULL;
319 }
320
321 bool IsClassClass() const;
322
323 bool IsStringClass() const;
324
325 bool IsThrowableClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
326
327 bool IsFieldClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
328
329 bool IsMethodClass() const;
330
331 Class* GetComponentType() const {
332 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
333 }
334
335 void SetComponentType(Class* new_component_type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
336 DCHECK(GetComponentType() == NULL);
337 DCHECK(new_component_type != NULL);
338 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
339 }
340
341 size_t GetComponentSize() const {
342 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
343 }
344
345 bool IsObjectClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
346 return !IsPrimitive() && GetSuperClass() == NULL;
347 }
348 bool IsInstantiable() const {
349 return !IsPrimitive() && !IsInterface() && !IsAbstract();
350 }
351
352 bool IsObjectArrayClass() const {
353 return GetComponentType() != NULL && !GetComponentType()->IsPrimitive();
354 }
355
356 // Creates a raw object instance but does not invoke the default constructor.
357 Object* AllocObject(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
358
359 bool IsVariableSize() const {
360 // Classes and arrays vary in size, and so the object_size_ field cannot
361 // be used to get their instance size
362 return IsClassClass() || IsArrayClass();
363 }
364
365 size_t SizeOf() const {
366 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
367 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
368 }
369
370 size_t GetClassSize() const {
371 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
372 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
373 }
374
375 void SetClassSize(size_t new_class_size)
376 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
377
378 size_t GetObjectSize() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
379
380 void SetObjectSize(size_t new_object_size) {
381 DCHECK(!IsVariableSize());
382 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
383 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
384 }
385
386 // Returns true if this class is in the same packages as that class.
387 bool IsInSamePackage(const Class* that) const
388 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
389
390 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
391
392 // Returns true if this class can access that class.
393 bool CanAccess(Class* that) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
394 return that->IsPublic() || this->IsInSamePackage(that);
395 }
396
397 // Can this class access a member in the provided class with the provided member access flags?
398 // Note that access to the class isn't checked in case the declaring class is protected and the
399 // method has been exposed by a public sub-class
400 bool CanAccessMember(Class* access_to, uint32_t member_flags) const
401 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
402 // Classes can access all of their own members
403 if (this == access_to) {
404 return true;
405 }
406 // Public members are trivially accessible
407 if (member_flags & kAccPublic) {
408 return true;
409 }
410 // Private members are trivially not accessible
411 if (member_flags & kAccPrivate) {
412 return false;
413 }
414 // Check for protected access from a sub-class, which may or may not be in the same package.
415 if (member_flags & kAccProtected) {
416 if (this->IsSubClass(access_to)) {
417 return true;
418 }
419 }
420 // Allow protected access from other classes in the same package.
421 return this->IsInSamePackage(access_to);
422 }
423
424 bool IsSubClass(const Class* klass) const
425 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
426
427 // Can src be assigned to this class? For example, String can be assigned to Object (by an
428 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
429 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
430 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
431 // to themselves. Classes for primitive types may not assign to each other.
432 bool IsAssignableFrom(const Class* src) const
433 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
434 DCHECK(src != NULL);
435 if (this == src) {
436 // Can always assign to things of the same type.
437 return true;
438 } else if (IsObjectClass()) {
439 // Can assign any reference to java.lang.Object.
440 return !src->IsPrimitive();
441 } else if (IsInterface()) {
442 return src->Implements(this);
443 } else if (src->IsArrayClass()) {
444 return IsAssignableFromArray(src);
445 } else {
446 return !src->IsInterface() && src->IsSubClass(this);
447 }
448 }
449
450 Class* GetSuperClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
451
452 void SetSuperClass(Class *new_super_class) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
453 // super class is assigned once, except during class linker initialization
454 Class* old_super_class = GetFieldObject<Class*>(
455 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
456 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
457 DCHECK(new_super_class != NULL);
458 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
459 }
460
461 bool HasSuperClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
462 return GetSuperClass() != NULL;
463 }
464
465 static MemberOffset SuperClassOffset() {
466 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
467 }
468
469 ClassLoader* GetClassLoader() const;
470
471 void SetClassLoader(ClassLoader* new_cl) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
472
473 static MemberOffset DexCacheOffset() {
474 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
475 }
476
477 enum {
478 kDumpClassFullDetail = 1,
479 kDumpClassClassLoader = (1 << 1),
480 kDumpClassInitialized = (1 << 2),
481 };
482
483 void DumpClass(std::ostream& os, int flags) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
484
485 DexCache* GetDexCache() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
486
487 void SetDexCache(DexCache* new_dex_cache) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
488
489 ObjectArray<AbstractMethod>* GetDirectMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
490
491 void SetDirectMethods(ObjectArray<AbstractMethod>* new_direct_methods)
492 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
493
494 AbstractMethod* GetDirectMethod(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
495
496 void SetDirectMethod(uint32_t i, AbstractMethod* f) // TODO: uint16_t
497 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
498
499 // Returns the number of static, private, and constructor methods.
500 size_t NumDirectMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
501
502 ObjectArray<AbstractMethod>* GetVirtualMethods() const
503 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
504
505 void SetVirtualMethods(ObjectArray<AbstractMethod>* new_virtual_methods)
506 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
507
508 // Returns the number of non-inherited virtual methods.
509 size_t NumVirtualMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
510
511 AbstractMethod* GetVirtualMethod(uint32_t i) const
512 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
513
514 AbstractMethod* GetVirtualMethodDuringLinking(uint32_t i) const
515 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
516
517 void SetVirtualMethod(uint32_t i, AbstractMethod* f) // TODO: uint16_t
518 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
519
520 ObjectArray<AbstractMethod>* GetVTable() const;
521
522 ObjectArray<AbstractMethod>* GetVTableDuringLinking() const;
523
524 void SetVTable(ObjectArray<AbstractMethod>* new_vtable)
525 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
526
527 static MemberOffset VTableOffset() {
528 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
529 }
530
531 // Given a method implemented by this class but potentially from a super class, return the
532 // specific implementation method for this class.
533 AbstractMethod* FindVirtualMethodForVirtual(AbstractMethod* method) const
534 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
535
536 // Given a method implemented by this class' super class, return the specific implementation
537 // method for this class.
538 AbstractMethod* FindVirtualMethodForSuper(AbstractMethod* method) const
539 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
540
541 // Given a method implemented by this class, but potentially from a
542 // super class or interface, return the specific implementation
543 // method for this class.
544 AbstractMethod* FindVirtualMethodForInterface(AbstractMethod* method) const
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800545 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800546
547 AbstractMethod* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const
548 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
549
550 AbstractMethod* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
551 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
552
553 AbstractMethod* FindVirtualMethodForVirtualOrInterface(AbstractMethod* method) const
554 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
555
556 AbstractMethod* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const
557 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
558
559 AbstractMethod* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
560 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
561
562 AbstractMethod* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const
563 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
564
565 AbstractMethod* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
566 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
567
568 AbstractMethod* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const
569 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
570
571 AbstractMethod* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
572 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
573
574 AbstractMethod* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const
575 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
576
577 AbstractMethod* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
578 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
579
580 int32_t GetIfTableCount() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
581
582 IfTable* GetIfTable() const;
583
584 void SetIfTable(IfTable* new_iftable) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
585
586 // Get instance fields of the class (See also GetSFields).
587 ObjectArray<Field>* GetIFields() const;
588
589 void SetIFields(ObjectArray<Field>* new_ifields) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
590
591 size_t NumInstanceFields() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
592
593 Field* GetInstanceField(uint32_t i) const // TODO: uint16_t
594 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
595
596 void SetInstanceField(uint32_t i, Field* f) // TODO: uint16_t
597 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
598
599 // Returns the number of instance fields containing reference types.
600 size_t NumReferenceInstanceFields() const {
601 DCHECK(IsResolved() || IsErroneous());
602 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
603 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
604 }
605
606 size_t NumReferenceInstanceFieldsDuringLinking() const {
607 DCHECK(IsLoaded() || IsErroneous());
608 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
609 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
610 }
611
612 void SetNumReferenceInstanceFields(size_t new_num) {
613 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
614 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
615 }
616
617 uint32_t GetReferenceInstanceOffsets() const {
618 DCHECK(IsResolved() || IsErroneous());
619 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
620 }
621
622 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets)
623 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
624
625 // Beginning of static field data
626 static MemberOffset FieldsOffset() {
627 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
628 }
629
630 // Returns the number of static fields containing reference types.
631 size_t NumReferenceStaticFields() const {
632 DCHECK(IsResolved() || IsErroneous());
633 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
634 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
635 }
636
637 size_t NumReferenceStaticFieldsDuringLinking() const {
638 DCHECK(IsLoaded() || IsErroneous());
639 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
640 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
641 }
642
643 void SetNumReferenceStaticFields(size_t new_num) {
644 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
645 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
646 }
647
648 // Gets the static fields of the class.
649 ObjectArray<Field>* GetSFields() const;
650
651 void SetSFields(ObjectArray<Field>* new_sfields) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
652
653 size_t NumStaticFields() const;
654
655 Field* GetStaticField(uint32_t i) const; // TODO: uint16_t
656
657 void SetStaticField(uint32_t i, Field* f); // TODO: uint16_t
658
659 uint32_t GetReferenceStaticOffsets() const {
660 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
661 }
662
663 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
664
665 // Find a static or instance field using the JLS resolution order
666 Field* FindField(const StringPiece& name, const StringPiece& type)
667 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
668
669 // Finds the given instance field in this class or a superclass.
670 Field* FindInstanceField(const StringPiece& name, const StringPiece& type)
671 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
672
673 // Finds the given instance field in this class or a superclass, only searches classes that
674 // have the same dex cache.
675 Field* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx)
676 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
677
678 Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type)
679 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
680
681 Field* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx)
682 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
683
684 // Finds the given static field in this class or a superclass.
685 Field* FindStaticField(const StringPiece& name, const StringPiece& type)
686 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
687
688 // Finds the given static field in this class or superclass, only searches classes that
689 // have the same dex cache.
690 Field* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx)
691 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
692
693 Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type)
694 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
695
696 Field* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx)
697 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
698
699 pid_t GetClinitThreadId() const {
700 DCHECK(IsIdxLoaded() || IsErroneous());
701 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
702 }
703
704 void SetClinitThreadId(pid_t new_clinit_thread_id) {
705 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
706 }
707
708 Class* GetVerifyErrorClass() const {
709 // DCHECK(IsErroneous());
710 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
711 }
712
713 uint16_t GetDexTypeIndex() const {
714 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false);
715 }
716
717 void SetDexTypeIndex(uint16_t type_idx) {
718 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false);
719 }
720
721 static Class* GetJavaLangClass() {
722 DCHECK(java_lang_Class_ != NULL);
723 return java_lang_Class_;
724 }
725
726 // Can't call this SetClass or else gets called instead of Object::SetClass in places.
727 static void SetClassClass(Class* java_lang_Class);
728 static void ResetClass();
729
730 private:
731 void SetVerifyErrorClass(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
732
733 bool Implements(const Class* klass) const
734 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
735 bool IsArrayAssignableFromArray(const Class* klass) const
736 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
737 bool IsAssignableFromArray(const Class* klass) const
738 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
739
740 // defining class loader, or NULL for the "bootstrap" system loader
741 ClassLoader* class_loader_;
742
743 // For array classes, the component class object for instanceof/checkcast
744 // (for String[][][], this will be String[][]). NULL for non-array classes.
745 Class* component_type_;
746
747 // DexCache of resolved constant pool entries (will be NULL for classes generated by the
748 // runtime such as arrays and primitive classes).
749 DexCache* dex_cache_;
750
751 // static, private, and <init> methods
752 ObjectArray<AbstractMethod>* direct_methods_;
753
754 // instance fields
755 //
756 // These describe the layout of the contents of an Object.
757 // Note that only the fields directly declared by this class are
758 // listed in ifields; fields declared by a superclass are listed in
759 // the superclass's Class.ifields.
760 //
761 // All instance fields that refer to objects are guaranteed to be at
762 // the beginning of the field list. num_reference_instance_fields_
763 // specifies the number of reference fields.
764 ObjectArray<Field>* ifields_;
765
766 // The interface table (iftable_) contains pairs of a interface class and an array of the
767 // interface methods. There is one pair per interface supported by this class. That means one
768 // pair for each interface we support directly, indirectly via superclass, or indirectly via a
769 // superinterface. This will be null if neither we nor our superclass implement any interfaces.
770 //
771 // Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()".
772 // Invoke faceObj.blah(), where "blah" is part of the Face interface. We can't easily use a
773 // single vtable.
774 //
775 // For every interface a concrete class implements, we create an array of the concrete vtable_
776 // methods for the methods in the interface.
777 IfTable* iftable_;
778
779 // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName
780 String* name_;
781
782 // Static fields
783 ObjectArray<Field>* sfields_;
784
785 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type.
786 Class* super_class_;
787
788 // If class verify fails, we must return same error on subsequent tries.
789 Class* verify_error_class_;
790
791 // virtual methods defined in this class; invoked through vtable
792 ObjectArray<AbstractMethod>* virtual_methods_;
793
794 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
795 // copied in, and virtual methods from our class either replace those from the super or are
796 // appended. For abstract classes, methods may be created in the vtable that aren't in
797 // virtual_ methods_ for miranda methods.
798 ObjectArray<AbstractMethod>* vtable_;
799
800 // access flags; low 16 bits are defined by VM spec
801 uint32_t access_flags_;
802
803 // Total size of the Class instance; used when allocating storage on gc heap.
804 // See also object_size_.
805 size_t class_size_;
806
807 // tid used to check for recursive <clinit> invocation
808 pid_t clinit_thread_id_;
809
810 // type index from dex file
811 // TODO: really 16bits
812 uint32_t dex_type_idx_;
813
814 // number of instance fields that are object refs
815 size_t num_reference_instance_fields_;
816
817 // number of static fields that are object refs
818 size_t num_reference_static_fields_;
819
820 // Total object size; used when allocating storage on gc heap.
821 // (For interfaces and abstract classes this will be zero.)
822 // See also class_size_.
823 size_t object_size_;
824
825 // primitive type value, or Primitive::kPrimNot (0); set for generated prim classes
826 Primitive::Type primitive_type_;
827
828 // Bitmap of offsets of ifields.
829 uint32_t reference_instance_offsets_;
830
831 // Bitmap of offsets of sfields.
832 uint32_t reference_static_offsets_;
833
834 // state of class initialization
835 Status status_;
836
837 // TODO: ?
838 // initiating class loader list
839 // NOTE: for classes with low serialNumber, these are unused, and the
840 // values are kept in a table in gDvm.
841 // InitiatingLoaderList initiating_loader_list_;
842
843 // Location of first static field.
844 uint32_t fields_[0];
845
846 // java.lang.Class
847 static Class* java_lang_Class_;
848
849 friend struct art::ClassOffsets; // for verifying offset information
850 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
851};
852
853std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
854
855class MANAGED ClassClass : public Class {
856 private:
857 int32_t padding_;
858 int64_t serialVersionUID_;
859 friend struct art::ClassClassOffsets; // for verifying offset information
860 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
861};
862
863} // namespace mirror
864} // namespace art
865
866#endif // ART_SRC_MIRROR_CLASS_H_