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:com.crossbusiness.resiliency.aspect.AbstractFallbackAspect.java

License:Open Source License

public Object rerouteToFallback(ProceedingJoinPoint pjp, Fallback fallbackConfig) throws Throwable {

    String[] fallbacks = fallbackConfig.value();
    Class<? extends Throwable>[] fallbackableExceptions = fallbackConfig.exceptions();

    List<Object> fallbackBeans = new ArrayList<Object>(fallbacks.length);
    for (String fallback : fallbacks) {
        try {/*from w  ww  .ja v a  2 s .c  o  m*/
            fallbackBeans.add(context.getBean(fallback));
        } catch (BeansException be) {
            log.error("configuration error: cannot find bean with name: '{}'", fallback, be);
            //configuration errors should be fixed immediately.    
            throw be;
        }
    }

    MethodSignature targetMethodSig = (MethodSignature) pjp.getSignature();
    Method targetMethod = targetMethodSig.getMethod();
    Class[] paramTypes = (Class[]) targetMethod.getParameterTypes();
    Object[] args = pjp.getArgs();

    log.debug("fallbacks: {} method: '{}'", fallbacks, targetMethod);

    try {
        return pjp.proceed();
    } catch (Throwable t) {

        // if the exception is not what we're looking for, rethrow it
        if (!isFallbackableException(t, fallbackableExceptions))
            throw t;

        log.debug("got exception while trying the targetBean method: '{}'. will try fallbackBean...",
                targetMethod);
        Iterator<Object> iter = fallbackBeans.iterator();
        while (iter.hasNext()) {
            Object fallbackBean = iter.next();
            Method fallbackMethod;
            try {
                fallbackMethod = fallbackBean.getClass().getMethod(targetMethod.getName(), paramTypes);
            } catch (NoSuchMethodException | SecurityException nsme) {
                log.error(
                        "configuration error: No matchig method found in fallbackBean: '{}' that matches to targetBean method: '{}'",
                        new Object[] { fallbackBean.getClass().getName(), targetMethod, nsme });
                //configuration errors should be fixed immediately.   
                throw nsme;
            }
            try {
                log.debug("trying fallbackBean method: '{}'...", fallbackMethod);
                return fallbackMethod.invoke(fallbackBean, args);
            } catch (IllegalArgumentException | IllegalAccessException iae) {
                log.error(
                        "configuration error: arguments missmatch: fallbackBean method: '{}' arguments  missmatch to targetBean method: '{}' arguments",
                        new Object[] { fallbackMethod, targetMethod, iae });
                //configuration errors should be fixed immediately.   
                throw iae;
            } catch (InvocationTargetException ite) {
                log.debug(
                        "got exception while trying the fallbackBean method: '{}'. will try next fallbackBean...",
                        fallbackMethod);
                //fallbackBean method thrown an exception. try next bean or throw exception if this is the last bean
                if (!iter.hasNext()) {
                    //TODO : do we still need to check isFallbackableException?
                    throw ite.getCause();
                }
            }
        }
        //code should never reach this line. 
        throw t;
    }
}

From source file:com.crossbusiness.resiliency.aspect.spring.AnnotationFallbackAspect.java

License:Open Source License

