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:io.github.huherto.springyRecords.RecordMapper.java

/**
 * Initialize the mapping metadata for the given class.
 * @param mappedClass the mapped class./*from   w  w w.j  ava2 s .  co  m*/
 */
protected void initialize(Class<T> mappedClass) {
    this.mappedClass = mappedClass;
    this.mappedFields = new HashMap<String, Field>();

    Field fields[] = mappedClass.getFields();
    for (Field field : fields) {
        int mod = field.getModifiers();
        if (Modifier.isPublic(mod) && !Modifier.isStatic(mod)) {
            for (Annotation a : field.getAnnotations()) {
                if (a.annotationType().isAssignableFrom(Column.class)) {
                    Column c = (Column) a;
                    String columnName = c.name();
                    mappedFields.put(columnName, field);
                }
            }
        }
    }
}

From source file:es.osoco.grails.plugins.otp.access.AnnotationMultipleVoterFilterInvocationDefinition.java

private Map<String, Set<String>> findActionRoles(final Class<?> clazz) {
    // since action closures are defined as "def foo = ..." they're
    // fields, but they end up as private
    Map<String, Set<String>> actionRoles = new HashMap<String, Set<String>>();
    for (Field field : clazz.getDeclaredFields()) {
        Annotation annotation = findAnnotation(field.getAnnotations());
        if (annotation != null) {
            actionRoles.put(field.getName(), asSet(getValue(annotation)));
        }//ww  w  . j  a  va 2s  .c o  m
    }
    for (Method method : clazz.getDeclaredMethods()) {
        Annotation annotation = findAnnotation(method.getAnnotations());
        if (annotation != null) {
            actionRoles.put(method.getName(), asSet(getValue(annotation)));
        }
    }
    return actionRoles;
}

From source file:org.openmobster.core.synchronizer.server.engine.MobileObjectGateway.java

private String extractRecordId(MobileBean record)
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    String id = "";

    Class recordClazz = record.getClass();
    Field[] declaredFields = recordClazz.getDeclaredFields();
    for (Field field : declaredFields) {
        Annotation[] annotations = field.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation instanceof MobileBeanId) {
                return BeanUtils.getProperty(record, field.getName());
            }/*from w  ww .j  av  a2 s.com*/
        }
    }

    return id;
}

From source file:org.kuali.rice.krad.web.form.UifFormManager.java

/**
 * Removes the values that are marked @SessionTransient from the form.
 *
 * @param form the form from which the session transient values have been purged
 *///from ww w. j a v  a  2s  .  c o  m
public void purgeForm(UifFormBase form) {
    List<Field> fields = new ArrayList<Field>();
    fields = getAllFields(fields, form.getClass(), UifFormBase.class);
    for (Field field : fields) {
        boolean purgeValue = false;

        if (!field.getType().isPrimitive()) {
            for (Annotation an : field.getAnnotations()) {
                if (an instanceof SessionTransient) {
                    purgeValue = true;
                }
            }
        }

        if (purgeValue && ObjectPropertyUtils.isWritableProperty(form, field.getName())) {
            ObjectPropertyUtils.setPropertyValue(form, field.getName(), null);
        }
    }
}

From source file:org.openhab.binding.weather.internal.metadata.MetadataHandler.java

/**
 * Scans the class and generates metadata.
 *//*from w  w  w  .  ja va2  s  . c  o  m*/
public void generate(Class<?> clazz) throws IllegalAccessException {
    if (clazz == null) {
        return;
    }

    for (Field field : clazz.getDeclaredFields()) {
        if (field.getType().getName().startsWith(PACKAGE_TO_SCAN) && !field.isEnumConstant()) {
            generate(field.getType());
        } else {
            for (Annotation annotation : field.getAnnotations()) {
                if (annotation.annotationType().equals(ProviderMappings.class)) {
                    ProviderMappings providerAnnotations = (ProviderMappings) annotation;
                    for (Provider provider : providerAnnotations.value()) {
                        Map<String, ProviderMappingInfo> mappings = providerMappings.get(provider.name());

                        if (mappings == null) {
                            mappings = new HashMap<String, ProviderMappingInfo>();
                            providerMappings.put(provider.name(), mappings);
                        }

                        Converter<?> converter = (Converter<?>) getConverter(field, provider.converter());
                        String target = clazz.getSimpleName().toLowerCase() + "." + field.getName();
                        ProviderMappingInfo pm = new ProviderMappingInfo(provider.property(), target,
                                converter);
                        mappings.put(pm.getSource(), pm);
                        logger.trace("Added provider mapping {}: {}", provider.name(), pm);
                    }
                } else if (annotation.annotationType().equals(ForecastMappings.class)) {
                    ForecastMappings forecastsAnnotations = (ForecastMappings) annotation;
                    for (Forecast forecast : forecastsAnnotations.value()) {
                        List<String> forecastProperties = forecastMappings.get(forecast.provider());
                        if (forecastProperties == null) {
                            forecastProperties = new ArrayList<String>();
                            forecastMappings.put(forecast.provider(), forecastProperties);
                        }
                        forecastProperties.add(forecast.property());
                        logger.trace("Added forecast mapping {}: {}", forecast.provider(), forecast.property());
                    }
                }
            }
        }
    }
}

