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:gr.abiss.calipso.tiers.specifications.GenericSpecifications.java

/**
 * Add a predicate to the given list if valid
 * @param clazz the entity type to query for
 * @param root the criteria root/* ww  w.j av  a  2 s .  c o  m*/
 * @param cb the criteria builder
 * @param predicates the list to add the predicate into
 * @param propertyValues the predicate values
 * @param propertyName the predicate name
 */
protected static void addPredicate(final Class clazz, Root<Persistable> root, CriteriaBuilder cb,
        LinkedList<Predicate> predicates, String[] propertyValues, String propertyName) {
    // dot notation only supports toOne.toOne.id
    if (propertyName.contains(".")) {
        predicates
                .add(anyToOneToOnePredicateFactory.getPredicate(root, cb, propertyName, null, propertyValues));
    } else {// normal single step predicate
        Field field = GenericSpecifications.getField(clazz, propertyName);
        if (field != null) {
            Class fieldType = field.getType();
            IPredicateFactory predicateFactory = getPredicateFactoryForClass(field);
            if (predicateFactory != null) {
                predicates
                        .add(predicateFactory.getPredicate(root, cb, propertyName, fieldType, propertyValues));
            }
        }
    }
}

From source file:org.LexGrid.LexBIG.caCore.utils.LexEVSCaCoreUtils.java

public static Object setFieldValue(Object input, Object value, String fieldName) throws Exception {
    Class searchClass = input.getClass();

    while (searchClass != null) {
        Field[] fields = searchClass.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals(fieldName)) {
                field.setAccessible(true);
                //Check to see if we're trying to set a int, long, etc with a String
                if (field.getType().getName().equals("java.lang.Long")) {
                    if (value instanceof String) {
                        field.set(input, Long.getLong((String) value));
                    } else {
                        field.set(input, value);
                    }/*from  w ww  .j a v a  2  s .  co m*/
                } else if (field.getType().getName().equals("java.lang.Integer")) {
                    if (value instanceof String) {
                        field.set(input, Integer.getInteger((String) value));
                    } else {
                        field.set(input, value);
                    }
                } else {
                    field.set(input, value);
                }
            }
        }
        searchClass = searchClass.getSuperclass();
    }
    return input;
}

From source file:net.minecraftforge.common.config.FieldWrapper.java

public static IFieldWrapper get(Object instance, Field field, String category) {
    if (ADAPTERS.get(field.getType()) != null)
        return new PrimitiveWrapper(category, field, instance);
    else if (Enum.class.isAssignableFrom(field.getType()))
        return new EnumWrapper(category, field, instance);
    else if (Map.class.isAssignableFrom(field.getType()))
        return new MapWrapper(category, field, instance);
    else if (field.getType().getSuperclass().equals(Object.class))
        throw new RuntimeException("Objects should not be handled by field wrappers");
    else//from  w ww  .  ja  va 2 s. c  o  m
        throw new IllegalArgumentException(
                String.format("Fields of type '%s' are not supported!", field.getType().getCanonicalName()));
}

From source file:de.beyondjava.angularFaces.core.ELTools.java

/**
 * Yields the type of the variable given by an expression.
 *
 * @param p_expression//  w ww .j  a v  a2  s  .c om
 * @return
 */
public static Class<?> getType(String p_expression) {
    Field declaredField = getField(p_expression);
    if (null != declaredField) {
        return declaredField.getType();
    }
    return null;
}

From source file:com.palantir.ptoss.util.Reflections.java

/**
 * Gets all fields from a given class that are assignable from the target class.
 * @param klass type to query for fields.
 * @param targetClass interface or class that fields must be assignable from to be
 * returned./*w  ww .  j a v  a  2 s.  co  m*/
 * @return all fields in <code>klass</code> that are assignable from
 * <code>targetClass</code>
 * @see Class#isAssignableFrom(Class)
 * @see Class#getDeclaredFields()
 */
public static List<Field> getFieldsOfType(Class<?> klass, Class<?> targetClass) {
    List<Field> fields = Lists.newArrayList();
    for (Field f : klass.getDeclaredFields()) {
        if (targetClass.isAssignableFrom(f.getType())) {
            fields.add(f);
        }
    }
    return fields;
}

