2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * Basic reflection calls and utility functions.
19 #ifndef DALVIK_REFLECT_REFLECT_H_
20 #define DALVIK_REFLECT_REFLECT_H_
23 * During startup, validate the "box" classes, e.g. java/lang/Integer.
25 bool dvmValidateBoxClasses();
28 * Get all fields declared by a class.
30 * Includes both class and instance fields.
32 ArrayObject* dvmGetDeclaredFields(ClassObject* clazz, bool publicOnly);
35 * Get the named field.
37 Object* dvmGetDeclaredField(ClassObject* clazz, StringObject* nameObj);
40 * Get all constructors declared by a class.
42 ArrayObject* dvmGetDeclaredConstructors(ClassObject* clazz, bool publicOnly);
45 * Get all methods declared by a class.
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.
51 ArrayObject* dvmGetDeclaredMethods(ClassObject* clazz, bool publicOnly);
54 * Get the named method.
56 Object* dvmGetDeclaredConstructorOrMethod(ClassObject* clazz,
57 StringObject* nameObj, ArrayObject* args);
60 * Get all interfaces a class implements. If this is unable to allocate
61 * the result array, this raises an OutOfMemoryError and returns NULL.
63 ArrayObject* dvmGetInterfaces(ClassObject* clazz);
66 * Convert slot numbers back to objects.
68 Field* dvmSlotToField(ClassObject* clazz, int slot);
69 Method* dvmSlotToMethod(ClassObject* clazz, int slot);
72 * Convert a primitive value, performing a widening conversion if necessary.
74 int dvmConvertPrimitiveValue(PrimitiveType srcType,
75 PrimitiveType dstType, const s4* srcPtr, s4* dstPtr);
78 * Convert the argument to the specified type.
80 * Returns the width of the argument (1 for most types, 2 for J/D, -1 on
83 int dvmConvertArgument(DataObject* arg, ClassObject* type, s4* ins);
86 * Box a primitive value into an object. If "returnType" is
87 * not primitive, this just returns "value" cast to an object.
89 DataObject* dvmBoxPrimitive(JValue value, ClassObject* returnType);
92 * Unwrap a boxed primitive. If "returnType" is not primitive, this just
93 * returns "value" cast into a JValue.
95 bool dvmUnboxPrimitive(Object* value, ClassObject* returnType,
99 * Return the class object that matches the method's signature. For
100 * primitive types, returns the box class.
102 ClassObject* dvmGetBoxedReturnType(const Method* meth);
105 * JNI reflection support.
107 Field* dvmGetFieldFromReflectObj(Object* obj);
108 Method* dvmGetMethodFromReflectObj(Object* obj);
109 Object* dvmCreateReflectObjForField(const ClassObject* clazz, Field* field);
110 Object* dvmCreateReflectObjForMethod(const ClassObject* clazz, Method* method);
113 * Quick test to determine if the method in question is a reflection call.
114 * Used for some stack parsing. Currently defined as "the method's declaring
115 * class is java.lang.reflect.Method".
117 INLINE bool dvmIsReflectionMethod(const Method* method)
119 return (method->clazz == gDvm.classJavaLangReflectMethod);
123 * Proxy class generation.
125 ClassObject* dvmGenerateProxyClass(StringObject* str, ArrayObject* interfaces,
129 * Create a new java.lang.reflect.Method object based on "meth".
131 Object* dvmCreateReflectMethodObject(const Method* meth);
134 * Return an array of Annotation objects for the specified piece. For method
135 * parameters this is an array of arrays of Annotation objects.
137 * Method also applies to Constructor.
139 ArrayObject* dvmGetClassAnnotations(const ClassObject* clazz);
140 ArrayObject* dvmGetMethodAnnotations(const Method* method);
141 ArrayObject* dvmGetFieldAnnotations(const Field* field);
142 ArrayObject* dvmGetParameterAnnotations(const Method* method);
145 * Return the annotation if it exists.
147 Object* dvmGetClassAnnotation(const ClassObject* clazz, const ClassObject* annotationClazz);
148 Object* dvmGetMethodAnnotation(const ClassObject* clazz, const Method* method,
149 const ClassObject* annotationClazz);
150 Object* dvmGetFieldAnnotation(const ClassObject* clazz, const Field* method,
151 const ClassObject* annotationClazz);
154 * Return true if the annotation exists.
156 bool dvmIsClassAnnotationPresent(const ClassObject* clazz, const ClassObject* annotationClazz);
157 bool dvmIsMethodAnnotationPresent(const ClassObject* clazz, const Method* method,
158 const ClassObject* annotationClazz);
159 bool dvmIsFieldAnnotationPresent(const ClassObject* clazz, const Field* method,
160 const ClassObject* annotationClazz);
163 * Find the default value for an annotation member.
165 Object* dvmGetAnnotationDefaultValue(const Method* method);
168 * Get the list of thrown exceptions for a method. Returns NULL if there
169 * are no exceptions listed.
171 ArrayObject* dvmGetMethodThrows(const Method* method);
174 * Get the Signature annotation.
176 ArrayObject* dvmGetClassSignatureAnnotation(const ClassObject* clazz);
177 ArrayObject* dvmGetMethodSignatureAnnotation(const Method* method);
178 ArrayObject* dvmGetFieldSignatureAnnotation(const Field* field);
181 * Get the EnclosingMethod attribute from an annotation. Returns a Method
184 Object* dvmGetEnclosingMethod(const ClassObject* clazz);
187 * Return clazz's declaring class, or NULL if there isn't one.
189 ClassObject* dvmGetDeclaringClass(const ClassObject* clazz);
192 * Return clazz's enclosing class, or NULL if there isn't one.
194 ClassObject* dvmGetEnclosingClass(const ClassObject* clazz);
197 * Get the EnclosingClass attribute from an annotation. If found, returns
198 * "true". A String with the original name of the class and the original
199 * access flags are returned through the arguments. (The name will be NULL
200 * for an anonymous inner class.)
202 bool dvmGetInnerClass(const ClassObject* clazz, StringObject** pName,
206 * Get an array of class objects from the MemberClasses annotation. Returns
207 * NULL if none found.
209 ArrayObject* dvmGetDeclaredClasses(const ClassObject* clazz);
212 * Used to pass values out of annotation (and encoded array) processing
215 struct AnnotationValue {
222 * Iterator structure for iterating over DexEncodedArray instances. The
223 * structure should be treated as opaque.
225 struct EncodedArrayIterator {
226 const u1* cursor; /* current cursor */
227 u4 elementsLeft; /* number of elements left to read */
228 const DexEncodedArray* encodedArray; /* instance being iterated over */
229 u4 size; /* number of elements in instance */
230 const ClassObject* clazz; /* class to resolve with respect to */
234 * Initializes an encoded array iterator.
236 * @param iterator iterator to initialize
237 * @param encodedArray encoded array to iterate over
238 * @param clazz class to use when resolving strings and types
240 void dvmEncodedArrayIteratorInitialize(EncodedArrayIterator* iterator,
241 const DexEncodedArray* encodedArray, const ClassObject* clazz);
244 * Returns whether there are more elements to be read.
246 bool dvmEncodedArrayIteratorHasNext(const EncodedArrayIterator* iterator);
249 * Returns the next decoded value from the iterator, advancing its
250 * cursor. This returns primitive values in their corresponding union
251 * slots, and returns everything else (including nulls) as object
252 * references in the "l" union slot.
254 * The caller must call dvmReleaseTrackedAlloc() on any returned reference.
256 * @param value pointer to store decoded value into
257 * @returns true if a value was decoded and the cursor advanced; false if
258 * the last value had already been decoded or if there was a problem decoding
260 bool dvmEncodedArrayIteratorGetNext(EncodedArrayIterator* iterator,
261 AnnotationValue* value);
263 #endif // DALVIK_REFLECT_REFLECT_H_