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.xaloon.core.api.interceptor.AbstractInterceptor.java

License:Apache License

/**
 * @param pjp/*from w  w  w  .  j a  va  2 s .  c  o m*/
 * @return
 * @throws Throwable
 */
public Object aroundInvokeAspectJ(ProceedingJoinPoint pjp) throws Throwable {
    org.aspectj.lang.Signature s = pjp.getSignature();
    final MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
    Method method = methodSignature.getMethod();
    if (method.getDeclaringClass().isInterface()) {
        method = ClassUtil.getMethodIfAvailable(pjp.getTarget().getClass(), method.getName(),
                method.getParameterTypes());
    }
    if (method == null) {
        throw new RuntimeException("Method not found!");
    }
    Signature signature = new Signature().setMethodName(s.getName()).setDeclaringType(s.getDeclaringType())
            .setDeclaringTypeName(s.getDeclaringTypeName()).setParameters(pjp.getArgs());
    beforeProceed(signature, method.getAnnotation(getAnnotationClass()));
    return pjp.proceed();
}

From source file:ru.caramel.juniperbot.web.common.aspect.GuildIdAspect.java

License:Open Source License

@Around("execution(public * *(.., @ru.caramel.juniperbot.web.common.aspect.GuildId (*), ..))")
public Object doAwesomeStuff(ProceedingJoinPoint thisJoinPoint) throws Throwable {
    try {/*  w  w w . j  a  v  a  2s .  c  o m*/
        Object[] methodArgs = thisJoinPoint.getArgs();
        int numArgs = methodArgs.length;
        MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature();
        Annotation[][] annotationMatrix = methodSignature.getMethod().getParameterAnnotations();
        for (int i = 0; i < numArgs; i++) {
            Annotation[] annotations = annotationMatrix[i];
            for (Annotation annotation : annotations) {
                if (annotation.annotationType() == GuildId.class && methodArgs[i] instanceof Long) {
                    GuildId guildIdAnnotation = (GuildId) annotation;
                    long guildId = (long) methodArgs[i];
                    MDC.put("guildId", String.valueOf(guildId));
                    if (guildIdAnnotation.validate()) {
                        DiscordGuildDetails details = tokenServices.getGuildById(guildId);
                        if (details == null) {
                            throw new NotFoundException();
                        }
                        if (!tokenServices.hasPermission(details)) {
                            throw new AccessDeniedException();
                        }
                    }
                }
            }
        }
        return thisJoinPoint.proceed();
    } finally {
        MDC.remove("guildId");
    }
}

From source file:se.omik.squash.SquashAdviser.java

License:Open Source License

/**
 * Aspect applied to read operations.//from  w w w.  j  a  va  2 s.co  m
 */
@Around(value = "execution(* *(..)) && @annotation(squashBefore)")
public Object readMemcached(ProceedingJoinPoint joinPoint, SquashBefore squashBefore) throws Throwable {
    long start = System.currentTimeMillis();
    String domain = getDomain(joinPoint);
    String keyToRead = squashBefore.key();
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    if (keyToRead == null || keyToRead.length() == 0) {
        StringBuilder guessedKey = new StringBuilder();
        for (int i = 0; i < joinPoint.getArgs().length; i++) {
            guessedKey.append(":$").append(i);
        }
        if (guessedKey.length() == 0) {
            guessedKey.append(":").append(methodSignature.getName());
        }
        if (guessedKey.length() > 0) {
            keyToRead = methodSignature.getReturnType().getSimpleName() + guessedKey.toString();
        }

    }
    String key = getKey(domain, keyToRead, joinPoint.getArgs());//get the key of the object
    int expiration = squashBefore.expiration();
    if (expiration == -1 && squashBefore.expirationFromParameter() >= 0) {
        expiration = Integer
                .parseInt(String.valueOf(joinPoint.getArgs()[squashBefore.expirationFromParameter()]));
    }
    if (logger.isDebugEnabled()) {
        logger.debug("cacheRead: " + methodSignature.getMethod().getDeclaringClass().getSimpleName() + "."
                + methodSignature.getMethod().getName() + " [" + key + "]");
    }
    Object object = squashProvider.get(key);//try to get the object from the cache
    if (object == null) {//if the object is not in the cache...
        object = joinPoint.proceed();//invoke the method 
        if (logger.isDebugEnabled()) {
            logger.debug("object didn't exist in cache, executing method: "
                    + methodSignature.getMethod().getDeclaringClass().getSimpleName() + "."
                    + methodSignature.getMethod().getName() + " took " + (System.currentTimeMillis() - start)
                    + "ms");
        }
        if (squashBefore.cacheWhenNull() && object == null) {
            object = "null";
        }
        if (object != null) {
            String keys = key;
            if (squashBefore.updateOtherEntries().length() > 0) {
                keys += "," + squashBefore.updateOtherEntries();
            }
            updateKeys(object, domain, keys, expiration, squashBefore.useKeyList(), squashBefore.mainKey(),
                    joinPoint.getArgs());
        }
    } else if (logger.isDebugEnabled()) {
        logger.debug("returning cached object");
    }
    if ("null".equals(object)) {
        object = null;
    }
    return object;
}

