Example usage for java.lang.reflect Field getAnnotations

List of usage examples for java.lang.reflect Field getAnnotations

Introduction

In this page you can find the example usage for java.lang.reflect Field getAnnotations.

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:com.test.edusys.common.utils.reflection.ReflectionUtils.java

/**
 * //w  ww.j av  a  2 s  . c  o  m
 */
public static String getKeyFieldName(final Class clazz) {
    Field[] fields = clazz.getDeclaredFields();
    for (Field f : fields) {
        String filedName = f.getName();
        Annotation[] annotation = f.getAnnotations();
        for (Annotation annotation2 : annotation) {
            Class annotationType = annotation2.annotationType();
            if (annotationType.getName().equals("org.nutz.dao.entity.annotation.Id"))
                return filedName;
            if (annotationType.getName().equals("org.nutz.dao.entity.annotation.Name"))
                return filedName;
        }
    }
    return null;
}

From source file:com.oembedler.moon.graphql.engine.dfs.ResolvableTypeAccessor.java

public static ResolvableTypeAccessor forEnumField(Enum en) {
    Field field = ReflectionUtils.findField(en.getClass(), ((Enum) en).name());
    return new ResolvableTypeAccessor(en.toString(), null, Lists.newArrayList(field.getAnnotations()),
            en.getClass());/*w w w  .ja  va2 s .  co m*/
}

From source file:de.beyondjava.angularFaces.core.ELTools.java

/**
 * Which annotations are given to an object described by an EL expression?
 *
 * @param p_component/*from ww  w.  j av  a  2 s.co  m*/
 * @return null if there are no annotations, or if they cannot be accessed
 */
public static Annotation[] readAnnotations(String p_expression) {
    Field declaredField = getField(p_expression);
    if (null != declaredField) {
        return declaredField.getAnnotations();
    }
    return null;
}

From source file:com.aaplab.android.roboboost.RoboBoost.java

private static void injectFields(Object holder, InjectionContext injectionContext, List<Field> fields) {
    for (Field field : fields) {
        Annotation[] annotations = field.getAnnotations();
        for (Annotation annotation : annotations) {
            Class<? extends Annotation> annotationClass = annotation.annotationType();
            if (InjectorRegister.contains(annotationClass)) {
                AbstractFieldInjector<Annotation> injector = InjectorRegister.getFieldInjector(annotationClass);
                injector.doInjection(holder, injectionContext, field, annotation);
            }/*from ww  w .j  a  v a  2 s.c om*/
        }
    }
}

From source file:org.dcm4che3.conf.core.api.internal.ConfigReflection.java

private static ClassInfo scanClass(Class clazz) {

    ClassInfo classInfo = new ClassInfo();
    classInfo.configurableProperties = new ArrayList<ConfigProperty>();

    // scan all fields from this class and superclasses
    for (Field field : getAllFields(clazz)) {
        if (field.getAnnotation(ConfigurableProperty.class) != null) {

            ConfigProperty ap = new ConfigProperty(annotationsArrayToMap(field.getAnnotations()),
                    field.getName(), field.getGenericType());
            classInfo.configurableProperties.add(ap);

            if (ap.isUuid()) {
                if (classInfo.uuidProperty != null) {
                    throw new IllegalArgumentException(
                            "A configurable class MUST NOT have more than one UUID field - violated by class "
                                    + clazz.getName());
                }//  ww  w  .  j  av a  2s.c o  m

                classInfo.uuidProperty = ap;
            }

            if (ap.isOlockHash()) {
                if (classInfo.olockHashProperty != null) {
                    throw new IllegalArgumentException(
                            "A configurable class MUST NOT have more than one optimistic locking hash field - violated by class "
                                    + clazz.getName());
                }

                classInfo.olockHashProperty = ap;
            }
        } else if (field.getAnnotation(Parent.class) != null) {
            if (classInfo.parentField != null) {
                throw new IllegalArgumentException(
                        "A configurable class MUST NOT have more than one field annotated with @Parent - violated by class "
                                + clazz.getName());
            }

            classInfo.parentField = field;
        }
    }

    if (!ConfigurableClassExtension.class.isAssignableFrom(clazz) && classInfo.uuidProperty == null
            && classInfo.parentField != null) {
        throw new IllegalArgumentException(
                "A configurable class that refers to a @Parent must have a uuid property defined (except the extensions). Violated by "
                        + clazz.getName());
    }

    return classInfo;
}

