Search in sources :

Example 51 with JoinPoint

use of org.aspectj.lang.JoinPoint in project irida by phac-nml.

the class ValidMethodParametersAspect method getParameterAnnotations.

/**
 * Gets the set of annotations applied to parameters from both an interface
 * and concrete implementation of a class.
 *
 * @param jp
 *            the {@link JoinPoint} that's currently executing.
 * @return the collection of annotations applied to parameters of the
 *         currently executing method.
 */
private List<List<Annotation>> getParameterAnnotations(JoinPoint jp) {
    List<List<Annotation>> annotations = new ArrayList<>();
    MethodSignature signature = (MethodSignature) jp.getSignature();
    Method m = signature.getMethod();
    // in the event that the class is *not* an interface, we need to get the
    // corresponding method from the interface to load any parameter
    // annotations from there. Note that we only load the direct super
    // interface, and stop when we find the first interface that matches the
    // method definition; order of interfaces is important.
    Class<?>[] interfaces = m.getDeclaringClass().getInterfaces();
    Method interfaceMethod = null;
    for (Class<?> iface : interfaces) {
        try {
            interfaceMethod = iface.getMethod(m.getName(), m.getParameterTypes());
        } catch (NoSuchMethodException | SecurityException e) {
        }
    }
    if (interfaceMethod == null) {
        interfaceMethod = m;
    }
    Annotation[][] interfaceAnnotations = interfaceMethod.getParameterAnnotations();
    for (Annotation[] interfaceAnnotation : interfaceAnnotations) {
        annotations.add(Lists.newArrayList(interfaceAnnotation));
    }
    try {
        Method implementedMethod = jp.getTarget().getClass().getMethod(m.getName(), m.getParameterTypes());
        // This is an array-of-arrays; the first index corresponds to the
        // arguments that were passed to the method, the second index
        // corresponds to each of the parameters that was applied to the
        // argument.
        Annotation[][] implementedAnnotations = implementedMethod.getParameterAnnotations();
        for (int i = 0; i < annotations.size(); i++) {
            annotations.get(i).addAll(Lists.newArrayList(implementedAnnotations[i]));
        }
    } catch (NoSuchMethodException | SecurityException e) {
        throw new IllegalStateException("A concrete instance of a class *must* implement" + " a method declared in an interface.");
    }
    return annotations;
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) JoinPoint(org.aspectj.lang.JoinPoint) ArrayList(java.util.ArrayList) List(java.util.List)

Example 52 with JoinPoint

use of org.aspectj.lang.JoinPoint in project motan by weibocom.

the class LoggingAspect method logAfter.

@AfterReturning(value = "anyPublicOperation() && execCommandOperation()", returning = "result")
public void logAfter(JoinPoint joinPoint, boolean result) {
    Object[] args = joinPoint.getArgs();
    OperationRecord record = new OperationRecord();
    record.setOperator(getUsername());
    record.setType(joinPoint.getSignature().getName());
    record.setGroupName(args[0].toString());
    record.setCommand(JSON.toJSONString(args[1]));
    int status = result ? 1 : 0;
    record.setStatus((byte) status);
    if (recordMapper == null) {
        LoggerUtil.accessLog(JSON.toJSONString(record));
    } else {
        recordMapper.insertSelective(record);
    }
}
Also used : OperationRecord(com.weibo.model.OperationRecord) JoinPoint(org.aspectj.lang.JoinPoint) AfterReturning(org.aspectj.lang.annotation.AfterReturning)

Example 53 with JoinPoint

use of org.aspectj.lang.JoinPoint in project hugo by JakeWharton.

the class Hugo method enterMethod.

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);
    }
}
Also used : CodeSignature(org.aspectj.lang.reflect.CodeSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint)

Example 54 with JoinPoint

use of org.aspectj.lang.JoinPoint in project wechat by dllwh.

the class LogFactory method createOperateLog.

/**
 * @方法描述 : 创建操作日志
 */