public Object rerouteToFallback(ProceedingJoinPoint pjp, Fallback fallbackConfig) throws Throwable {

    String[] fallbacks = fallbackConfig.value();
    Class<? extends Throwable>[] fallbackableExceptions = fallbackConfig.exceptions();

    List<Object> fallbackBeans = new ArrayList<Object>(fallbacks.length);
    for (String fallback : fallbacks) {
        try {/*from ww w.  j  a v a 2s . c om*/
            fallbackBeans.add(context.getBean(fallback));
        } catch (BeansException be) {
            log.error("configuration error: cannot find bean with name: '{}'", fallback, be);
            //configuration errors should be fixed immediately.
            throw be;
        }
    }

    MethodSignature targetMethodSig = (MethodSignature) pjp.getSignature();
    Method targetMethod = targetMethodSig.getMethod();
    Class[] paramTypes = (Class[]) targetMethod.getParameterTypes();
    Object[] args = pjp.getArgs();

    log.debug("fallbacks: {} method: '{}'", fallbacks, targetMethod);

    try {
        return pjp.proceed();
    } catch (Throwable t) {

        // if the exception is not what we're looking for, rethrow it
        if (!isFallbackableException(t, fallbackableExceptions))
            throw t;

        log.debug("got exception while trying the targetBean method: '{}'. will try fallbackBean...",
                targetMethod);
        Iterator<Object> iter = fallbackBeans.iterator();
        while (iter.hasNext()) {
            Object fallbackBean = iter.next();
            Method fallbackMethod;
            try {
                fallbackMethod = fallbackBean.getClass().getMethod(targetMethod.getName(), paramTypes);
            } catch (NoSuchMethodException | SecurityException nsme) {
                log.error(
                        "configuration error: No matchig method found in fallbackBean: '{}' that matches to targetBean method: '{}'",
                        new Object[] { fallbackBean.getClass().getName(), targetMethod, nsme });
                //configuration errors should be fixed immediately.
                throw nsme;
            }
            try {
                log.debug("trying fallbackBean method: '{}'...", fallbackMethod);
                return fallbackMethod.invoke(fallbackBean, args);
            } catch (IllegalArgumentException | IllegalAccessException iae) {
                log.error(
                        "configuration error: arguments missmatch: fallbackBean method: '{}' arguments  missmatch to targetBean method: '{}' arguments",
                        new Object[] { fallbackMethod, targetMethod, iae });
                //configuration errors should be fixed immediately.
                throw iae;
            } catch (InvocationTargetException ite) {
                log.debug(
                        "got exception while trying the fallbackBean method: '{}'. will try next fallbackBean...",
                        fallbackMethod);
                //fallbackBean method thrown an exception. try next bean or throw exception if this is the last bean
                if (!iter.hasNext()) {
                    //TODO : do we still need to check isFallbackableException?
                    throw ite.getCause();
                }
            }
        }
        //code should never reach this line.
        throw t;
    }
}

From source file:com.datastax.hectorjpa.spring.ConsistencyLevelAspect.java

License:Open Source License

/**
 * Validates any method that has the valid annotation on it and is wired as
 * a spring service/*from  w w  w .j  av  a 2  s  . c  om*/
 * 
 * @param jp
 * @throws Throwable
 */
@Around("@annotation(com.datastax.hectorjpa.spring.Consistency)")
public Object setConsistency(ProceedingJoinPoint pjp) throws Throwable {

    logger.debug("Invoking before advice for @Consistency annotation.  Target object is {} on method {}",
            pjp.getTarget(), pjp.getSignature());

    MethodSignature sig = (MethodSignature) pjp.getSignature();

    Object[] args = pjp.getArgs();

    Method signatureMethod = sig.getMethod();

    Class<?>[] signatureTypes = signatureMethod.getParameterTypes();

    // we do this because we want to get the best match from the child
    // classes
    Class<?>[] runtimeArgs = new Class<?>[signatureTypes.length];

    for (int i = 0; i < signatureTypes.length; i++) {

        if (args[i] != null) {
            runtimeArgs[i] = args[i].getClass();
        } else {
            runtimeArgs[i] = signatureTypes[i];
        }
    }

    Class<?> runtimeClass = pjp.getTarget().getClass();

    // check if this is annotated, if not proceed and execute it

    HConsistencyLevel level = consistency(runtimeClass, signatureMethod.getName(), runtimeArgs);

    if (level == null) {
        return pjp.proceed(args);
    }

    Stack<HConsistencyLevel> stack = threadStack.get();

    stack.push(level);
    JPAConsistency.set(level);

    Object result = null;

    try {
        result = pjp.proceed(args);
    } finally {
        stack.pop();

        if (stack.size() > 0) {
            JPAConsistency.set(stack.peek());
        } else {
            JPAConsistency.remove();
        }

    }

    return result;
}