From source file:com.oembedler.moon.graphql.engine.dfs.ResolvableTypeAccessor.java

public static ResolvableTypeAccessor forField(Field field, Class<?> implClass) {
    ResolvableType resolvableType = ResolvableType.forField(field, implClass);
    return new ResolvableTypeAccessor(field.getName(), resolvableType,
            Lists.newArrayList(field.getAnnotations()), implClass);
}

From source file:org.dcm4che3.conf.core.util.ConfigIterators.java

private static List<AnnotatedConfigurableProperty> processAnnotatedProperties(Class clazz) {
    List<AnnotatedConfigurableProperty> l;
    l = new ArrayList<AnnotatedConfigurableProperty>();

    // scan all fields from this class and superclasses
    for (Field field : getFieldsUpTo(clazz, null)) {
        if (field.getAnnotation(ConfigurableProperty.class) != null) {

            AnnotatedConfigurableProperty ap = new AnnotatedConfigurableProperty();
            ap.setAnnotations(annotationsArrayToMap(field.getAnnotations()));
            ap.setType(field.getGenericType());
            ap.setName(field.getName());

            l.add(ap);/*ww w .  j av  a 2 s  . c o  m*/
        }
    }

    configurableFieldsCache.put(clazz, l);
    return l;
}

From source file:hu.javaforum.commons.ReflectionHelper.java

/**
 * Returns with the name of the field./* w w w .ja  va2 s.  c  o m*/
 * Returns:
 * - with annotated name, if the Field has XmlElement annotation
 * - otherwise with the name of the field
 *
 * @param field The Field instance
 * @return Field name as char array
 */
public static char[] getFieldName(final Field field) {
    if (field == null) {
        return new char[0];
    }

    if (XML_ELEMENT_LOADED) {
        final Annotation[] annotations = field.getAnnotations();
        for (Annotation annotation : annotations) {
            if (XmlElement.class.equals(annotation.annotationType())) {
                return ((XmlElement) annotation).name().toCharArray();
            }
        }
    }

    return field.getName().toCharArray();
}

From source file:hu.javaforum.util.internal.ReflectionHelper.java

/**
 * Returns with the name of the field./*from   w  w w .  ja  v a  2s . c o  m*/
 * Returns:
 * - with annotated name, if the Field has XmlElement annotation
 * - otherwise with the name of the field
 *
 * @param field The Field instance
 * @return Field name as char array
 */
public static char[] getFieldName(final Field field) {
    if (field == null) {
        return new char[0];
    }

    if (XML_ELEMENT_LOADED) {
        final Annotation[] annotations = field.getAnnotations();
        for (Annotation annotation : annotations) {
            if (XmlElement.class.equals(annotation.annotationType())) {
                if (!"##default".equals(((XmlElement) annotation).name())) {
                    return ((XmlElement) annotation).name().toCharArray();
                }
            }
        }
    }

    return field.getName().toCharArray();
}

