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:framework.GlobalHelpers.java

public static void setValueOfFieldOrProperty(Object object, String name, Object value) {
    Field field;
    try {//from w  ww  . java  2  s  . co  m
        field = findInheritedField(object.getClass(), name);
        if (field != null) {
            field.setAccessible(true);
            Class<?> type = field.getType();
            if (value != null && !type.equals(value.getClass())) {
                // TODO conversion
                if (type.equals(String.class)) {
                    value = String.valueOf(value);
                } else if (type.equals(short.class) || type.equals(Short.class)) {
                    value = Short.valueOf(String.valueOf(value));
                } else if (type.equals(int.class) || type.equals(Integer.class)) {
                    value = Integer.valueOf(String.valueOf(value));
                } else if (type.equals(long.class) || type.equals(Long.class)) {
                    value = Long.valueOf(String.valueOf(value));
                } else if (type.equals(boolean.class) || type.equals(Boolean.class)) {
                    value = Boolean.valueOf(String.valueOf(value));
                } else if (type.equals(float.class) || type.equals(Float.class)) {
                    value = Float.valueOf(String.valueOf(value));
                } else if (type.equals(double.class) || type.equals(Double.class)) {
                    value = Double.valueOf(String.valueOf(value));
                } else if (type.equals(BigDecimal.class)) {
                    value = new BigDecimal(String.valueOf(value));
                }
            }
            field.set(object, value);
        } else {
            BeanUtils.setProperty(object, name, value);
        }
    } catch (Exception e) {
        // do nth, just skip not existing field (maybe log sth?)
    }
}

From source file:br.com.cobranca.util.Util.java

public static <T> T atribuirValores(Class<T> classe, ResultSet rs) throws Exception {

    T obj = classe.newInstance();//from  w  ww  . j  a v  a  2s.c om

    //Percorre lista de colunas
    for (int j = 0; j < rs.getMetaData().getColumnCount(); j++) {

        String nomeColuna = rs.getMetaData().getColumnName(j + 1);

        Field field = obj.getClass().getDeclaredField(nomeColuna);
        field.setAccessible(true);

        String tipoColuna = field.getType().getSimpleName();

        if (tipoColuna.toUpperCase().contains("INT")) {
            tipoColuna = "Int";
        } else {
            tipoColuna = StringPrimeiraLetraMaiuscula(tipoColuna);

        }

        // rs . get + tipo da coluna, passando o nome da coluna como parmetro
        Method met = rs.getClass().getMethod("get" + tipoColuna, String.class);

        if (tipoColuna.equals("Int")) {
            Integer valor = (Integer) met.invoke(rs, nomeColuna);

            met = obj.getClass().getMethod("set" + StringPrimeiraLetraMaiuscula(nomeColuna), Integer.class);
            met.invoke(obj, valor);
        } else if (tipoColuna.equals("String")) {
            String valor = (String) met.invoke(rs, nomeColuna);

            met = obj.getClass().getMethod("set" + StringPrimeiraLetraMaiuscula(nomeColuna), String.class);
            met.invoke(obj, valor);
        } else if (tipoColuna.equals("Double")) {
            Double valor = (Double) met.invoke(rs, nomeColuna);

            met = obj.getClass().getMethod("set" + StringPrimeiraLetraMaiuscula(nomeColuna), Double.class);
            met.invoke(obj, valor);
        } else if (tipoColuna.equals("Float")) {
            Float valor = (Float) met.invoke(rs, nomeColuna);

            met = obj.getClass().getMethod("set" + StringPrimeiraLetraMaiuscula(nomeColuna), Float.class);
            met.invoke(obj, valor);
        } else if (tipoColuna.equals("Long")) {
            Long valor = (Long) met.invoke(rs, nomeColuna);

            met = obj.getClass().getMethod("set" + StringPrimeiraLetraMaiuscula(nomeColuna), Long.class);
            met.invoke(obj, valor);
        } else if (tipoColuna.equals("Boolean")) {
            Boolean valor = (Boolean) met.invoke(rs, nomeColuna);

            met = obj.getClass().getMethod("set" + StringPrimeiraLetraMaiuscula(nomeColuna), Boolean.class);
            met.invoke(obj, valor);
        } else if (tipoColuna.equals("Date")) {
            Date valor = (Date) met.invoke(rs, nomeColuna);

            met = obj.getClass().getMethod("set" + StringPrimeiraLetraMaiuscula(nomeColuna), Date.class);
            met.invoke(obj, valor);
        } else {
            break;
        }
    }

    return obj;

}

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

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

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

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

From source file:ch.rasc.extclassgenerator.ModelGenerator.java

