summaryrefslogtreecommitdiffstats
path: root/vm/reflect/Reflect.h
blob: 21bb08db71f567163a3df22aa3d065a28fa3d852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * Basic reflection calls and utility functions.
 */
#ifndef DALVIK_REFLECT_REFLECT_H_
#define DALVIK_REFLECT_REFLECT_H_

/*
 * During startup, validate the "box" classes, e.g. java/lang/Integer.
 */
bool dvmValidateBoxClasses();

/*
 * Get all fields declared by a class.
 *
 * Includes both class and instance fields.
 */
ArrayObject* dvmGetDeclaredFields(ClassObject* clazz, bool publicOnly);

/*
 * Get the named field.
 */
Object* dvmGetDeclaredField(ClassObject* clazz, StringObject* nameObj);

/*
 * Get all constructors declared by a class.
 */
ArrayObject* dvmGetDeclaredConstructors(ClassObject* clazz, bool publicOnly);

/*
 * Get all methods declared by a class.
 *
 * This includes both static and virtual methods, and can include private
 * members if "publicOnly" is false.  It does not include Miranda methods,
 * since those weren't declared in the class, or constructors.
 */
ArrayObject* dvmGetDeclaredMethods(ClassObject* clazz, bool publicOnly);

/*
 * Get the named method.
 */
Object* dvmGetDeclaredConstructorOrMethod(ClassObject* clazz,
    StringObject* nameObj, ArrayObject* args);

/*
 * Get all interfaces a class implements. If this is unable to allocate
 * the result array, this raises an OutOfMemoryError and returns NULL.
 */
ArrayObject* dvmGetInterfaces(ClassObject* clazz);

/*
 * Convert slot numbers back to objects.
 */
Field* dvmSlotToField(ClassObject* clazz, int slot);
Method* dvmSlotToMethod(ClassObject* clazz, int slot);

/*
 * Convert a primitive value, performing a widening conversion if necessary.
 */
int dvmConvertPrimitiveValue(PrimitiveType srcType,
    PrimitiveType dstType, const s4* srcPtr, s4* dstPtr);

/*
 * Convert the argument to the specified type.
 *
 * Returns the width of the argument (1 for most types, 2 for J/D, -1 on
 * error).
 */
int dvmConvertArgument(DataObject* arg, ClassObject* type, s4* ins);

/*
 * Box a primitive value into an object.  If "returnType" is
 * not primitive, this just returns "value" cast to an object.
 */
DataObject* dvmBoxPrimitive(JValue value, ClassObject* returnType);

/*
 * Unwrap a boxed primitive.  If "returnType" is not primitive, this just
 * returns "value" cast into a JValue.
 */
bool dvmUnboxPrimitive(Object* value, ClassObject* returnType,
    JValue* pResult);

/*
 * Return the class object that matches the method's signature.  For
 * primitive types, returns the box class.
 */
ClassObject* dvmGetBoxedReturnType(const Method* meth);

/*
 * JNI reflection support.
 */
Field* dvmGetFieldFromReflectObj(Object* obj);
Method* dvmGetMethodFromReflectObj(Object* obj);
Object* dvmCreateReflectObjForField(const ClassObject* clazz, Field* field);
Object* dvmCreateReflectObjForMethod(const ClassObject* clazz, Method* method);

/*
 * Quick test to determine if the method in question is a reflection call.
 * Used for some stack parsing.  Currently defined as "the method's declaring
 * class is java.lang.reflect.Method".
 */
INLINE bool dvmIsReflectionMethod(const Method* method)
{
    return (method->clazz == gDvm.classJavaLangReflectMethod);
}

/*
 * Proxy class generation.
 */
ClassObject* dvmGenerateProxyClass(StringObject* str, ArrayObject* interfaces,
    Object* loader);

/*
 * Create a new java.lang.reflect.Method object based on "meth".
 */
Object* dvmCreateReflectMethodObject(const Method* meth);

/*
 * Return an array of Annotation objects for the specified piece.  For method
 * parameters this is an array of arrays of Annotation objects.
 *
 * Method also applies to Constructor.
 */
ArrayObject* dvmGetClassAnnotations(const ClassObject* clazz);
ArrayObject* dvmGetMethodAnnotations(const Method* method);
ArrayObject* dvmGetFieldAnnotations(const Field* field);
ArrayObject* dvmGetParameterAnnotations(const Method* method);

