public static AnnotationNode getAnnotation(ClassNode m,Class<? extends Annotation> annotationType) { AnnotationNode a = getAnnotation(m.visibleAnnotations, annotationType); if(null == a){ a = getAnnotation(m.invisibleAnnotations, annotationType); } return a; }
protected static boolean isMonitored(MethodNode mn) { if(ASM.isAnnotationPresent(mn, Monitored.class)) { return true; } if(ASM.isConstructor(mn) || ASM.isStaticInit(mn)) { return false; } if(Modifier.isStatic(mn.access)) { return false; } String name = mn.name; if(name.startsWith("get") && name.length() > 3 && !ASM.hasArgument(mn)) { return false; } if(name.startsWith("is") && name.length() > 2 && !ASM.hasArgument(mn)) { return false; } if(name.startsWith("set") && name.length() > 3 && ASM.getArgumentSize(mn) == 1) { return false; } return true; }
protected MonitoredMethodVisitor(Type type, MethodNode mn, MethodVisitor mv, int access, String name, String desc) { super(ASM.API, mv, access, name, desc); this.type = type; this.mn = mn; this.argumentTypes = ASM.getArgumentTypes(mn); }
Type[] argumentTypes = ASM.getArgumentTypes(method); mv = ASM.visitMethod(cw, method); mg = new GeneratorAdapter(mv, method.access, method.name, method.desc); if(!ASM.isStatic(method)){ mg.loadThis(); if(ASM.isStatic(method)){ mg.visitMethodInsn(Opcodes.INVOKESTATIC, MODEL_CLASS_NAME, method.name, method.desc,false); }else{ if(ASM.hasReturnValue(method)){ returnValueIndex = mg.newLocal(ASM.getReturnType(method)); mg.storeLocal(returnValueIndex);
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodNode mn = ASM.getMethod(cn, name, desc); AnnotationNode a = ASM.getAnnotation(mn, Transactional.class); if(null != a) { log.trace(" #transactional method : {}", name); MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); return new TxMethodVisitor(a, mv , access, name, desc); } return super.visitMethod(access, name, desc, signature, exceptions); }
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodNode mn = ASM.getMethod(cn, name, desc); if(!methodBodyOnly && ASM.isStaticInit(mn)) { visitStaticInit = true; MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); return new ClinitMethodVisitor(mv, access, name, desc); } if(isMonitored(mn)) { MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); return new MonitoredMethodVisitor(type, mn, mv , access, name, desc); } return super.visitMethod(access, name, desc, signature, exceptions); }
ClassNode cn = ASM.getClassNode(cr); ic.setBeanDeclared(ASM.isAnnotationPresent(cn, Bean.class));
boolean hasReturnValue = ASM.hasReturnValue(m); Type[] argumentTypes = am.argTypes; mv.checkCast(ASM.getReturnType(m));
@Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { return ASM.isAnnotationPresent(m, annotationType); }
AnnotationNode a = ASM.getAnnotation(cn, Monitored.class); if(null != a) { isMonitorDeclared = true; Map<String,Object> params = ASM.getAnnotationValues(a); if(!params.isEmpty()) { Boolean value = (Boolean)params.get("value");
public static boolean hasArgument(MethodNode m){ return getArgumentSize(m) > 0; }
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodNode method = ASM.getMethod(cn, name, desc); MethodVisitor mv; for(ModelTransformer transformer : transformers){ if((mv = transformer.transform(cn, cw, method)) != null){ return mv; } } return super.visitMethod(access, name, desc, signature, exceptions); } }
protected void instrument(AppInstrumentContext context, AppInstrumentClass ic, ClassReader cr, ClassWriter cw) { ClassNode cn = ASM.getClassNode(cr); transformClass(cr,cw, cn); instrumentDelegates(cr,cw); cw.visitEnd(); byte[] data = cw.toByteArray(); context.updateInstrumented(ic, this, data, false); }
if(ASM.isStatic(mn)) { continue;
protected TxMethodVisitor(AnnotationNode a, MethodVisitor mv, int access, String name, String desc) { super(ASM.API, mv, access, name, desc); Map<String,Object> values = ASM.getAnnotationValues(a); if(!values.isEmpty()) { propagation = (TransactionDefinition.Propagation) values.get("propagation"); }else{ propagation = TransactionDefinition.Propagation.REQUIRED; } }
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodNode mn = ASM.getMethod(cn, name, desc); if(ASM.isStaticInit(mn)) { visitStaticInit = true; MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); return new ClinitMethodVisitor(mv, access, name, desc); } AopMethod am = isIntercepted(mn); if(null != am) { String newName = name + "$aop"; visitInterceptedMethod(am, newName); return super.visitMethod(access, newName, desc, signature, exceptions); }else{ return super.visitMethod(access, name, desc, signature, exceptions); } }
@Override protected void processClass(AppInstrumentContext context, AppInstrumentClass ic, ClassInfo ci, boolean methodBodyOnly) { if(ASM.isAnnotationPresent(ci.cn, NonEntity.class)){ return; } boolean isModel = false; String superName = ci.cr.getSuperName(); if(MODEL_CLASS_NAME.equals(superName)){ isModel = true; }else if(!superName.equals("java/lang/Object")){ for(;;){ superName = readSuperName(ci.rs, superName); if(null == superName || superName.equals("java/lang/Object")){ break; } if(superName.equals(MODEL_CLASS_NAME)){ isModel = true; break; } } } if(isModel){ ClassWriter cw = new ClassWriter(ci.cr,ClassWriter.COMPUTE_FRAMES); instrument(context, ic, ci.cr, cw); } }
@Override protected void processClass(AppInstrumentContext context, AppInstrumentClass ic, ClassInfo ci, boolean methodBodyOnly) { ClassNode cn = ci.cn; if(null != cn.methods) { boolean hasTransactionalMethods = false; for(MethodNode mn : cn.methods) { if(ASM.isAnnotationPresent(mn, Transactional.class)) { hasTransactionalMethods = true; break; } } if(hasTransactionalMethods) { log.debug("Instrument Transactional class : {}", ic.getClassName()); Try.throwUnchecked(() -> { try(InputStream in = ci.is.getInputStream()) { context.updateInstrumented(ic, this, instrumentClass(ci.cn, new ClassReader(in), true), true); } }); } } }
public static AnnotationNode getAnnotation(MethodNode m,Class<? extends Annotation> annotationType) { AnnotationNode a = getAnnotation(m.visibleAnnotations, annotationType); if(null == a){ a = getAnnotation(m.invisibleAnnotations, annotationType); } return a; }
private void init() { int count = 1; for(int i=1;i<=methods.size();i++) { AopMethod am = methods.get(i-1); am.field = METHOD_FIELD_PREFIX + i; am.argTypes = ASM.getArgumentTypes(am.getMethod()); for(MethodInterceptorConfig interceptor : am.interceptors) { if(!Strings.isEmpty(interceptor.getBeanName())) { if(!nameInterceptorFields.containsKey(interceptor.getBeanName())) { nameInterceptorFields.put(interceptor.getBeanName(), INTERCEPTOR_FIELD_PREFIX + count); count++; } }else{ if(!classInterceptorFields.containsKey(interceptor.getClassName())) { classInterceptorFields.put(interceptor.getClassName(), INTERCEPTOR_FIELD_PREFIX + count); count++; } } } } }