Discussion:
guice-aop get mathods and parameters
周慎之
2018-10-29 16:00:19 UTC
Permalink
how to get mathods and parameters by guice-aop?
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/b227970b-1b53-453e-adc9-454da6c2378b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
周慎之
2018-10-30 12:02:45 UTC
Permalink
public class PageIntercepter implements MethodInterceptor {

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object[] args = invocation.getArguments();

//党路埄名
String classType =
invocation.getThis().getClass().getName();------------this line is
wrong,but idont know how to get classType-------------
Class<?> clazz = Class.forName(classType);
String clazzName = clazz.getName();
//获取方法名称
String methodName = invocation.getMethod().getName();

//获取参数名称和倌
Map<String,Object > nameAndArgs =
this.getFieldsName(this.getClass(), clazzName, methodName,args);
System.out.println("参数名和倌" + nameAndArgs.toString());

//劚态的获取PageSize
//Integer pageSize =Integer.parseInt(redisCli.get("pagesize"));

int pageSize = 50;
if(nameAndArgs !=null && nameAndArgs.size()>0){
Integer pagesize = (Integer) nameAndArgs.get("pagesize");
if(pagesize <= pageSize && pagesize >0){
//满足条件攟行
return invocation.proceed();
}
//䞍满足就䞍调甚方法
return null;
}
//无参数攟行
return invocation.proceed();
}

//需甚到javassit包
private Map getFieldsName(Class cls, String clazzName, String
methodName, Object[] args) throws NotFoundException {
Map map = new HashMap();
ClassPool pool = ClassPool.getDefault();
ClassClassPath classPath = new ClassClassPath(cls);
pool.insertClassPath(classPath);
CtClass cc = pool.get(clazzName);
System.out.println(cc);
//接口党路埄
CtMethod cm = cc.getDeclaredMethod(methodName);
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute)
codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
UMSLogger.info("*** PaginationHelper attr is null === ");
return null;
}
// String[] paramNames = new String[cm.getParameterTypes().length];
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i < cm.getParameterTypes().length; i++) {
//paramNames即参数名
map.put(attr.variableName(i + pos), args[i]);
}
return map;
}
}

圚 2018幎10月30日星期二 UTC+8䞊午12:00:19呚慎之写道
Post by 周慎之
how to get mathods and parameters by guice-aop?
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/1c2ab2b1-3ee4-410b-a7c6-e4d46cfdb946%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...