在上文中主要概括介绍了自定义注解的实现原理,本文将主要介绍通过getAnnotation()使用动态代理生成注解对应代理类的源码实现过程。
cls.getAnnotation(注解.class);
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
| private static Annotation parseAnnotation2(ByteBuffer var0, ConstantPool var1, Class<?> var2, boolean var3, Class<? extends Annotation>[] var4) { int var5 = var0.getShort() & '\uffff'; Class var6 = null; String var7 = "[unknown]";
try { try { var7 = var1.getUTF8At(var5); var6 = parseSig(var7, var2); } catch (IllegalArgumentException var18) { var6 = var1.getClassAt(var5); } } catch (NoClassDefFoundError var19) { if (var3) { throw new TypeNotPresentException(var7, var19); }
skipAnnotation(var0, false); return null; } catch (TypeNotPresentException var20) { if (var3) { throw var20; }
skipAnnotation(var0, false); return null; }
if (var4 != null && !contains(var4, var6)) { skipAnnotation(var0, false); return null; } else { AnnotationType var8 = null;
try { var8 = AnnotationType.getInstance(var6); } catch (IllegalArgumentException var17) { skipAnnotation(var0, false); return null; }
Map var9 = var8.memberTypes(); LinkedHashMap var10 = new LinkedHashMap(var8.memberDefaults()); int var11 = var0.getShort() & '\uffff';
for(int var12 = 0; var12 < var11; ++var12) { int var13 = var0.getShort() & '\uffff'; String var14 = var1.getUTF8At(var13); Class var15 = (Class)var9.get(var14); if (var15 == null) { skipMemberValue(var0); } else { Object var16 = parseMemberValue(var15, var0, var1, var2); if (var16 instanceof AnnotationTypeMismatchExceptionProxy) { ((AnnotationTypeMismatchExceptionProxy)var16).setMember((Method)var8.members().get(var14)); }
var10.put(var14, var16); } }
return annotationForMap(var6, var10); } }
|
cretateAnnotationData()函数:
declaredAnnnotations用来存储所有的注解
class和ClassLoader