org.aspectj.lang.reflect.CodeSignature Java Examples

The following examples show how to use org.aspectj.lang.reflect.CodeSignature. 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: DebugLogAspect.java    From AndroidProject with Apache License 2.0 6 votes vote down vote up
/**
 * 方法执行前切入
 */
private void enterMethod(ProceedingJoinPoint joinPoint, DebugLog debugLog) {
    if (!AppConfig.isDebug()) {
        return;
    }

    CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();

    // 方法所在类
    String className = codeSignature.getDeclaringType().getName();
    // 方法名
    String methodName = codeSignature.getName();
    // 方法参数名集合
    String[] parameterNames = codeSignature.getParameterNames();
    // 方法参数集合
    Object[] parameterValues = joinPoint.getArgs();

    //记录并打印方法的信息
    StringBuilder builder = getMethodLogInfo(className, methodName, parameterNames, parameterValues);

    log(debugLog.value(), builder.toString());

    final String section = builder.toString().substring(2);
    Trace.beginSection(section);
}
 
Example #2
Source File: ControllerLogContextAspects.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Before("com.sequenceiq.redbeams.aspect.ControllerLogContextAspects.interceptControllerMethodCalls()")
public void buildLogContextForControllerCalls(JoinPoint joinPoint) {

    LOGGER.debug("Intercepted controller method {}", joinPoint.toShortString());

    try {
        Object[] args = joinPoint.getArgs();
        CodeSignature sig = (CodeSignature) joinPoint.getSignature();
        String[] paramNames = sig.getParameterNames();

        // FIXME Once DatabaseConfigController is changed to use environmentCrn, this param name patching can be removed
        // This does not help getting the environment CRN from a request object anyway
        for (int i = 0; i < paramNames.length; i++) {
            if (paramNames[i].equalsIgnoreCase("environmentid")) {
                paramNames[i] = "environmentCrn";
            }
        }
        logContextService.buildMDCParams(joinPoint.getTarget(), paramNames, args);

        LOGGER.debug("A controller method has been intercepted: {} with params {}, {}, MDC logger context is built.", joinPoint.toShortString(),
                sig.getParameterNames(), AnonymizerUtil.anonymize(Arrays.toString(args)));
    } catch (Exception e) {
        LOGGER.warn("Failed to add controller method parameters to MDC, continuing with call", e);
    }
}
 
Example #3
Source File: Utils.java    From automon with Apache License 2.0 5 votes vote down vote up
private static Object[] getParameterNames(Object[] argValues, JoinPoint jp) {
    Signature signature = jp.getSignature();
    if (signature instanceof CodeSignature) {
        return ((CodeSignature) signature).getParameterNames();
    } else {
        return new Object[argValues.length];
    }
}
 
Example #4
Source File: UtilsTest.java    From automon with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgNameValuePairs_ArgValueAndArgName() throws Exception {
    JoinPoint jp = mock(JoinPoint.class);
    CodeSignature signature = mock(CodeSignature.class);
    when(jp.getSignature()).thenReturn(signature);
    when(signature.getParameterNames()).thenReturn(new String[]{"firstName"});
    when(jp.getArgs()).thenReturn(new Object[]{"Steve"});
    assertThat(Utils.getArgNameValuePairs(jp)).containsExactly("firstName: Steve");
}
 
Example #5
Source File: UtilsTest.java    From automon with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgNameValuePairs_ArgValueAndArgName_multiple() throws Exception {
    JoinPoint jp = mock(JoinPoint.class);
    CodeSignature signature = mock(CodeSignature.class);
    when(jp.getSignature()).thenReturn(signature);
    when(signature.getParameterNames()).thenReturn(new String[]{"firstName", "number"});
    when(jp.getArgs()).thenReturn(new Object[]{"Steve", 20});
    assertThat(Utils.getArgNameValuePairs(jp)).containsExactly("firstName: Steve", "number: 20");
}
 
