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.smallchill.core.aop.PermissionAop.java

License:Apache License

@Around("cutPermission()")
public Object doPermission(ProceedingJoinPoint point) throws Throwable {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();//from www  .j a  v a 2 s .co  m
    MethodSignature ms = (MethodSignature) point.getSignature();
    Method method = ms.getMethod();
    Permission permission = method.getAnnotation(Permission.class);
    Object[] permissions = permission.value();
    if ((permissions.length == 1 && Func.format(permissions[0]).equals("ALL")) || permissions == null
            || permissions.length == 0) {
        //
        boolean result = PermissionCheckManager.checkAll(request);
        if (result) {
            return point.proceed();
        } else {
            throw new NoPermissionException();
        }
    } else {
        //
        boolean result = PermissionCheckManager.check(permissions, request);
        if (result) {
            return point.proceed();
        } else {
            throw new NoPermissionException();
        }
    }

}

From source file:com.spidertracks.datanucleus.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 ava2s.  co  m
 * 
 * @param jp
 * @throws Throwable
 */
@Around("@annotation(com.spidertracks.datanucleus.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

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

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

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

    stack.push(level);
    com.spidertracks.datanucleus.client.Consistency.set(level);

    Object result = null;

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

        if (stack.size() > 0) {
            com.spidertracks.datanucleus.client.Consistency.set(stack.peek());
        } else {
            com.spidertracks.datanucleus.client.Consistency.remove();
        }

    }

    return result;
}

From source file:com.spstudio.session.filter.SessionAOP.java

private UserSessionType getSessionType(ProceedingJoinPoint pj) {
    // ? Method  
    MethodSignature joinPointObject = (MethodSignature) pj.getSignature();
    Method method = joinPointObject.getMethod();
    boolean flag = method.isAnnotationPresent(UserSession.class);
    if (flag) {//from   www .j a va 2 s  .c  o m
        UserSession annotation = method.getAnnotation(UserSession.class);
        return annotation.value();
    }
    return null;
}

From source file:com.stratelia.webactiv.util.annotation.AnnotationUtil.java

License:Open Source License

/**
 * Provides a centralized way to extract annotation of a method.
 * @param invocationContext// w  ww.  jav a  2 s  . co  m
 * @return
 * @throws Exception
 */
private static Method getMethodFromContext(Object invocationContext) throws Exception {
    if (invocationContext instanceof ProceedingJoinPoint) {
        ProceedingJoinPoint context = (ProceedingJoinPoint) invocationContext;
        final String methodName = context.getSignature().getName();
        final MethodSignature methodSignature = (MethodSignature) context.getSignature();
        Method method = methodSignature.getMethod();
        if (method.getDeclaringClass().isInterface()) {
            method = context.getTarget().getClass().getDeclaredMethod(methodName, method.getParameterTypes());
        }
        return method;
    } else if (invocationContext instanceof InvocationContext) {
        return ((InvocationContext) invocationContext).getMethod();
    }
    throw new NotImplementedException();
}

From source file:com.tacitknowledge.flip.aspectj.FlipAbstractAspect.java

License:Apache License

