blob: 7f2fbf6625b2db8a74467290a3d6ad5f055a9a93 [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2008 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 */
Andy McFaddenb51ea112009-05-08 16:50:17 -070016
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080017/*
18 * Declaration of the fundamental Object type and refinements thereof, plus
19 * some functions for manipulating them.
20 */
21#ifndef _DALVIK_OO_OBJECT
22#define _DALVIK_OO_OBJECT
23
Andy McFadden861b3382010-03-05 15:58:31 -080024#include <Atomic.h>
25
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080026#include <stddef.h>
27
28/* fwd decl */
29struct DataObject;
Barry Hayes2c987472009-04-06 10:03:48 -070030struct InitiatingLoaderList;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080031struct ClassObject;
32struct StringObject;
33struct ArrayObject;
34struct Method;
35struct ExceptionEntry;
36struct LineNumEntry;
37struct StaticField;
38struct InstField;
39struct Field;
40struct RegisterMap;
41typedef struct DataObject DataObject;
Barry Hayes2c987472009-04-06 10:03:48 -070042typedef struct InitiatingLoaderList InitiatingLoaderList;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080043typedef struct ClassObject ClassObject;
44typedef struct StringObject StringObject;
45typedef struct ArrayObject ArrayObject;
46typedef struct Method Method;
47typedef struct ExceptionEntry ExceptionEntry;
48typedef struct LineNumEntry LineNumEntry;
49typedef struct StaticField StaticField;
50typedef struct InstField InstField;
51typedef struct Field Field;
52typedef struct RegisterMap RegisterMap;
53
54/*
55 * Native function pointer type.
56 *
57 * "args[0]" holds the "this" pointer for virtual methods.
58 *
59 * The "Bridge" form is a super-set of the "Native" form; in many places
60 * they are used interchangeably. Currently, all functions have all
61 * arguments passed in, but some functions only care about the first two.
62 * Passing extra arguments to a C function is (mostly) harmless.
63 */
64typedef void (*DalvikBridgeFunc)(const u4* args, JValue* pResult,
65 const Method* method, struct Thread* self);
66typedef void (*DalvikNativeFunc)(const u4* args, JValue* pResult);
67
68
69/* vm-internal access flags and related definitions */
70typedef enum AccessFlags {
71 ACC_MIRANDA = 0x8000, // method (internal to VM)
72 JAVA_FLAGS_MASK = 0xffff, // bits set from Java sources (low 16)
73} AccessFlags;
74
75/* Use the top 16 bits of the access flags field for
76 * other class flags. Code should use the *CLASS_FLAG*()
77 * macros to set/get these flags.
78 */
79typedef enum ClassFlags {
80 CLASS_ISFINALIZABLE = (1<<31), // class/ancestor overrides finalize()
81 CLASS_ISARRAY = (1<<30), // class is a "[*"
82 CLASS_ISOBJECTARRAY = (1<<29), // class is a "[L*" or "[[*"
83 CLASS_ISREFERENCE = (1<<28), // class is a soft/weak/phantom ref
84 // only ISREFERENCE is set --> soft
85 CLASS_ISWEAKREFERENCE = (1<<27), // class is a weak reference
86 CLASS_ISPHANTOMREFERENCE = (1<<26), // class is a phantom reference
87
88 CLASS_MULTIPLE_DEFS = (1<<25), // DEX verifier: defs in multiple DEXs
89
90 /* unlike the others, these can be present in the optimized DEX file */
91 CLASS_ISOPTIMIZED = (1<<17), // class may contain opt instrs
92 CLASS_ISPREVERIFIED = (1<<16), // class has been pre-verified
93} ClassFlags;
94
95/* bits we can reasonably expect to see set in a DEX access flags field */
96#define EXPECTED_FILE_FLAGS \
97 (ACC_CLASS_MASK | CLASS_ISPREVERIFIED | CLASS_ISOPTIMIZED)
98
Andy McFaddenb51ea112009-05-08 16:50:17 -070099/*
100 * Get/set class flags.
101 */
102#define SET_CLASS_FLAG(clazz, flag) \
103 do { (clazz)->accessFlags |= (flag); } while (0)
104
105#define CLEAR_CLASS_FLAG(clazz, flag) \
106 do { (clazz)->accessFlags &= ~(flag); } while (0)
107
108#define IS_CLASS_FLAG_SET(clazz, flag) \
109 (((clazz)->accessFlags & (flag)) != 0)
110
111#define GET_CLASS_FLAG_GROUP(clazz, flags) \
112 ((u4)((clazz)->accessFlags & (flags)))
113
114/*
115 * Use the top 16 bits of the access flags field for other method flags.
116 * Code should use the *METHOD_FLAG*() macros to set/get these flags.
117 */
118typedef enum MethodFlags {
119 METHOD_ISWRITABLE = (1<<31), // the method's code is writable
120} MethodFlags;
121
122/*
123 * Get/set method flags.
124 */
125#define SET_METHOD_FLAG(method, flag) \
126 do { (method)->accessFlags |= (flag); } while (0)
127
128#define CLEAR_METHOD_FLAG(method, flag) \
129 do { (method)->accessFlags &= ~(flag); } while (0)
130
131#define IS_METHOD_FLAG_SET(method, flag) \
132 (((method)->accessFlags & (flag)) != 0)
133
134#define GET_METHOD_FLAG_GROUP(method, flags) \
135 ((u4)((method)->accessFlags & (flags)))
136
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800137/* current state of the class, increasing as we progress */
138typedef enum ClassStatus {
139 CLASS_ERROR = -1,
140
141 CLASS_NOTREADY = 0,
Barry Hayesc49db852010-05-14 13:43:34 -0700142 CLASS_IDX = 1, /* loaded, DEX idx in super or ifaces */
143 CLASS_LOADED = 2, /* DEX idx values resolved */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800144 CLASS_RESOLVED = 3, /* part of linking */
145 CLASS_VERIFYING = 4, /* in the process of being verified */
146 CLASS_VERIFIED = 5, /* logically part of linking; done pre-init */
147 CLASS_INITIALIZING = 6, /* class init in progress */
148 CLASS_INITIALIZED = 7, /* ready to go */
149} ClassStatus;
150
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800151/*
Barry Hayeseac47ed2009-06-22 11:45:20 -0700152 * Definitions for packing refOffsets in ClassObject.
153 */
154/*
155 * A magic value for refOffsets. Ignore the bits and walk the super
156 * chain when this is the value.
157 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
158 * fields followed by 2 ref instance fields.]
159 */
160#define CLASS_WALK_SUPER ((unsigned int)(3))
161#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
162#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
163#define CLASS_OFFSET_ALIGNMENT 4
164#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
165/*
Barry Hayes6daaac12009-07-08 10:01:56 -0700166 * Given an offset, return the bit number which would encode that offset.
167 * Local use only.
168 */
169#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
170 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
171 CLASS_OFFSET_ALIGNMENT)
172/*
173 * Is the given offset too large to be encoded?
174 */
175#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
176 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
177/*
178 * Return a single bit, encoding the offset.
179 * Undefined if the offset is too large, as defined above.
Barry Hayeseac47ed2009-06-22 11:45:20 -0700180 */
181#define CLASS_BIT_FROM_OFFSET(byteOffset) \
Barry Hayes6daaac12009-07-08 10:01:56 -0700182 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
Barry Hayeseac47ed2009-06-22 11:45:20 -0700183/*
184 * Return an offset, given a bit number as returned from CLZ.
185 */
186#define CLASS_OFFSET_FROM_CLZ(rshift) \
187 (((int)(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
188
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800189
190/*
191 * Used for iftable in ClassObject.
192 */
193typedef struct InterfaceEntry {
194 /* pointer to interface class */
195 ClassObject* clazz;
196
197 /*
198 * Index into array of vtable offsets. This points into the ifviPool,
199 * which holds the vtables for all interfaces declared by this class.
200 */
201 int* methodIndexArray;
202} InterfaceEntry;
203
204
205
206/*
207 * There are three types of objects:
208 * Class objects - an instance of java.lang.Class
209 * Array objects - an object created with a "new array" instruction
210 * Data objects - an object that is neither of the above
211 *
212 * We also define String objects. At present they're equivalent to
213 * DataObject, but that may change. (Either way, they make some of the
214 * code more obvious.)
215 *
216 * All objects have an Object header followed by type-specific data.
217 */
218typedef struct Object {
219 /* ptr to class object */
220 ClassObject* clazz;
221
Carl Shapiro8d7f9b22009-12-21 20:23:45 -0800222 /*
223 * A word containing either a "thin" lock or a "fat" monitor. See
224 * the comments in Sync.c for a description of its layout.
225 */
226 u4 lock;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800227} Object;
228
229/*
230 * Properly initialize an Object.
231 * void DVM_OBJECT_INIT(Object *obj, ClassObject *clazz_)
232 */
Barry Hayes364f9d92010-06-11 16:12:47 -0700233#define DVM_OBJECT_INIT(obj, clazz_) \
234 do { \
235 dvmSetFieldObject((Object *)obj, offsetof(Object, clazz), \
236 (Object *)clazz_); \
237 DVM_LOCK_INIT(&(obj)->lock); \
238 } while (0)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800239
240/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800241 * Data objects have an Object header followed by their instance data.
242 */
243struct DataObject {
244 Object obj; /* MUST be first item */
245
246 /* variable #of u4 slots; u8 uses 2 slots */
247 u4 instanceData[1];
248};
249
250/*
251 * Strings are used frequently enough that we may want to give them their
252 * own unique type.
253 *
254 * Using a dedicated type object to access the instance data provides a
255 * performance advantage but makes the java/lang/String.java implementation
256 * fragile.
257 *
258 * Currently this is just equal to DataObject, and we pull the fields out
259 * like we do for any other object.
260 */
261struct StringObject {
262 Object obj; /* MUST be first item */
263
264 /* variable #of u4 slots; u8 uses 2 slots */
265 u4 instanceData[1];
266};
267
268
269/*
270 * Array objects have these additional fields.
271 *
272 * We don't currently store the size of each element. Usually it's implied
273 * by the instruction. If necessary, the width can be derived from
Elliott Hughesbeea0b72009-11-13 11:20:15 -0800274 * the first char of obj->clazz->descriptor.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800275 */
276struct ArrayObject {
277 Object obj; /* MUST be first item */
278
279 /* number of elements; immutable after init */
280 u4 length;
281
282 /*
283 * Array contents; actual size is (length * sizeof(type)). This is
284 * declared as u8 so that the compiler inserts any necessary padding
285 * (e.g. for EABI); the actual allocation may be smaller than 8 bytes.
286 */
287 u8 contents[1];
288};
289
290/*
Barry Hayes2c987472009-04-06 10:03:48 -0700291 * For classes created early and thus probably in the zygote, the
292 * InitiatingLoaderList is kept in gDvm. Later classes use the structure in
293 * Object Class. This helps keep zygote pages shared.
294 */
295struct InitiatingLoaderList {
296 /* a list of initiating loader Objects; grown and initialized on demand */
297 Object** initiatingLoaders;
298 /* count of loaders in the above list */
299 int initiatingLoaderCount;
300};
301
302/*
Barry Hayes03aa70a2010-03-01 15:49:41 -0800303 * Generic field header. We pass this around when we want a generic Field
304 * pointer (e.g. for reflection stuff). Testing the accessFlags for
305 * ACC_STATIC allows a proper up-cast.
306 */
307struct Field {
308 ClassObject* clazz; /* class in which the field is declared */
309 const char* name;
310 const char* signature; /* e.g. "I", "[C", "Landroid/os/Debug;" */
311 u4 accessFlags;
312#ifdef PROFILE_FIELD_ACCESS
313 u4 gets;
314 u4 puts;
315#endif
316};
317
318/*
319 * Static field.
320 */
321struct StaticField {
322 Field field; /* MUST be first item */
323 JValue value; /* initially set from DEX for primitives */
324};
325
326/*
327 * Instance field.
328 */
329struct InstField {
330 Field field; /* MUST be first item */
331
332 /*
333 * This field indicates the byte offset from the beginning of the
334 * (Object *) to the actual instance data; e.g., byteOffset==0 is
335 * the same as the object pointer (bug!), and byteOffset==4 is 4
336 * bytes farther.
337 */
338 int byteOffset;
339};
340
341/*
Andy McFaddenb51ea112009-05-08 16:50:17 -0700342 * This defines the amount of space we leave for field slots in the
343 * java.lang.Class definition. If we alter the class to have more than
344 * this many fields, the VM will abort at startup.
345 */
346#define CLASS_FIELD_SLOTS 4
347
348/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800349 * Class objects have many additional fields. This is used for both
350 * classes and interfaces, including synthesized classes (arrays and
351 * primitive types).
352 *
353 * Class objects are unusual in that they have some fields allocated with
354 * the system malloc (or LinearAlloc), rather than on the GC heap. This is
355 * handy during initialization, but does require special handling when
356 * discarding java.lang.Class objects.
357 *
358 * The separation of methods (direct vs. virtual) and fields (class vs.
359 * instance) used in Dalvik works out pretty well. The only time it's
360 * annoying is when enumerating or searching for things with reflection.
361 */
362struct ClassObject {
363 Object obj; /* MUST be first item */
364
365 /* leave space for instance data; we could access fields directly if we
366 freeze the definition of java/lang/Class */
367 u4 instanceData[CLASS_FIELD_SLOTS];
368
369 /* UTF-8 descriptor for the class; from constant pool, or on heap
370 if generated ("[C") */
371 const char* descriptor;
372 char* descriptorAlloc;
373
374 /* access flags; low 16 bits are defined by VM spec */
375 u4 accessFlags;
376
377 /* VM-unique class serial number, nonzero, set very early */
378 u4 serialNumber;
379
380 /* DexFile from which we came; needed to resolve constant pool entries */
381 /* (will be NULL for VM-generated, e.g. arrays and primitive classes) */
382 DvmDex* pDvmDex;
383
384 /* state of class initialization */
385 ClassStatus status;
386
387 /* if class verify fails, we must return same error on subsequent tries */
388 ClassObject* verifyErrorClass;
389
390 /* threadId, used to check for recursive <clinit> invocation */
391 u4 initThreadId;
392
393 /*
394 * Total object size; used when allocating storage on gc heap. (For
395 * interfaces and abstract classes this will be zero.)
396 */
397 size_t objectSize;
398
399 /* arrays only: class object for base element, for instanceof/checkcast
400 (for String[][][], this will be String) */
401 ClassObject* elementClass;
402
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800403 /* arrays only: number of dimensions, e.g. int[][] is 2 */
404 int arrayDim;
405
406 /* primitive type index, or PRIM_NOT (-1); set for generated prim classes */
407 PrimitiveType primitiveType;
408
409 /* superclass, or NULL if this is java.lang.Object */
410 ClassObject* super;
411
412 /* defining class loader, or NULL for the "bootstrap" system loader */
413 Object* classLoader;
414
415 /* initiating class loader list */
Barry Hayes2c987472009-04-06 10:03:48 -0700416 /* NOTE: for classes with low serialNumber, these are unused, and the
417 values are kept in a table in gDvm. */
418 InitiatingLoaderList initiatingLoaderList;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800419
420 /* array of interfaces this class implements directly */
421 int interfaceCount;
422 ClassObject** interfaces;
423
424 /* static, private, and <init> methods */
425 int directMethodCount;
426 Method* directMethods;
427
428 /* virtual methods defined in this class; invoked through vtable */
429 int virtualMethodCount;
430 Method* virtualMethods;
431
432 /*
433 * Virtual method table (vtable), for use by "invoke-virtual". The
434 * vtable from the superclass is copied in, and virtual methods from
435 * our class either replace those from the super or are appended.
436 */
437 int vtableCount;
438 Method** vtable;
439
440 /*
441 * Interface table (iftable), one entry per interface supported by
442 * this class. That means one entry for each interface we support
443 * directly, indirectly via superclass, or indirectly via
444 * superinterface. This will be null if neither we nor our superclass
445 * implement any interfaces.
446 *
447 * Why we need this: given "class Foo implements Face", declare
448 * "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah" is
449 * part of the Face interface. We can't easily use a single vtable.
450 *
451 * For every interface a concrete class implements, we create a list of
452 * virtualMethod indices for the methods in the interface.
453 */
454 int iftableCount;
455 InterfaceEntry* iftable;
456
457 /*
458 * The interface vtable indices for iftable get stored here. By placing
459 * them all in a single pool for each class that implements interfaces,
460 * we decrease the number of allocations.
461 */
462 int ifviPoolCount;
463 int* ifviPool;
464
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800465 /* instance fields
466 *
467 * These describe the layout of the contents of a DataObject-compatible
468 * Object. Note that only the fields directly defined by this class
469 * are listed in ifields; fields defined by a superclass are listed
470 * in the superclass's ClassObject.ifields.
471 *
472 * All instance fields that refer to objects are guaranteed to be
473 * at the beginning of the field list. ifieldRefCount specifies
474 * the number of reference fields.
475 */
476 int ifieldCount;
477 int ifieldRefCount; // number of fields that are object refs
478 InstField* ifields;
479
Barry Hayeseac47ed2009-06-22 11:45:20 -0700480 /* bitmap of offsets of ifields */
481 u4 refOffsets;
482
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800483 /* source file name, if known */
484 const char* sourceFile;
Barry Hayes03aa70a2010-03-01 15:49:41 -0800485
486 /* static fields */
487 int sfieldCount;
488 StaticField sfields[]; /* MUST be last item */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800489};
490
491/*
492 * A method. We create one of these for every method in every class
493 * we load, so try to keep the size to a minimum.
494 *
495 * Much of this comes from and could be accessed in the data held in shared
496 * memory. We hold it all together here for speed. Everything but the
497 * pointers could be held in a shared table generated by the optimizer;
498 * if we're willing to convert them to offsets and take the performance
499 * hit (e.g. "meth->insns" becomes "baseAddr + meth->insnsOffset") we
500 * could move everything but "nativeFunc".
501 */
502struct Method {
503 /* the class we are a part of */
504 ClassObject* clazz;
505
506 /* access flags; low 16 bits are defined by spec (could be u2?) */
507 u4 accessFlags;
508
509 /*
510 * For concrete virtual methods, this is the offset of the method
511 * in "vtable".
512 *
513 * For abstract methods in an interface class, this is the offset
514 * of the method in "iftable[n]->methodIndexArray".
515 */
516 u2 methodIndex;
517
518 /*
519 * Method bounds; not needed for an abstract method.
520 *
521 * For a native method, we compute the size of the argument list, and
522 * set "insSize" and "registerSize" equal to it.
523 */
524 u2 registersSize; /* ins + locals */
525 u2 outsSize;
526 u2 insSize;
527
528 /* method name, e.g. "<init>" or "eatLunch" */
529 const char* name;
530
531 /*
532 * Method prototype descriptor string (return and argument types).
533 *
534 * TODO: This currently must specify the DexFile as well as the proto_ids
535 * index, because generated Proxy classes don't have a DexFile. We can
536 * remove the DexFile* and reduce the size of this struct if we generate
537 * a DEX for proxies.
538 */
539 DexProto prototype;
540
541 /* short-form method descriptor string */
542 const char* shorty;
543
544 /*
545 * The remaining items are not used for abstract or native methods.
546 * (JNI is currently hijacking "insns" as a function pointer, set
547 * after the first call. For internal-native this stays null.)
548 */
549
550 /* the actual code */
551 const u2* insns; /* instructions, in memory-mapped .dex */
552
553 /* cached JNI argument and return-type hints */
554 int jniArgInfo;
555
556 /*
557 * Native method ptr; could be actual function or a JNI bridge. We
558 * don't currently discriminate between DalvikBridgeFunc and
559 * DalvikNativeFunc; the former takes an argument superset (i.e. two
560 * extra args) which will be ignored. If necessary we can use
561 * insns==NULL to detect JNI bridge vs. internal native.
562 */
563 DalvikBridgeFunc nativeFunc;
564
565 /*
566 * Register map data, if available. This will point into the DEX file
567 * if the data was computed during pre-verification, or into the
568 * linear alloc area if not.
569 */
570 const RegisterMap* registerMap;
571
Andy McFadden0d615c32010-08-18 12:19:51 -0700572 /* set if method was called during method profiling */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800573 bool inProfile;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800574};
575
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800576
577/*
578 * Find a method within a class. The superclass is not searched.
579 */
580Method* dvmFindDirectMethodByDescriptor(const ClassObject* clazz,
581 const char* methodName, const char* signature);
582Method* dvmFindVirtualMethodByDescriptor(const ClassObject* clazz,
583 const char* methodName, const char* signature);
584Method* dvmFindVirtualMethodByName(const ClassObject* clazz,
585 const char* methodName);
586Method* dvmFindDirectMethod(const ClassObject* clazz, const char* methodName,
587 const DexProto* proto);
588Method* dvmFindVirtualMethod(const ClassObject* clazz, const char* methodName,
589 const DexProto* proto);
590
591
592/*
593 * Find a method within a class hierarchy.
594 */
595Method* dvmFindDirectMethodHierByDescriptor(const ClassObject* clazz,
596 const char* methodName, const char* descriptor);
597Method* dvmFindVirtualMethodHierByDescriptor(const ClassObject* clazz,
598 const char* methodName, const char* signature);
599Method* dvmFindDirectMethodHier(const ClassObject* clazz,
600 const char* methodName, const DexProto* proto);
601Method* dvmFindVirtualMethodHier(const ClassObject* clazz,
602 const char* methodName, const DexProto* proto);
Andy McFadden1d9206d2009-07-15 14:34:49 -0700603Method* dvmFindMethodHier(const ClassObject* clazz, const char* methodName,
604 const DexProto* proto);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800605
606/*
Andy McFaddenfc8044d2011-01-06 17:04:05 -0800607 * Find a method in an interface hierarchy.
608 */
609Method* dvmFindInterfaceMethodHierByDescriptor(const ClassObject* iface,
610 const char* methodName, const char* descriptor);
611Method* dvmFindInterfaceMethodHier(const ClassObject* iface,
612 const char* methodName, const DexProto* proto);
613
614/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800615 * Find the implementation of "meth" in "clazz".
616 *
617 * Returns NULL and throws an exception if not found.
618 */
619const Method* dvmGetVirtualizedMethod(const ClassObject* clazz,
620 const Method* meth);
621
622/*
623 * Get the source file associated with a method.
624 */
625const char* dvmGetMethodSourceFile(const Method* meth);
626
627/*
628 * Find a field within a class. The superclass is not searched.
629 */
630InstField* dvmFindInstanceField(const ClassObject* clazz,
631 const char* fieldName, const char* signature);
632StaticField* dvmFindStaticField(const ClassObject* clazz,
633 const char* fieldName, const char* signature);
634
635/*
636 * Find a field in a class/interface hierarchy.
637 */
638InstField* dvmFindInstanceFieldHier(const ClassObject* clazz,
639 const char* fieldName, const char* signature);
640StaticField* dvmFindStaticFieldHier(const ClassObject* clazz,
641 const char* fieldName, const char* signature);
Andy McFadden1d9206d2009-07-15 14:34:49 -0700642Field* dvmFindFieldHier(const ClassObject* clazz, const char* fieldName,
643 const char* signature);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800644
645/*
646 * Find a field and return the byte offset from the object pointer. Only
647 * searches the specified class, not the superclass.
648 *
649 * Returns -1 on failure.
650 */
651INLINE int dvmFindFieldOffset(const ClassObject* clazz,
652 const char* fieldName, const char* signature)
653{
654 InstField* pField = dvmFindInstanceField(clazz, fieldName, signature);
655 if (pField == NULL)
656 return -1;
657 else
658 return pField->byteOffset;
659}
660
661/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800662 * Helpers.
663 */
664INLINE bool dvmIsPublicMethod(const Method* method) {
665 return (method->accessFlags & ACC_PUBLIC) != 0;
666}
667INLINE bool dvmIsPrivateMethod(const Method* method) {
668 return (method->accessFlags & ACC_PRIVATE) != 0;
669}
670INLINE bool dvmIsStaticMethod(const Method* method) {
671 return (method->accessFlags & ACC_STATIC) != 0;
672}
673INLINE bool dvmIsSynchronizedMethod(const Method* method) {
674 return (method->accessFlags & ACC_SYNCHRONIZED) != 0;
675}
676INLINE bool dvmIsDeclaredSynchronizedMethod(const Method* method) {
677 return (method->accessFlags & ACC_DECLARED_SYNCHRONIZED) != 0;
678}
679INLINE bool dvmIsFinalMethod(const Method* method) {
680 return (method->accessFlags & ACC_FINAL) != 0;
681}
682INLINE bool dvmIsNativeMethod(const Method* method) {
683 return (method->accessFlags & ACC_NATIVE) != 0;
684}
685INLINE bool dvmIsAbstractMethod(const Method* method) {
686 return (method->accessFlags & ACC_ABSTRACT) != 0;
687}
Jesse Wilson3c6f4c02011-02-22 20:20:36 -0800688INLINE bool dvmIsSyntheticMethod(const Method* method) {
689 return (method->accessFlags & ACC_SYNTHETIC) != 0;
690}
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800691INLINE bool dvmIsMirandaMethod(const Method* method) {
692 return (method->accessFlags & ACC_MIRANDA) != 0;
693}
694INLINE bool dvmIsConstructorMethod(const Method* method) {
695 return *method->name == '<';
696}
697/* Dalvik puts private, static, and constructors into non-virtual table */
698INLINE bool dvmIsDirectMethod(const Method* method) {
699 return dvmIsPrivateMethod(method) ||
700 dvmIsStaticMethod(method) ||
701 dvmIsConstructorMethod(method);
702}
703/* Get whether the given method has associated bytecode. This is the
704 * case for methods which are neither native nor abstract. */
705INLINE bool dvmIsBytecodeMethod(const Method* method) {
706 return (method->accessFlags & (ACC_NATIVE | ACC_ABSTRACT)) == 0;
707}
708
709INLINE bool dvmIsProtectedField(const Field* field) {
710 return (field->accessFlags & ACC_PROTECTED) != 0;
711}
712INLINE bool dvmIsStaticField(const Field* field) {
713 return (field->accessFlags & ACC_STATIC) != 0;
714}
715INLINE bool dvmIsFinalField(const Field* field) {
716 return (field->accessFlags & ACC_FINAL) != 0;
717}
Andy McFadden861b3382010-03-05 15:58:31 -0800718INLINE bool dvmIsVolatileField(const Field* field) {
719 return (field->accessFlags & ACC_VOLATILE) != 0;
720}
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800721
722INLINE bool dvmIsInterfaceClass(const ClassObject* clazz) {
723 return (clazz->accessFlags & ACC_INTERFACE) != 0;
724}
725INLINE bool dvmIsPublicClass(const ClassObject* clazz) {
726 return (clazz->accessFlags & ACC_PUBLIC) != 0;
727}
728INLINE bool dvmIsFinalClass(const ClassObject* clazz) {
729 return (clazz->accessFlags & ACC_FINAL) != 0;
730}
731INLINE bool dvmIsAbstractClass(const ClassObject* clazz) {
732 return (clazz->accessFlags & ACC_ABSTRACT) != 0;
733}
Carl Shapirode750892010-06-08 16:37:12 -0700734INLINE bool dvmIsAnnotationClass(const ClassObject* clazz) {
735 return (clazz->accessFlags & ACC_ANNOTATION) != 0;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800736}
737INLINE bool dvmIsPrimitiveClass(const ClassObject* clazz) {
738 return clazz->primitiveType != PRIM_NOT;
739}
740
741/* linked, here meaning prepared and resolved */
742INLINE bool dvmIsClassLinked(const ClassObject* clazz) {
743 return clazz->status >= CLASS_RESOLVED;
744}
745/* has class been verified? */
746INLINE bool dvmIsClassVerified(const ClassObject* clazz) {
747 return clazz->status >= CLASS_VERIFIED;
748}
749
750/*
751 * Get the associated code struct for a method. This returns NULL
752 * for non-bytecode methods.
753 */
754INLINE const DexCode* dvmGetMethodCode(const Method* meth) {
755 if (dvmIsBytecodeMethod(meth)) {
756 /*
757 * The insns field for a bytecode method actually points at
758 * &(DexCode.insns), so we can subtract back to get at the
759 * DexCode in front.
760 */
761 return (const DexCode*)
762 (((const u1*) meth->insns) - offsetof(DexCode, insns));
763 } else {
764 return NULL;
765 }
766}
767
768/*
769 * Get the size of the insns associated with a method. This returns 0
770 * for non-bytecode methods.
771 */
772INLINE u4 dvmGetMethodInsnsSize(const Method* meth) {
773 const DexCode* pCode = dvmGetMethodCode(meth);
774 return (pCode == NULL) ? 0 : pCode->insnsSize;
775}
776
777/* debugging */
778void dvmDumpObject(const Object* obj);
779
780#endif /*_DALVIK_OO_OBJECT*/