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.google.gwt.dev.shell.mac.WebKitDispatchAdapter.java

public void setField(String member, long value) {
    JsValue jsValue = new JsValueSaf(value);
    int dispId = getDispId(member);
    if (dispId < 0) {
        // TODO (knorton): We could allow expandos, but should we?
        throw new RuntimeException("No such field " + member);
    }/*from   w w  w  .j a  va 2  s  .c  o m*/
    if (javaDispatch.isMethod(dispId)) {
        throw new RuntimeException("Cannot reassign method " + member);
    }
    Field field = javaDispatch.getField(dispId);
    Object val = JsValueGlue.get(jsValue, classLoader, field.getType(), "setField");
    javaDispatch.setFieldValue(dispId, val);
}

From source file:net.cpollet.jixture.helper.MappingBuilder.java

private Object getEmbeddedFieldValue(Object object, Field field) {
    Object value = getFieldValue(object, field);

    if (null == value) {
        value = createInstanceOfClass(field.getType());
        setFieldValue(object, field, value);
    }/*from  w  w w  .  j av  a 2s .co m*/

    return value;
}

From source file:be.fedict.trust.xkms2.ServiceConsumerInstanceResolver.java

private void injectServices(T endpoint, TrustService trustService) {
    LOG.debug("injecting services into JAX-WS endpoint...");
    Field[] fields = endpoint.getClass().getDeclaredFields();
    for (Field field : fields) {
        EJB ejbAnnotation = field.getAnnotation(EJB.class);
        if (null == ejbAnnotation) {
            continue;
        }/*from ww  w .java 2 s  .  com*/
        if (field.getType().equals(TrustService.class)) {
            field.setAccessible(true);
            try {
                field.set(endpoint, trustService);
            } catch (Exception e) {
                throw new RuntimeException("injection error: " + e.getMessage(), e);
            }
        }
    }
}

From source file:capital.scalable.restdocs.constraints.ConstraintReaderImpl.java

private List<String> getEnumConstraintMessage(Class<?> javaBaseClass, String javaFieldName) {
    // could be getter actually
    Field field = findField(javaBaseClass, javaFieldName);
    if (field == null) {
        return emptyList();
    }//from   ww  w . j  ava  2s  . c o m

    Class<?> rawClass = field.getType();
    if (!rawClass.isEnum()) {
        return emptyList();
    }

    Class<Enum> enumClass = (Class<Enum>) rawClass;

    String value = arrayToString(enumClass.getEnumConstants());
    String enumName = enumClass.getCanonicalName();
    String message = constraintDescriptionResolver
            .resolveDescription(new Constraint(enumName, singletonMap(VALUE, (Object) value)));

    // fallback
    if (isBlank(message) || message.equals(enumName)) {
        message = "Must be one of " + value;
    }
    return singletonList(message);
}

From source file:com.mylife.hbase.mapper.serialization.json.JsonSerializer.java

@Override
public <T> T deserialize(final byte[] byteArray, final Field field) throws IOException {
    if (byteArray == null || field == null) {
        return null;
    }/*from w  w  w .ja  v  a  2 s .  c  o  m*/
    @SuppressWarnings("unchecked")
    final Class<T> type = (Class<T>) field.getType();
    if (type.isAssignableFrom(Map.class)) {
        final Type[] types = TypeHandler.getGenericTypesFromField(field);
        return OBJECT_MAPPER.reader(OBJECT_MAPPER.getTypeFactory().constructMapType(HashMap.class,
                (Class<?>) types[0], (Class<?>) types[1])).readValue(byteArray);
    }
    return OBJECT_MAPPER.readValue(byteArray, type);
}

From source file:candr.yoclip.option.OptionPropertiesField.java

/**
 * Initializes the field descriptor to create an association between the bean {@code Field} and the
 * {@code OptionProperties} annotation./*from   w  w  w.  ja  v  a2s . co  m*/
 *
 * @param optionProperties The annotation associated with the bean field.
 * @param field            The bean field annotated with {@code OptionProperties}.
 * @throws candr.yoclip.OptionsBadTypeException if the field is not assignable to the Java {@code Map} interface.
 * @throws candr.yoclip.OptionsBadNameException if the {@link candr.yoclip.annotation.OptionProperties#name() name}
 *                                              is empty.
 */
protected OptionPropertiesField(final OptionProperties optionProperties, final Field field) {

    super(field);

    if (!Map.class.isAssignableFrom(field.getType())) {
        throw new OptionsBadTypeException(field.getName() + " must be a Map");
    }
    if (StringUtils.isEmpty(optionProperties.name())) {
        throw new OptionsBadNameException(field.getName() + " option name is empty.");
    }

    this.optionProperties = optionProperties;
}

From source file:de.micromata.mgc.javafx.launcher.gui.AbstractConfigTabController.java

protected void addTooltip(String field) {
    Field nodeField = PrivateBeanUtils.findField(getClass(), field);
    if (Control.class.isAssignableFrom(nodeField.getType()) == false) {
        return;/*from   w w  w  .  j a  va2 s .  co m*/
    }
    Control node = (Control) PrivateBeanUtils.readField(this, nodeField);
    if (node == null) {
        return;
    }
    addTooltip(node, field);
}

From source file:com.cfs.util.AESCriptografia.java

public void cifrarObjetoComKey(Object objeto, String key) {
    try {/*w  ww.j  a  v  a  2  s .  co  m*/
        Field[] campos = objeto.getClass().getDeclaredFields();
        for (Field campo : campos) {
            campo.setAccessible(true);
            if (campo.getType().equals(String.class)) {
                if (campo.getName().equals("key")) {
                    campo.set(objeto, campo.get(objeto));
                } else {
                    campo.set(objeto, cifrar(String.valueOf(campo.get(objeto)), key));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.glaf.core.util.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 java.util.ArrayList<Object>(0);
    } else if (returnType.isAssignableFrom(HashSet.class)) {
        return new HashSet<Object>(0);
    } else if (returnType.isAssignableFrom(HashMap.class)) {
        return new java.util.concurrent.ConcurrentHashMap<Object, Object>(0);
    } else if (String.class.equals(returnType)) {
        return "";
    } else if (!returnType.isInterface()) {
        try {//from  w ww  .  j a v 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:in.hatimi.nosh.support.CmdLineManager.java

private boolean injectString(Object target, Field field, String value) {
    if (field.getType().equals(Boolean.class) || field.getType().equals(Boolean.TYPE)) {
        Boolean vobj = new Boolean(value);
        return injectImpl(target, field, vobj);
    }/*  ww  w  .  j ava 2 s.  com*/
    if (field.getType().equals(Double.class) || field.getType().equals(Double.TYPE)) {
        Double vobj = Double.valueOf(value);
        return injectImpl(target, field, vobj);
    }
    if (field.getType().equals(Float.class) || field.getType().equals(Float.TYPE)) {
        Float vobj = Float.valueOf(value);
        return injectImpl(target, field, vobj);
    }
    if (field.getType().equals(Long.class) || field.getType().equals(Long.TYPE)) {
        Long vobj = Long.valueOf(value);
        return injectImpl(target, field, vobj);
    }
    if (field.getType().equals(Integer.class) || field.getType().equals(Integer.TYPE)) {
        Integer vobj = Integer.valueOf(value);
        return injectImpl(target, field, vobj);
    }
    if (field.getType().equals(String.class)) {
        return injectImpl(target, field, value);
    }
    if (field.getType().equals(byte[].class)) {
        return injectImpl(target, field, value.getBytes());
    }
    if (field.getType().equals(char[].class)) {
        return injectImpl(target, field, value.toCharArray());
    }
    return false;
}