From source file:com.feilong.spring.aop.AbstractAspect.java

License:Apache License

/**
 * ?annotaion./*from  w  ww  .ja  va 2 s  .  com*/
 *
 * @param <T>
 *            the generic type
 * @param joinPoint
 *            the join point
 * @param annotationClass
 *            the annotation class
 * @return the annotation
 */
protected <T extends Annotation> T getAnnotation(JoinPoint joinPoint, Class<T> annotationClass) {
    if (Validator.isNullOrEmpty(joinPoint)) {
        throw new NullPointerException("joinPoint can't be null/empty!");
    }
    if (Validator.isNullOrEmpty(annotationClass)) {
        throw new NullPointerException("annotationClass can't be null/empty!");
    }

    T annotation = null;

    Target annotationTarget = annotationClass.getAnnotation(Target.class);
    ElementType[] elementTypes = annotationTarget.value();

    // ElementType.METHOD
    // ??ElementType.METHODannotation
    boolean isMethodAnnotation = ArrayUtil.isContain(elementTypes, ElementType.METHOD);
    if (isMethodAnnotation) {

        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();

        // ???annotaion,annotation
        if (method.isAnnotationPresent(annotationClass)) {
            annotation = method.getAnnotation(annotationClass);
        }
    }

    // ElementType.TYPE
    if (null == annotation) {
        // ?? ElementType.TYPEannotation
        boolean isTypeAnnotation = ArrayUtil.isContain(elementTypes, ElementType.TYPE);
        if (isTypeAnnotation) {
            Object target = joinPoint.getTarget();
            // ?  
            Class<?> targetClass = target.getClass();
            annotation = targetClass.getAnnotation(annotationClass);
        }
    }

    // String methodName = method.getName();
    // Class<?>[] parameterTypes = method.getParameterTypes();
    // method = targetClass.getMethod(methodName, parameterTypes);

    return annotation;
}

From source file:com.feilong.spring.aop.AbstractAspect.java

License:Apache License

/**
 *  method.//  w w  w .j a v  a  2  s  .  co m
 * 
 * @param joinPoint
 *            the join point
 * @param klass
 *            the clazz
 * @return the method
 * @deprecated ??,??
 */
@Deprecated
protected Method getMethod(JoinPoint joinPoint, Class<? extends Annotation> klass) {
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    Method method = methodSignature.getMethod();
    if (method.isAnnotationPresent(klass)) {
        return method;
    }
    Target annotation = klass.getAnnotation(Target.class);
    ElementType[] value = annotation.value();
    try {
        Object target = joinPoint.getTarget();
        Class<? extends Object> targetClass = target.getClass();
        String methodName = method.getName();
        Class<?>[] parameterTypes = method.getParameterTypes();
        Method m1 = targetClass.getMethod(methodName, parameterTypes);
        if (m1.isAnnotationPresent(klass)) {
            return m1;
        }
    } catch (Exception e) {
        log.error(e.getClass().getName(), e);
    }
    throw new RuntimeException("No Proper annotation found.");
}

From source file:com.feilong.spring.aop.AbstractAspect.java

License:Apache License

/**
 * Checks if is annotation present./*from w  w  w .  j a  v  a 2 s  .  c  o m*/
 * 
 * @param joinPoint
 *            the join point
 * @param clazz
 *            the clazz
 * @return true, if checks if is annotation present
 * @deprecated ??,??
 */