From source file:se.omik.squash.SquashAdviser.java

License:Open Source License

/**
 * Aspect applied to delete operations.//  w  w w . j a  v a 2s.c o m
 */
@Around(value = "execution(* *(..)) && @annotation(deleteFromSquash)")
public void deleteMemcached(ProceedingJoinPoint joinPoint, DeleteFromSquash deleteFromSquash) throws Throwable {
    joinPoint.proceed();
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    if (logger.isDebugEnabled()) {
        logger.debug("cacheDelete: " + methodSignature.getMethod().getDeclaringClass().getSimpleName() + "."
                + methodSignature.getMethod().getName() + " [" + joinPoint.getArgs()[0] + "]");
    }
    String domain = getDomain(joinPoint);
    String deleteEntries = deleteFromSquash.deleteEntries();
    if (deleteEntries == null || deleteEntries.length() == 0) {
        deleteEntries = ((MethodSignature) joinPoint.getSignature()).getReturnType().getSimpleName() + ":$"
                + deleteFromSquash.keyField();
        if (logger.isDebugEnabled()) {
            logger.debug("guessed: " + deleteEntries);
        }
    }
    deleteKeys(joinPoint.getArgs()[0], domain, deleteEntries, deleteFromSquash.useKeyList(),
            joinPoint.getArgs());
}

From source file:se.omik.squash.SquashAdviser.java

License:Open Source License

/**
 * Aspect applied to update operations./*  ww  w .  j a v a 2 s.com*/
 */
@AfterReturning(value = "execution(* *(..)) && @annotation(squashAfter)", returning = "object")
public void updateMemcached(JoinPoint joinPoint, Object object, SquashAfter squashAfter) {
    if (object != null) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        if (logger.isDebugEnabled()) {
            logger.debug("cacheUpdate: " + methodSignature.getMethod().getDeclaringClass().getSimpleName() + "."
                    + methodSignature.getMethod().getName() + " [" + object + "]");
        }
        String domain = getDomain(joinPoint);
        String updateEntries = squashAfter.updateEntries();
        int expiration = squashAfter.expiration();
        if (expiration == -1 && squashAfter.expirationFromParameter() >= 0) {
            expiration = Integer
                    .parseInt(String.valueOf(joinPoint.getArgs()[squashAfter.expirationFromParameter()]));
        }
        if (updateEntries == null || updateEntries.length() == 0) {
            updateEntries = methodSignature.getReturnType().getSimpleName() + ":$" + squashAfter.keyField();
            if (logger.isDebugEnabled()) {
                logger.debug("guessed: " + updateEntries);
            }
        }
        updateKeys(object, domain, updateEntries, expiration, squashAfter.useKeyList(), squashAfter.mainKey(),
                joinPoint.getArgs());
        deleteKeys(object, domain, squashAfter.deleteEntries(), false, joinPoint.getArgs());
    }
}

From source file:se.omik.squash.SquashAdviser.java

License:Open Source License

/**
 * Aspect applied to collection operations.
 */// w  ww.  ja  va2 s  .  com
@AfterReturning(value = "execution(* *(..)) && @annotation(squashedCollection)", returning = "collection")
public void cacheListElementsInMemcached(JoinPoint joinPoint, Object collection,
        SquashedCollection squashedCollection) {
    if (collection != null) {
        if (collection instanceof Collection) {
            for (Object object : (Collection) collection) {
                MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "cacheUpdate: " + methodSignature.getMethod().getDeclaringClass().getSimpleName()
                                    + "." + methodSignature.getMethod().getName() + " [" + object + "]");
                }
                String domain = getDomain(joinPoint);
                String updateEntries = squashedCollection.updateEntries();
                int expiration = squashedCollection.expiration();
                if (expiration == -1 && squashedCollection.expirationFromParameter() >= 0) {
                    expiration = Integer.parseInt(
                            String.valueOf(joinPoint.getArgs()[squashedCollection.expirationFromParameter()]));
                }
                if (updateEntries == null || updateEntries.length() == 0) {
                    updateEntries = methodSignature.getReturnType().getSimpleName() + ":$"
                            + squashedCollection.keyField();
                    if (logger.isDebugEnabled()) {
                        logger.debug("guessed: " + updateEntries);
                    }
                }
                updateKeys(object, domain, updateEntries, expiration, false, 0, squashedCollection.newOnly(),
                        joinPoint.getArgs());
            }
        } else {
            logger.error("squashedCollection annotation around a method that doesn't return a collection ...");
        }
    }
}

