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:it.nicola_amatucci.util.Json.java

public static <T> JSONObject json_from_object(Object o, Class<T> objClass) throws Exception {
    JSONObject json = new JSONObject();
    Field[] fields = objClass.getFields();
    try {//from   w  w  w  .j a  va 2  s .  c  om
        for (Field field : fields) {
            //valore del campo
            try {
                String typeName = field.getType().getSimpleName();

                //in base al tipo richiamo l'opportuna funzione
                if (typeName.equals("String") || typeName.equals("boolean") || typeName.equals("int")
                        || typeName.equals("long") || typeName.equals("double")) {
                    json.put(field.getName(), objClass.getField(field.getName()).get(o));
                } else if (typeName.equals("Date")) {
                    json.put(field.getName(), new SimpleDateFormat(Json.DATA_FORMAT)
                            .format((java.util.Date) objClass.getField(field.getName()).get(o)));
                } else if (field.getType().isArray()) {
                    try {
                        JSONArray array = new JSONArray();

                        Class c = Class.forName(field.getType().getName()).getComponentType();
                        Object[] objArray = (Object[]) objClass.getField(field.getName()).get(o);
                        for (int i = 0; i < objArray.length; i++) {
                            array.put(json_from_object(objArray[i], c));
                        }
                        json.put(field.getName(), array);
                    } catch (Exception e) {
                        throw e;
                    }
                } else {
                    JSONObject jsonObj = json_from_object(objClass.getField(field.getName()).get(o),
                            field.getType());
                    json.put(field.getName(), jsonObj);
                }

            } catch (Exception e) {
                throw e;
            }

        }
    } catch (Exception e) {
        throw e;
    }

    return json;
}

From source file:gov.va.vinci.leo.tools.LeoUtils.java

/**
 * Create the set of ConfigurationParameter objects from the Param class of an object.
 *
 * @param c Param class// w  w w  .j  ava2s .c  o  m
 * @return Set of ConfigurationParameter objects
 */
public static Set<ConfigurationParameter> getStaticConfigurationParameters(Class c) {
    Set<ConfigurationParameter> list = new HashSet<ConfigurationParameter>();
    Field[] fields = c.getFields();
    for (Field field : fields) {
        try {
            if (field.getType().equals(ConfigurationParameter.class)
                    && Modifier.isStatic(field.getModifiers())) {
                list.add((ConfigurationParameter) field.get(null));
            }
        } catch (IllegalAccessException e) {
            // Handle exception here
        }
    }
    return list;
}

From source file:IntrospectionUtil.java

public static Field findField(Class clazz, String targetName, Class targetType, boolean checkInheritance,
        boolean strictType) throws NoSuchFieldException {
    if (clazz == null)
        throw new NoSuchFieldException("No class");
    if (targetName == null)
        throw new NoSuchFieldException("No field name");

    try {//from www.j  a v a2  s. c o m
        Field field = clazz.getDeclaredField(targetName);
        if (strictType) {
            if (field.getType().equals(targetType))
                return field;
        } else {
            if (field.getType().isAssignableFrom(targetType))
                return field;
        }
        if (checkInheritance) {
            return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), targetName, targetType,
                    strictType);
        } else
            throw new NoSuchFieldException("No field with name " + targetName + " in class " + clazz.getName()
                    + " of type " + targetType);
    } catch (NoSuchFieldException e) {
        return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), targetName, targetType,
                strictType);
    }
}

From source file:lite.flow.util.ActivityInspector.java

protected static FieldInfo[] discoverParameters(Class<?> clazz) {
    Field[] fields = FieldUtils.getFieldsWithAnnotation(clazz, Parameter.class);
    FieldInfo[] fieldInfos = new FieldInfo[fields.length];
    for (int i = 0; i < fieldInfos.length; i++) {
        Field field = fields[i];
        fieldInfos[i] = new FieldInfo(field.getName(), field.getType());
    }//from   w w w.ja  v  a2s . c o  m

    return fieldInfos;
}

From source file:lite.flow.util.ActivityInspector.java

protected static FieldInfo[] discoverResources(Class<?> clazz) {
    Field[] fields = FieldUtils.getFieldsWithAnnotation(clazz, Resource.class);
    FieldInfo[] fieldInfos = new FieldInfo[fields.length];
    for (int i = 0; i < fieldInfos.length; i++) {
        Field field = fields[i];
        fieldInfos[i] = new FieldInfo(field.getName(), field.getType());
    }/*from  w w w.j  a  v  a 2s.c  o  m*/

    return fieldInfos;
}

From source file:org.fastmongo.odm.util.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}./*from  ww  w .  ja va 2 s .com*/
 *
 * @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) {
    Class<?> searchType = clazz;
    while (!Object.class.equals(searchType) && searchType != null) {
        Field[] fields = searchType.getDeclaredFields();
        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:com.jkoolcloud.tnt4j.streams.parsers.ActivityJavaObjectParser.java

private static Object getFieldValue(String[] path, Object dataObj, int i) {
    if (ArrayUtils.isEmpty(path) || dataObj == null) {
        return null;
    }//ww  w  .  j av a2  s .c  o  m

    try {
        Field f = dataObj.getClass().getDeclaredField(path[i]);
        Object obj = f.get(dataObj);

        if (i < path.length - 1 && !f.getType().isPrimitive()) {
            obj = getFieldValue(path, obj, ++i);
        }

        return obj;
    } catch (Exception exc) {
        LOGGER.log(OpLevel.WARNING,
                StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME,
                        "ActivityJavaObjectParser.could.not.get.declared.field"),
                path[i], dataObj.getClass().getSimpleName(), toString(dataObj));
        return null;
    }
}

From source file:com.taobao.itest.spring.context.SpringContextManager.java

public static void registerBeanDefinition(Field field, ApplicationContext applicationContext) {
    @SuppressWarnings("deprecation")
    RootBeanDefinition beanDefinition = new RootBeanDefinition(field.getType(), true);
    beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);
    DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) (applicationContext
            .getAutowireCapableBeanFactory());
    defaultListableBeanFactory.registerBeanDefinition(field.getName(), beanDefinition);
}

From source file:com.impetus.client.cassandra.common.CassandraUtilities.java

public static ByteBuffer toBytes(Object value, Field f) {
    return toBytes(value, f.getType());

}

From source file:jp.co.acroquest.endosnipe.perfdoctor.rule.RuleInstanceUtil.java

/**
 * ??//w w w. j  a  v  a2 s  . co  m
 * @param obj ?
 * @param fieldName ??
 * @param value 
 * @throws RuleCreateException ???????
 */
protected static void setValue(final Object obj, final String fieldName, final String value)
        throws RuleCreateException {
    Class<? extends Object> clazz = obj.getClass();
    Object[] args = new Object[] { clazz.getCanonicalName(), fieldName, value };

    try {
        Field field = clazz.getField(fieldName);

        Object convertedValue = ConvertUtils.convert(value, field.getType());
        field.set(obj, convertedValue);
    } catch (NoSuchFieldException ex) {
        throw new RuleCreateException(PerfConstants.PROPERTY_NOT_FOUND, args);
    } catch (SecurityException ex) {
        throw new RuleCreateException(PerfConstants.PROPERTY_ERROR, args);
    } catch (IllegalArgumentException ex) {
        throw new RuleCreateException(PerfConstants.PROPERTY_TYPE_ERROR, args);
    } catch (IllegalAccessException ex) {
        throw new RuleCreateException(PerfConstants.PROPERTY_ACCESS_ERROR, args);
    }
}