@Deprecated
protected boolean isAnnotationPresent(JoinPoint joinPoint, Class<? extends Annotation> clazz) {
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    Method method = methodSignature.getMethod();
    if (method.isAnnotationPresent(clazz)) {
        return true;
    }
    Target annotation = clazz.getAnnotation(Target.class);
    ElementType[] value = annotation.value();
    try {
        Object target = joinPoint.getTarget();
        Class<? extends Object> targetClass = target.getClass();
        String methodName = method.getName();
        Class<?>[] parameterTypes = method.getParameterTypes();
        Method m1 = targetClass.getMethod(methodName, parameterTypes);
        if (m1.isAnnotationPresent(clazz)) {
            return true;
        }
    } catch (Exception e) {
        log.error(e.getClass().getName(), e);
    }
    return false;
}

From source file:com.feilong.spring.aop.ProceedingJoinPointUtil.java

License:Apache License

/**
 *  map for log./*from  w w w.j  a v a  2  s  .c o  m*/
 *
 * @param proceedingJoinPoint
 *            the proceeding join point
 * @return the map for log
 */
public final static Map<String, Object> getMapForLog(ProceedingJoinPoint proceedingJoinPoint) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();

    Signature signature = proceedingJoinPoint.getSignature();
    MethodSignature methodSignature = (MethodSignature) signature;

    Method method = methodSignature.getMethod();
    Object[] args = proceedingJoinPoint.getArgs();

    Object target = proceedingJoinPoint.getTarget();

    Class<?> declaringType = methodSignature.getDeclaringType();

    Class<? extends Object> targetClass = target.getClass();
    map.put("target", targetClass.getCanonicalName());
    map.put("method", method.getName());
    map.put("args", args);

    return map;
}

From source file:com.feilong.spring.jdbc.datasource.MultipleGroupReadWriteDataSourceAspect.java

License:Apache License

/**
 * Point.//from ww  w  . j a va  2s. c o  m
 *
 * @param proceedingJoinPoint
 *            the proceeding join point
 * @return the object
 * @throws Throwable
 *             the throwable
 */
@Around("this(loxia.dao.ReadWriteSupport)")
public Object point(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

    Signature signature = proceedingJoinPoint.getSignature();
    MethodSignature methodSignature = (MethodSignature) signature;
    //
    TransactionAttribute transactionAttribute = null;
    if (null != transactionAttributeSouce) {
        transactionAttribute = transactionAttributeSouce.getTransactionAttribute(methodSignature.getMethod(),
                proceedingJoinPoint.getTarget().getClass());
    }

    MultipleGroupDataSource multipleGroupDataSourceAnnotation = getAnnotation(proceedingJoinPoint,
            MultipleGroupDataSource.class);
    //??
    String groupName;
    //?multipleGroupDataSourceAnnotation
    //? ? 
    if (null == multipleGroupDataSourceAnnotation
            || Validator.isNullOrEmpty(multipleGroupDataSourceAnnotation.value())) {
        //nothing to do
        groupName = null;
    } else {
        groupName = multipleGroupDataSourceAnnotation.value();
    }
    return this.proceed(proceedingJoinPoint, transactionAttribute, groupName);
}

From source file:com.genyherrera.performancelog.aspect.PerformanceLogAspect.java

License:Apache License

private PerfLog retrievePerfLogAnnotation(JoinPoint joinpoint) {
    MethodSignature signature = (MethodSignature) joinpoint.getSignature();
    Method method = signature.getMethod();

    PerfLog perfLog = method.getAnnotation(PerfLog.class);
    return perfLog;
}

From source file:com.github.sebhoss.contract.lifecycle.AspectContractLifecycle.java

License:Open Source License

@Override
protected ContractVerifier createVerifier() {
    final ContractVerifierBuilder builder = getContractVerifierFactory().createContractVerifier();
    final MethodSignature methodSignature = (MethodSignature) pjp.getSignature();

    builder.method(methodSignature.getMethod());
    builder.parameterNames(methodSignature.getParameterNames());
    builder.instance(pjp.getThis());/* www . j ava  2  s .  com*/
    builder.arguments(pjp.getArgs());
    builder.contract(contract);

    return builder.get();
}