Example usage for java.lang.reflect Method getAnnotation

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

Introduction

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

Prototype

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) 

Source Link

Usage

From source file:org.querybyexample.jpa.JpaUtil.java

public static <T extends Identifiable<?>> boolean hasSimplePk(T entity) {
    for (Method m : entity.getClass().getMethods()) {
        if (m.getAnnotation(Id.class) != null) {
            return true;
        }//  w w w .jav a2  s .com
    }
    for (Field f : entity.getClass().getFields()) {
        if (f.getAnnotation(Id.class) != null) {
            return true;
        }
    }
    return false;
}

From source file:org.querybyexample.jpa.JpaUtil.java

private static boolean isPrimaryKey(Method method) {
    return isPublic(method.getModifiers())
            && (method.getAnnotation(Id.class) != null || method.getAnnotation(EmbeddedId.class) != null);
}

From source file:me.Laubi.MineMaze.SubCommands.java

private static boolean validateRegion(Region r, Method subCmd) {
    int maxHeight = -1, maxLength = -1, maxWidth = -1, minHeight = 1, minWidth = 5, minLength = 5;

    if (subCmd.isAnnotationPresent(SizeValidation.class)) {
        SizeValidation sizev = subCmd.getAnnotation(SizeValidation.class);
        maxHeight = sizev.maxHeight();//  w  w w. j  a v  a  2 s . c o  m
        maxLength = sizev.maxLength();
        maxWidth = sizev.maxWidth();
        minHeight = sizev.minHeight();
        minWidth = sizev.minWidth();
        minLength = sizev.minLength();
    }

    return !((r.getWidth() < minWidth) || (r.getHeight() < minHeight) || (r.getLength() < minLength) ||

            (maxWidth < 0 ? false : r.getWidth() > maxWidth)
            || (maxHeight < 0 ? false : r.getHeight() > maxHeight)
            || (maxLength < 0 ? false : r.getLength() > maxLength));
}

From source file:com.spstudio.common.log.ServiceExceptionLogAspect.java

/**
 * *//from  www.  j  av a  2s .  co  m
 * ???? service *
 *
 *
 * @param joinPoint 
 *
 * @return ??
 *
 * @throws Exception *
 */
public static String getServiceMthodDescription(JoinPoint joinPoint) throws Exception {

    String targetName = joinPoint.getTarget().getClass().getName();

    String methodName = joinPoint.getSignature().getName();

    Object[] arguments = joinPoint.getArgs();

    Class targetClass = Class.forName(targetName);

    Method[] methods = targetClass.getMethods();

    String description = ":" + targetName + " ;??:" + methodName
            + " ;???:";

    for (Method method : methods) {

        if (method.getName().equals(methodName)) {

            Class[] clazzs = method.getParameterTypes();

            if (clazzs.length == arguments.length) {

                description += method.getAnnotation(ServiceExceptionLog.class).description();

                break;

            }

        }

    }

    return description;

}

From source file:RssAtomGenerationTest.java

public static List<Method> getSyndicationElementsOfType(Class clazz, FeedType type) {
    List<Method> methodList = new ArrayList<Method>();
    final Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(SyndicationElement.class)
                && method.getAnnotation(SyndicationElement.class).type().equals(type)) {
            methodList.add(method);/*from  w  w  w. j  av  a 2  s . co  m*/
        }
    }
    return methodList;
}

From source file:com.haulmont.cuba.core.config.ConfigUtil.java

public static SourceType getSourceType(Class<?> configInterface, Method method) {
    Source source = method.getAnnotation(Source.class);
    if (source == null) {
        Method getMethod = getGetMethod(configInterface, method);
        if (getMethod != null && !method.equals(getMethod)) {
            source = getMethod.getAnnotation(Source.class);
        }//  ww  w.  j  av a2  s . c o m
        if (source == null) {
            source = configInterface.getAnnotation(Source.class);
            if (source == null)
                return SourceType.DATABASE;
        }
    }
    return source.type();
}

From source file:nz.co.senanque.validationengine.ValidationUtils.java