private static void createModelBean(ModelBean model, AccessibleObject accessibleObject,
        OutputConfig outputConfig) {/*w w w.j av a 2 s .c  o m*/
    Class<?> javaType = null;
    String name = null;
    Class<?> declaringClass = null;

    if (accessibleObject instanceof Field) {
        Field field = (Field) accessibleObject;
        javaType = field.getType();
        name = field.getName();
        declaringClass = field.getDeclaringClass();
    } else if (accessibleObject instanceof Method) {
        Method method = (Method) accessibleObject;

        javaType = method.getReturnType();
        if (javaType.equals(Void.TYPE)) {
            return;
        }

        if (method.getName().startsWith("get")) {
            name = StringUtils.uncapitalize(method.getName().substring(3));
        } else if (method.getName().startsWith("is")) {
            name = StringUtils.uncapitalize(method.getName().substring(2));
        } else {
            name = method.getName();
        }

        declaringClass = method.getDeclaringClass();
    }

    ModelType modelType = null;
    if (model.isAutodetectTypes()) {
        for (ModelType mt : ModelType.values()) {
            if (mt.supports(javaType)) {
                modelType = mt;
                break;
            }
        }
    } else {
        modelType = ModelType.AUTO;
    }

    ModelFieldBean modelFieldBean = null;

    ModelField modelFieldAnnotation = accessibleObject.getAnnotation(ModelField.class);
    if (modelFieldAnnotation != null) {

        if (StringUtils.hasText(modelFieldAnnotation.value())) {
            name = modelFieldAnnotation.value();
        }

        if (StringUtils.hasText(modelFieldAnnotation.customType())) {
            modelFieldBean = new ModelFieldBean(name, modelFieldAnnotation.customType());
        } else {
            ModelType type = null;
            if (modelFieldAnnotation.type() != ModelType.NOT_SPECIFIED) {
                type = modelFieldAnnotation.type();
            } else {
                type = modelType;
            }

            modelFieldBean = new ModelFieldBean(name, type);
        }

        updateModelFieldBean(modelFieldBean, modelFieldAnnotation);
        model.addField(modelFieldBean);
    } else {
        if (modelType != null) {
            modelFieldBean = new ModelFieldBean(name, modelType);
            model.addField(modelFieldBean);
        }
    }

    ModelId modelIdAnnotation = accessibleObject.getAnnotation(ModelId.class);
    if (modelIdAnnotation != null) {
        model.setIdProperty(name);
    }

    ModelClientId modelClientId = accessibleObject.getAnnotation(ModelClientId.class);
    if (modelClientId != null) {
        model.setClientIdProperty(name);
        model.setClientIdPropertyAddToWriter(modelClientId.configureWriter());
    }

    ModelVersion modelVersion = accessibleObject.getAnnotation(ModelVersion.class);
    if (modelVersion != null) {
        model.setVersionProperty(name);
    }

    ModelAssociation modelAssociationAnnotation = accessibleObject.getAnnotation(ModelAssociation.class);
    if (modelAssociationAnnotation != null) {
        model.addAssociation(AbstractAssociation.createAssociation(modelAssociationAnnotation, model, javaType,
                declaringClass, name));
    }

    if (modelFieldBean != null && outputConfig.getIncludeValidation() != IncludeValidation.NONE) {

        Set<ModelValidation> modelValidationAnnotations = AnnotationUtils
                .getRepeatableAnnotation(accessibleObject, ModelValidations.class, ModelValidation.class);
        if (!modelValidationAnnotations.isEmpty()) {
            for (ModelValidation modelValidationAnnotation : modelValidationAnnotations) {
                AbstractValidation modelValidation = AbstractValidation.createValidation(name,
                        modelValidationAnnotation, outputConfig.getIncludeValidation());
                if (modelValidation != null) {
                    model.addValidation(modelValidation);
                }
            }
        } else {
            Annotation[] fieldAnnotations = accessibleObject.getAnnotations();

            for (Annotation fieldAnnotation : fieldAnnotations) {
                AbstractValidation.addValidationToModel(model, modelFieldBean, fieldAnnotation,
                        outputConfig.getIncludeValidation());
            }

            if (accessibleObject instanceof Field) {
                PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(declaringClass, name);
                if (pd != null && pd.getReadMethod() != null) {
                    for (Annotation readMethodAnnotation : pd.getReadMethod().getAnnotations()) {
                        AbstractValidation.addValidationToModel(model, modelFieldBean, readMethodAnnotation,
                                outputConfig.getIncludeValidation());
                    }
                }
            }
        }
    }

}

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

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

From source file:com.free.exception.ExceptionHelper.java

/**
 * <p>// w  ww  .j a v a  2s. c om
 * Finds a <code>Throwable</code> by field name.
 * </p>
 * 
 * @param throwable
 *          the exception to examine
 * @param fieldName
 *          the name of the attribute to examine
 * @return the wrapped exception, or <code>null</code> if not found
 */
private static Throwable getCauseUsingFieldName(Throwable throwable, String fieldName) {
    Field field = null;
    try {
        field = throwable.getClass().getField(fieldName);
    } catch (NoSuchFieldException ignored) {
    } catch (SecurityException ignored) {
    }

    if (field != null && Throwable.class.isAssignableFrom(field.getType())) {
        try {
            return (Throwable) field.get(throwable);
        } catch (IllegalAccessException ignored) {
        } catch (IllegalArgumentException ignored) {
        }
    }
    return null;
}

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

private void checkSupportedType(Field field) {
    Class<?> type = field.getType();
    if (!StateFactory.class.equals(type)) {
        throw new IllegalArgumentException(
                State.class + " annotation must be used on a " + StateFactory.class + " class");
    }/* w w  w.  j av a2 s  .c  om*/
}

From source file:com.khubla.cbean.serializer.impl.json.JSONUUIDFieldSerializer.java

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

From source file:com.pandich.dropwizard.curator.refresh.FieldRefresher.java

@Override
protected Class<?> getType(final Field element) {
    return element.getType();
}