From source file:se.omik.squash.SquashAdviser.java

License:Open Source License

@AfterReturning(value = "execution(* *(..)) && @annotation(flushAllFromSquash)", returning = "object")
public void flushMemcached(JoinPoint joinPoint, Object object, FlushAllFromSquash flushAllFromSquash) {
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    if (logger.isDebugEnabled()) {
        logger.debug("cacheFlush: " + methodSignature.getMethod().getDeclaringClass().getSimpleName() + "."
                + methodSignature.getMethod().getName());
    }/*w  ww .  j a  va2 s  .  c  o m*/
    squashProvider.flushAll();
}

From source file:uk.gov.ofwat.fountain.aspect.audit.AuditAdvice.java

License:Open Source License

private HashMap<String, String> getParams(ProceedingJoinPoint pjp) {
    HashMap<String, String> params = new HashMap<String, String>();
    Object[] args = pjp.getArgs();
    Signature signature = pjp.getStaticPart().getSignature();
    if (signature instanceof MethodSignature) {
        MethodSignature ms = (MethodSignature) signature;
        Method method = ms.getMethod();
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();

        String[] parameterNames = ms.getParameterNames();
        for (int i = 0; i < args.length; i++) {
            String argsString = "";
            if (args[i] != null) {
                argsString = args[i].toString();
            }//from   w  w  w .  j  ava  2  s. c o  m
            params.put(parameterNames[i], argsString);
        }
    }
    return params;
}

From source file:uk.gov.ofwat.fountain.aspect.audit.AuditAdvice.java

License:Open Source License

private HashMap<String, Object> getContent(ProceedingJoinPoint pjp) {
    HashMap<String, Object> content = new HashMap<String, Object>();
    Object[] args = pjp.getArgs();
    Signature signature = pjp.getStaticPart().getSignature();
    if (signature instanceof MethodSignature) {
        MethodSignature ms = (MethodSignature) signature;
        Method method = ms.getMethod();
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        String[] parameterNames = ms.getParameterNames();
        Class<?>[] parameterTypes = method.getParameterTypes();
        for (int i = 0; i < parameterAnnotations.length; i++) {
            if (org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput.class == parameterTypes[i]
                    || javax.ws.rs.core.SecurityContext.class == parameterTypes[i]
                    || javax.ws.rs.core.UriInfo.class == parameterTypes[i]
                    || javax.servlet.http.HttpServletRequest.class == parameterTypes[i]) {
                continue;
            }//from  www  .  j a v  a 2 s . c  o m
            content.put(parameterNames[i], args[i]);
        }
    }
    return content;
}

From source file:uk.ltd.woodsideconsultancy.aop.cache.CacheKeyFinder.java

License:Open Source License

/**
 * determine keys to use for cache storage based on joinpoint
 * these are either the parameters for the method or only those
 * with a CacheKey annotation/*  w ww  .  j av  a  2  s  . c  o  m*/
 * @param joinPoint
 * @return
 */
@SuppressWarnings("unchecked")
public static Object[] getKeys(JoinPoint joinPoint) {
    Object keys[] = joinPoint.getArgs();
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Object target = joinPoint.getTarget();

    String methodName = signature.getMethod().getName();
    Class<?>[] parameterTypes = signature.getMethod().getParameterTypes();
    try {
        List<Object> keyList = new ArrayList<Object>();
        int paramIdx = 0;
        Annotation[][] annotations = target.getClass().getMethod(methodName, parameterTypes)
                .getParameterAnnotations();
        for (Annotation[] anos : annotations) {
            for (Annotation ano : anos) {
                if (ano.annotationType().equals(CacheKey.class)) {
                    CacheKey sk = (CacheKey) ano;
                    @SuppressWarnings("rawtypes")
                    Formater formater = formaters.get(sk.style());
                    if (formater != null) {
                        keyList.add(formater.format(keys[paramIdx], sk.format()));
                    } else {
                        keyList.add(keys[paramIdx]);
                    }
                }
            }
            paramIdx++;
        }
        if (keyList.size() > 0) {
            keys = keyList.toArray();
        }
    } catch (NoSuchMethodException nsme) {

    }

    return keys;
}