Example usage for java.lang.reflect Field getType

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

Introduction

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

Prototype

public Class<?> getType() 

Source Link

Document

Returns a Class object that identifies the declared type for the field represented by this Field object.

Usage

From source file:de.cbb.mplayer.mapping.MappingUtil.java

private static void mapFieldToEntity(Field field, Object presenter, Object entity) {
    try {//from  w w  w .java  2  s  . co m
        if (!Control.class.isAssignableFrom(field.getType()))
            return;
    } catch (Exception ex) {
        log.debug("mapFieldToEntity failed", ex);
    }
    try {
        String fieldname = field.getName();
        field.setAccessible(true);
        Object value1 = null;

        ValueWrapper wrapper = MappingFactory.buildValueWrapper(presenter, field, value1, entity, fieldname);
        if (wrapper == null)
            return;
        value1 = wrapper.getValue();

        PropertyUtils.setSimpleProperty(entity, fieldname, value1);
    } catch (Exception ex) {
        log.debug("Unmapped control: " + field.getName() + " [" + ex.toString() + "]");
    }
}

From source file:com.github.jasonruckman.sidney.core.type.Types.java

public static TypeBindings binding(Field field, TypeBindings parentBindings) {
    Type t = field.getGenericType();
    if (t == null) {
        t = field.getType();
    }/*  w  w  w .ja  va  2 s. c  o  m*/
    JavaType javaType = TypeFactory.defaultInstance().constructType(t, parentBindings);
    return new TypeBindings(TypeFactory.defaultInstance(), javaType);
}

From source file:io.github.benas.jpopulator.impl.BeanValidationRandomizer.java

/**
 * Generate a random value according to the validation constraint present on a field.
 *
 * @param field the field to populate/*from  w w w  . ja  v  a 2 s  .  com*/
 * @return a random value according to the validation constraint
 */
public static Object getRandomValue(final Field field) {

    Class<?> fieldType = field.getType();

    Object result = null;

    if (field.isAnnotationPresent(AssertFalse.class)) {
        result = false;
    }
    if (field.isAnnotationPresent(AssertTrue.class)) {
        result = true;
    }
    if (field.isAnnotationPresent(Null.class)) {
        result = null;
    }
    if (field.isAnnotationPresent(Future.class)) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, ConstantsUtil.DEFAULT_DATE_RANGE);
        result = new DateRangeRandomizer(new Date(), calendar.getTime()).getRandomValue();
    }
    if (field.isAnnotationPresent(Past.class)) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -ConstantsUtil.DEFAULT_DATE_RANGE);
        result = new DateRangeRandomizer(calendar.getTime(), new Date()).getRandomValue();
    }
    if (field.isAnnotationPresent(Max.class)) {
        Max maxAnnotation = field.getAnnotation(Max.class);
        long maxValue = maxAnnotation.value();
        result = MaxValueRandomizer.getRandomValue(fieldType, maxValue);
    }
    if (field.isAnnotationPresent(DecimalMax.class)) {
        DecimalMax decimalMaxAnnotation = field.getAnnotation(DecimalMax.class);
        BigDecimal decimalMaxValue = new BigDecimal(decimalMaxAnnotation.value());
        result = MaxValueRandomizer.getRandomValue(fieldType, decimalMaxValue.longValue());
    }
    if (field.isAnnotationPresent(Min.class)) {
        Min minAnnotation = field.getAnnotation(Min.class);
        long minValue = minAnnotation.value();
        result = MinValueRandomizer.getRandomValue(fieldType, minValue);
    }
    if (field.isAnnotationPresent(DecimalMin.class)) {
        DecimalMin decimalMinAnnotation = field.getAnnotation(DecimalMin.class);
        BigDecimal decimalMinValue = new BigDecimal(decimalMinAnnotation.value());
        result = MinValueRandomizer.getRandomValue(fieldType, decimalMinValue.longValue());
    }
    if (field.isAnnotationPresent(Size.class)) {
        Size sizeAnnotation = field.getAnnotation(Size.class);
        int minSize = sizeAnnotation.min();
        int maxSize = sizeAnnotation.max();
        result = RandomStringUtils.randomAlphabetic(new RandomDataGenerator().nextInt(minSize, maxSize));
    }
    return result;

}

From source file:io.rhiot.utils.Reflections.java

