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.entando.entando.aps.system.services.cache.CacheInfoManager.java

License:Open Source License

@Around("@annotation(cacheableInfo)")
public Object aroundCacheableMethod(ProceedingJoinPoint pjp, CacheableInfo cacheableInfo) throws Throwable {
    Object result = pjp.proceed();
    if (cacheableInfo.expiresInMinute() < 0
            && (cacheableInfo.groups() == null || cacheableInfo.groups().trim().length() == 0)) {
        return result;
    }//from   w w w.  j  av a2  s  .  com
    try {
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        Method targetMethod = methodSignature.getMethod();
        Class targetClass = pjp.getTarget().getClass();
        Method effectiveTargetMethod = targetClass.getMethod(targetMethod.getName(),
                targetMethod.getParameterTypes());
        Cacheable cacheable = effectiveTargetMethod.getAnnotation(Cacheable.class);
        if (null == cacheable) {
            return result;
        }
        String[] cacheNames = cacheable.value();
        Object key = this.evaluateExpression(cacheable.key().toString(), targetMethod, pjp.getArgs(),
                effectiveTargetMethod, targetClass);
        for (String cacheName : cacheNames) {
            if (cacheableInfo.groups() != null && cacheableInfo.groups().trim().length() > 0) {
                Object groupsCsv = this.evaluateExpression(cacheableInfo.groups().toString(), targetMethod,
                        pjp.getArgs(), effectiveTargetMethod, targetClass);
                if (null != groupsCsv && groupsCsv.toString().trim().length() > 0) {
                    String[] groups = groupsCsv.toString().split(",");
                    this.putInGroup(cacheName, key.toString(), groups);
                }
            }
            if (cacheableInfo.expiresInMinute() > 0) {
                this.setExpirationTime(cacheName, key.toString(), cacheableInfo.expiresInMinute());
            }
        }
    } catch (Throwable t) {
        logger.error("Error while evaluating cacheableInfo annotation", t);
        throw new ApsSystemException("Error while evaluating cacheableInfo annotation", t);
    }
    return result;
}

From source file:org.entando.entando.aps.system.services.cache.CacheInfoManager.java

License:Open Source License

@Around("@annotation(cacheInfoEvict)")
public Object aroundCacheInfoEvictMethod(ProceedingJoinPoint pjp, CacheInfoEvict cacheInfoEvict)
        throws Throwable {
    try {/*from  www .  ja v  a 2  s.  com*/
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        Method targetMethod = methodSignature.getMethod();
        Class targetClass = pjp.getTarget().getClass();
        Method effectiveTargetMethod = targetClass.getMethod(targetMethod.getName(),
                targetMethod.getParameterTypes());
        String[] cacheNames = cacheInfoEvict.value();
        Object groupsCsv = this.evaluateExpression(cacheInfoEvict.groups().toString(), targetMethod,
                pjp.getArgs(), effectiveTargetMethod, targetClass);
        if (null != groupsCsv && groupsCsv.toString().trim().length() > 0) {
            String[] groups = groupsCsv.toString().split(",");
            for (String group : groups) {
                for (String cacheName : cacheNames) {
                    this.flushGroup(cacheName, group);
                }
            }
        }
    } catch (Throwable t) {
        logger.error("Error while flushing group", t);
        throw new ApsSystemException("Error while flushing group", t);
    }
    return pjp.proceed();
}

From source file:org.exist.security.PermissionRequiredAspect.java

License:Open Source License

@Before("methodParameterWithPermissionRequired(permission, o)")
public void enforcePermissionsOnParameter(JoinPoint joinPoint, Permission permission, Object o)
        throws PermissionDeniedException {

    //the next two lines can be replaced when this aspectj bug is closed - https://bugs.eclipse.org/bugs/show_bug.cgi?id=259416
    final MethodSignature ms = (MethodSignature) joinPoint.getSignature();
    final PermissionRequired parameterPermissionRequired = (PermissionRequired) ms.getMethod()
            .getParameterAnnotations()[0][0];

    //1) check if we should allow DBA access
    if (((parameterPermissionRequired.user() & IS_DBA) == IS_DBA) && permission.isCurrentSubjectDBA()) {
        return;//w  w  w.  j a v a2  s .  c o  m
    }

    //2) check if the user is in the target group
    if ((parameterPermissionRequired.user() & IS_MEMBER) == IS_MEMBER) {
        final Integer groupId = (Integer) o;
        if (permission.isCurrentSubjectInGroup(groupId)) {
            return;
        }
    }

    //3) check if we are looking for setGID
    if ((parameterPermissionRequired.mode() & IS_SET_GID) == IS_SET_GID) {
        final Permission other = (Permission) o;
        if (other.isSetGid()) {
            return;
        }
    }

    throw new PermissionDeniedException("You must be a member of the group you are changing the item to");
}