From source file:org.apache.axis2.jaxws.server.endpoint.injection.impl.WebServiceContextInjectorImpl.java

private Field searchFieldsForResourceAnnotation(Class bean) {
    if (bean == null) {
        return null;
    }//from ww  w  .ja v a 2 s  .  co m
    List<Field> fields = getFields(bean);
    for (Field field : fields) {
        Annotation[] annotations = field.getAnnotations();
        for (Annotation an : annotations) {
            if (Resource.class.isAssignableFrom(an.getClass())) {
                //check to make sure it is a @Resource for WebServiceContext.
                Resource atResource = (Resource) an;
                Class type = atResource.type();
                if (isWebServiceContextResource(atResource, field)) {
                    return field;
                }
            }
        }
    }
    return null;
}

From source file:org.ms123.common.form.BaseFormServiceImpl.java

public Annotation[] findFieldAnnotations(Field field) throws Exception {
    return field.getAnnotations();
}

From source file:com.conversantmedia.mapreduce.tool.annotation.handler.MaraAnnotationUtil.java

/**
 * Use any relevant annotations on the supplied class to configure
 * the given job.//from w  ww.  j  a  v a 2 s.  c om
 * 
 * @param clazz            the class to reflect for mara-related annotations
 * @param job            the job being configured
 * @throws ToolException   if reflection fails for any reason
 */
public void configureJobFromClass(Class<?> clazz, Job job) throws ToolException {

    configureJobFromAnnotations(job, clazz.getAnnotations(), clazz);

    for (Field field : clazz.getDeclaredFields()) {
        configureJobFromAnnotations(job, field.getAnnotations(), field);
    }
    for (Method method : clazz.getDeclaredMethods()) {
        configureJobFromAnnotations(job, method.getAnnotations(), method);
    }
}

From source file:org.kie.workbench.common.screens.datamodeller.backend.server.DataModelTestUtil.java

public DataObject createDataObject(Class clazz) throws NoSuchMethodException, SecurityException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    Class superClass = clazz.getSuperclass();
    String superClassName = null;
    if (superClass != null && !superClass.equals(Object.class)) {
        superClassName = superClass.getCanonicalName();
    }/* www .  java  2 s.c  o m*/
    DataObject dataObj = createDataObject(clazz.getPackage().getName(), clazz.getSimpleName(), superClassName);
    addAnnotations(dataObj, clazz.getAnnotations());

    for (Field field : clazz.getDeclaredFields()) {
        String fieldName = field.getName();
        String fieldType = field.getType().getCanonicalName();
        ObjectProperty fieldProp = addProperty(dataObj, fieldName, fieldType, true, false, null);

        addAnnotations(fieldProp, field.getAnnotations());
    }

    return dataObj;
}

From source file:de.grobmeier.jjson.convert.JSONAnnotationEncoder.java

private int serializeFields(Object c, StringBuilder builder) throws JSONException {
    Field[] fields = ArrayUtils.addAll(c.getClass().getDeclaredFields(), c.getClass().getFields());

    int count = 0;
    boolean first = true;
    for (Field field : fields) {
        Annotation[] anons = field.getAnnotations();
        for (Annotation annotation : anons) {
            if (annotation.annotationType().isAssignableFrom(JSON.class)) {
                if (!first) {
                    builder.append(COMMA);
                } else {
                    first = false;/*from ww w  . j  ava 2  s .  co m*/
                }

                String methodName = null;
                // primitive boolean getters have is as prefix
                // Use class.getComponentType instead of this
                if (PRIMITIVE_BOOLEAN.equals(field.getType().toString())) {
                    methodName = JSONReflectionUtils.createGetter(field.getName(), JSONReflectionUtils.IS);
                } else {
                    methodName = JSONReflectionUtils.createGetter(field.getName(), JSONReflectionUtils.GET);
                }

                try {
                    Method method = c.getClass().getMethod(methodName, (Class[]) null);
                    Object result = method.invoke(c, (Object[]) null);

                    encodeString(field.getName(), builder, (JSON) annotation);
                    builder.append(COLON);
                    encode(result, builder, (JSON) annotation);

                    count++;
                } catch (SecurityException e) {
                    throw new JSONException(e);
                } catch (NoSuchMethodException e) {
                    throw new JSONException("No appropriate getter found: " + methodName, e);
                } catch (IllegalArgumentException e) {
                    throw new JSONException(e);
                } catch (IllegalAccessException e) {
                    throw new JSONException(e);
                } catch (InvocationTargetException e) {
                    throw new JSONException(e);
                }
            }
        }
    }

    return count;
}