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:com.gemstone.gemfire.test.junit.rules.serializable.SerializableTemporaryFolderTest.java

@Test
public void fieldFolderShouldExist() throws Exception {
    Field field = TemporaryFolder.class.getDeclaredField(FIELD_FOLDER);
    assertThat(field.getType()).isEqualTo(File.class);
}

From source file:br.gov.frameworkdemoiselle.internal.implementation.ConfigurationArrayValueExtractor.java

@Override
public Object getValue(String prefix, String key, Field field, Configuration configuration) throws Exception {
    return new DataConfiguration(configuration).getArray(field.getType().getComponentType(), prefix + key);
}

From source file:com.teasoft.teavote.util.AppProperties.java

public Properties getProperties() throws IllegalAccessException {
    Properties prop = new Properties();
    Field[] fields = AppProperties.class.getDeclaredFields();
    for (Field field : fields) {
        prop.setProperty("teavote" + "." + field.getName(), field.getType().cast(field.get(this)).toString());
    }// ww w .jav a2 s.c o m
    return prop;
}

From source file:com.gemstone.gemfire.test.junit.rules.serializable.SerializableTemporaryFolderTest.java

@Test
public void fieldParentFolderShouldExist() throws Exception {
    Field field = TemporaryFolder.class.getDeclaredField(FIELD_PARENT_FOLDER);
    assertThat(field.getType()).isEqualTo(File.class);
}

From source file:com.gemstone.gemfire.test.junit.rules.serializable.SerializableTimeoutTest.java

@Test
public void fieldTimeoutShouldExist() throws Exception {
    Field field = Timeout.class.getDeclaredField(FIELD_TIMEOUT);
    assertThat(field.getType()).isEqualTo(Long.TYPE);
}

From source file:com.gemstone.gemfire.test.junit.rules.serializable.SerializableTimeoutTest.java

@Test
public void fieldTimeUnitShouldExist() throws Exception {
    Field field = Timeout.class.getDeclaredField(FIELD_TIME_UNIT);
    assertThat(field.getType()).isEqualTo(TimeUnit.class);
}

From source file:com.hc.wx.server.common.bytecode.ReflectUtils.java

private static Object getEmptyObject(Class<?> returnType, Map<Class<?>, Object> emptyInstances, int level) {
    if (level > 2)
        return null;
    if (returnType == null) {
        return null;
    } else if (returnType == boolean.class || returnType == Boolean.class) {
        return false;
    } else if (returnType == char.class || returnType == Character.class) {
        return '\0';
    } else if (returnType == byte.class || returnType == Byte.class) {
        return (byte) 0;
    } else if (returnType == short.class || returnType == Short.class) {
        return (short) 0;
    } else if (returnType == int.class || returnType == Integer.class) {
        return 0;
    } else if (returnType == long.class || returnType == Long.class) {
        return 0L;
    } else if (returnType == float.class || returnType == Float.class) {
        return 0F;
    } else if (returnType == double.class || returnType == Double.class) {
        return 0D;
    } else if (returnType.isArray()) {
        return Array.newInstance(returnType.getComponentType(), 0);
    } else if (returnType.isAssignableFrom(ArrayList.class)) {
        return new ArrayList<Object>(0);
    } else if (returnType.isAssignableFrom(HashSet.class)) {
        return new HashSet<Object>(0);
    } else if (returnType.isAssignableFrom(HashMap.class)) {
        return new HashMap<Object, Object>(0);
    } else if (String.class.equals(returnType)) {
        return "";
    } else if (!returnType.isInterface()) {
        try {/*from  w  w w . j av a 2  s  .co  m*/
            Object value = emptyInstances.get(returnType);
            if (value == null) {
                value = returnType.newInstance();
                emptyInstances.put(returnType, value);
            }
            Class<?> cls = value.getClass();
            while (cls != null && cls != Object.class) {
                Field[] fields = cls.getDeclaredFields();
                for (Field field : fields) {
                    Object property = getEmptyObject(field.getType(), emptyInstances, level + 1);
                    if (property != null) {
                        try {
                            if (!field.isAccessible()) {
                                field.setAccessible(true);
                            }
                            field.set(value, property);
                        } catch (Throwable e) {
                        }
                    }
                }
                cls = cls.getSuperclass();
            }
            return value;
        } catch (Throwable e) {
            return null;
        }
    } else {
        return null;
    }
}

From source file:moe.yuna.palinuridae.xutilsmodel.cache.XUtilsRedisCacheImpl.java

public Object formatOjbect(Field field, String object) {
    if (field.getType().equals(Date.class)) {
        return new Date(Long.valueOf(object));
    }/*from  w  ww  . ja v a 2s . c  om*/
    Object convert = conversionService.convert(object, field.getType());
    return convert;
}

From source file:net.bafeimao.umbrella.support.data.entity.AbstractEntityParser.java

/**
 * ????????//from   w  ww. j a va 2s. c om
 *
 * @param field 
 * @return ??
 */
protected Converter<String, ?> getConverter(Field field) {
    Class<?> fieldType = field.getType();

    if (field.getName().equals("id")) { // TODO ???
        fieldType = Long.class;
    }

    Converter<?, ?> converter = convertersByType.get(fieldType);

    if (converter == null && field.isAnnotationPresent(DataConverter.class)) {
        DataConverter anno = field.getAnnotation(DataConverter.class);
        Class<?> converterClass = anno.value();
        try {
            converter = (Converter<?, ?>) converterClass.newInstance();
            convertersByType.put(field.getType(), converter);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    return (Converter<String, ?>) converter;
}

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

/**
 * If this is a multiple output we're annotating, see if there are type parameters to
 * use for the key/value classes.//  www  . ja  v  a 2 s .co  m
 * 
 * @param target   object to reflect for type params
 * @return         the key/value type parameters
 */
protected Pair<Type, Type> getGenericTypeParams(Object target) {
    Pair<Type, Type> kvTypePair = null;
    if (target instanceof Field) {
        Field field = (Field) target;
        if (field.getType() == MultipleOutputs.class) {
            Type genericType = field.getGenericType();
            if (genericType instanceof ParameterizedType) {
                Type[] keyValueTypes = ((ParameterizedType) genericType).getActualTypeArguments();
                if (keyValueTypes != null && keyValueTypes.length == 2) {
                    kvTypePair = new ImmutablePair<>(keyValueTypes[0], keyValueTypes[1]);
                }
            }
        }
    }
    return kvTypePair;
}