From source file:com.github.strawberry.redis.RedisLoader.java

private static Map<?, ?> mapOf(Field field, Jedis jedis, String key) {
    Map map = mapImplementationOf(field.getType());
    JedisType jedisType = JedisType.valueOf(jedis.type(key).toUpperCase());
    switch (jedisType) {
    case STRING: {
        map.put(key, jedis.get(key));/*from w w w .  j a  va 2  s. c om*/
    }
        break;
    case HASH: {
        Option<Type> valueType = genericTypeOf(field, 1);
        if (valueType.exists(isAssignableTo(Map.class)) || valueType.exists(isEqualTo(Object.class))) {
            map.put(key, jedis.hgetAll(key));
        } else {
            map.putAll(jedis.hgetAll(key));
        }
    }
        break;
    case LIST: {
        map.put(key, jedis.lrange(key, 0, -1));
    }
        break;
    case SET: {
        map.put(key, jedis.smembers(key));
    }
        break;
    case ZSET: {
        map.put(key, jedis.zrange(key, 0, -1));
    }
        break;
    }
    return map;
}

From source file:fi.foyt.foursquare.api.JSONFieldParser.java

/**
 * Returns class for class's field/* w w w  .java2 s. com*/
 * 
 * @param entityClass class
 * @param fieldName field
 * @return class's field's class
 */
private static Class<?> getFieldClass(Class<?> entityClass, String fieldName) {
    Field field = getField(entityClass, fieldName);
    return field != null ? field.getType() : null;
}

From source file:com.mh.commons.utils.Reflections.java

/**
 * ?? trim() //from   w w  w .  j ava2  s . c o m
 * ?? trim();  ??
 * @param obj
 * @param escapeList ??trim()
 * @return
 */
public static Object trim(Object obj, List<String> escapeList) {
    if (obj == null)
        return null;
    try {
        Field[] fields = obj.getClass().getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field field : fields) {
                if (field.getModifiers() < 15 && field.getType().toString().equals("class java.lang.String")) {
                    Object val = FieldUtils.readField(field, obj, true);
                    if (val != null) {
                        if (escapeList != null && escapeList.indexOf(field.getName()) != -1)
                            continue;
                        FieldUtils.writeField(field, obj, val.toString().trim(), true);
                    }
                }
            }
        }
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:com.googlecode.dex2jar.test.TestUtils.java

public static byte[] testDexASMifier(Class<?> clz, String methodName, String generateClassName)
        throws Exception {
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, generateClassName, null, "java/lang/Object", null);
    EmptyVisitor em = new EmptyVisitor() {
        public DexMethodVisitor visitMethod(int accessFlags, com.googlecode.dex2jar.Method method) {
            return new V3MethodAdapter(accessFlags, method, null,
                    V3.OPTIMIZE_SYNCHRONIZED | V3.TOPOLOGICAL_SORT) {
                @Override// www  . j  av  a 2  s .com
                public void visitEnd() {
                    super.visitEnd();
                    methodNode.accept(cw);
                }
            };
        }

        @Override
        public DexFieldVisitor visitField(int accessFlags, com.googlecode.dex2jar.Field field, Object value) {
            FieldVisitor fv = cw.visitField(accessFlags, field.getName(), field.getType(), null, value);
            fv.visitEnd();
            return null;
        }
    };
    Method m = clz.getMethod(methodName, DexClassVisitor.class);
    if (m == null) {
        throw new java.lang.NoSuchMethodException(methodName);
    }
    m.setAccessible(true);
    m.invoke(null, em);
    byte[] data = cw.toByteArray();
    ClassReader cr = new ClassReader(data);
    TestUtils.verify(cr);
    return data;
}

From source file:com.austin.base.commons.util.ReflectUtil.java

/**
 * @param object//from   w ww . ja  v  a  2s.c o m
 * @param type
 * @return
 */
public static List<Field> getFieldsByType(Object object, Class type) {
    List<Field> list = new ArrayList<Field>();
    Field[] fields = object.getClass().getDeclaredFields();
    for (Field field : fields) {
        if (field.getType().isAssignableFrom(type)) {
            list.add(field);
        }
    }
    return list;
}