blob: 4f8ab7d90a01997286a532c972cdc8a98f0a2222 [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_MIRROR_CLASS_H_
18#define ART_RUNTIME_MIRROR_CLASS_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
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 */
Brian Carlstromfb6996f2013-07-18 18:21:14 -070030#define CLASS_WALK_SUPER 3U
31#define CLASS_BITS_PER_WORD (sizeof(uint32_t) * 8)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#define CLASS_OFFSET_ALIGNMENT 4
Brian Carlstromfb6996f2013-07-18 18:21:14 -070033#define CLASS_HIGH_BIT (1U << (CLASS_BITS_PER_WORD - 1))
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034/*
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
Brian Carlstromea46f952013-07-30 01:26:50 -070066class ArtField;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067class ClassLoader;
68class DexCache;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069class 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));
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700128 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), true));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 }
130
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700131 void SetStatus(Status new_status, Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800132
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
Ian Rogers04f94f42013-06-10 15:09:26 -0700238 // Can references of this type be assigned to by things of another type? For non-array types
239 // this is a matter of whether sub-classes may exist - which they can't if the type is final.
240 // For array classes, where all the classes are final due to there being no sub-classes, an
241 // Object[] may be assigned to by a String[] but a String[] may not be assigned to by other
242 // types as the component is final.
243 bool CannotBeAssignedFromOtherTypes() const {
244 if (!IsArrayClass()) {
245 return IsFinal();
246 } else {
247 Class* component = GetComponentType();
248 if (component->IsPrimitive()) {
249 return false;
250 } else {
251 return component->CannotBeAssignedFromOtherTypes();
252 }
253 }
254 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800255
256 String* GetName() const; // Returns the cached name.
257 void SetName(String* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); // Sets the cached name.
258 // Computes the name, then sets the cached value.
259 String* ComputeName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
260
261 bool IsProxyClass() const {
262 // Read access flags without using getter as whether something is a proxy can be check in
263 // any loaded state
264 // TODO: switch to a check if the super class is java.lang.reflect.Proxy?
265 uint32_t access_flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
266 return (access_flags & kAccClassIsProxy) != 0;
267 }
268
269 Primitive::Type GetPrimitiveType() const {
270 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
271 return static_cast<Primitive::Type>(
272 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
273 }
274
275 void SetPrimitiveType(Primitive::Type new_type) {
276 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
277 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
278 }
279
280 // Returns true if the class is a primitive type.
281 bool IsPrimitive() const {
282 return GetPrimitiveType() != Primitive::kPrimNot;
283 }
284
285 bool IsPrimitiveBoolean() const {
286 return GetPrimitiveType() == Primitive::kPrimBoolean;
287 }
288
289 bool IsPrimitiveByte() const {
290 return GetPrimitiveType() == Primitive::kPrimByte;
291 }
292
293 bool IsPrimitiveChar() const {
294 return GetPrimitiveType() == Primitive::kPrimChar;
295 }
296
297 bool IsPrimitiveShort() const {
298 return GetPrimitiveType() == Primitive::kPrimShort;
299 }
300
301 bool IsPrimitiveInt() const {
302 return GetPrimitiveType() == Primitive::kPrimInt;
303 }
304
305 bool IsPrimitiveLong() const {
306 return GetPrimitiveType() == Primitive::kPrimLong;
307 }
308
309 bool IsPrimitiveFloat() const {
310 return GetPrimitiveType() == Primitive::kPrimFloat;
311 }
312
313 bool IsPrimitiveDouble() const {
314 return GetPrimitiveType() == Primitive::kPrimDouble;
315 }
316
317 bool IsPrimitiveVoid() const {
318 return GetPrimitiveType() == Primitive::kPrimVoid;
319 }
320
321 bool IsPrimitiveArray() const {
322 return IsArrayClass() && GetComponentType()->IsPrimitive();
323 }
324
325 // Depth of class from java.lang.Object
326 size_t Depth() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
327 size_t depth = 0;
328 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
329 depth++;
330 }
331 return depth;
332 }
333
334 bool IsArrayClass() const {
335 return GetComponentType() != NULL;
336 }
337
338 bool IsClassClass() const;
339
340 bool IsStringClass() const;
341
342 bool IsThrowableClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
343
Brian Carlstromea46f952013-07-30 01:26:50 -0700344 bool IsArtFieldClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345
Brian Carlstromea46f952013-07-30 01:26:50 -0700346 bool IsArtMethodClass() const;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347
348 Class* GetComponentType() const {
349 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
350 }
351
352 void SetComponentType(Class* new_component_type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
353 DCHECK(GetComponentType() == NULL);
354 DCHECK(new_component_type != NULL);
355 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
356 }
357
358 size_t GetComponentSize() const {
359 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
360 }
361
362 bool IsObjectClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
363 return !IsPrimitive() && GetSuperClass() == NULL;
364 }
365 bool IsInstantiable() const {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700366 return (!IsPrimitive() && !IsInterface() && !IsAbstract()) || ((IsAbstract()) && IsArrayClass());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367 }
368
369 bool IsObjectArrayClass() const {
370 return GetComponentType() != NULL && !GetComponentType()->IsPrimitive();
371 }
372
373 // Creates a raw object instance but does not invoke the default constructor.
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700374 Object* AllocObject(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
375 return AllocObjectInstrumented(self);
376 }
377
378 Object* AllocObjectUninstrumented(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
379 Object* AllocObjectInstrumented(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380
381 bool IsVariableSize() const {
382 // Classes and arrays vary in size, and so the object_size_ field cannot
383 // be used to get their instance size
384 return IsClassClass() || IsArrayClass();
385 }
386
387 size_t SizeOf() const {
388 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
389 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
390 }
391
392 size_t GetClassSize() const {
393 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
394 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
395 }
396
397 void SetClassSize(size_t new_class_size)
398 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
399
400 size_t GetObjectSize() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
401
402 void SetObjectSize(size_t new_object_size) {
403 DCHECK(!IsVariableSize());
404 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
405 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
406 }
407
408 // Returns true if this class is in the same packages as that class.
409 bool IsInSamePackage(const Class* that) const
410 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
411
412 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
413
414 // Returns true if this class can access that class.
415 bool CanAccess(Class* that) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
416 return that->IsPublic() || this->IsInSamePackage(that);
417 }
418
419 // Can this class access a member in the provided class with the provided member access flags?
420 // Note that access to the class isn't checked in case the declaring class is protected and the
421 // method has been exposed by a public sub-class
422 bool CanAccessMember(Class* access_to, uint32_t member_flags) const
423 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
424 // Classes can access all of their own members
425 if (this == access_to) {
426 return true;
427 }
428 // Public members are trivially accessible
429 if (member_flags & kAccPublic) {
430 return true;
431 }
432 // Private members are trivially not accessible
433 if (member_flags & kAccPrivate) {
434 return false;
435 }
436 // Check for protected access from a sub-class, which may or may not be in the same package.
437 if (member_flags & kAccProtected) {
438 if (this->IsSubClass(access_to)) {
439 return true;
440 }
441 }
442 // Allow protected access from other classes in the same package.
443 return this->IsInSamePackage(access_to);
444 }
445
446 bool IsSubClass(const Class* klass) const
447 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
448
449 // Can src be assigned to this class? For example, String can be assigned to Object (by an
450 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
451 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
452 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
453 // to themselves. Classes for primitive types may not assign to each other.
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700454 inline bool IsAssignableFrom(const Class* src) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800455 DCHECK(src != NULL);
456 if (this == src) {
457 // Can always assign to things of the same type.
458 return true;
459 } else if (IsObjectClass()) {
460 // Can assign any reference to java.lang.Object.
461 return !src->IsPrimitive();
462 } else if (IsInterface()) {
463 return src->Implements(this);
464 } else if (src->IsArrayClass()) {
465 return IsAssignableFromArray(src);
466 } else {
467 return !src->IsInterface() && src->IsSubClass(this);
468 }
469 }
470
471 Class* GetSuperClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
472
473 void SetSuperClass(Class *new_super_class) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
474 // super class is assigned once, except during class linker initialization
475 Class* old_super_class = GetFieldObject<Class*>(
476 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
477 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
478 DCHECK(new_super_class != NULL);
479 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
480 }
481
482 bool HasSuperClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
483 return GetSuperClass() != NULL;
484 }
485
486 static MemberOffset SuperClassOffset() {
487 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
488 }
489
490 ClassLoader* GetClassLoader() const;
491
492 void SetClassLoader(ClassLoader* new_cl) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
493
494 static MemberOffset DexCacheOffset() {
495 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
496 }
497
498 enum {
499 kDumpClassFullDetail = 1,
500 kDumpClassClassLoader = (1 << 1),
501 kDumpClassInitialized = (1 << 2),
502 };
503
504 void DumpClass(std::ostream& os, int flags) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
505
506 DexCache* GetDexCache() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
507
508 void SetDexCache(DexCache* new_dex_cache) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
509
Brian Carlstromea46f952013-07-30 01:26:50 -0700510 ObjectArray<ArtMethod>* GetDirectMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800511
Brian Carlstromea46f952013-07-30 01:26:50 -0700512 void SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800513 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
514
Brian Carlstromea46f952013-07-30 01:26:50 -0700515 ArtMethod* GetDirectMethod(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800516
Brian Carlstromea46f952013-07-30 01:26:50 -0700517 void SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800518 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
519
520 // Returns the number of static, private, and constructor methods.
521 size_t NumDirectMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
522
Brian Carlstromea46f952013-07-30 01:26:50 -0700523 ObjectArray<ArtMethod>* GetVirtualMethods() const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800524 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
525
Brian Carlstromea46f952013-07-30 01:26:50 -0700526 void SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800527 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
528
529 // Returns the number of non-inherited virtual methods.
530 size_t NumVirtualMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
531
Brian Carlstromea46f952013-07-30 01:26:50 -0700532 ArtMethod* GetVirtualMethod(uint32_t i) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800533 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
534
Brian Carlstromea46f952013-07-30 01:26:50 -0700535 ArtMethod* GetVirtualMethodDuringLinking(uint32_t i) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800536 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
537
Brian Carlstromea46f952013-07-30 01:26:50 -0700538 void SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800539 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
540
Brian Carlstromea46f952013-07-30 01:26:50 -0700541 ObjectArray<ArtMethod>* GetVTable() const;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800542
Brian Carlstromea46f952013-07-30 01:26:50 -0700543 ObjectArray<ArtMethod>* GetVTableDuringLinking() const;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800544
Brian Carlstromea46f952013-07-30 01:26:50 -0700545 void SetVTable(ObjectArray<ArtMethod>* new_vtable)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800546 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
547
548 static MemberOffset VTableOffset() {
549 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
550 }
551
552 // Given a method implemented by this class but potentially from a super class, return the
553 // specific implementation method for this class.
Brian Carlstromea46f952013-07-30 01:26:50 -0700554 ArtMethod* FindVirtualMethodForVirtual(ArtMethod* method) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
556
557 // Given a method implemented by this class' super class, return the specific implementation
558 // method for this class.
Brian Carlstromea46f952013-07-30 01:26:50 -0700559 ArtMethod* FindVirtualMethodForSuper(ArtMethod* method) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800560 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
561
562 // Given a method implemented by this class, but potentially from a
563 // super class or interface, return the specific implementation
564 // method for this class.
Brian Carlstromea46f952013-07-30 01:26:50 -0700565 ArtMethod* FindVirtualMethodForInterface(ArtMethod* method) const
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800566 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800567
Brian Carlstromea46f952013-07-30 01:26:50 -0700568 ArtMethod* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800569 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
570
Brian Carlstromea46f952013-07-30 01:26:50 -0700571 ArtMethod* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800572 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
573
Brian Carlstromea46f952013-07-30 01:26:50 -0700574 ArtMethod* FindVirtualMethodForVirtualOrInterface(ArtMethod* method) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800575 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
576
Brian Carlstromea46f952013-07-30 01:26:50 -0700577 ArtMethod* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800578 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
579
Brian Carlstromea46f952013-07-30 01:26:50 -0700580 ArtMethod* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800581 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
582
Brian Carlstromea46f952013-07-30 01:26:50 -0700583 ArtMethod* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800584 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
585
Brian Carlstromea46f952013-07-30 01:26:50 -0700586 ArtMethod* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800587 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
588
Brian Carlstromea46f952013-07-30 01:26:50 -0700589 ArtMethod* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800590 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
591
Brian Carlstromea46f952013-07-30 01:26:50 -0700592 ArtMethod* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800593 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
594
Brian Carlstromea46f952013-07-30 01:26:50 -0700595 ArtMethod* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800596 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
597
Brian Carlstromea46f952013-07-30 01:26:50 -0700598 ArtMethod* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800599 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
600
601 int32_t GetIfTableCount() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
602
603 IfTable* GetIfTable() const;
604
605 void SetIfTable(IfTable* new_iftable) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
606
607 // Get instance fields of the class (See also GetSFields).
Brian Carlstromea46f952013-07-30 01:26:50 -0700608 ObjectArray<ArtField>* GetIFields() const;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800609
Brian Carlstromea46f952013-07-30 01:26:50 -0700610 void SetIFields(ObjectArray<ArtField>* new_ifields) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611
612 size_t NumInstanceFields() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
613
Brian Carlstromea46f952013-07-30 01:26:50 -0700614 ArtField* GetInstanceField(uint32_t i) const // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800615 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
616
Brian Carlstromea46f952013-07-30 01:26:50 -0700617 void SetInstanceField(uint32_t i, ArtField* f) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800618 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
619
620 // Returns the number of instance fields containing reference types.
621 size_t NumReferenceInstanceFields() const {
622 DCHECK(IsResolved() || IsErroneous());
623 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
624 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
625 }
626
627 size_t NumReferenceInstanceFieldsDuringLinking() const {
628 DCHECK(IsLoaded() || IsErroneous());
629 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
630 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
631 }
632
633 void SetNumReferenceInstanceFields(size_t new_num) {
634 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
635 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
636 }
637
638 uint32_t GetReferenceInstanceOffsets() const {
639 DCHECK(IsResolved() || IsErroneous());
640 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
641 }
642
643 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets)
644 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
645
646 // Beginning of static field data
647 static MemberOffset FieldsOffset() {
648 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
649 }
650
651 // Returns the number of static fields containing reference types.
652 size_t NumReferenceStaticFields() const {
653 DCHECK(IsResolved() || IsErroneous());
654 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
655 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
656 }
657
658 size_t NumReferenceStaticFieldsDuringLinking() const {
659 DCHECK(IsLoaded() || IsErroneous());
660 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
661 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
662 }
663
664 void SetNumReferenceStaticFields(size_t new_num) {
665 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
666 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
667 }
668
669 // Gets the static fields of the class.
Brian Carlstromea46f952013-07-30 01:26:50 -0700670 ObjectArray<ArtField>* GetSFields() const;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800671
Brian Carlstromea46f952013-07-30 01:26:50 -0700672 void SetSFields(ObjectArray<ArtField>* new_sfields) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800673
674 size_t NumStaticFields() const;
675
Brian Carlstromea46f952013-07-30 01:26:50 -0700676 ArtField* GetStaticField(uint32_t i) const; // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800677
Brian Carlstromea46f952013-07-30 01:26:50 -0700678 void SetStaticField(uint32_t i, ArtField* f); // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800679
680 uint32_t GetReferenceStaticOffsets() const {
681 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
682 }
683
684 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
685
686 // Find a static or instance field using the JLS resolution order
Brian Carlstromea46f952013-07-30 01:26:50 -0700687 ArtField* FindField(const StringPiece& name, const StringPiece& type)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800688 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
689
690 // Finds the given instance field in this class or a superclass.
Brian Carlstromea46f952013-07-30 01:26:50 -0700691 ArtField* FindInstanceField(const StringPiece& name, const StringPiece& type)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800692 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
693
694 // Finds the given instance field in this class or a superclass, only searches classes that
695 // have the same dex cache.
Brian Carlstromea46f952013-07-30 01:26:50 -0700696 ArtField* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800697 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
698
Brian Carlstromea46f952013-07-30 01:26:50 -0700699 ArtField* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800700 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
701
Brian Carlstromea46f952013-07-30 01:26:50 -0700702 ArtField* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800703 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
704
705 // Finds the given static field in this class or a superclass.
Brian Carlstromea46f952013-07-30 01:26:50 -0700706 ArtField* FindStaticField(const StringPiece& name, const StringPiece& type)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800707 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
708
709 // Finds the given static field in this class or superclass, only searches classes that
710 // have the same dex cache.
Brian Carlstromea46f952013-07-30 01:26:50 -0700711 ArtField* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800712 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
713
Brian Carlstromea46f952013-07-30 01:26:50 -0700714 ArtField* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800715 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
716
Brian Carlstromea46f952013-07-30 01:26:50 -0700717 ArtField* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800718 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
719
720 pid_t GetClinitThreadId() const {
721 DCHECK(IsIdxLoaded() || IsErroneous());
722 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
723 }
724
725 void SetClinitThreadId(pid_t new_clinit_thread_id) {
726 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
727 }
728
729 Class* GetVerifyErrorClass() const {
730 // DCHECK(IsErroneous());
731 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
732 }
733
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700734 uint16_t GetDexClassDefIndex() const {
735 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_class_def_idx_), false);
736 }
737
738 void SetDexClassDefIndex(uint16_t class_def_idx) {
739 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_class_def_idx_), class_def_idx, false);
740 }
741
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800742 uint16_t GetDexTypeIndex() const {
743 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false);
744 }
745
746 void SetDexTypeIndex(uint16_t type_idx) {
747 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false);
748 }
749
750 static Class* GetJavaLangClass() {
751 DCHECK(java_lang_Class_ != NULL);
752 return java_lang_Class_;
753 }
754
755 // Can't call this SetClass or else gets called instead of Object::SetClass in places.
756 static void SetClassClass(Class* java_lang_Class);
757 static void ResetClass();
758
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200759 // When class is verified, set the kAccPreverified flag on each method.
760 void SetPreverifiedFlagOnAllMethods() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
761
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800762 private:
763 void SetVerifyErrorClass(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
764
765 bool Implements(const Class* klass) const
766 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
767 bool IsArrayAssignableFromArray(const Class* klass) const
768 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
769 bool IsAssignableFromArray(const Class* klass) const
770 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
771
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700772 void CheckObjectAlloc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
773
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800774 // defining class loader, or NULL for the "bootstrap" system loader
775 ClassLoader* class_loader_;
776
777 // For array classes, the component class object for instanceof/checkcast
778 // (for String[][][], this will be String[][]). NULL for non-array classes.
779 Class* component_type_;
780
781 // DexCache of resolved constant pool entries (will be NULL for classes generated by the
782 // runtime such as arrays and primitive classes).
783 DexCache* dex_cache_;
784
785 // static, private, and <init> methods
Brian Carlstromea46f952013-07-30 01:26:50 -0700786 ObjectArray<ArtMethod>* direct_methods_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800787
788 // instance fields
789 //
790 // These describe the layout of the contents of an Object.
791 // Note that only the fields directly declared by this class are
792 // listed in ifields; fields declared by a superclass are listed in
793 // the superclass's Class.ifields.
794 //
795 // All instance fields that refer to objects are guaranteed to be at
796 // the beginning of the field list. num_reference_instance_fields_
797 // specifies the number of reference fields.
Brian Carlstromea46f952013-07-30 01:26:50 -0700798 ObjectArray<ArtField>* ifields_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800799
800 // The interface table (iftable_) contains pairs of a interface class and an array of the
801 // interface methods. There is one pair per interface supported by this class. That means one
802 // pair for each interface we support directly, indirectly via superclass, or indirectly via a
803 // superinterface. This will be null if neither we nor our superclass implement any interfaces.
804 //
805 // Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()".
806 // Invoke faceObj.blah(), where "blah" is part of the Face interface. We can't easily use a
807 // single vtable.
808 //
809 // For every interface a concrete class implements, we create an array of the concrete vtable_
810 // methods for the methods in the interface.
811 IfTable* iftable_;
812
813 // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName
814 String* name_;
815
816 // Static fields
Brian Carlstromea46f952013-07-30 01:26:50 -0700817 ObjectArray<ArtField>* sfields_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800818
819 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type.
820 Class* super_class_;
821
822 // If class verify fails, we must return same error on subsequent tries.
823 Class* verify_error_class_;
824
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700825 // Virtual methods defined in this class; invoked through vtable.
Brian Carlstromea46f952013-07-30 01:26:50 -0700826 ObjectArray<ArtMethod>* virtual_methods_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800827
828 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
829 // copied in, and virtual methods from our class either replace those from the super or are
830 // appended. For abstract classes, methods may be created in the vtable that aren't in
831 // virtual_ methods_ for miranda methods.
Brian Carlstromea46f952013-07-30 01:26:50 -0700832 ObjectArray<ArtMethod>* vtable_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800833
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700834 // Access flags; low 16 bits are defined by VM spec.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800835 uint32_t access_flags_;
836
837 // Total size of the Class instance; used when allocating storage on gc heap.
838 // See also object_size_.
839 size_t class_size_;
840
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700841 // Tid used to check for recursive <clinit> invocation.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800842 pid_t clinit_thread_id_;
843
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700844 // ClassDef index in dex file, -1 if no class definition such as an array.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800845 // TODO: really 16bits
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700846 int32_t dex_class_def_idx_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800847
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700848 // Type index in dex file.
849 // TODO: really 16bits
850 int32_t dex_type_idx_;
851
852 // Number of instance fields that are object refs.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800853 size_t num_reference_instance_fields_;
854
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700855 // Number of static fields that are object refs,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800856 size_t num_reference_static_fields_;
857
858 // Total object size; used when allocating storage on gc heap.
859 // (For interfaces and abstract classes this will be zero.)
860 // See also class_size_.
861 size_t object_size_;
862
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700863 // Primitive type value, or Primitive::kPrimNot (0); set for generated primitive classes.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800864 Primitive::Type primitive_type_;
865
866 // Bitmap of offsets of ifields.
867 uint32_t reference_instance_offsets_;
868
869 // Bitmap of offsets of sfields.
870 uint32_t reference_static_offsets_;
871
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700872 // State of class initialization.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800873 Status status_;
874
875 // TODO: ?
876 // initiating class loader list
877 // NOTE: for classes with low serialNumber, these are unused, and the
878 // values are kept in a table in gDvm.
879 // InitiatingLoaderList initiating_loader_list_;
880
881 // Location of first static field.
882 uint32_t fields_[0];
883
884 // java.lang.Class
885 static Class* java_lang_Class_;
886
887 friend struct art::ClassOffsets; // for verifying offset information
888 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
889};
890
891std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
892
893class MANAGED ClassClass : public Class {
894 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800895 int64_t serialVersionUID_;
896 friend struct art::ClassClassOffsets; // for verifying offset information
897 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
898};
899
900} // namespace mirror
901} // namespace art
902
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700903#endif // ART_RUNTIME_MIRROR_CLASS_H_