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 * Basic reflection calls and utility functions. 18 */ 19 #ifndef _DALVIK_REFLECT_REFLECT 20 #define _DALVIK_REFLECT_REFLECT 21 22 bool dvmReflectStartup(void); 23 bool dvmReflectProxyStartup(void); 24 bool dvmReflectAnnotationStartup(void); 25 void dvmReflectShutdown(void); 26 27 /* 28 * During startup, validate the "box" classes, e.g. java/lang/Integer. 29 */ 30 bool dvmValidateBoxClasses(); 31 32 /* 33 * Get all fields declared by a class. 34 * 35 * Includes both class and instance fields. 36 */ 37 ArrayObject* dvmGetDeclaredFields(ClassObject* clazz, bool publicOnly); 38 39 /* 40 * Get all constructors declared by a class. 41 */ 42 ArrayObject* dvmGetDeclaredConstructors(ClassObject* clazz, bool publicOnly); 43 44 /* 45 * Get all methods declared by a class. 46 * 47 * This includes both static and virtual methods, and can include private 48 * members if "publicOnly" is false. It does not include Miranda methods, 49 * since those weren't declared in the class, or constructors. 50 */ 51 ArrayObject* dvmGetDeclaredMethods(ClassObject* clazz, bool publicOnly); 52 53 /* 54 * Get all interfaces a class implements. If this is unable to allocate 55 * the result array, this raises an OutOfMemoryError and returns NULL. 56 */ 57 ArrayObject* dvmGetInterfaces(ClassObject* clazz); 58 59 /* 60 * Convert slot numbers back to objects. 61 */ 62 Field* dvmSlotToField(ClassObject* clazz, int slot); 63 Method* dvmSlotToMethod(ClassObject* clazz, int slot); 64 65 /* 66 * Convert a primitive value, performing a widening conversion if necessary. 67 */ 68 int dvmConvertPrimitiveValue(PrimitiveType srcType, 69 PrimitiveType dstType, const s4* srcPtr, s4* dstPtr); 70 71 /* 72 * Convert the argument to the specified type. 73 * 74 * Returns the width of the argument (1 for most types, 2 for J/D, -1 on 75 * error). 76 */ 77 int dvmConvertArgument(DataObject* arg, ClassObject* type, s4* ins); 78 79 /* 80 * Create a wrapper object for a primitive data type. If "returnType" is 81 * not primitive, this just returns "value" cast to an object. 82 */ 83 DataObject* dvmWrapPrimitive(JValue value, ClassObject* returnType); 84 85 /* 86 * Unwrap a boxed primitive. If "returnType" is not primitive, this just 87 * returns "value" cast into a JValue. 88 */ 89 bool dvmUnwrapPrimitive(Object* value, ClassObject* returnType, 90 JValue* pResult); 91 92 /* 93 * Return the class object that matches the method's signature. For 94 * primitive types, returns the box class. 95 */ 96 ClassObject* dvmGetBoxedReturnType(const Method* meth); 97 98 /* 99 * JNI reflection support. 100 */ 101 Field* dvmGetFieldFromReflectObj(Object* obj); 102 Method* dvmGetMethodFromReflectObj(Object* obj); 103 Object* dvmCreateReflectObjForField(const ClassObject* clazz, Field* field); 104 Object* dvmCreateReflectObjForMethod(const ClassObject* clazz, Method* method); 105 106 /* 107 * Quick test to determine if the method in question is a reflection call. 108 * Used for some stack parsing. Currently defined as "the method's declaring 109 * class is java.lang.reflect.Method". 110 */ 111 INLINE bool dvmIsReflectionMethod(const Method* method) 112 { 113 return (method->clazz == gDvm.classJavaLangReflectMethod); 114 } 115 116 /* 117 * Proxy class generation. 118 */ 119 ClassObject* dvmGenerateProxyClass(StringObject* str, ArrayObject* interfaces, 120 Object* loader); 121 122 /* 123 * Create a new java.lang.reflect.Method object based on "meth". 124 */ 125 Object* dvmCreateReflectMethodObject(const Method* meth); 126 127 /* 128 * Return an array of Annotation objects for the specified piece. For method 129 * parameters this is an array of arrays of Annotation objects. 130 * 131 * Method also applies to Constructor. 132 */ 133 ArrayObject* dvmGetClassAnnotations(const ClassObject* clazz); 134 ArrayObject* dvmGetMethodAnnotations(const Method* method); 135 ArrayObject* dvmGetFieldAnnotations(const Field* field); 136 ArrayObject* dvmGetParameterAnnotations(const Method* method); 137 138 /* 139 * Find the default value for an annotation member. 140 */ 141 Object* dvmGetAnnotationDefaultValue(const Method* method); 142 143 /* 144 * Get the list of thrown exceptions for a method. Returns NULL if there 145 * are no exceptions listed. 146 */ 147 ArrayObject* dvmGetMethodThrows(const Method* method); 148 149 /* 150 * Get the Signature annotation. 151 */ 152 ArrayObject* dvmGetClassSignatureAnnotation(const ClassObject* clazz); 153 ArrayObject* dvmGetMethodSignatureAnnotation(const Method* method); 154 ArrayObject* dvmGetFieldSignatureAnnotation(const Field* field); 155 156 /* 157 * Get the EnclosingMethod attribute from an annotation. Returns a Method 158 * object, or NULL. 159 */ 160 Object* dvmGetEnclosingMethod(const ClassObject* clazz); 161 162 /* 163 * Return clazz's declaring class, or NULL if there isn't one. 164 */ 165 ClassObject* dvmGetDeclaringClass(const ClassObject* clazz); 166 167 /* 168 * Return clazz's enclosing class, or NULL if there isn't one. 169 */ 170 ClassObject* dvmGetEnclosingClass(const ClassObject* clazz); 171 172 /* 173 * Get the EnclosingClass attribute from an annotation. If found, returns 174 * "true". A String with the original name of the class and the original 175 * access flags are returned through the arguments. (The name will be NULL 176 * for an anonymous inner class.) 177 */ 178 bool dvmGetInnerClass(const ClassObject* clazz, StringObject** pName, 179 int* pAccessFlags); 180 181 /* 182 * Get an array of class objects from the MemberClasses annotation. Returns 183 * NULL if none found. 184 */ 185 ArrayObject* dvmGetDeclaredClasses(const ClassObject* clazz); 186 187 /* 188 * Used to pass values out of annotation (and encoded array) processing 189 * functions. 190 */ 191 typedef struct AnnotationValue { 192 JValue value; 193 u1 type; 194 } AnnotationValue; 195 196 197 /** 198 * Iterator structure for iterating over DexEncodedArray instances. The 199 * structure should be treated as opaque. 200 */ 201 typedef struct { 202 const u1* cursor; /* current cursor */ 203 u4 elementsLeft; /* number of elements left to read */ 204 const DexEncodedArray* encodedArray; /* instance being iterated over */ 205 u4 size; /* number of elements in instance */ 206 const ClassObject* clazz; /* class to resolve with respect to */ 207 } EncodedArrayIterator; 208 209 /** 210 * Initializes an encoded array iterator. 211 * 212 * @param iterator iterator to initialize 213 * @param encodedArray encoded array to iterate over 214 * @param clazz class to use when resolving strings and types 215 */ 216 void dvmEncodedArrayIteratorInitialize(EncodedArrayIterator* iterator, 217 const DexEncodedArray* encodedArray, const ClassObject* clazz); 218 219 /** 220 * Returns whether there are more elements to be read. 221 */ 222 bool dvmEncodedArrayIteratorHasNext(const EncodedArrayIterator* iterator); 223 224 /** 225 * Returns the next decoded value from the iterator, advancing its 226 * cursor. This returns primitive values in their corresponding union 227 * slots, and returns everything else (including nulls) as object 228 * references in the "l" union slot. 229 * 230 * The caller must call dvmReleaseTrackedAlloc() on any returned reference. 231 * 232 * @param value pointer to store decoded value into 233 * @returns true if a value was decoded and the cursor advanced; false if 234 * the last value had already been decoded or if there was a problem decoding 235 */ 236 bool dvmEncodedArrayIteratorGetNext(EncodedArrayIterator* iterator, 237 AnnotationValue* value); 238 239 #endif /*_DALVIK_REFLECT_REFLECT*/ 240