/*
 * Return the annotation if it exists.
 */
Object* dvmGetClassAnnotation(const ClassObject* clazz, const ClassObject* annotationClazz);
Object* dvmGetMethodAnnotation(const ClassObject* clazz, const Method* method,
        const ClassObject* annotationClazz);
Object* dvmGetFieldAnnotation(const ClassObject* clazz, const Field* method,
        const ClassObject* annotationClazz);

/*
 * Return true if the annotation exists.
 */
bool dvmIsClassAnnotationPresent(const ClassObject* clazz, const ClassObject* annotationClazz);
bool dvmIsMethodAnnotationPresent(const ClassObject* clazz, const Method* method,
        const ClassObject* annotationClazz);
bool dvmIsFieldAnnotationPresent(const ClassObject* clazz, const Field* method,
        const ClassObject* annotationClazz);

/*
 * Find the default value for an annotation member.
 */
Object* dvmGetAnnotationDefaultValue(const Method* method);

/*
 * Get the list of thrown exceptions for a method.  Returns NULL if there
 * are no exceptions listed.
 */
ArrayObject* dvmGetMethodThrows(const Method* method);

/*
 * Get the Signature annotation.
 */
ArrayObject* dvmGetClassSignatureAnnotation(const ClassObject* clazz);
ArrayObject* dvmGetMethodSignatureAnnotation(const Method* method);
ArrayObject* dvmGetFieldSignatureAnnotation(const Field* field);

/*
 * Get the EnclosingMethod attribute from an annotation.  Returns a Method
 * object, or NULL.
 */
Object* dvmGetEnclosingMethod(const ClassObject* clazz);

/*
 * Return clazz's declaring class, or NULL if there isn't one.
 */
ClassObject* dvmGetDeclaringClass(const ClassObject* clazz);

/*
 * Return clazz's enclosing class, or NULL if there isn't one.
 */
ClassObject* dvmGetEnclosingClass(const ClassObject* clazz);

/*
 * Get the EnclosingClass attribute from an annotation.  If found, returns
 * "true".  A String with the original name of the class and the original
 * access flags are returned through the arguments.  (The name will be NULL
 * for an anonymous inner class.)
 */
bool dvmGetInnerClass(const ClassObject* clazz, StringObject** pName,
    int* pAccessFlags);

/*
 * Get an array of class objects from the MemberClasses annotation.  Returns
 * NULL if none found.
 */
ArrayObject* dvmGetDeclaredClasses(const ClassObject* clazz);

/*
 * Used to pass values out of annotation (and encoded array) processing
 * functions.
 */
struct AnnotationValue {
    JValue  value;
    u1      type;
};


/**
 * Iterator structure for iterating over DexEncodedArray instances. The
 * structure should be treated as opaque.
 */
struct EncodedArrayIterator {
    const u1* cursor;                    /* current cursor */
    u4 elementsLeft;                     /* number of elements left to read */
    const DexEncodedArray* encodedArray; /* instance being iterated over */
    u4 size;                             /* number of elements in instance */
    const ClassObject* clazz;            /* class to resolve with respect to */
};

/**
 * Initializes an encoded array iterator.
 *
 * @param iterator iterator to initialize
 * @param encodedArray encoded array to iterate over
 * @param clazz class to use when resolving strings and types
 */
void dvmEncodedArrayIteratorInitialize(EncodedArrayIterator* iterator,
        const DexEncodedArray* encodedArray, const ClassObject* clazz);

/**
 * Returns whether there are more elements to be read.
 */
bool dvmEncodedArrayIteratorHasNext(const EncodedArrayIterator* iterator);

/**
 * Returns the next decoded value from the iterator, advancing its
 * cursor. This returns primitive values in their corresponding union
 * slots, and returns everything else (including nulls) as object
 * references in the "l" union slot.
 *
 * The caller must call dvmReleaseTrackedAlloc() on any returned reference.
 *
 * @param value pointer to store decoded value into
 * @returns true if a value was decoded and the cursor advanced; false if
 * the last value had already been decoded or if there was a problem decoding
 */
bool dvmEncodedArrayIteratorGetNext(EncodedArrayIterator* iterator,
        AnnotationValue* value);

#endif  // DALVIK_REFLECT_REFLECT_H_