List of usage examples for java.lang.reflect Field getType
public Class<?> getType()
From source file:com.ejisto.modules.web.util.FieldSerializationUtil.java
public static List<MockedField> translateObject(Object object, String containerClassName, String fieldName, String contextPath) {/*from w ww . ja v a2 s . c o m*/ if (object == null) { return emptyList(); } if (isBlackListed(object.getClass())) { String fieldType = object.getClass().getName(); MockedField out = buildMockedField(containerClassName, fieldName, contextPath, fieldType); return asList(out); } DefaultSupportedType type = DefaultSupportedType.evaluate(object); if (type != null && type.isPrimitiveOrSimpleValue()) { return asList(getSingleFieldFromDefaultSupportedType(object, type, contextPath, containerClassName, fieldName)); } List<MockedField> out = new ArrayList<MockedField>(); Class<?> objectClass = object.getClass(); String className = objectClass.getName(); for (Field field : objectClass.getDeclaredFields()) { if (!Modifier.isTransient(field.getModifiers()) && !isBlackListed(field.getType())) { out.add(translateField(field, object, className, contextPath)); } } return out; }
From source file:be.fedict.commons.eid.consumer.tlv.TlvParser.java
private static <T> T parseThrowing(final byte[] file, final Class<T> tlvClass) throws InstantiationException, IllegalAccessException, DataConvertorException, UnsupportedEncodingException { final Field[] fields = tlvClass.getDeclaredFields(); final Map<Integer, Field> tlvFields = new HashMap<Integer, Field>(); final T tlvObject = tlvClass.newInstance(); for (Field field : fields) { final TlvField tlvFieldAnnotation = field.getAnnotation(TlvField.class); if (null != tlvFieldAnnotation) { final int tagId = tlvFieldAnnotation.value(); if (tlvFields.containsKey(new Integer(tagId))) { throw new IllegalArgumentException("TLV field duplicate: " + tagId); }//from w w w . j a va 2 s.c o m tlvFields.put(new Integer(tagId), field); } final OriginalData originalDataAnnotation = field.getAnnotation(OriginalData.class); if (null != originalDataAnnotation) { field.setAccessible(true); field.set(tlvObject, file); } } int idx = 0; while (idx < file.length - 1) { final byte tag = file[idx]; idx++; byte lengthByte = file[idx]; int length = lengthByte & 0x7f; while ((lengthByte & 0x80) == 0x80) { idx++; lengthByte = file[idx]; length = (length << 7) + (lengthByte & 0x7f); } idx++; if (0 == tag) { idx += length; continue; } if (tlvFields.containsKey(new Integer(tag))) { final Field tlvField = tlvFields.get(new Integer(tag)); final Class<?> tlvType = tlvField.getType(); final ConvertData convertDataAnnotation = tlvField.getAnnotation(ConvertData.class); final byte[] tlvValue = copy(file, idx, length); Object fieldValue; if (null != convertDataAnnotation) { final Class<? extends DataConvertor<?>> dataConvertorClass = convertDataAnnotation.value(); final DataConvertor<?> dataConvertor = dataConvertorClass.newInstance(); fieldValue = dataConvertor.convert(tlvValue); } else if (String.class == tlvType) { fieldValue = new String(tlvValue, "UTF-8"); } else if (Boolean.TYPE == tlvType) { fieldValue = true; } else if (tlvType.isArray() && Byte.TYPE == tlvType.getComponentType()) { fieldValue = tlvValue; } else { throw new IllegalArgumentException("unsupported field type: " + tlvType.getName()); } if (null != tlvField.get(tlvObject) && false == tlvField.getType().isPrimitive()) { throw new RuntimeException("field was already set: " + tlvField.getName()); } tlvField.setAccessible(true); tlvField.set(tlvObject, fieldValue); } else { LOG.debug("unknown tag: " + (tag & 0xff) + ", length: " + length); } idx += length; } return tlvObject; }
From source file:fr.paris.lutece.util.bean.BeanUtil.java
/** * Populate a bean using parameters in http request * /* w w w . j a va 2s . c o m*/ * @param bean bean to populate * @param request http request */ public static void populate(Object bean, HttpServletRequest request) { for (Field field : bean.getClass().getDeclaredFields()) { try { // for all boolean field, init to false if (field.getType().getName().equals(Boolean.class.getName()) || field.getType().getName().equals(boolean.class.getName())) { field.setAccessible(true); field.set(bean, false); } } catch (Exception e) { String error = "La valeur du champ " + field.getName() + " de la classe " + bean.getClass().getName() + " n'a pas pu tre rcupr "; AppLogService.error(error); throw new RuntimeException(error, e); } } try { BeanUtils.populate(bean, convertMap(request.getParameterMap())); } catch (IllegalAccessException e) { AppLogService.error("Unable to fetch data from request", e); } catch (InvocationTargetException e) { AppLogService.error("Unable to fetch data from request", e); } }
From source file:com.cognifide.qa.bb.mapper.field.PageObjectProviderHelper.java
private static By retrieveSelectorFromPageObject(Field field, boolean useGeneric) { String cssValue = useGeneric// w w w. j a v a 2 s . c o m ? PageObjectProviderHelper.getGenericType(field).getAnnotation(PageObject.class).css() : field.getType().getAnnotation(PageObject.class).css(); if (StringUtils.isNotEmpty(cssValue)) { return By.cssSelector(cssValue); } String xpathValue = useGeneric ? PageObjectProviderHelper.getGenericType(field).getAnnotation(PageObject.class).xpath() : field.getType().getAnnotation(PageObject.class).xpath(); if (StringUtils.isNotEmpty(xpathValue)) { return By.xpath(xpathValue); } throw new IllegalArgumentException( "PageObject has to have defined selector when used with FindPageObject annotation"); }
From source file:com.sample.common.util.CommonsUtil.java
/** * Metodo di utilita' per controllare che un'istanza di una classe abbia almeno un campo valorizzato * //www . j a v a 2s . co m * @param instance * @return true se l'istanza ha almeno un campo valorizzato, false altrimenti */ public static <T> boolean isFilled(T instance) { boolean isFilled = false; if (instance != null) { Field[] fields = instance.getClass().getDeclaredFields(); for (Field field : fields) { if (field.getType().getCanonicalName().startsWith("java.lang")) { String getterMethodName = "get" + capitalize(field.getName()); Method method = findMethod(instance.getClass(), getterMethodName, new Class[] {}); if (method != null) { try { Object value = method.invoke(instance, new Object[] {}); if (field.getType().getSimpleName().equals("String")) { String valueStr = (String) value; if (value != null && valueStr.trim().length() > 0) { isFilled = true; break; } } else { if (value != null) { isFilled = true; break; } } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } } return isFilled; }
From source file:py.una.pol.karaku.test.util.TestUtils.java
private static Class<?> getType(Field field) { Class<?> toRet = field.getType(); // Si tenemos una lista, buscar el tipo de la lista. if (Collection.class.isAssignableFrom(toRet)) { toRet = GenericCollectionTypeResolver.getCollectionFieldType(field); }/* ww w.j a va 2 s. c o m*/ return toRet; }
From source file:IntrospectionUtil.java
protected static Field findInheritedField(Package pack, Class clazz, String fieldName, Class fieldType, boolean strictType) throws NoSuchFieldException { if (clazz == null) throw new NoSuchFieldException("No class"); if (fieldName == null) throw new NoSuchFieldException("No field name"); try {/*from w w w. java 2 s . c o m*/ Field field = clazz.getDeclaredField(fieldName); if (isInheritable(pack, field) && isTypeCompatible(fieldType, field.getType(), strictType)) return field; else return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), fieldName, fieldType, strictType); } catch (NoSuchFieldException e) { return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), fieldName, fieldType, strictType); } }
From source file:com.greenline.hrs.admin.util.db.SchemaExport.java
public static String exportMySQL(Class type) { if (type == null) { return StringUtils.EMPTY; }//from w w w .j av a 2s . c o m String tableName = type.getName().substring(type.getName().lastIndexOf('.') + 1); tableName = StringUtil.underscoreName(tableName); StringBuilder sb = new StringBuilder(); sb.append("DROP TABLE IF EXISTS `" + tableName + "`;" + LINUX_LINE_DELIMITER); sb.append("CREATE TABLE `"); sb.append(tableName); sb.append("` (" + LINUX_LINE_DELIMITER + LINUX_LINE_DELIMITER); sb.append("`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '',"); Field[] fields = type.getDeclaredFields(); for (Field field : fields) { if (!Modifier.isStatic(field.getModifiers())) { sb.append("`" + StringUtil.underscoreName(field.getName()) + "` " + JdbcColumnUtil.getColumeTypeDesc(field.getType()) + " NOT NULL COMMENT ''," + LINUX_LINE_DELIMITER); } } sb.append("PRIMARY KEY (`id`)" + LINUX_LINE_DELIMITER); sb.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8;"); return sb.toString(); }
From source file:lite.flow.util.ActivityInspector.java
static public ArrayList<Field> getOutputs(Class<?> clazz) { ArrayList<Field> outputs = new ArrayList<>(); for (Field field : clazz.getDeclaredFields()) { Class<?> decl = field.getType(); if (Output.class.isAssignableFrom(decl)) { outputs.add(field);/*from w ww . j a v a 2 s .c om*/ } } return outputs; }
From source file:org.jongo.model.IdSpecSet.java
public static <T> T id(T specInstance, Object id) { Field field = idField(specInstance.getClass()); try {/*from w ww . ja va 2 s. c om*/ field.setAccessible(true); if (ObjectId.class.isAssignableFrom(field.getType())) { field.set(specInstance, (ObjectId) id); } else { field.set(specInstance, id.toString()); } return specInstance; } catch (Exception e) { throw new RuntimeException("could not set id field", e); } }