From source file:org.failearly.ajunit.AjUnitAspectTest.java

License:Open Source License

private static MethodSignature getMethodSignature() throws NoSuchMethodException {
    final MethodSignature methodSignature = Mockito.mock(MethodSignature.class);
    Mockito.when(methodSignature.getMethod()).thenReturn(method());
    return methodSignature;
}

From source file:org.failearly.ajunit.internal.universe.matcher.MethodJoinPointMatcher.java

License:Open Source License

@Override
protected boolean doMatchSignature(MethodSignature signature, AjJoinPoint ajUnitJoinPoint) {
    return signature.getMethod().equals(ajUnitJoinPoint.getMethod());
}

From source file:org.failearly.ajunit.internal.universe.matcher.MethodJoinPointMatcherTest.java

License:Open Source License

private Signature createMethodSignatureMock(String methodName) {
    final MethodSignature methodSignature = mock(MethodSignature.class);
    when(methodSignature.getMethod()).thenReturn(resolveMethod(methodName));

    LOGGER.debug("MethodSignature Mock - getField() = {}", methodSignature.getMethod());

    return methodSignature;
}

From source file:org.finra.dm.core.StopWatchAdvice.java

License:Apache License

/**
 * Logs the time it takes to execute the method at the join point if the class or method isn't annotated with SuppressLogging and if the log level is set to
 * info./* w w w. jav a  2s. co m*/
 *
 * @param pjp the join point.
 *
 * @return the return value of the method at the join point.
 * @throws Throwable if any errors were encountered.
 */
@SuppressWarnings("rawtypes")
public static Object logMethodTime(ProceedingJoinPoint pjp) throws Throwable {
    // Get the target class being called.
    Class targetClass = pjp.getTarget().getClass();

    // Get the target method being called.
    MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature();
    Method targetMethod = targetMethodSignature.getMethod();
    if (targetMethod.getDeclaringClass().isInterface()) {
        // Get the underlying implementation if we are given an interface.
        targetMethod = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(),
                targetMethod.getParameterTypes());
    }

    // Only keep a stop watch if the class and method aren't suppressing logging and the log level is info.
    if ((AnnotationUtils.findAnnotation(targetClass, SuppressLogging.class) == null)
            && (AnnotationUtils.findAnnotation(targetMethod, SuppressLogging.class) == null)
            && (LOGGER.isInfoEnabled())) {
        // Start the stop watch.
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        // Proceed to the join point (i.e. call the method and let it return).
        Object returnValue = pjp.proceed();

        // Log the duration.
        LOGGER.info("Method " + targetClass.getName() + "." + targetMethodSignature.getName() + " took "
                + DmDateUtils.formatDuration(stopWatch.getTime(), true) + ".");

        // Return the method return value.
        return returnValue;
    } else {
        // Invoke the method normally.
        return pjp.proceed();
    }
}

From source file:org.finra.herd.core.StopWatchAdvice.java

License:Apache License

/**
 * Logs the time it takes to execute the method at the join point if the class or method isn't annotated with SuppressLogging and if the log level is set to
 * info.//  ww  w. ja v  a2  s.  c  o m
 *
 * @param pjp the join point.
 *
 * @return the return value of the method at the join point.
 * @throws Throwable if any errors were encountered.
 */
