分为几个模块
1,class对象的认识
//待续
2,anno的认识:
首先初始化一些注解map,
static int initAnnotation() {
Class *enum_cls, *map_cls, *anno_inv_cls, *obj_ary_cls;
Class *anno_ary_cls, *dbl_anno_ary_cls;
enum_cls = findSystemClass("java/lang/Enum");
map_cls = findSystemClass("java/util/HashMap");
anno_inv_cls = findSystemClass("sun/reflect/annotation/Annotation"
"InvocationHandler");
obj_ary_cls = findArrayClass("[Ljava/lang/Object;");
anno_ary_cls = findArrayClass("[Ljava/lang/annotation/Annotation;");
dbl_anno_ary_cls = findArrayClass("[[Ljava/lang/annotation/Annotation;");
if(!enum_cls || !map_cls || !anno_inv_cls || !obj_ary_cls
|| !anno_ary_cls || !dbl_anno_ary_cls)
return FALSE;
map_init_mb = findMethod(map_cls, SYMBOL(object_init), SYMBOL(___V));
map_put_mb = findMethod(map_cls, SYMBOL(put),
newUtf8("(Ljava/lang/Object;Ljava/lang/Object;)"
"Ljava/lang/Object;"));
anno_create_mb = findMethod(anno_inv_cls, newUtf8("create"),
newUtf8("(Ljava/lang/Class;Ljava/util/Map;)"
"Ljava/lang/annotation/Annotation;"));
enum_valueof_mb = findMethod(enum_cls, newUtf8("valueOf"),
newUtf8("(Ljava/lang/Class;Ljava/lang/String;)"
"Ljava/lang/Enum;"));
if(!map_init_mb || !map_put_mb || !anno_create_mb || !enum_valueof_mb) {
/* FindMethod doesn't throw an exception... */
signalException(java_lang_InternalError,
"Expected field/method doesn't exist");
return FALSE;
}
registerStaticClassRefLocked(&enum_class, enum_cls);
registerStaticClassRefLocked(&map_class, map_cls);
registerStaticClassRefLocked(&anno_inv_class, anno_inv_cls);
registerStaticClassRefLocked(&obj_array_class, obj_ary_cls);
registerStaticClassRefLocked(&anno_array_class, anno_ary_cls);
registerStaticClassRefLocked(&dbl_anno_array_class, dbl_anno_ary_cls);
}
//解析方法,调用方法
Object *parseAnnotation(Class *class, u1 **data_ptr, int *data_len) {
ClassBlock *cb = CLASS_CB(class);
ConstantPool *cp = &cb->constant_pool;
Object *map, *anno;
int no_value_pairs;
Class *type_class;
int type_idx;
int i;
if((map = allocObject(map_class)) == NULL)
return NULL;
executeMethod(map, map_init_mb);
if(exceptionOccurred())
return NULL;
READ_TYPE_INDEX(type_idx, cp, CONSTANT_Utf8, *data_ptr, *data_len);
if((type_class = findClassFromSignature(CP_UTF8(cp, type_idx),
class)) == NULL)
return NULL;
READ_U2(no_value_pairs, *data_ptr, *data_len);
for(i = 0; i < no_value_pairs; i++) {
Object *element_name, *element_value;
int element_name_idx;
READ_TYPE_INDEX(element_name_idx, cp, CONSTANT_Utf8, *data_ptr,
*data_len);
element_name = createString(CP_UTF8(cp, element_name_idx));
element_value = parseElementValue(class, data_ptr, data_len);
if(element_name == NULL || element_value == NULL)
return NULL;
executeMethod(map, map_put_mb, element_name, element_value);
if(exceptionOccurred())
return NULL;
}
anno = *(Object**)executeStaticMethod(anno_inv_class, anno_create_mb,
type_class, map);
if(exceptionOccurred())
return NULL;
return anno;
}
其中 executeMethod(map, map_init_mb)调用executeMethodArgs(...),后者调用
executeMethodVaList(),
void *executeMethodArgs(Object *ob, Class *class, MethodBlock *mb, ...) {
va_list jargs;
void *ret;
va_start(jargs, mb);
ret = executeMethodVaList(ob, class, mb, jargs);
va_end(jargs);
return ret;
}
而executeMethodVaList实现方法的真正调用,
void *executeMethodVaList(Object *ob, Class *class, MethodBlock *mb,
va_list jargs) {
ExecEnv *ee = getExecEnv();
char *sig = mb->type;
uintptr_t *sp;
void *ret;
CREATE_TOP_FRAME(ee, class, mb, sp, ret);
/* copy args onto stack */
if(ob)
*sp++ = (uintptr_t) ob; /* push receiver first */
SCAN_SIG(sig, VA_DOUBLE(jargs, sp), VA_SINGLE(jargs, sp))
if(mb->access_flags & ACC_SYNCHRONIZED)
objectLock(ob ? ob : mb->class);
if(mb->access_flags & ACC_NATIVE)
(*mb->native_invoker)(class, mb, ret);
else
executeJava();
if(mb->access_flags & ACC_SYNCHRONIZED)
objectUnlock(ob ? ob : mb->class);
POP_TOP_FRAME(ee);
return ADJUST_RET_ADDR(ret, *sig);
}
上面的 (*mb->native_invoker)(class, mb, ret)实现方法调用,
struct methodblock {。。。。}有个NativeMethod native_invoker,其中:typedef uintptr_t *(*NativeMethod)(Class*, MethodBlock*, uintptr_t*);