public static <T> T readField(Object object, String field, Class<T> type) {
    try {//from   w w  w.j ava 2 s  .  c  o  m
        Field actualField = getField(object.getClass(), field, true);
        if (!isInstanceOfOrWrappable(actualField.getType(), type)) {
            String message = format("Field %s is a type of %s instead of %s.", field, actualField.getType(),
                    type);
            throw new IllegalStateException(message);
        }
        return (T) FieldUtils.readField(actualField, object, true);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.cbb.mplayer.util.ReflectionHelper.java

public static Class getFieldType(Object source, String fieldname) {
    try {/*from  w w  w  .j  av  a2 s  . c  om*/
        Field field = getField(source.getClass(), fieldname);
        return field.getType();
    } catch (NoSuchFieldException ex) {
        Logger.getLogger(ReflectionHelper.class.getName()).log(Level.SEVERE, null, ex);
    }
    return String.class;
}

From source file:lite.flow.runtime.kiss.ComponentUtil.java

public static void injectOutput(String outputName, Output<?> output, Object componentInstance)
        throws IllegalArgumentException, IllegalAccessException {

    Class<?> componentClazz = componentInstance.getClass();

    // find activity all Output type fields
    for (Field field : FieldUtils.getAllFields(componentClazz)) {
        Class<?> decl = field.getType();
        if (Output.class.isAssignableFrom(decl)) {

            String name = field.getName();
            if (name != null && name.equals(outputName)) {
                field.setAccessible(true);
                field.set(componentInstance, output);
                return;
            }/*w w  w  .j a  v  a  2 s  .co  m*/
        }
    }

    throw new IllegalArgumentException(
            format("Class '%s' do not contain output '%s'", componentClazz.getName(), outputName));
}

From source file:org.apache.aries.blueprint.plugin.model.Property.java

/**
 * Assume it is defined in another manually created blueprint context with default name
 * @param field/*from   w  w w.j  a  v  a2s .c  o  m*/
 * @return
 */
private static String getRefName(Field field) {
    Named named = field.getAnnotation(Named.class);
    return (named != null) ? named.value() : Bean.getBeanName(field.getType());
}

From source file:Main.java

/**
 * Returns a {@code Class} object that identifies the
 * declared class for the field represented by the given {@code String name} parameter inside
 * the invoked {@code Class<?> clazz} parameter.
 *
 * @param clazz the {@code Class} object whose declared fields to be
 *       checked for a certain field.//from   w  ww .  j  av  a  2 s  .  c o  m
 * @param name the field name as {@code String} to be
 *       compared with {@link Field#getName()}
 * @return the {@code Class} object representing the type of given field name.
 *
 * @see {@link Class#getDeclaredFields()}
 * @see {@link Field#getType()}
 */
public static Class<?> getFieldClass(Class<?> clazz, String name) {
    if (clazz == null || name == null || name.isEmpty()) {
        return null;
    }

    Class<?> propertyClass = null;

    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        if (field.getName().equalsIgnoreCase(name)) {
            propertyClass = field.getType();
            break;
        }
    }

    return propertyClass;
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

/**
 * For a field of type List<Enumeration<Foo>>, returns Foo
 *///w ww  .  j  a  va  2  s . com
public static Class<?> getGenericCollectionTypeOfFieldWithSecondOrderForList(Field next) {
    if (!List.class.isAssignableFrom(next.getType())) {
        return getGenericCollectionTypeOfField(next);
    }

    Class<?> type;
    ParameterizedType collectionType = (ParameterizedType) next.getGenericType();
    Type firstArg = collectionType.getActualTypeArguments()[0];
    if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) {
        ParameterizedType pt = ((ParameterizedType) firstArg);
        Type pt2 = pt.getActualTypeArguments()[0];
        return (Class<?>) pt2;
    }
    type = (Class<?>) firstArg;
    return type;
}

From source file:Main.java

public static void classInspector(Object object) {
    Class cls = object.getClass();
    System.out.println(cls.toString());
    Field[] fieldlist = cls.getDeclaredFields();
    for (int i = 0; i < fieldlist.length; i++) {
        Field fld = fieldlist[i];

        System.out.println("name = " + fld.getName());
        System.out.println("decl class = " + fld.getDeclaringClass());
        System.out.println("type = " + fld.getType());
        System.out.println("-----");
    }//  w w  w .  j a  v a  2 s. c  o  m
}