public static void setDefaults(ValidationObject object) {
    for (Field field : object.getClass().getDeclaredFields()) {
        XmlElement xmlElement = field.getAnnotation(XmlElement.class);
        if (xmlElement != null && hasText(xmlElement.defaultValue())) {
            String value = xmlElement.defaultValue();
            Method setter = figureSetter(field.getName(), object.getClass());
            Class<?> type = setter.getParameterTypes()[0];
            try {
                setter.invoke(object, ConvertUtils.convertToObject(type, value, null));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }//from w w  w. j a v a 2  s.  c o m
        }
        String n = field.getName();
        if (n.startsWith("m_")) {
            n = n.substring(2);
        }
        try {
            Method getter = figureGetter(n, object.getClass());
            Annotation unknown = getter.getAnnotation(Unknown.class);
            if (unknown != null) {
                object.getMetadata().addUnknown(field.getName());
            }
        } catch (RuntimeException e) {
            // ignore
        }
    }
}

From source file:net.sourceforge.stripes.integration.spring.SpringHelper.java

/**
 * Looks for all methods and fields annotated with {@code @SpringBean} and attempts
 * to lookup and inject a managed bean into the field/property. If any annotated
 * element cannot be injected an exception is thrown.
 *
 * @param bean the bean into which to inject spring beans
 * @param ctx the Spring application context
 *//* w  ww. ja  va2s  .  c  o  m*/
public static void injectBeans(Object bean, ApplicationContext ctx) {
    // First inject any values using annotated methods
    for (Method m : getMethods(bean.getClass())) {
        try {
            SpringBean springBean = m.getAnnotation(SpringBean.class);
            boolean nameSupplied = !"".equals(springBean.value());
            String name = nameSupplied ? springBean.value() : methodToPropertyName(m);
            Class<?> beanType = m.getParameterTypes()[0];
            Object managedBean = findSpringBean(ctx, name, beanType, !nameSupplied);
            m.invoke(bean, managedBean);
        } catch (Exception e) {
            throw new StripesRuntimeException(
                    "Exception while trying to lookup and inject " + "a Spring bean into a bean of type "
                            + bean.getClass().getSimpleName() + " using method " + m.toString(),
                    e);
        }
    }

    // And then inject any properties that are annotated
    for (Field f : getFields(bean.getClass())) {
        try {
            SpringBean springBean = f.getAnnotation(SpringBean.class);
            boolean nameSupplied = !"".equals(springBean.value());
            String name = nameSupplied ? springBean.value() : f.getName();
            Object managedBean = findSpringBean(ctx, name, f.getType(), !nameSupplied);
            f.set(bean, managedBean);
        } catch (Exception e) {
            throw new StripesRuntimeException(
                    "Exception while trying to lookup and inject " + "a Spring bean into a bean of type "
                            + bean.getClass().getSimpleName() + " using field access on field " + f.toString(),
                    e);
        }
    }
}

From source file:com.crosstreelabs.junited.elasticsearch.ElasticsearchRule.java

protected static List<ElasticSetup> getAnnotations(final Description description) {
    Class<?> testClass = description.getTestClass();
    String methodName = description.getMethodName();

    List<ElasticSetup> result = new ArrayList<>();
    if (testClass.isAnnotationPresent(ElasticSetup.class)) {
        result.add(testClass.getAnnotation(ElasticSetup.class));
    }/*from   ww w .  ja  v  a 2s  .  co  m*/
    if (testClass.isAnnotationPresent(ElasticSetups.class)) {
        result.addAll(Arrays.asList(testClass.getAnnotation(ElasticSetups.class).value()));
    }
    if (methodName == null) {
        return result;
    }

    try {
        Method method = testClass.getDeclaredMethod(methodName);
        if (method.isAnnotationPresent(ElasticSetup.class)) {
            result.add(method.getAnnotation(ElasticSetup.class));
        }
        if (method.isAnnotationPresent(ElasticSetups.class)) {
            result.addAll(Arrays.asList(method.getAnnotation(ElasticSetups.class).value()));
        }
    } catch (NoSuchMethodException | SecurityException ex) {
    }
    return result;
}

From source file:org.querybyexample.jpa.JpaUtil.java

public static <T> boolean isPk(ManagedType<T> mt, SingularAttribute<? super T, ?> attr) {
    try {//from  w  w w  .ja  v  a2s .  c  o m
        Method m = BeanUtils.findMethod(mt.getJavaType(), "get" + WordUtils.capitalize(attr.getName()));
        if (m != null && m.getAnnotation(Id.class) != null) {
            return true;
        }

        Field field = mt.getJavaType().getField(attr.getName());
        return field.getAnnotation(Id.class) != null;
    } catch (Exception e) {
        return false;
    }
}