@SuppressWarnings("rawtypes")
public static Object logMethodTime(ProceedingJoinPoint pjp) throws Throwable {
    // Get the target class being called.
    Class targetClass = pjp.getTarget().getClass();

    // Get the target method being called.
    MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature();
    Method targetMethod = targetMethodSignature.getMethod();
    if (targetMethod.getDeclaringClass().isInterface()) {
        // Get the underlying implementation if we are given an interface.
        targetMethod = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(),
                targetMethod.getParameterTypes());
    }

    // Only keep a stop watch if the class and method aren't suppressing logging and the log level is info.
    if ((AnnotationUtils.findAnnotation(targetClass, SuppressLogging.class) == null)
            && (AnnotationUtils.findAnnotation(targetMethod, SuppressLogging.class) == null)
            && (LOGGER.isInfoEnabled())) {
        // Start the stop watch.
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        // Proceed to the join point (i.e. call the method and let it return).
        Object returnValue = pjp.proceed();

        // Log the duration.
        LOGGER.info("Method " + targetClass.getName() + "." + targetMethodSignature.getName() + " took "
                + HerdDateUtils.formatDuration(stopWatch.getTime(), true) + ".");

        // Return the method return value.
        return returnValue;
    } else {
        // Invoke the method normally.
        return pjp.proceed();
    }
}

From source file:org.finra.herd.dao.MethodLoggingAdvice.java

License:Apache License

/**
 * Around advice that logs methods being invoked for all DAO operations methods.
 *
 * @param pjp the proceeding join point.
 *
 * @return the return value of the method we are advising.
 * @throws Throwable if there were any problems executing the method.
 *///w  w w  . j a  v a2  s  .co m
@Around("operationsMethods()")
public Object logMethodBeingInvoked(ProceedingJoinPoint pjp) throws Throwable {
    // Get the target class being called.
    Class<?> targetClass = pjp.getTarget().getClass();

    // Get the target method being called.
    MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature();
    Method targetMethod = targetMethodSignature.getMethod();
    if (targetMethod.getDeclaringClass().isInterface()) {
        // Get the underlying implementation if we are given an interface.
        targetMethod = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(),
                targetMethod.getParameterTypes());
    }

    // Only log the method if the class and method aren't suppressing logging and the log level is debug.
    if ((AnnotationUtils.findAnnotation(targetClass, SuppressLogging.class) == null)
            && (AnnotationUtils.findAnnotation(targetMethod, SuppressLogging.class) == null)
            && (LOGGER.isDebugEnabled())) {
        LOGGER.debug("javaMethod=\"{}.{}\"", targetClass.getName(), targetMethodSignature.getName());
    }

    // Proceed to the join point (i.e. call the method and let it return).
    return pjp.proceed();
}

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

License:Apache License

/**
 * Check permission on the service methods before the execution. The method is expected to throw AccessDeniedException if current user does not have the
 * permissions./*from  w w  w  .  ja  va2  s . com*/
 * 
 * @param joinPoint The join point
 */
@Before("serviceMethods()")
public void checkPermission(JoinPoint joinPoint) {

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

    List<NamespacePermission> namespacePermissions = new ArrayList<>();
    if (method.isAnnotationPresent(NamespacePermissions.class)) {
        namespacePermissions.addAll(Arrays.asList(method.getAnnotation(NamespacePermissions.class).value()));
    } else if (method.isAnnotationPresent(NamespacePermission.class)) {
        namespacePermissions.add(method.getAnnotation(NamespacePermission.class));
    }

    if (!namespacePermissions.isEmpty()) {
        String[] parameterNames = methodSignature.getParameterNames();
        Object[] args = joinPoint.getArgs();

        Map<String, Object> variables = new HashMap<>();
        for (int i = 0; i < parameterNames.length; i++) {
            variables.put(parameterNames[i], args[i]);
        }

        List<AccessDeniedException> accessDeniedExceptions = new ArrayList<>();
        for (NamespacePermission namespacePermission : namespacePermissions) {
            for (String field : namespacePermission.fields()) {
                try {
                    namespaceSecurityHelper.checkPermission(
                            spelExpressionHelper.evaluate(field, Object.class, variables),
                            namespacePermission.permissions());
                } catch (AccessDeniedException accessDeniedException) {
                    accessDeniedExceptions.add(accessDeniedException);
                }
            }
        }
        if (!accessDeniedExceptions.isEmpty()) {
            throw namespaceSecurityHelper.getAccessDeniedException(accessDeniedExceptions);
        }
    }
}