Example #6
Source File: ControllerLogContextAspects.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Before("com.sequenceiq.datalake.logger.ControllerLogContextAspects.interceptControllerMethodCalls()")
public void buildLogContextForControllerCalls(JoinPoint joinPoint) {
    try {
        Object[] args = joinPoint.getArgs();
        CodeSignature sig = (CodeSignature) joinPoint.getSignature();
        String[] paramNames = sig.getParameterNames();
        logContextService.buildMDCParams(joinPoint.getTarget(), paramNames, args);
        MDCBuilder.addRequestId(MDCBuilder.getMdcContextMap().get(LoggerContextKey.REQUEST_ID.toString()));
        LOGGER.debug("A controller method has been intercepted: {} with params {}, {}, MDC logger context is built.", joinPoint.toShortString(),
                sig.getParameterNames(), AnonymizerUtil.anonymize(Arrays.toString(args)));
    } catch (Exception any) {
        LOGGER.warn("MDCContext build failed: ", any);
    }
}
 
Example #7
Source File: ControllerLogContextAspects.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Before("com.sequenceiq.freeipa.logger.ControllerLogContextAspects.interceptControllerMethodCalls()")
public void buildLogContextForControllerCalls(JoinPoint joinPoint) {
    try {
        Object[] args = joinPoint.getArgs();
        CodeSignature sig = (CodeSignature) joinPoint.getSignature();
        String[] paramNames = sig.getParameterNames();
        logContextService.buildMDCParams(joinPoint.getTarget(), paramNames, args);
        MDCBuilder.addRequestId(MDCBuilder.getMdcContextMap().get(LoggerContextKey.REQUEST_ID.toString()));
        LOGGER.debug("A controller method has been intercepted: {} with params {}, {}, MDC logger context is built.", joinPoint.toShortString(),
                sig.getParameterNames(), AnonymizerUtil.anonymize(Arrays.toString(args)));
    } catch (Exception any) {
        LOGGER.warn("MDCContext build failed: ", any);
    }
}
 
Example #8
Source File: ControllerLogContextAspects.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Before("com.sequenceiq.environment.logger.ControllerLogContextAspects.interceptControllerMethodCalls()")
public void buildLogContextForControllerCalls(JoinPoint joinPoint) {
    try {
        Object[] args = joinPoint.getArgs();
        CodeSignature sig = (CodeSignature) joinPoint.getSignature();
        String[] paramNames = sig.getParameterNames();
        logContextService.buildMDCParams(joinPoint.getTarget(), paramNames, args);
        MDCBuilder.addRequestId(MDCBuilder.getMdcContextMap().get(LoggerContextKey.REQUEST_ID.toString()));
        LOGGER.debug("A controller method has been intercepted: {} with params {}, {}, MDC logger context is built.", joinPoint.toShortString(),
                sig.getParameterNames(), AnonymizerUtil.anonymize(Arrays.toString(args)));
    } catch (Exception any) {
        LOGGER.warn("MDCContext build failed: ", any);
    }
}
 
Example #9
Source File: ControllerLogContextAspects.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Before("com.sequenceiq.cloudbreak.logger.ControllerLogContextAspects.interceptControllerMethodCalls()")
public void buildLogContextForControllerCalls(JoinPoint joinPoint) {
    try {
        Object[] args = joinPoint.getArgs();
        CodeSignature sig = (CodeSignature) joinPoint.getSignature();
        String[] paramNames = sig.getParameterNames();
        logContextService.buildMDCParams(joinPoint.getTarget(), paramNames, args);
        MDCBuilder.addRequestId(MDCBuilder.getMdcContextMap().get(LoggerContextKey.REQUEST_ID.toString()));
        LOGGER.debug("A controller method has been intercepted: {} with params {}, {}, MDC logger context is built.", joinPoint.toShortString(),
                sig.getParameterNames(), AnonymizerUtil.anonymize(Arrays.toString(args)));
    } catch (Exception any) {
        LOGGER.warn("MDCContext build failed: ", any);
    }
}
 
