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.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 w  w  w .  j  ava  2  s  . c  om*/
        UserSession annotation = method.getAnnotation(UserSession.class);
        return annotation.value();
    }
    return null;
}

From source file:org.nuxeo.ecm.platform.ui.web.invalidations.DocumentContextInvalidatorInterceptor.java

private void beforeInvocation(InvocationContext invocationContext) {
    Object target = invocationContext.getTarget();
    for (Method meth : target.getClass().getMethods()) {
        if (meth.isAnnotationPresent(DocumentContextInvalidation.class)) {
            try {
                doInvalidationCall(target, meth);
            } catch (Exception e) {
                log.error("Error during Invalidation method call", e);
            }/*from   w w  w  .ja va 2  s .c  o  m*/
        }
    }
}

From source file:org.waastad.javaeeangular.security.AuthSecurityInterceptor.java

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    String authId = requestContext.getHeaderString(AuthAccessElement.PARAM_AUTH_ID);
    String authToken = requestContext.getHeaderString(AuthAccessElement.PARAM_AUTH_TOKEN);
    log.info("Filter: {} {}", authId, authToken);
    Method methodInvoked = resourceInfo.getResourceMethod();

    if (methodInvoked.isAnnotationPresent(RolesAllowed.class)) {
        log.info("Method is protected..");
        RolesAllowed rolesAllowedAnnotation = methodInvoked.getAnnotation(RolesAllowed.class);
        Set<String> rolesAllowed = new HashSet<>(Arrays.asList(rolesAllowedAnnotation.value()));
        //            try {
        //                authServiceBeanLocal = (AuthServiceBeanLocal) new InitialContext().lookup("java:global/AuthServiceBean!org.waastad.javaeeangular.ejb.AuthServiceBeanLocal");

        if (!authServiceBeanLocal.isAuthorized(authId, authToken, rolesAllowed)) {
            requestContext.abortWith(ACCESS_UNAUTHORIZED);
        }/*from   w ww .j  av  a  2s .  c om*/
        //            } catch (NamingException e) {
        //                log.error("Error: {}", ExceptionUtils.getRootCauseMessage(e));
        //                requestContext.abortWith(ACCESS_UNAUTHORIZED);
        //            }
    } else {
        log.info("Method is NOT protected..");
    }
}

From source file:com.cloudera.nav.plugin.model.ValidationUtil.java

public void validateRequiredMProperties(Object mPropertyObj) {
    for (Method method : mPropertyObj.getClass().getMethods()) {
        if (!method.isBridge() && method.isAnnotationPresent(MProperty.class)
                && method.getAnnotation(MProperty.class).required()) {
            Class<?> returnType = method.getReturnType();
            Object value;// w ww . ja v a 2  s . c  o  m
            try {
                value = method.invoke(mPropertyObj);
            } catch (IllegalAccessException e) {
                throw new IllegalArgumentException("Could not access MProperty method " + method.getName(), e);
            } catch (InvocationTargetException e) {
                throw new IllegalArgumentException("Could not get MProperty value for " + method.getName(), e);
            }
            if (Collection.class.isAssignableFrom(returnType)) {
                Preconditions.checkArgument(!CollectionUtils.isEmpty((Collection) value),
                        method.getName() + " returned empty collection");
            } else if (String.class.isAssignableFrom(returnType)) {
                Preconditions.checkArgument(!StringUtils.isEmpty((String) value),
                        method.getName() + " returned null or empty string");
            } else {
                Preconditions.checkArgument(value != null, method.getName() + " returned null");
            }
        }
    }
}

From source file:org.sipfoundry.sipxconfig.setting.BeanValueStorage.java

public BeanValueStorage(Object bean) {
    m_bean = bean;//  w  ww.j ava 2  s . co m
    for (Method m : m_bean.getClass().getMethods()) {
        if (m.isAnnotationPresent(SettingEntry.class)) {
            SettingEntry entry = m.getAnnotation(SettingEntry.class);
            if (!StringUtils.isBlank(entry.path())) {
                m_methods.put(entry.path(), m);
            } else {
                for (String s : entry.paths()) {
                    m_methods.put(s, m);
                }
            }
        }
    }
}

