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.khubla.cbean.serializer.impl.json.JSONDateFieldSerializer.java

@Override
public boolean applies(Field field) {
    final Class<?> type = field.getType();
    return type == Date.class;
}

From source file:cn.aposoft.util.spring.ReflectionUtils.java

/**
 * Attempt to find a {@link Field field} on the supplied {@link Class} with
 * the supplied {@code name} and/or {@link Class type}. Searches all
 * superclasses up to {@link Object}.//ww w  .j a va  2 s  .  c om
 * 
 * @param clazz
 *            the class to introspect
 * @param name
 *            the name of the field (may be {@code null} if type is
 *            specified)
 * @param type
 *            the type of the field (may be {@code null} if name is
 *            specified)
 * @return the corresponding Field object, or {@code null} if not found
 */
public static Field findField(Class<?> clazz, String name, Class<?> type) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.isTrue(name != null || type != null, "Either name or type of the field must be specified");
    Class<?> searchType = clazz;
    while (Object.class != searchType && searchType != null) {
        Field[] fields = getDeclaredFields(searchType);
        for (Field field : fields) {
            if ((name == null || name.equals(field.getName()))
                    && (type == null || type.equals(field.getType()))) {
                return field;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:ro.fortsoft.hedgedog.spring.SpringAnnotationBeanFinder.java

@Override
protected Object getBean(Sting sting, Field field, Object fieldOwner) {
    return applicationContext.getBean(field.getType());
}

From source file:stormy.pythian.core.description.PropertyDescriptionFactory.java

private List<String> retrieveEnumValues(Field field) {
    Object[] enumConstants = field.getType().getEnumConstants();

    List<String> enumValues = new ArrayList<>(enumConstants.length);
    for (Object enumConstant : enumConstants) {
        enumValues.add(enumConstant.toString());
    }/* w w  w.j a  v  a 2s. c om*/

    return enumValues;
}

From source file:com.taobao.android.builder.tools.guide.AtlasConfigHelper.java

private static void readConfig(Object object, String prefix, List<AtlasConfigField> configFieldList,
        int groupOrder, String variantName) throws IllegalAccessException {

    if (null == object) {
        return;//from w  w  w. j  a v a2s . c om
    }

    for (Field field : getAllFields(object.getClass())) {

        field.setAccessible(true);

        Config config = field.getAnnotation(Config.class);
        if (null != config) {
            AtlasConfigField configField = new AtlasConfigField();
            configField.name = prefix + "." + field.getName();
            configField.order = config.order();
            configField.desc = config.message();
            Object obj = field.get(object);
            configField.value = (null == obj ? null : String.valueOf(obj));
            configField.groupOrder = groupOrder;
            configField.variantName = variantName;
            configField.type = field.getType().getSimpleName();
            configField.advanced = config.advance();
            configField.group = config.group();
            configFieldList.add(configField);
            continue;
        }

        ConfigGroup configGroup = field.getAnnotation(ConfigGroup.class);
        if (null != configGroup) {

            Object nestedValue = field.get(object);

            if (nestedValue instanceof NamedDomainObjectContainer) {

                readConfig(((NamedDomainObjectContainer) nestedValue).maybeCreate("debug"),
                        prefix + "." + field.getName() + ".debug", configFieldList, configGroup.order(),
                        "debug");
                readConfig(((NamedDomainObjectContainer) nestedValue).maybeCreate("release"),
                        prefix + "." + field.getName() + ".release", configFieldList, configGroup.order(),
                        "release");
            } else {

                readConfig(nestedValue, prefix + "." + field.getName(), configFieldList, configGroup.order(),
                        "");
            }
        }
    }
}

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

@Override
public boolean isSupported(Field field) {
    return field.getType().isEnum();
}

From source file:br.gov.frameworkdemoiselle.internal.configuration.JDBCConfigValueExtractor.java

@Override
public boolean isSupported(Field field) {
    return field.getType() == JDBCConfigurationStore.class;
}

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

@Override
public boolean isSupported(Field field) {
    return field.getType() == Map.class;
}

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

@Override
public boolean isSupported(Field field) {
    return field.getType().isPrimitive() || wrappers.contains(field.getType());
}

From source file:edu.usu.sdl.openstorefront.doc.JaxrsProcessor.java

private static void mapComplexTypes(List<APITypeModel> typeModels, Field fields[], boolean onlyConsumeField) {
    //Should strip duplicate types
    Set<String> typesInList = new HashSet<>();
    typeModels.forEach(type -> {/*w  w  w  .  ja  v a  2 s  .  co  m*/
        typesInList.add(type.getName());
    });

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    for (Field field : fields) {
        boolean capture = true;
        if (onlyConsumeField) {
            ConsumeField consumeField = (ConsumeField) field.getAnnotation(ConsumeField.class);
            if (consumeField == null) {
                capture = false;
            }
        }

        if (capture) {

            Class fieldClass = field.getType();
            DataType dataType = (DataType) field.getAnnotation(DataType.class);
            if (dataType != null) {
                fieldClass = dataType.value();
            }

            if (ReflectionUtil.isComplexClass(fieldClass)) {

                APITypeModel typeModel = new APITypeModel();
                typeModel.setName(fieldClass.getSimpleName());

                APIDescription aPIDescription = (APIDescription) fieldClass.getAnnotation(APIDescription.class);
                if (aPIDescription != null) {
                    typeModel.setDescription(aPIDescription.value());
                }

                Set<String> fieldList = mapValueField(typeModel.getFields(), fieldClass.getDeclaredFields(),
                        onlyConsumeField);
                if (fieldClass.isEnum()) {
                    typeModel.setObject(Arrays.toString(fieldClass.getEnumConstants()));
                } else {
                    if (fieldClass.isInterface() == false) {
                        try {
                            typeModel.setObject(objectMapper.writeValueAsString(fieldClass.newInstance()));

                            String cleanUpJson = StringProcessor.stripeFieldJSON(typeModel.getObject(),
                                    fieldList);
                            typeModel.setObject(cleanUpJson);

                        } catch (InstantiationException | IllegalAccessException | JsonProcessingException ex) {
                            log.log(Level.WARNING,
                                    "Unable to process/map complex field: " + fieldClass.getSimpleName(), ex);
                            typeModel.setObject("{ Unable to view }");
                        }
                        mapComplexTypes(typeModels, fieldClass.getDeclaredFields(), onlyConsumeField);
                    }
                }
                typeModels.add(typeModel);
                typesInList.add(typeModel.getName());
            }

        }
    }
}