Example usage for java.lang.reflect Method isAnnotationPresent

List of usage examples for java.lang.reflect Method isAnnotationPresent

Introduction

In this page you can find the example usage for java.lang.reflect Method isAnnotationPresent.

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:com.netflix.hystrix.contrib.javanica.utils.MethodProvider.java

/**
 * Gets fallback method for command method.
 *
 * @param type          type/*from   www . j  a va  2s  .  c o m*/
 * @param commandMethod the command method. in the essence it can be a fallback
 *                      method annotated with HystrixCommand annotation that has a fallback as well.
 * @param extended      true if the given commandMethod was derived using additional parameter, otherwise - false
 * @return new instance of {@link FallbackMethod} or {@link FallbackMethod#ABSENT} if there is no suitable fallback method for the given command
 */
public FallbackMethod getFallbackMethod(Class<?> type, Method commandMethod, boolean extended) {
    if (commandMethod.isAnnotationPresent(HystrixCommand.class)) {
        HystrixCommand hystrixCommand = commandMethod.getAnnotation(HystrixCommand.class);
        if (StringUtils.isNotBlank(hystrixCommand.fallbackMethod())) {
            Class<?>[] parameterTypes = commandMethod.getParameterTypes();
            if (extended && parameterTypes[parameterTypes.length - 1] == Throwable.class) {
                parameterTypes = ArrayUtils.remove(parameterTypes, parameterTypes.length - 1);
            }
            Class<?>[] exParameterTypes = Arrays.copyOf(parameterTypes, parameterTypes.length + 1);
            exParameterTypes[parameterTypes.length] = Throwable.class;
            Optional<Method> exFallbackMethod = getMethod(type, hystrixCommand.fallbackMethod(),
                    exParameterTypes);
            Optional<Method> fMethod = getMethod(type, hystrixCommand.fallbackMethod(), parameterTypes);
            Method method = exFallbackMethod.or(fMethod).orNull();
            if (method == null) {
                throw new FallbackDefinitionException("fallback method wasn't found: "
                        + hystrixCommand.fallbackMethod() + "(" + Arrays.toString(parameterTypes) + ")");
            }
            return new FallbackMethod(method, exFallbackMethod.isPresent());
        }
    }
    return FallbackMethod.ABSENT;
}

From source file:com.liuguangqiang.permissionhelper.PermissionHelper.java

/**
 * Invoke a method with annotation PermissionGranted.
 *
 * @param obj//from w  w w  .  j  a  va 2 s. c o m
 * @param permission
 */
private void invokeGrantedMethod(Object obj, String permission) {
    Class clazz = obj.getClass();
    PermissionGranted permissionGranted;

    for (Method method : clazz.getMethods()) {
        if (method.isAnnotationPresent(PermissionGranted.class)) {
            permissionGranted = method.getAnnotation(PermissionGranted.class);
            if (permissionGranted.permission().equals(permission)) {
                if (method.getModifiers() != Modifier.PUBLIC) {
                    throw new IllegalArgumentException(
                            String.format("Annotation method %s must be public.", method));
                }

                if (method.getParameterTypes().length > 0) {
                    throw new RuntimeException(String.format("Cannot execute non-void method %s.", method));
                }

                try {
                    method.invoke(obj);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:com.liuguangqiang.permissionhelper.PermissionHelper.java

/**
 * Invoke a method with annotation PermissionDenied.
 *
 * @param obj/*w w  w  . j  av a 2  s  .c om*/
 * @param permission
 */
private void invokeDeniedMethod(Object obj, String permission) {
    Class clazz = obj.getClass();
    PermissionDenied permissionDenied;

    for (Method method : clazz.getMethods()) {
        if (method.isAnnotationPresent(PermissionDenied.class)) {
            permissionDenied = method.getAnnotation(PermissionDenied.class);
            if (permissionDenied.permission().equals(permission)) {
                if (method.getModifiers() != Modifier.PUBLIC) {
                    throw new IllegalArgumentException(
                            String.format("Annotation method %s must be public.", method));
                }

                if (method.getParameterTypes().length > 0) {
                    throw new RuntimeException(String.format("Cannot execute non-void method %s.", method));
                }

                try {
                    method.invoke(obj);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:io.smartspaces.workbench.project.test.JunitTestClassDetector.java

/**
 * Is the class a JUnit test class?/*  www.j  a va 2 s.c o  m*/
 *
 * @param potentialTestClass
 *          the class to test
 * @param log
 *          the logger to use
 *
 * @return {@code true} if the class is testable and can be instantiated
 */
private boolean isTestClass(Class<?> potentialTestClass, Log log) {
    if (potentialTestClass.isInterface() || !classHasPublicConstructor(potentialTestClass, log)) {
        return false;
    }

    // Do not attempt to run tests on abstract test classes.
    if (Modifier.isAbstract(potentialTestClass.getModifiers())) {
        return false;
    }

    for (Method method : potentialTestClass.getMethods()) {
        if (method.isAnnotationPresent(Test.class)) {

            // No need to check any more if we have 1
            return true;
        }
    }

    return false;
}

From source file:org.devproof.portal.core.module.common.repository.DataProviderRepositoryImpl.java

private boolean isGetterWithBeanQuery(Method method) {
    return method.getName().startsWith("get") && method.isAnnotationPresent(BeanQuery.class);
}

From source file:guru.qas.martini.DefaultMartini.java

@Override
public boolean isAnyStepAnnotated(Class<? extends Annotation> annotationClass) {
    checkNotNull(annotationClass, "null Class");
    Map<Step, StepImplementation> index = getStepIndex();

    boolean evaluation = false;
    Iterator<Map.Entry<Step, StepImplementation>> i = index.entrySet().iterator();
    while (!evaluation && i.hasNext()) {
        Map.Entry<Step, StepImplementation> mapEntry = i.next();
        StepImplementation implementation = mapEntry.getValue();
        Method method = implementation.getMethod();
        evaluation = null != method && method.isAnnotationPresent(annotationClass);
    }//  w  w w .  java2s .  c  om
    return evaluation;
}

From source file:org.alfresco.repo.policy.annotation.AnnotatedBehaviourPostProcessor.java

/**
 * Register behaviours.//w w w .ja va 2  s. c o m
 *
 * @param bean      bean
 * @param beanName  bean name
 */
private void registerBehaviours(Object bean, String beanName) {
    if (bean.getClass().isAnnotationPresent(BehaviourBean.class)) {
        BehaviourBean behaviourBean = bean.getClass().getAnnotation(BehaviourBean.class);

        if (logger.isDebugEnabled()) {
            logger.debug("Annotated behaviour post processing for " + beanName);
        }

        Method[] methods = bean.getClass().getMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(Behaviour.class)) {
                registerBehaviour(behaviourBean, bean, beanName, method);
            }
        }
    }
}

From source file:com.rockagen.gnext.service.spring.security.aspect.OpAspect.java

/**
 * Get method description/* www .  ja  v a 2 s. c  o  m*/
 * 
 * @param method
 * @return method description
 */
private String getMethodDesc(Method method) {

    boolean hasAnnotation = method.isAnnotationPresent(OPLog.class);

    if (hasAnnotation) {
        OPLog annotation = method.getAnnotation(OPLog.class);

        String methodDescp = annotation.description();
        if (log.isDebugEnabled()) {
            log.debug("Target method: " + method.getName() + " Description: " + methodDescp);
        }
        return methodDescp;
    } else {
        return method.getName();
    }
}

From source file:com.evanzeimet.queryinfo.jpa.attribute.DefaultEntityAnnotationsAttributeInfoResolver.java

protected List<Method> findAnnotatedMethods(Class<T> entityClass, Class<? extends Annotation> annotationClass) {
    Method[] methods = entityClass.getMethods();

    int methodCount = methods.length;
    List<Method> annotatedMethods = new ArrayList<Method>(methodCount);

    for (Method method : methods) {
        if (method.isAnnotationPresent(annotationClass)) {
            annotatedMethods.add(method);
        }//  ww  w  .jav a  2 s . c  o  m
    }

    return annotatedMethods;
}

From source file:com.github.tddts.jet.view.fx.spring.DialogProvider.java

private void processInitMethods(Class<?> type, Dialog<?> dialog, Object[] args) {

    List<Method> initMethods = dialogInitCache.get(type);

    // Add dialog init methods to cache
    if (initMethods == null) {
        initMethods = new ArrayList<>();
        for (Method method : type.getDeclaredMethods()) {
            if (method.isAnnotationPresent(FxDialogInit.class))
                initMethods.add(method);
        }//from ww w  .  j a v  a  2 s  .  c o  m
        dialogInitCache.put(type, initMethods);
    }

    // Invoke init methods
    try {
        for (Method method : initMethods) {
            method.setAccessible(true);
            if (method.getParameterCount() == 0) {
                method.invoke(dialog, EMPTY_ARGS);
            } else {
                method.invoke(dialog, args);
            }
        }

    } catch (IllegalAccessException | InvocationTargetException e) {
        throw new DialogException("Could not initialize dialog!", e);
    }
}