Example usage for java.lang.reflect Field getAnnotation

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

Introduction

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

Prototype

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

Source Link

Usage

From source file:com.stratio.deep.commons.utils.AnnotationUtils.java

/**
 * Return a pair of Field[] whose left element is
 * the array of keys fields./*w  w  w. ja v a  2  s  .com*/
 * The right element contains the array of all other non-key fields.
 *
 * @param clazz the Class object
 * @return a pair object whose first element contains key fields, and whose second element contains all other columns.
 */
public static Pair<Field[], Field[]> filterKeyFields(Class clazz) {
    Field[] filtered = filterDeepFields(clazz);
    List<Field> keys = new ArrayList<>();
    List<Field> others = new ArrayList<>();

    for (Field field : filtered) {
        if (isKey(field.getAnnotation(DeepField.class))) {
            keys.add(field);
        } else {
            others.add(field);
        }
    }

    return Pair.create(keys.toArray(new Field[keys.size()]), others.toArray(new Field[others.size()]));
}

From source file:io.apiman.plugins.keycloak_oauth_policy.ClaimLookup.java

private static boolean hasJsonPropertyAnnotation(Field f) {
    for (Field g : f.getType().getDeclaredFields()) {
        g.setAccessible(true);/*www .j  a  v  a2s  . c  om*/
        if (g.getAnnotation(JsonProperty.class) != null)
            return true;
    }
    return false;
}

From source file:org.jdal.annotation.AnnotatedElementAccessor.java

/**
 * Find annotated elements on types/*from w  w  w . ja va  2  s. c o  m*/
 * @param ann annotation to search
 * @param clazz class to search on.
 * @return List with annotated elements
 */
public static List<AnnotatedElement> findAnnotatedElements(final Class<? extends Annotation> annotationType,
        Class<?> clazz) {
    final ArrayList<AnnotatedElement> elements = new ArrayList<AnnotatedElement>();
    // Lookup fields
    ReflectionUtils.doWithFields(clazz, new FieldCallback() {

        @Override
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            if (field.getAnnotation(annotationType) != null) {
                elements.add(field);
            }

        }
    });
    // Lookup methods
    ReflectionUtils.doWithMethods(clazz, new MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {

            if (org.springframework.core.annotation.AnnotationUtils.getAnnotation(method,
                    annotationType) != null)
                elements.add(method);
        }
    });

    return elements;
}

From source file:com.px100systems.util.PropertyAccessor.java

@SuppressWarnings("unused")
public static String findAnnotatedField(Class<?> topClass, Class<? extends Annotation> annotation) {
    for (Class<?> cls = topClass; cls != null; cls = cls.getSuperclass())
        for (Field field : cls.getDeclaredFields())
            if (field.getAnnotation(annotation) != null)
                return field.getName();
    return null;/*from w  w w .j a  v a2 s  . c  o m*/
}

From source file:com.braffdev.server.core.container.injection.DependencyInjector.java

/**
 * Injects the given field of the given object.
 *
 * @param field the field to inject.//from   www  .  j  a  v a 2s  .co  m
 * @param target the object.
 * @param container the container.
 */
private static void injectField(Field field, Object target, Container container) {
    Dependency dep = field.getAnnotation(Dependency.class);
    String dependencyName = dep.value();
    if (StringUtils.isBlank(dependencyName)) {
        // use the classes name if no name is provided
        dependencyName = DependencyInjectionUtils.getDefaultName(field);
    }

    Object dependency = container.getDependencyRegistry().get(dependencyName);
    if (dependency != null) {
        DependencyInjector.injectFieldDependency(field, target, dependencyName, dependency);

    } else {
        LOGGER.error("No dependency found with name \"" + dependencyName + "\" in class \""
                + target.getClass().getSimpleName() + "\"");
    }
}

From source file:com.smartitengineering.util.bean.BeanFactoryRegistrar.java