Example #10
Source File: Hugo.java    From hugo with Apache License 2.0 5 votes vote down vote up
private static void enterMethod(JoinPoint joinPoint) {
  if (!enabled) return;

  CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();

  Class<?> cls = codeSignature.getDeclaringType();
  String methodName = codeSignature.getName();
  String[] parameterNames = codeSignature.getParameterNames();
  Object[] parameterValues = joinPoint.getArgs();

  StringBuilder builder = new StringBuilder("\u21E2 ");
  builder.append(methodName).append('(');
  for (int i = 0; i < parameterValues.length; i++) {
    if (i > 0) {
      builder.append(", ");
    }
    builder.append(parameterNames[i]).append('=');
    builder.append(Strings.toString(parameterValues[i]));
  }
  builder.append(')');

  if (Looper.myLooper() != Looper.getMainLooper()) {
    builder.append(" [Thread:\"").append(Thread.currentThread().getName()).append("\"]");
  }

  Log.v(asTag(cls), builder.toString());

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    final String section = builder.toString().substring(2);
    Trace.beginSection(section);
  }
}
 
Example #11
Source File: LogAspect.java    From springboot-learn with MIT License 4 votes vote down vote up
/**
 * 环绕增强记录日志
 *
 * @param joinPoint
 * @return
 * @throws Throwable
 */
@Around("logPointcut()")
public Object logAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
    Object result = null;
    try {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取被拦截的方法
        Method targetMethod = signature.getMethod();
        // 获取redis缓存注解信息
        Log log = targetMethod.getAnnotation(Log.class);

        HttpServletRequest request = getRequest();
        String ip = IpUtil.getIpAddr(request);//ip
        String ipAddress = ipAddressService.getAddressByIp(ip);//ip所在地区
        String description = log.description();//描述
        String username = ShiroUtils.getUsername();//操作用户
        String requestUri = request.getRequestURI();//URI
        String requestUrl = request.getRequestURL().toString();//URL
        String requestMethod = request.getMethod();//请求类型
        String className = joinPoint.getTarget().getClass().getName();//类名称
        String methodName = signature.getName();//方法名称
        Long startTime = System.currentTimeMillis();//开始时间

        Object[] paramValues = joinPoint.getArgs();// 参数值
        String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();// 参数名称

        StringBuffer arguments = new StringBuffer("{");//参数
        for (int i = 0; i < paramNames.length; i++) {
            // 过滤掉与密码相关的参数
            if (!paramNames[i].toLowerCase().contains("password")) {
                arguments.append(paramNames[i]);
                arguments.append(":");
                arguments.append(paramValues[i]);
                arguments.append(" ");
            }
        }
        arguments.append("}");

        result = joinPoint.proceed();
        Long endTime = System.currentTimeMillis();//结束时间
        Long spendTime = endTime - startTime;//消耗时间

        logger.info("ip:" + ip);
        logger.info("ipAddress:" + ipAddress);
        logger.info("操作描述:" + description);
        logger.info("用户名:" + username);
        logger.info("requestUri:" + requestUri);
        logger.info("请求路径:" + requestUrl);
        logger.info("请求方式:" + requestMethod);
        logger.info("类名:" + className);
        logger.info("方法名:" + methodName);
        logger.info("参数:" + arguments.toString());
        logger.info("执行结果:" + result.toString());
        logger.info("开始时间:" + startTime);
        logger.info("结束时间:" + endTime);
        logger.info("消耗时间:" + spendTime);

        com.lnjecit.entity.system.Log logEntity = new com.lnjecit.entity.system.Log();
        logEntity.setIp(ip);
        logEntity.setIpAddress(ipAddress);
        logEntity.setDescription(description);
        logEntity.setUsername(ShiroUtils.getUsername());
        logEntity.setRequestUri(requestUri);
        logEntity.setRequestUrl(requestUrl);
        logEntity.setRequestMethod(requestMethod);
        logEntity.setClassName(className);
        logEntity.setMethodName(methodName);
        logEntity.setArguments(arguments.toString());
        logEntity.setResult(result.toString());
        logEntity.setStartTime(startTime);
        logEntity.setEndTime(endTime);
        logEntity.setSpendTime(spendTime);

        logService.insert(logEntity);
    } catch (Exception e) {
        logger.error("记录日志出错", e);
    }
    return result;
}