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.