Java Code Examples for org.aspectj.lang.reflect.MethodSignature#getName()
The following examples show how to use
org.aspectj.lang.reflect.MethodSignature#getName() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogAspect.java From WeBASE-Sign with Apache License 2.0 | 6 votes |
@Around("logPointCut()") public Object methodAround(ProceedingJoinPoint point) throws Throwable { Instant startTime = Instant.now(); Class targetClass = point.getTarget().getClass(); MethodSignature methodSignature = (MethodSignature) point.getSignature(); String methodName = methodSignature.getName(); Object[] args = point.getArgs(); Logger logger = LoggerManager.getLogger(targetClass); // log args of param in controller // if args contains BindingResult(recursive of request entity and itself), stack over flow logger.debug("startTime:{} methodName:{} args:{}", startTime, methodName, JsonUtils.toJSONString(this.excludeBindingResult(args))); Object result = null; try { result = point.proceed(); } catch (Throwable throwable) { logger.warn("fail request. methodName:{} ", methodName, throwable); throw throwable; } String resultStr = Optional.ofNullable(result).map(JsonUtils::toJSONString).orElse(null); logger.debug("methodName:{} usedTime:{} result:{}", methodName, Duration.between(startTime, Instant.now()), resultStr); return result; }
Example 2
Source File: SimplePerformanceMonitor.java From spring-cloud-study with Apache License 2.0 | 6 votes |
@Around("logTime()") public Object timeAround(ProceedingJoinPoint joinPoint) { // 定义返回对象、得到方法需要的参数 Object obj = null; Object[] args = joinPoint.getArgs(); long startTime = System.currentTimeMillis(); try { obj = joinPoint.proceed(args); } catch (Throwable e) { e.printStackTrace(); log.error("统计某方法执行耗时环绕通知出错", e); } // 获取执行的方法名 long endTime = System.currentTimeMillis(); MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = signature.getDeclaringTypeName() + "." + signature.getName(); // 打印耗时的信息 log.info(String.format("「%s」执行时间为:%d ms",methodName, endTime - startTime)); return obj; }
Example 3
Source File: SimplePerformanceMonitor.java From spring-cloud-study with Apache License 2.0 | 6 votes |
@Around("logTime()") public Object timeAround(ProceedingJoinPoint joinPoint) { // 定义返回对象、得到方法需要的参数 Object obj = null; Object[] args = joinPoint.getArgs(); long startTime = System.currentTimeMillis(); try { obj = joinPoint.proceed(args); } catch (Throwable e) { e.printStackTrace(); log.error("统计某方法执行耗时环绕通知出错", e); } // 获取执行的方法名 long endTime = System.currentTimeMillis(); MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = signature.getDeclaringTypeName() + "." + signature.getName(); // 打印耗时的信息 log.info(String.format("「%s」执行时间为:%d ms",methodName, endTime - startTime)); return obj; }
Example 4
Source File: TimeInterceptor.java From newblog with Apache License 2.0 | 6 votes |
/** * 环绕通知 * 统计方法执行耗时Around * * @param joinPoint * @return * @throws Throwable */ @Around("execution(* (com.myblog.service.impl.*+&&!com.myblog.service.impl.AsyncServiceImpl).*(..))") public Object timeAround(ProceedingJoinPoint joinPoint) throws Throwable { // 定义返回对象、得到方法需要的参数 Object obj = null; Object[] args = joinPoint.getArgs(); long startTime = System.currentTimeMillis(); // try { obj = joinPoint.proceed(args); // } catch (Throwable e) { // logger.error("统计某方法执行耗时环绕通知出错", e); // } // 获取执行的方法名 long endTime = System.currentTimeMillis(); MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = signature.getDeclaringTypeName() + "." + signature.getName(); // 打印耗时的信息 this.printExecTime(methodName, startTime, endTime); return obj; }
Example 5
Source File: TimeLogAspect.java From Aurora with Apache License 2.0 | 6 votes |
@Around("methodAnnotated() || constructorAnnotated()")//在连接点进行方法替换 public Object aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Timber.i(methodSignature.getMethod().getDeclaringClass().getCanonicalName()); String className = methodSignature.getDeclaringType().getSimpleName(); String methodName = methodSignature.getName(); long startTime = System.nanoTime(); Object result = joinPoint.proceed();//执行原方法 StringBuilder keyBuilder = new StringBuilder(); keyBuilder.append(methodName + ":"); for (Object obj : joinPoint.getArgs()) { if (obj instanceof String) keyBuilder.append((String) obj); else if (obj instanceof Class) keyBuilder.append(((Class) obj).getSimpleName()); } String key = keyBuilder.toString(); Timber.i((className + "." + key + joinPoint.getArgs().toString() + " --->:" + "[" + (TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)) + "ms]"));// 打印时间差 return result; }
Example 6
Source File: OperationLogAspect.java From Shiro-Action with MIT License | 5 votes |
private void saveLog(ProceedingJoinPoint joinPoint, long time) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SysLog sysLog = new SysLog(); // 获取注解上的操作描述 OperationLog operationLogAnnotation = method.getAnnotation(OperationLog.class); if (operationLogAnnotation != null) { sysLog.setOperation(operationLogAnnotation.value()); } // 请求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); sysLog.setMethod(className + "." + methodName + "()"); // 请求的方法参数 Object[] args = joinPoint.getArgs(); LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); String[] paramNames = parameterNameDiscoverer.getParameterNames(method); if (args != null && paramNames != null) { StringBuilder params = new StringBuilder(); for (int i = 0; i < args.length; i++) { params.append(" ").append(paramNames[i]).append(": ").append(args[i]); } sysLog.setParams(params.toString()); } sysLog.setIp(IPUtils.getIpAddr()); // 获取当前登录用户名 if (SecurityUtils.getSubject().isAuthenticated()) { User user = ShiroUtil.getCurrentUser(); sysLog.setUsername(user.getUsername()); } sysLog.setTime((int) time); sysLogMapper.insert(sysLog); }
Example 7
Source File: ServiceLogAspect.java From spring-boot-api-project-seed with Apache License 2.0 | 5 votes |
/** * 环绕通知方法 * @param joinPoint 连接点 * @return 切入点返回值 * @throws Throwable 异常信息 */ @Around("@within(com.company.project.common.annotations.ServiceLog) ||@annotation(com.company.project.common.annotations.ServiceLog)") public Object doServiceLog(ProceedingJoinPoint joinPoint) throws Throwable{ MethodSignature signature = (MethodSignature) joinPoint.getSignature(); // 拦截的实体类 Object target = joinPoint.getTarget(); // 拦截的方法名称 String methodName = signature.getName(); // 拦截的放参数类型 Class[] parameterTypes = signature.getMethod().getParameterTypes(); Method method = target.getClass().getMethod(methodName, parameterTypes); if(null == method){ return joinPoint.proceed(); } // 判断是否包含自定义的注解 if (!method.isAnnotationPresent(ServiceLog.class)) { return joinPoint.proceed(); } String methodParams = LogAspectUtil.getMethodParams(joinPoint); logger.info("开始请求方法:[{}] 参数:[{}]", methodName, methodParams); long start = System.currentTimeMillis(); Object result = joinPoint.proceed(); long end = System.currentTimeMillis(); String deleteSensitiveContent = LogAspectUtil.deleteSensitiveContent(result); logger.info("结束请求方法:[{}] 参数:[{}] 返回结果[{}] 耗时:[{}]毫秒 ", methodName, methodParams, deleteSensitiveContent, end - start); return result; }
Example 8
Source File: ControllerAspect.java From SuperBoot with MIT License | 5 votes |
/** * 根据注解判断接口是否限定了特殊访问的角色,配置级别方法级大于类级 * * @param methodSignature 切面对象 * @param token 用户信息 * @return */ public boolean checkAuth(MethodSignature methodSignature, BaseToken token) { //判断方法是否需要权限认证 boolean success = false; AuthRoles cAuth = methodSignature.getMethod().getAnnotation(AuthRoles.class); if (null != cAuth) { success = checkAuthRole(cAuth, token); if (success) { return success; } } //方法校验未通过校验类 if (!success && null == cAuth) { //判断类里所有方法是否全部需要权限认证 cAuth = (AuthRoles) methodSignature.getDeclaringType().getAnnotation(AuthRoles.class); if (null != cAuth) { success = checkAuthRole(cAuth, token); if (success) { return success; } } } //类校验不通过校验授权 if (!success) { //判断用户分配的菜单关联的权限信息 String key = module_id + methodSignature.getDeclaringTypeName() + methodSignature.getName(); //判断用户是否有执行的权限 if (null == redisUtils.getUserResource(token.getUserId())) { throw new BaseException(StatusCode.UNAUTHORIZED); } return redisUtils.getUserResource(token.getUserId()).containsKey(key); } return success; }
Example 9
Source File: CheckAllowedMethodAdvice.java From herd with Apache License 2.0 | 5 votes |
/** * Checks whether the requested operation is permitted. * * @param pjp the join point. * * @return the return value of the method at the join point. * @throws Throwable if any errors were encountered. */ @SuppressWarnings("rawtypes") public Object checkNotAllowedMethods(ProceedingJoinPoint pjp) throws Throwable { // Get the method name being invoked. Class targetClass = pjp.getTarget().getClass(); MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature(); String methodName = targetClass.getName() + "." + targetMethodSignature.getName(); configurationDaoHelper.checkNotAllowedMethod(methodName); return pjp.proceed(); }
Example 10
Source File: LogAspect.java From SpringAll with MIT License | 5 votes |
private void saveLog(ProceedingJoinPoint joinPoint, long time) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SysLog sysLog = new SysLog(); Log logAnnotation = method.getAnnotation(Log.class); if (logAnnotation != null) { // 注解上的描述 sysLog.setOperation(logAnnotation.value()); } // 请求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); sysLog.setMethod(className + "." + methodName + "()"); // 请求的方法参数值 Object[] args = joinPoint.getArgs(); // 请求的方法参数名称 LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer(); String[] paramNames = u.getParameterNames(method); if (args != null && paramNames != null) { String params = ""; for (int i = 0; i < args.length; i++) { params += " " + paramNames[i] + ": " + args[i]; } sysLog.setParams(params); } // 获取request HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); // 设置IP地址 sysLog.setIp(IPUtils.getIpAddr(request)); // 模拟一个用户名 sysLog.setUsername("mrbird"); sysLog.setTime((int) time); Date date = new Date(); sysLog.setCreateTime(date); // 保存系统日志 sysLogDao.saveSysLog(sysLog); }
Example 11
Source File: TraceAspect.java From AndroidPlayground with MIT License | 5 votes |
@Around("methodToTrace() || constructorToTrace()") public Object trace(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String className = signature.getDeclaringType().getSimpleName(); String methodName = signature.getName(); long start = System.nanoTime(); Object result = joinPoint.proceed(); long stop = System.nanoTime(); Log.d(className + "::" + methodName, "" + (stop - start)); return result; }
Example 12
Source File: TimerAspect.java From jigsaw-payment with Apache License 2.0 | 5 votes |
private Timer getTimerAnnotation(ProceedingJoinPoint joinPoint) throws NoSuchMethodException { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Timer timer = method.getAnnotation(Timer.class); if (timer == null && method.getDeclaringClass().isInterface()) { final String methodName = signature.getName(); final Class<?> implementationClass = joinPoint.getTarget() .getClass(); final Method implementationMethod = implementationClass .getDeclaredMethod(methodName, method.getParameterTypes()); timer = implementationMethod.getAnnotation(Timer.class); } return timer; }
Example 13
Source File: SysLogAspect.java From renren-fast with GNU General Public License v3.0 | 5 votes |
@Before("logPointCut()") public void saveSysLog(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SysLogEntity sysLog = new SysLogEntity(); SysLog syslog = method.getAnnotation(SysLog.class); if(syslog != null){ //注解上的描述 sysLog.setOperation(syslog.value()); } //请求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); sysLog.setMethod(className + "." + methodName + "()"); //请求的参数 Object[] args = joinPoint.getArgs(); String params = new Gson().toJson(args[0]); sysLog.setParams(params); //获取request HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); //设置IP地址 sysLog.setIp(IPUtils.getIpAddr(request)); //用户名 String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername(); sysLog.setUsername(username); sysLog.setCreateDate(new Date()); //保存系统日志 sysLogService.save(sysLog); }
Example 14
Source File: SysLogAspect.java From sdb-mall with Apache License 2.0 | 5 votes |
private void saveSysLog(ProceedingJoinPoint joinPoint, long time) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); io.sdb.model.SysLog sysLog = new io.sdb.model.SysLog(); SysLog syslog = method.getAnnotation(SysLog.class); if(syslog != null){ //注解上的描述 sysLog.setOperation(syslog.value()); } //请求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); sysLog.setMethod(className + "." + methodName + "()"); //请求的参数 Object[] args = joinPoint.getArgs(); try{ String params = new Gson().toJson(args[0]); sysLog.setParams(params); }catch (Exception e){ } //获取request HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); //设置IP地址 sysLog.setIp(IPUtils.getIpAddr(request)); //用户名 String username = ((SysUser) SecurityUtils.getSubject().getPrincipal()).getUsername(); sysLog.setUsername(username); sysLog.setTime(time); sysLog.setCreateDate(new Date()); //保存系统日志 sysLog.save(); }
Example 15
Source File: TraceAspect.java From AspectJDemo with Apache License 2.0 | 5 votes |
/** * 截获原方法,并替换 * @param joinPoint * @return * @throws Throwable */ @Around("methodAnnotated()") public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable { if (currentObject == null){ currentObject = joinPoint.getTarget(); } //初始化计时器 final StopWatch stopWatch = new StopWatch(); //开始监听 stopWatch.start(); //调用原方法的执行。 Object result = joinPoint.proceed(); //监听结束 stopWatch.stop(); //获取方法信息对象 MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); String className; //获取当前对象,通过反射获取类别详细信息 className = joinPoint.getThis().getClass().getName(); String methodName = methodSignature.getName(); String msg = buildLogMessage(methodName, stopWatch.getTotalTime(1)); if (currentObject != null && currentObject.equals(joinPoint.getTarget())){ DebugLog.log(new MethodMsg(className,msg,stopWatch.getTotalTime(1))); }else if(currentObject != null && !currentObject.equals(joinPoint.getTarget())){ DebugLog.log(new MethodMsg(className, msg,stopWatch.getTotalTime(1))); Log.e(className,msg); currentObject = joinPoint.getTarget(); // DebugLog.outPut(new Path()); //日志存储 // DebugLog.ReadIn(new Path()); //日志读取 } return result; }
Example 16
Source File: AutoLogAspect.java From jeecg-boot-with-activiti with MIT License | 4 votes |
private void saveSysLog(ProceedingJoinPoint joinPoint, long time) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SysLog sysLog = new SysLog(); AutoLog syslog = method.getAnnotation(AutoLog.class); if(syslog != null){ //注解上的描述,操作日志内容 sysLog.setLogContent(syslog.value()); sysLog.setLogType(syslog.logType()); } //请求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); sysLog.setMethod(className + "." + methodName + "()"); //设置操作类型 if (sysLog.getLogType() == CommonConstant.LOG_TYPE_2) { sysLog.setOperateType(getOperateType(methodName, syslog.operateType())); } //请求的参数 Object[] args = joinPoint.getArgs(); try{ String params = JSONObject.toJSONString(args); sysLog.setRequestParam(params); }catch (Exception e){ } //获取request HttpServletRequest request = SpringContextUtils.getHttpServletRequest(); //设置IP地址 sysLog.setIp(IPUtils.getIpAddr(request)); //获取登录用户信息 LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal(); if(sysUser!=null){ sysLog.setUserid(sysUser.getUsername()); sysLog.setUsername(sysUser.getRealname()); } //耗时 sysLog.setCostTime(time); sysLog.setCreateTime(new Date()); //保存系统日志 sysLogService.save(sysLog); }
Example 17
Source File: AutoLogAspect.java From jeecg-cloud with Apache License 2.0 | 4 votes |
private void saveSysLog(ProceedingJoinPoint joinPoint, long time) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SysLog sysLog = new SysLog(); AutoLog syslog = method.getAnnotation(AutoLog.class); if(syslog != null){ //注解上的描述,操作日志内容 sysLog.setLogContent(syslog.value()); sysLog.setLogType(syslog.logType()); } //请求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); sysLog.setMethod(className + "." + methodName + "()"); //设置操作类型 if (sysLog.getLogType() == CommonConstant.LOG_TYPE_2) { sysLog.setOperateType(getOperateType(methodName, syslog.operateType())); } //获取request HttpServletRequest request = SpringContextUtils.getHttpServletRequest(); //请求的参数 sysLog.setRequestParam(getReqestParams(request,joinPoint)); //设置IP地址 sysLog.setIp(IPUtils.getIpAddr(request)); //获取登录用户信息 LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal(); if(sysUser!=null){ sysLog.setUserid(sysUser.getUsername()); sysLog.setUsername(sysUser.getRealname()); } //耗时 sysLog.setCostTime(time); sysLog.setCreateTime(new Date()); //保存系统日志 JSONObject jsonObject = (JSONObject) JSONObject.toJSON(sysLog); sysBaseRemoteApi.saveSysLog(jsonObject); }
Example 18
Source File: SysLogAspect.java From kitty with GNU Lesser General Public License v3.0 | 4 votes |
private void saveSysLog(ProceedingJoinPoint joinPoint, long time) { if(ShiroUtils.getUser() == null) { return ; } String userName = ShiroUtils.getUser().getName(); if(joinPoint.getTarget() instanceof SysLogService) { return ; } MethodSignature signature = (MethodSignature) joinPoint.getSignature(); SysLog sysLog = new SysLog(); // Method method = signature.getMethod(); // com.louis.merak.admin.annotation.SysLog syslogAnno = method.getAnnotation(com.louis.merak.admin.annotation.SysLog.class); // if(syslogAnno != null){ // //注解上的描述 // sysLog.setOperation(syslogAnno.value()); // } // 请求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); sysLog.setMethod(className + "." + methodName + "()"); // 请求的参数 Object[] args = joinPoint.getArgs(); try{ String params = JSONObject.toJSONString(args[0]); if(params.length() > 200) { params = params.substring(0, 200) + "..."; } sysLog.setParams(params); } catch (Exception e){ } // 获取request HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); // 设置IP地址 sysLog.setIp(IPUtils.getIpAddr(request)); // 用户名 sysLog.setUserName(userName); // 执行时长(毫秒) sysLog.setTime(time); // 保存系统日志 sysLogService.save(sysLog); }
Example 19
Source File: AutoLogAspect.java From jeecg-boot with Apache License 2.0 | 4 votes |
private void saveSysLog(ProceedingJoinPoint joinPoint, long time) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SysLog sysLog = new SysLog(); AutoLog syslog = method.getAnnotation(AutoLog.class); if(syslog != null){ //注解上的描述,操作日志内容 sysLog.setLogContent(syslog.value()); sysLog.setLogType(syslog.logType()); } //请求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); sysLog.setMethod(className + "." + methodName + "()"); //设置操作类型 if (sysLog.getLogType() == CommonConstant.LOG_TYPE_2) { sysLog.setOperateType(getOperateType(methodName, syslog.operateType())); } //获取request HttpServletRequest request = SpringContextUtils.getHttpServletRequest(); //请求的参数 sysLog.setRequestParam(getReqestParams(request,joinPoint)); //设置IP地址 sysLog.setIp(IPUtils.getIpAddr(request)); //获取登录用户信息 LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal(); if(sysUser!=null){ sysLog.setUserid(sysUser.getUsername()); sysLog.setUsername(sysUser.getRealname()); } //耗时 sysLog.setCostTime(time); sysLog.setCreateTime(new Date()); //保存系统日志 sysLogService.save(sysLog); }
Example 20
Source File: TracePerformanceAspect.java From batch-processing-large-datasets-spring with MIT License | 4 votes |
@Around ("execution(* com.techshard..*.*(..)))") public Object logTracePerformanceAspect(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); //Get intercepted method details String className = methodSignature.getDeclaringType().getSimpleName(); String methodName = methodSignature.getName(); long start = System.currentTimeMillis(); Object result = joinPoint.proceed(); long end = System.currentTimeMillis(); //Log method execution time logger.info("Execution time of " + className + "." + methodName + " :: " + (end - start) + " ms"); return result; }