public static SysLogEntity createOperateLog(JoinPoint joinPoint, long time, Throwable throwable, Object opResult, String ip, String browser) {
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();
    SysLogEntity sysLog = new SysLogEntity();
    SystemLog systemLog = method.getAnnotation(SystemLog.class);
    if (systemLog != null) {
        // 注解上的描述
        sysLog.setRemark(systemLog.desc());
        // 注解上的操作相关表
        sysLog.setTableName(StringUtils.join(systemLog.tableName(), ","));
        // 注解上的操作类型
        sysLog.setOpType(systemLog.opType().getValue());
    }
    // 请求方法名
    // 获取目标类名
    String targetName = joinPoint.getTarget().getClass().getName();
    sysLog.setMethod(targetName + "." + signature.getName() + "()");
    // 访问目标方法的参数:
    Object[] args = joinPoint.getArgs();
    String params = JSON.toJSONString(args[0]);
    // sysLog.setParams(JsonMapper.toJsonString(joinPoint.getArgs()));
    sysLog.setParams(params);
    // 操作人的信息
    int userId = -1;
    if (ShiroHelper.isLogin()) {
        userId = WebUtilHelper.getCurrentUserId();
    }
    sysLog.setUserCode(userId);
    if (opResult != null) {
        sysLog.setOpResult(JsonMapper.toJsonString(opResult));
    }
    if (throwable != null) {
        sysLog.setLogType(-1);
        sysLog.setExceptionCode(sysLog.getClass().getName());
        sysLog.setExceptionDetail(throwable.getMessage());
    }
    sysLog.setIpAddress(ip);
    sysLog.setBrowser(browser);
    sysLog.setTime(time);
    return sysLog;
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) SystemLog(com.cdeledu.core.annotation.SystemLog) SysLogEntity(com.cdeledu.model.system.SysLogEntity) JoinPoint(org.aspectj.lang.JoinPoint)

Example 55 with JoinPoint

use of org.aspectj.lang.JoinPoint in project spring-framework by spring-projects.

the class MethodInvocationProceedingJoinPointTests method toShortAndLongStringFormedCorrectly.

@Test
public void toShortAndLongStringFormedCorrectly() throws Exception {
    final Object raw = new TestBean();
    ProxyFactory pf = new ProxyFactory(raw);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
        // makeEncSJP, although meant for computing the enclosing join point,
        // it serves our purpose here
        StaticPart aspectJVersionJp = Factory.makeEncSJP(method);
        JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
        assertThat(jp.getSignature().toLongString()).isEqualTo(aspectJVersionJp.getSignature().toLongString());
        assertThat(jp.getSignature().toShortString()).isEqualTo(aspectJVersionJp.getSignature().toShortString());
        assertThat(jp.getSignature().toString()).isEqualTo(aspectJVersionJp.getSignature().toString());
        assertThat(jp.toLongString()).isEqualTo(aspectJVersionJp.toLongString());
        assertThat(jp.toShortString()).isEqualTo(aspectJVersionJp.toShortString());
        assertThat(jp.toString()).isEqualTo(aspectJVersionJp.toString());
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    itb.getAge();
    itb.setName("foo");
    itb.getDoctor();
    itb.getStringArray();
    itb.getSpouse();
    itb.setSpouse(new TestBean());
    try {
        itb.unreliableFileOperation();
    } catch (IOException ex) {
    // we don't really care...
    }
}
Also used : ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) AopUtils(org.springframework.aop.support.AopUtils) SourceLocation(org.aspectj.lang.reflect.SourceLocation) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) AopContext(org.springframework.aop.framework.AopContext) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) IOException(java.io.IOException) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Aggregations

JoinPoint (org.aspectj.lang.JoinPoint)59 MethodSignature (org.aspectj.lang.reflect.MethodSignature)31 Method (java.lang.reflect.Method)30 Test (org.junit.Test)29 AccessDeniedException (org.springframework.security.access.AccessDeniedException)26 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)25 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)25 SecurityUserWrapper (org.finra.herd.model.dto.SecurityUserWrapper)22 ApplicationUser (org.finra.herd.model.dto.ApplicationUser)21 NamespaceAuthorization (org.finra.herd.model.api.xml.NamespaceAuthorization)14 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)11 Before (org.aspectj.lang.annotation.Before)5 CodeSignature (org.aspectj.lang.reflect.CodeSignature)4 Annotation (java.lang.annotation.Annotation)3 ArrayList (java.util.ArrayList)3 IView (com.yydcdut.note.views.IView)2 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2