private static boolean aggregate(Class<? extends Object> aggregatorClass, Object aggregator)
        throws SecurityException {
    if (aggregatorClass.equals(Object.class)) {
        return true;
    }/*from  w  ww .  java 2s . c o m*/
    Class<? extends Object> superClass = aggregatorClass.getSuperclass();
    if (superClass != null) {
        aggregate(superClass, aggregator);
    }
    Aggregator aggregatorAnnotation = aggregatorClass.getAnnotation(Aggregator.class);
    if (aggregatorAnnotation == null || StringUtils.isBlank(aggregatorAnnotation.contextName())) {
        return true;
    }
    BeanFactory beanFactory = getBeanFactorForContext(aggregatorAnnotation.contextName());
    if (beanFactory == null) {
        return true;
    }
    Field[] declaredFields = aggregatorClass.getDeclaredFields();
    for (Field declaredField : declaredFields) {
        InjectableField injectableField = declaredField.getAnnotation(InjectableField.class);
        if (injectableField == null) {
            continue;
        }
        String beanName = StringUtils.isBlank(injectableField.beanName()) && beanFactory.isNameMandatory()
                ? declaredField.getName()
                : injectableField.beanName();
        if (StringUtils.isBlank(beanName) && beanFactory.isNameMandatory()) {
            return true;
        }
        try {
            declaredField.setAccessible(true);
            final Class<?> fieldType = declaredField.getType();
            if (beanFactory.containsBean(beanName, fieldType)) {
                declaredField.set(aggregator, beanFactory.getBean(beanName, fieldType));
            }
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }

    }
    return false;
}

From source file:br.com.bea.androidtools.api.model.EntityUtils.java

public static final Object convert(final Field field, final JSONObject object) throws Exception {
    if (field.getType().equals(String.class))
        return object.getString(field.getAnnotation(Metadata.class).value());
    if (field.getType().equals(Integer.class))
        return object.getInt(field.getAnnotation(Metadata.class).value());
    if (field.getType().equals(Long.class))
        return object.getLong(field.getAnnotation(Metadata.class).value());
    if (field.getType().equals(Date.class))
        return DATETIME_FORMAT.parse(object.getString(field.getAnnotation(Metadata.class).value()));
    return object.get(field.getAnnotation(Metadata.class).value());
}

From source file:com.evolveum.midpoint.prism.marshaller.XNodeProcessorUtil.java

public static <T> Field findXmlValueField(Class<T> beanClass) {
    for (Field field : beanClass.getDeclaredFields()) {
        XmlValue xmlValue = field.getAnnotation(XmlValue.class);
        if (xmlValue != null) {
            return field;
        }//  w ww .j  a  va2  s. c o  m
    }
    return null;
}

From source file:br.com.bea.androidtools.api.model.EntityUtils.java

public static final Object convert(final Field field, final Cursor cursor) throws Exception {
    if (field.getType().equals(Long.class))
        return cursor.getLong(cursor.getColumnIndex(field.getAnnotation(Column.class).name()));
    if (field.getType().equals(Integer.class))
        return cursor.getInt(cursor.getColumnIndex(field.getAnnotation(Column.class).name()));
    if (field.getType().equals(Date.class))
        return DATE_FORMAT
                .parse(cursor.getString(cursor.getColumnIndex(field.getAnnotation(Column.class).name())));
    return cursor.getString(cursor.getColumnIndex(field.getAnnotation(Column.class).name()));
}

From source file:io.apiman.plugins.keycloak_oauth_policy.ClaimLookup.java

private static void getProperties(Class<?> klazz, String path, Deque<Field> fieldChain) {
    for (Field f : klazz.getDeclaredFields()) {
        f.setAccessible(true);//from  www  .jav a  2s  .  c o  m
        JsonProperty jsonProperty = f.getAnnotation(JsonProperty.class);
        if (jsonProperty != null) {
            fieldChain.push(f);
            // If the inspected type has nested @JsonProperty annotations, we need to inspect it
            if (hasJsonPropertyAnnotation(f)) {
                getProperties(f.getType(), f.getName() + ".", fieldChain); // Add "." when traversing into new object.
            } else { // Otherwise, just assume it's simple as the best we can do is #toString
                List<Field> fieldList = new ArrayList<>(fieldChain);
                Collections.reverse(fieldList);
                STANDARD_CLAIMS_FIELD_MAP.put(path + jsonProperty.value(), fieldList);
                fieldChain.pop(); // Pop, as we have now reached end of this chain.
            }
        }
    }
}