Example usage for org.aspectj.lang.reflect MethodSignature getMethod

List of usage examples for org.aspectj.lang.reflect MethodSignature getMethod

Introduction

In this page you can find the example usage for org.aspectj.lang.reflect MethodSignature getMethod.

Prototype

Method getMethod();

Source Link

Usage

From source file:org.finra.herd.service.advice.StopWatchAdviceTest.java

License:Apache License

/**
 * Creates and returns a mocked join point of the method call.
 *
 * @param targetObject the target object
 * @param method the method/*from   ww  w . jav  a 2s. c  o m*/
 *
 * @return the mocked ProceedingJoinPoint
 */
private ProceedingJoinPoint getMockedProceedingJoinPoint(Object targetObject, Method method) throws Exception {
    ProceedingJoinPoint joinPoint = mock(ProceedingJoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    when(joinPoint.getTarget()).thenReturn(targetObject);
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    when(methodSignature.getMethod()).thenReturn(method);
    when(methodSignature.getName()).thenReturn(method.getName());

    return joinPoint;
}

From source file:org.frat.common.validation.ValidateInterceptor.java

License:Open Source License

/**
 * validate the business logic before executing target method.
 *///from  w  w w .  ja  v  a2s . c  om
@Before("findValidateAnnotation()")
public void validate(final JoinPoint jp) throws ConstraintViolationException {
    final Signature signature = jp.getSignature();
    final Object[] args = jp.getArgs();
    final MethodSignature methodSignature = (MethodSignature) signature;
    final Method targetMethod = methodSignature.getMethod();
    Set<ConstraintViolation<?>> result = new HashSet<ConstraintViolation<?>>();
    final Annotation[][] paraAnnotations = targetMethod.getParameterAnnotations();
    // get the parameters annotations
    if (paraAnnotations != null && paraAnnotations.length > 0) {
        for (int i = 0; i < paraAnnotations.length; i++) {
            int paraAnnotationLength = paraAnnotations[i].length;
            // current parameter annotation length
            if (paraAnnotationLength == 0) {
                // no annotation on current parameter
                continue;
            }
            for (int j = 0; j < paraAnnotationLength; j++) {
                if (paraAnnotations[i][j] instanceof OnValid) {
                    OnValid validated = (OnValid) (paraAnnotations[i][j]);
                    Class<?>[] groups = validated.value();
                    Object validatedObj = args[i];
                    executeValidate(validatedObj, groups, result);
                    break;
                }
            }
        }
    }
    if (!result.isEmpty()) {
        throw new ConstraintViolationException(result);
    }
}

From source file:org.hellojavaer.ddal.spring.scan.EnableDBClusterRouteAnnotation.java

License:Apache License

@Around("@annotation(dbClusterRoute)")
public Object around(ProceedingJoinPoint joinPoint, DBClusterRoute dbClusterRoute) throws Throwable {
    try {/*from   w  w  w .  j  a  va 2  s.  co  m*/
        DBClusterRouteContext.pushContext();
        Object[] args = joinPoint.getArgs();
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        MethodBasedSpelExpression expression = expressionCache.get(method);
        if (expression == null) {
            synchronized (expressionCache) {
                expression = expressionCache.get(method);
                if (expression == null) {
                    expression = new MethodBasedSpelExpression(dbClusterRoute.clusterName(), method);
                    expressionCache.put(method, expression);
                }
            }
        }
        String targetClusterName = expression.parse(String.class, args);
        DBClusterRouteContext.setClusterName(targetClusterName);
        return joinPoint.proceed(args);
    } finally {
        DBClusterRouteContext.popContext();
    }
}

From source file:org.hellojavaer.ddal.spring.scan.EnableShardRouteAnnotation.java

License:Apache License

@Around("@annotation(shardRoute)")
public Object around(ProceedingJoinPoint joinPoint, ShardRoute shardRoute) throws Throwable {
    try {//w w w  .ja  va2  s .  c om
        ShardRouteContext.pushContext();
        if (shardRoute.scName() != null && shardRoute.scName().length() > 0 //
                && shardRoute.sdValue() != null && shardRoute.sdValue().length() > 0) {
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            Method method = methodSignature.getMethod();
            MethodBasedSpelExpression expression = expressionCache.get(method);
            Object[] args = joinPoint.getArgs();
            if (expression == null) {
                synchronized (expressionCache) {
                    expression = expressionCache.get(method);
                    if (expression == null) {
                        expression = new MethodBasedSpelExpression(shardRoute.sdValue(), method);
                        expressionCache.put(method, expression);
                    }
                }
            }
            Object val = expression.parse(Object.class, args);
            String[] scNames = shardRoute.scName().split(",");
            for (String scName : scNames) {
                ShardRouteContext.setRouteInfo(scName, val);
            }
        } else {
            if ((shardRoute.scName() == null || shardRoute.scName().length() == 0)
                    && (shardRoute.sdValue() == null || shardRoute.sdValue().length() == 0)) {
                // ok
            } else {
                throw new IllegalArgumentException(
                        "scName and sdValue should either both have a non-empty value or both have a empty value");
            }
        }
        return joinPoint.proceed(joinPoint.getArgs());
    } finally {
        ShardRouteContext.popContext();
    }
}

From source file:org.hswebframework.web.AopUtils.java

License:Apache License

public static <T extends Annotation> T findAnnotation(JoinPoint pjp, Class<T> annClass) {
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Method m = signature.getMethod();
    Class<?> targetClass = pjp.getTarget().getClass();
    return findAnnotation(targetClass, m, annClass);
}

From source file:org.jbb.lib.properties.encrypt.PropertiesEncryptionAspect.java

License:Apache License

@Around("this(org.jbb.lib.properties.ModuleProperties)")
public Object decryptIfApplicable(ProceedingJoinPoint joinPoint) throws Throwable {
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();

    Config.Key key = method.getAnnotation(Config.Key.class);
    if (key != null) {
        ModuleProperties properties = (ModuleProperties) joinPoint.getThis();
        String value = properties.getProperty(key.value());
        if (isInDecPlaceholder(value)) {
            String decryptedString = propertiesEncryption.decryptIfNeeded(value);
            Class returnType = ((MethodSignature) joinPoint.getSignature()).getReturnType();
            return typeCasting.resolve(decryptedString, returnType);
        }/*from  w w w.  ja va  2 s .  c  om*/
    }

    return joinPoint.proceed();
}

From source file:org.jbb.lib.properties.SafeBlankPropertyAspect.java

License:Apache License

@Around("this(org.jbb.lib.properties.ModuleProperties)")
public Object makeSafeBlankProperty(ProceedingJoinPoint joinPoint) throws Throwable {
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();

    Config.Key key = method.getAnnotation(Config.Key.class);
    if (key != null) {
        ModuleProperties properties = (ModuleProperties) joinPoint.getThis();
        String value = properties.getProperty(key.value());
        if (StringUtils.isBlank(value)) {
            return null;
        }//from   w  w w.  j  av a  2 s  .  com
    }

    return joinPoint.proceed();
}

From source file:org.lexevs.cache.MethodCachingProxy.java

License:Open Source License

@Override
protected Method getMethod(ProceedingJoinPoint joinPoint) {
    MethodSignature sig = (MethodSignature) joinPoint.getSignature();

    return sig.getMethod();
}

From source file:org.mifos.rest.approval.aop.AspectJRESTApprovalInterceptor.java

License:Open Source License

@Around("restMethods() && requestMapping() && excludeAPI() && exludeRestfulServices()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
    Signature signature = pjp.getStaticPart().getSignature();
    LOG.debug(this.getClass().getSimpleName() + " staring");

    // FIXME : somehow autowiring is not working
    if (approvalService == null) {
        approvalService = ApplicationContextProvider.getBean(ApprovalService.class);
    }//from  w  ww.ja  v  a 2 s.c o  m
    if (configurationServiceFacade == null) {
        configurationServiceFacade = ApplicationContextProvider.getBean(ConfigurationServiceFacade.class);
    }
    if (parameterNameDiscoverer == null) {
        parameterNameDiscoverer = ApplicationContextProvider.getBean(ParameterNameDiscoverer.class);
    }

    if (!RESTConfigKey.isApprovalRequired(configurationServiceFacade)) {
        LOG.debug(pjp.getSignature() + " skip approval");
        return pjp.proceed();
    }

    if (signature instanceof MethodSignature) {
        MethodSignature ms = (MethodSignature) signature;
        Method m = ms.getMethod();
        RequestMapping mapping = m.getAnnotation(RequestMapping.class);

        if (isReadOnly(mapping)) {
            LOG.debug(m.getName() + " is read only, hence returning control");
            return pjp.proceed();
        }

        Class<?> methodClassType = m.getDeclaringClass();

        if (!methodClassType.getSimpleName().endsWith("RESTController")) {
            LOG.debug(m.getName() + " is not from REST controller, hence returning control");
            return pjp.proceed();
        }

        Object[] argValues = pjp.getArgs();

        Class<?>[] argTypes = m.getParameterTypes();
        String methodName = m.getName();
        String[] names = parameterNameDiscoverer.getParameterNames(m);

        MethodArgHolder args = new MethodArgHolder(argTypes, argValues, names);
        ApprovalMethod method = new ApprovalMethod(methodName, methodClassType, args);
        approvalService.create(method);
    }
    return pjp.proceed();
}

From source file:org.newhart.japi.aspect.AuditAspect.java

License:Apache License

private List<Object> getDataArgs(final JoinPoint joinPoint) {
    final List<Object> dataArgs = new LinkedList<Object>();

    final Object[] args = joinPoint.getArgs();
    final Signature sig = joinPoint.getSignature();
    final MethodSignature methodSig = (sig instanceof MethodSignature) ? (MethodSignature) sig : null;

    //System.out.println("beforeAuditMethod(" + joinPoint + ", " + joinPoint.getKind()+ ")... args " + (args == null?"is null":"has length " + args.length) + ", signature=" + joinPoint.getSignature());
    if (methodSig != null) {
        final Method method = methodSig.getMethod();
        final Annotation annotations[][] = method.getParameterAnnotations();
        int i = 0;
        for (Annotation parameter[] : annotations) {
            boolean isDataParameter = false;
            for (Annotation a : parameter) {
                if (a instanceof AuditData) {
                    isDataParameter = true;
                }/*from w  w  w.j  av a  2  s.co m*/
            }
            if (isDataParameter) {
                dataArgs.add(args[i]);
            }
            i++;
        }
    }

    return dataArgs;
}