The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 38 | typedef 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 | |
| 50 | bool dvmClassStartup(void); |
| 51 | void dvmClassShutdown(void); |
| 52 | bool dvmPrepBootClassPath(bool isNormalStart); |
| 53 | |
| 54 | /* |
| 55 | * Boot class path accessors, for class loader getResources(). |
| 56 | */ |
| 57 | int dvmGetBootPathSize(void); |
| 58 | StringObject* dvmGetBootPathResource(const char* name, int idx); |
| 59 | void dvmDumpBootClassPath(void); |
| 60 | |
| 61 | /* |
| 62 | * Determine whether "path" is a member of "cpe". |
| 63 | */ |
| 64 | bool dvmClassPathContains(const ClassPathEntry* cpe, const char* path); |
| 65 | |
| 66 | /* |
The Android Open Source Project | bcd637a | 2009-01-22 00:13:40 -0800 | [diff] [blame^] | 67 | * Set clazz->serialNumber to the next available value. |
| 68 | */ |
| 69 | void dvmSetClassSerialNumber(ClassObject* clazz); |
| 70 | |
| 71 | /* |
The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | * 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 | */ |
| 77 | ClassObject* dvmFindClass(const char* descriptor, Object* loader); |
| 78 | ClassObject* dvmFindClassNoInit(const char* descriptor, Object* loader); |
| 79 | |
| 80 | /* |
| 81 | * Like dvmFindClass, but only for system classes. |
| 82 | */ |
| 83 | ClassObject* dvmFindSystemClass(const char* descriptor); |
| 84 | ClassObject* 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 | */ |
| 96 | ClassObject* 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 | */ |
| 103 | ClassObject* 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 | */ |
| 111 | bool dvmLinkClass(ClassObject* clazz, bool classesResolved); |
| 112 | |
| 113 | /* |
| 114 | * Determine if a class has been initialized. |
| 115 | */ |
| 116 | INLINE bool dvmIsClassInitialized(const ClassObject* clazz) { |
| 117 | return (clazz->status == CLASS_INITIALIZED); |
| 118 | } |
| 119 | bool dvmIsClassInitializing(const ClassObject* clazz); |
| 120 | |
| 121 | /* |
| 122 | * Initialize a class. |
| 123 | */ |
| 124 | bool dvmInitClass(ClassObject* clazz); |
| 125 | |
| 126 | /* |
| 127 | * Retrieve the system class loader. |
| 128 | */ |
| 129 | Object* dvmGetSystemClassLoader(void); |
| 130 | |
| 131 | /* |
| 132 | * Utility functions. |
| 133 | */ |
| 134 | ClassObject* dvmLookupClass(const char* descriptor, Object* loader, |
| 135 | bool unprepOkay); |
| 136 | void dvmFreeClassInnards(ClassObject* clazz); |
| 137 | bool dvmAddClassToHash(ClassObject* clazz); |
| 138 | void dvmAddInitiatingLoader(ClassObject* clazz, Object* loader); |
| 139 | bool dvmLoaderInInitiatingList(const ClassObject* clazz, const Object* loader); |
| 140 | |
| 141 | /* |
| 142 | * Update method's "nativeFunc" and "insns" after native method resolution. |
| 143 | */ |
| 144 | void dvmSetNativeFunc(const Method* method, DalvikBridgeFunc func, |
| 145 | const u2* insns); |
| 146 | |
| 147 | /* during DEX optimizing, add an extra DEX to the bootstrap class path */ |
| 148 | INLINE void dvmSetBootPathExtraDex(DvmDex* pDvmDex); |
| 149 | |
| 150 | /* |
| 151 | * Debugging. |
| 152 | */ |
| 153 | void dvmDumpClass(const ClassObject* clazz, int flags); |
| 154 | void dvmDumpAllClasses(int flags); |
| 155 | void dvmDumpLoaderStats(const char* msg); |
| 156 | int dvmGetNumLoadedClasses(); |
| 157 | |
| 158 | #ifdef PROFILE_FIELD_ACCESS |
| 159 | void 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 | */ |
| 173 | INLINE 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 | */ |
| 187 | INLINE 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 | */ |
| 196 | INLINE int dvmCompareMethodProtos(const Method* method1, |
| 197 | const Method* method2) |
| 198 | { |
| 199 | return dexProtoCompare(&method1->prototype, &method2->prototype); |
| 200 | } |
| 201 | |
| 202 | /* |
The Android Open Source Project | 89c1feb | 2008-12-17 18:03:55 -0800 | [diff] [blame] | 203 | * 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 | */ |
| 207 | INLINE int dvmCompareMethodParameterProtos(const Method* method1, |
| 208 | const Method* method2) |
| 209 | { |
| 210 | return dexProtoCompareParameters(&method1->prototype, &method2->prototype); |
| 211 | } |
| 212 | |
| 213 | /* |
The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 214 | * 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 | */ |
| 218 | int dvmCompareMethodNamesAndProtos(const Method* method1, |
| 219 | const Method* method2); |
| 220 | |
| 221 | /* |
The Android Open Source Project | 89c1feb | 2008-12-17 18:03:55 -0800 | [diff] [blame] | 222 | * 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 | */ |
| 227 | int dvmCompareMethodNamesAndParameterProtos(const Method* method1, |
| 228 | const Method* method2); |
| 229 | |
| 230 | /* |
The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 231 | * 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 | */ |
| 235 | INLINE 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 | */ |
| 248 | int 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 | */ |
| 257 | int dvmCompareNameDescriptorAndMethod(const char* name, |
| 258 | const char* descriptor, const Method* method); |
| 259 | |
| 260 | #endif /*_DALVIK_OO_CLASS*/ |