blob: 67dbc1c7762f993e1e4b99e80194a431071d2762 [file] [log] [blame]
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -07001/*
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 */
16/*
17 * Class loader.
18 */
19#ifndef _DALVIK_OO_CLASS
20#define _DALVIK_OO_CLASS
21
22/*
23 * The classpath and bootclasspath differ in that only the latter is
24 * consulted when looking for classes needed by the VM. When searching
25 * for an arbitrary class definition, we start with the bootclasspath,
26 * look for optional packages (a/k/a standard extensions), and then try
27 * the classpath.
28 *
29 * In Dalvik, a class can be found in one of three ways:
30 * - as a "loose" .class file in a directory
31 * - as a .class file held in a JAR archive
32 * - in a .dex file
33 *
34 * These three may be freely intermixed in a classpath specification.
35 * Ordering is significant. (Currently only ".dex" is supported directly
36 * by the VM.)
37 */
38typedef struct ClassPathEntry {
39 enum {
40 kCpeUnknown = 0,
41 kCpeDir,
42 kCpeJar,
43 kCpeDex,
44 kCpeLastEntry /* used as sentinel at end of array */
45 } kind;
46 char* fileName;
47 void* ptr; /* JarFile* or DexFile* */
48} ClassPathEntry;
49
50bool dvmClassStartup(void);
51void dvmClassShutdown(void);
52bool dvmPrepBootClassPath(bool isNormalStart);
53
54/*
55 * Boot class path accessors, for class loader getResources().
56 */
57int dvmGetBootPathSize(void);
58StringObject* dvmGetBootPathResource(const char* name, int idx);
59void dvmDumpBootClassPath(void);
60
61/*
62 * Determine whether "path" is a member of "cpe".
63 */
64bool dvmClassPathContains(const ClassPathEntry* cpe, const char* path);
65
66/*
The Android Open Source Projectbcd637a2009-01-22 00:13:40 -080067 * Set clazz->serialNumber to the next available value.
68 */
69void dvmSetClassSerialNumber(ClassObject* clazz);
70
71/*
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070072 * Find the class with the given descriptor. Load it if it hasn't already
73 * been.
74 *
75 * "loader" is the initiating class loader.
76 */
77ClassObject* dvmFindClass(const char* descriptor, Object* loader);
78ClassObject* dvmFindClassNoInit(const char* descriptor, Object* loader);
79
80/*
81 * Like dvmFindClass, but only for system classes.
82 */
83ClassObject* dvmFindSystemClass(const char* descriptor);
84ClassObject* dvmFindSystemClassNoInit(const char* descriptor);
85
86/*
87 * Find a loaded class by descriptor. Returns the first one found.
88 * Because there can be more than one if class loaders are involved,
89 * this is not an especially good API. (Currently only used by the
90 * debugger and "checking" JNI.)
91 *
92 * "descriptor" should have the form "Ljava/lang/Class;" or
93 * "[Ljava/lang/Class;", i.e. a descriptor and not an internal-form
94 * class name.
95 */
96ClassObject* dvmFindLoadedClass(const char* descriptor);
97
98/*
99 * Load the named class (by descriptor) from the specified DEX file.
100 * Used by class loaders to instantiate a class object from a
101 * VM-managed DEX.
102 */
103ClassObject* dvmDefineClass(DvmDex* pDvmDex, const char* descriptor,
104 Object* classLoader);
105
106/*
107 * Link a loaded class. Normally done as part of one of the "find class"
108 * variations, this is only called explicitly for synthetic class
109 * generation (e.g. reflect.Proxy).
110 */
111bool dvmLinkClass(ClassObject* clazz, bool classesResolved);
112
113/*
114 * Determine if a class has been initialized.
115 */
116INLINE bool dvmIsClassInitialized(const ClassObject* clazz) {
117 return (clazz->status == CLASS_INITIALIZED);
118}
119bool dvmIsClassInitializing(const ClassObject* clazz);
120
121/*
122 * Initialize a class.
123 */
124bool dvmInitClass(ClassObject* clazz);
125
126/*
127 * Retrieve the system class loader.
128 */
129Object* dvmGetSystemClassLoader(void);
130
131/*
132 * Utility functions.
133 */
134ClassObject* dvmLookupClass(const char* descriptor, Object* loader,
135 bool unprepOkay);
136void dvmFreeClassInnards(ClassObject* clazz);
137bool dvmAddClassToHash(ClassObject* clazz);
138void dvmAddInitiatingLoader(ClassObject* clazz, Object* loader);
139bool dvmLoaderInInitiatingList(const ClassObject* clazz, const Object* loader);
140
141/*
142 * Update method's "nativeFunc" and "insns" after native method resolution.
143 */
144void dvmSetNativeFunc(const Method* method, DalvikBridgeFunc func,
145 const u2* insns);
146
147/* during DEX optimizing, add an extra DEX to the bootstrap class path */
148INLINE void dvmSetBootPathExtraDex(DvmDex* pDvmDex);
149
150/*
151 * Debugging.
152 */
153void dvmDumpClass(const ClassObject* clazz, int flags);
154void dvmDumpAllClasses(int flags);
155void dvmDumpLoaderStats(const char* msg);
156int dvmGetNumLoadedClasses();
157
158#ifdef PROFILE_FIELD_ACCESS
159void dvmDumpFieldAccessCounts(void);
160#endif
161
162/* flags for dvmDumpClass / dvmDumpAllClasses */
163#define kDumpClassFullDetail 1
164#define kDumpClassClassLoader (1 << 1)
165#define kDumpClassInitialized (1 << 2)
166
167
168/*
169 * Store a copy of the method prototype descriptor string
170 * for the given method into the given DexStringCache, returning the
171 * stored string for convenience.
172 */
173INLINE char* dvmCopyDescriptorStringFromMethod(const Method* method,
174 DexStringCache *pCache)
175{
176 const char* result =
177 dexProtoGetMethodDescriptor(&method->prototype, pCache);
178 return dexStringCacheEnsureCopy(pCache, result);
179}
180
181/*
182 * Compute the number of argument words (u4 units) required by the
183 * given method's prototype. For example, if the method descriptor is
184 * "(IJ)D", this would return 3 (one for the int, two for the long;
185 * return value isn't relevant).
186 */
187INLINE int dvmComputeMethodArgsSize(const Method* method)
188{
189 return dexProtoComputeArgsSize(&method->prototype);
190}
191
192/*
193 * Compare the two method prototypes. The two prototypes are compared
194 * as if by strcmp() on the result of dexProtoGetMethodDescriptor().
195 */
196INLINE int dvmCompareMethodProtos(const Method* method1,
197 const Method* method2)
198{
199 return dexProtoCompare(&method1->prototype, &method2->prototype);
200}
201
202/*
The Android Open Source Project89c1feb2008-12-17 18:03:55 -0800203 * Compare the two method prototypes, considering only the parameters
204 * (i.e. ignoring the return types). The two prototypes are compared
205 * as if by strcmp() on the result of dexProtoGetMethodDescriptor().
206 */
207INLINE int dvmCompareMethodParameterProtos(const Method* method1,
208 const Method* method2)
209{
210 return dexProtoCompareParameters(&method1->prototype, &method2->prototype);
211}
212
213/*
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700214 * Compare the two method names and prototypes, a la strcmp(). The
215 * name is considered the "major" order and the prototype the "minor"
216 * order. The prototypes are compared as if by dexProtoGetMethodDescriptor().
217 */
218int dvmCompareMethodNamesAndProtos(const Method* method1,
219 const Method* method2);
220
221/*
The Android Open Source Project89c1feb2008-12-17 18:03:55 -0800222 * Compare the two method names and prototypes, a la strcmp(), ignoring
223 * the return type. The name is considered the "major" order and the
224 * prototype the "minor" order. The prototypes are compared as if by
225 * dexProtoGetMethodDescriptor().
226 */
227int dvmCompareMethodNamesAndParameterProtos(const Method* method1,
228 const Method* method2);
229
230/*
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700231 * Compare a method descriptor string with the prototype of a method,
232 * as if by converting the descriptor to a DexProto and comparing it
233 * with dexProtoCompare().
234 */
235INLINE int dvmCompareDescriptorAndMethodProto(const char* descriptor,
236 const Method* method)
237{
238 // Sense is reversed.
239 return -dexProtoCompareToDescriptor(&method->prototype, descriptor);
240}
241
242/*
243 * Compare a (name, prototype) pair with the (name, prototype) of
244 * a method, a la strcmp(). The name is considered the "major" order and
245 * the prototype the "minor" order. The descriptor and prototype are
246 * compared as if by dvmCompareDescriptorAndMethodProto().
247 */
248int dvmCompareNameProtoAndMethod(const char* name,
249 const DexProto* proto, const Method* method);
250
251/*
252 * Compare a (name, method descriptor) pair with the (name, prototype) of
253 * a method, a la strcmp(). The name is considered the "major" order and
254 * the prototype the "minor" order. The descriptor and prototype are
255 * compared as if by dvmCompareDescriptorAndMethodProto().
256 */
257int dvmCompareNameDescriptorAndMethod(const char* name,
258 const char* descriptor, const Method* method);
259
260#endif /*_DALVIK_OO_CLASS*/