From source file:my.school.spring.beans.PostProxyInvokerContextListener.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    ApplicationContext context = event.getApplicationContext();

    for (String beanName : context.getBeanDefinitionNames()) {
        final String beanClassName = beanFactory.getBeanDefinition(beanName).getBeanClassName();
        if (beanClassName == null) {
            continue;
        }//from  w  ww. ja v  a 2  s . c om
        try {
            Class<?> originalClass = Class.forName(beanClassName);
            for (Method originalMethod : originalClass.getMethods()) {
                if (originalMethod.isAnnotationPresent(PostProxy.class)) {
                    try {
                        Object bean = context.getBean(beanName);
                        Method currentMethod = bean.getClass().getMethod(originalMethod.getName(),
                                originalMethod.getParameterTypes());
                        currentMethod.invoke(bean);
                    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
                            | NoSuchMethodException | SecurityException ex) {
                        LOG.error(ex.getLocalizedMessage());
                    }
                }
            }
        } catch (NullPointerException | ClassNotFoundException ex) {
            LOG.warn("Failed to find bean's class: {} (bean name: {})", beanClassName, beanName);
        }
    }
}

From source file:io.stallion.reflection.PropertyUtils.java

public static <T extends Annotation> T getAnnotationForProperty(Class cls, String name, Class<T> anno) {
    String postFix = name.toUpperCase();
    if (name.length() > 1) {
        postFix = name.substring(0, 1).toUpperCase() + name.substring(1);
    }/*w ww  .  j  a v a 2  s  . co  m*/
    Method method = null;
    try {
        method = cls.getMethod("get" + postFix);
    } catch (NoSuchMethodException e) {

    }
    if (method == null) {
        try {
            method = cls.getMethod("is" + postFix);
        } catch (NoSuchMethodException e) {

        }
    }
    if (method == null) {
        return null;
    }
    if (method.getModifiers() != Modifier.PUBLIC) {
        return null;
    }
    if (method.isAnnotationPresent(anno)) {
        return method.getDeclaredAnnotation(anno);
    }
    return null;
}

From source file:org.socialsignin.spring.data.dynamodb.mapping.DynamoDBMappingContext.java

@Override
protected boolean shouldCreatePersistentEntityFor(TypeInformation<?> type) {

    boolean hasHashKey = false;
    boolean hasRangeKey = false;
    for (Method method : type.getType().getMethods()) {
        if (method.isAnnotationPresent(DynamoDBHashKey.class))
            hasHashKey = true;/*w  ww.  j a v a  2 s . c om*/
        if (method.isAnnotationPresent(DynamoDBRangeKey.class))
            hasRangeKey = true;

    }
    for (Field field : type.getType().getFields()) {
        if (field.isAnnotationPresent(DynamoDBHashKey.class))
            hasHashKey = true;
        if (field.isAnnotationPresent(DynamoDBRangeKey.class))
            hasRangeKey = true;

    }
    return type.getType().isAnnotationPresent(DynamoDBTable.class) || (hasHashKey && hasRangeKey);
}

From source file:py.una.pol.karaku.repo.KarakuBaseDao.java

private void cargarMetodos() {

    Method[] m = this.getClassOfT().getMethods();

    for (Method method : m) {
        if (method.isAnnotationPresent(PrePersist.class)) {
            this.prePersist = method;
        }//from   www. ja  v a2  s  .com
        if (method.isAnnotationPresent(PreUpdate.class)) {
            this.preUpdate = method;
        }
    }

}

From source file:org.openmrs.web.dwr.DeprecationCheckTest.java

/**
 * For the given class, checks if it contains any {@literal @}Deprecated annotation (at method/class level).
 * @param metadataReader//from   w w  w. j a  v a2s . com
 * @return true if it finds {@literal @}Deprecated annotation in the class or any of its methods.
 * @throws ClassNotFoundException
 */
private boolean doesClassContainDeprecatedAnnotation(MetadataReader metadataReader)
        throws ClassNotFoundException {
    try {
        Class dwrClass = Class.forName(metadataReader.getClassMetadata().getClassName());

        if (dwrClass.isAnnotationPresent(Deprecated.class)) {
            return true;
        }

        Method[] methods = dwrClass.getDeclaredMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(Deprecated.class))
                return true;
        }
    } catch (Throwable e) {
    }
    return false;
}