From source file:com.astamuse.asta4d.util.annotation.AnnotatedPropertyUtil.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private static AnnotatedPropertyInfoMap retrievePropertiesMap(Class cls) {
    String cacheKey = cls.getName();
    AnnotatedPropertyInfoMap map = propertiesMapCache.get(cacheKey);
    if (map == null) {
        List<AnnotatedPropertyInfo> infoList = new LinkedList<>();
        Set<String> beanPropertyNameSet = new HashSet<>();

        Method[] mtds = cls.getMethods();
        for (Method method : mtds) {
            List<Annotation> annoList = ConvertableAnnotationRetriever
                    .retrieveAnnotationHierarchyList(AnnotatedProperty.class, method.getAnnotations());

            if (CollectionUtils.isEmpty(annoList)) {
                continue;
            }// w  ww .  ja v  a2 s . co  m

            AnnotatedPropertyInfo info = new AnnotatedPropertyInfo();
            info.setAnnotations(annoList);

            boolean isGet = false;
            boolean isSet = false;
            String propertySuffixe = method.getName();
            if (propertySuffixe.startsWith("set")) {
                propertySuffixe = propertySuffixe.substring(3);
                isSet = true;
            } else if (propertySuffixe.startsWith("get")) {
                propertySuffixe = propertySuffixe.substring(3);
                isGet = true;
            } else if (propertySuffixe.startsWith("is")) {
                propertySuffixe = propertySuffixe.substring(2);
                isSet = true;
            } else {
                String msg = String.format("Method [%s]:[%s] can not be treated as a getter or setter method.",
                        cls.getName(), method.toGenericString());
                throw new RuntimeException(msg);
            }

            char[] cs = propertySuffixe.toCharArray();
            cs[0] = Character.toLowerCase(cs[0]);
            info.setBeanPropertyName(new String(cs));

            AnnotatedProperty ap = (AnnotatedProperty) annoList.get(0);// must by
            String name = ap.name();
            if (StringUtils.isEmpty(name)) {
                name = info.getBeanPropertyName();
            }

            info.setName(name);

            if (isGet) {
                info.setGetter(method);
                info.setType(method.getReturnType());
                String setterName = "set" + propertySuffixe;
                Method setter = null;
                try {
                    setter = cls.getMethod(setterName, method.getReturnType());
                } catch (NoSuchMethodException | SecurityException e) {
                    String msg = "Could not find setter method:[{}({})] in class[{}] for annotated getter:[{}]";
                    logger.warn(msg, new Object[] { setterName, method.getReturnType().getName(), cls.getName(),
                            method.getName() });
                }
                info.setSetter(setter);
            }

            if (isSet) {
                info.setSetter(method);
                info.setType(method.getParameterTypes()[0]);
                String getterName = "get" + propertySuffixe;
                Method getter = null;
                try {
                    getter = cls.getMethod(getterName);
                } catch (NoSuchMethodException | SecurityException e) {
                    String msg = "Could not find getter method:[{}:{}] in class[{}] for annotated setter:[{}]";
                    logger.warn(msg, new Object[] { getterName, method.getReturnType().getName(), cls.getName(),
                            method.getName() });
                }
                info.setGetter(getter);
            }

            infoList.add(info);
            beanPropertyNameSet.add(info.getBeanPropertyName());
        }

        List<Field> list = new ArrayList<>(ClassUtil.retrieveAllFieldsIncludeAllSuperClasses(cls));
        Iterator<Field> it = list.iterator();

        while (it.hasNext()) {
            Field f = it.next();
            List<Annotation> annoList = ConvertableAnnotationRetriever
                    .retrieveAnnotationHierarchyList(AnnotatedProperty.class, f.getAnnotations());
            if (CollectionUtils.isNotEmpty(annoList)) {
                AnnotatedProperty ap = (AnnotatedProperty) annoList.get(0);// must by

                String beanPropertyName = f.getName();
                if (beanPropertyNameSet.contains(beanPropertyName)) {
                    continue;
                }

                String name = ap.name();
                if (StringUtils.isEmpty(name)) {
                    name = f.getName();
                }

                AnnotatedPropertyInfo info = new AnnotatedPropertyInfo();
                info.setAnnotations(annoList);
                info.setBeanPropertyName(beanPropertyName);
                info.setName(name);
                info.setField(f);
                info.setGetter(null);
                info.setSetter(null);
                info.setType(f.getType());
                infoList.add(info);
            }
        }

        map = new AnnotatedPropertyInfoMap(infoList);
        if (Configuration.getConfiguration().isCacheEnable()) {
            propertiesMapCache.put(cacheKey, map);
        }
    }
    return map;
}