/**
 * Intercept calls to the methods marked with {@link Flippable} annotation.
 * Firstly it processes the method parameters marked with {@link FlipParam} 
 * annotation. Each parameter value is replaced with the value declared in
 * {@link FlipParam#disabledValue()} if the feature declared in {@link FlipParam#feature() }
 * is disabled./*from  ww w.  j  av a 2  s.  co m*/
 * After properties evaluation this method checks if the feature marked in
 * {@link Flippable#feature() } is disabled then the value from {@link Flippable#disabledValue() }
 * is returned.
 * 
 * @param flip the {@link Flippable} annotation instance which marks the method to intercept.
 * @param pjp the object obtained from AspectJ method interceptor.
 * @return the processed value of the method depending from feature state.
 * @throws Throwable 
 */
@Around(value = "anyMethod() && @annotation(flip)", argNames = "flip")
public Object aroundFlippableMethods(ProceedingJoinPoint pjp, Flippable flip) throws Throwable {
    MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
    Method method = methodSignature.getMethod();

    if (isFeatureEnabled(flip.feature())) {
        Annotation[][] paramAnnotations = method.getParameterAnnotations();
        Object[] params = pjp.getArgs();
        Class[] paramTypes = method.getParameterTypes();

        for (int i = 0; i < paramAnnotations.length; i++) {
            FlipParam flipParam = findFlipParamAnnoattion(paramAnnotations[i]);
            if (!isFeatureEnabled(flipParam.feature())) {
                params[i] = getProcessedDisabledValue(paramTypes[i], flipParam.disabledValue(), pjp.getThis());
            }
        }

        return pjp.proceed(params);
    } else {
        return getProcessedDisabledValue(method.getReturnType(), flip.disabledValue(), pjp.getThis());
    }
}

From source file:com.vico.license.aop.SecurityAspect.java

@Around("needAnnotation()")
public Object execute(ProceedingJoinPoint pjp) throws Throwable {

    System.out.println("=====SysLogAspect =====");
    //??/*from w  ww  .  j  a  va  2  s .co m*/
    MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
    Method method = methodSignature.getMethod();

    //,(ignore?)
    if (method.isAnnotationPresent(IgnoreSecurity.class)) {
        return pjp.proceed();
    }

    //request header??token
    System.out.println(WebContext.getRequest());
    String token = WebContext.getRequest().getHeader(tokenName); //,
    System.out.println(token);

    //token
    if (!tokenManager.checkToken(token)) {
        String message = String.format("token [%s] is invalid", token);
        throw new TokenException(message);
    }

    //
    return pjp.proceed();
}

From source file:com.virtusa.isq.vtaf.aspects.AspectClass.java

License:Apache License

/**
 * Gets the recovery method names.//from w w w .  j  a  v  a 2 s.  c  o m
 *
 * @param joinPoint the join point
 * 
 */
@Before("@annotation(org.testng.annotations.Test)")
public final void getRecoveryMethodNames(final JoinPoint joinPoint) {
    MethodSignature ms = (MethodSignature) joinPoint.getSignature();
    Method m = ms.getMethod();
    VTAFRecoveryMethods recoveryAnnot = m.getAnnotation(VTAFRecoveryMethods.class);
    if (recoveryAnnot != null) {
        recoveryMethodNames = recoveryAnnot.recoveryMethods();
        onerrorMethodNames = recoveryAnnot.onerrorMethods();
        System.out.println(Arrays.asList(recoveryMethodNames));
    }
}

From source file:com.visural.domo.spring.TransactionInterceptor.java

License:Apache License

@Around("execution(@com.visural.domo.spring.Transactional * *(..))")
public Object transactional(ProceedingJoinPoint mi) throws Throwable {
    MethodSignature signature = (MethodSignature) mi.getSignature();
    Method method = signature.getMethod();
    String connectionSource = method.getAnnotation(Transactional.class).connectionSource();
    boolean alreadyTxForSource = scope.isInScope(connectionSource);
    scope.enter(connectionSource);/*from   ww w .  j  av  a2  s.  c  om*/
    try {
        if (scope.getSeed(connectionSource, ConnectionSource.class) == null) {
            GeneratorProvider gp = transactionConfig.getConnectionProvider()
                    .getGeneratorProvider(connectionSource);
            scope.seed(GeneratorProvider.class, gp);
            ConnectionSource con = transactionConfig.getConnectionProvider().get(connectionSource);
            scope.seed(ConnectionSource.class, con);
        }
        if (alreadyTxForSource) {
            Savepoint savepoint = null;
            if (transactionConfig.isSavepointAndRollbackNested()) {
                try {
                    savepoint = tx.savepoint();
                } catch (SQLFeatureNotSupportedException ns) {
                    // TODO: log
                    // not supported feature
                }
            }
            try {
                return mi.proceed();
            } catch (Throwable t) {
                if (savepoint != null) {
                    tx.rollbackToSavepoint(savepoint);
                }
                throw t;
            }
        } else {
            try {
                Object o = mi.proceed();
                tx.commit();
                return o;
            } catch (Throwable t) {
                try {
                    tx.rollback();
                } catch (SQLException se) {
                    // prefer to bubble the original error, but log
                    Logger.getLogger(TransactionInterceptor.class.getName()).log(Level.SEVERE,
                            "Failed rolling back transaction after prior error.", se);
                }
                throw t;
            }
        }
    } finally {
        // close connection if we are the outermost tx for this source
        if (!alreadyTxForSource) {
            IOUtil.silentClose(TransactionInterceptor.class, tx.getConnection());
        }
        scope.exit(connectionSource);
    }
}

From source file:com.vladmihalcea.util.ReflectionUtils.java

License:Apache License

public static <T extends Annotation> T getAnnotation(ProceedingJoinPoint pjp, Class<T> annotationClass)
        throws NoSuchMethodException {
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Method method = signature.getMethod();
    T annotation = AnnotationUtils.findAnnotation(method, annotationClass);

    if (annotation != null) {
        return annotation;
    }//from   w w  w  .  j  av  a  2  s.co  m

    Class[] argClasses = new Class[pjp.getArgs().length];
    for (int i = 0; i < pjp.getArgs().length; i++) {
        argClasses[i] = pjp.getArgs()[i].getClass();
    }
    method = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), argClasses);
    return AnnotationUtils.findAnnotation(method, annotationClass);
}

From source file:com.vmware.bdd.aop.software.DefaultPreStartServicesAdvice.java

License:Open Source License

@Around("@annotation(com.vmware.bdd.software.mgmt.plugin.aop.PreConfiguration)")
public Object preClusterConfiguration(ProceedingJoinPoint pjp) throws Throwable {
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Method method = signature.getMethod();
    PreConfiguration beforeConfig = AnnotationUtils.findAnnotation(method, PreConfiguration.class);
    String nameParam = beforeConfig.clusterNameParam();

    String[] paramNames = signature.getParameterNames();
    Object[] args = pjp.getArgs();
    String clusterName = null;/*from w  w w. java  2  s .c  om*/
    for (int i = 0; i < paramNames.length; i++) {
        if (paramNames[i].equals(nameParam)) {
            clusterName = (String) args[i];
        }
    }
    if (clusterName == null) {
        logger.error("Cluster name is not specified in method");
        throw BddException.INTERNAL(null, "Wrong annotation usage. Cluster name must be specified in method.");
    }
    ClusterEntity cluster = clusterEntityMgr.findByName(clusterName);
    if (cluster == null) {
        throw BddException.NOT_FOUND("Cluster", clusterName);
    }
    preStartServices(clusterName);
    return pjp.proceed();
}