public <T> T convert(Object object, Class<T> type) { return ReflectUtils.getInstance().convert(object, type); }
/** * Gets the fieldname of the identifier field for an entity class type * @param type any entity class type * @return the name of the identifier field for this entity OR null if it cannot be determined */ public static String getEntityIdField(Class<?> type) { String entityIdField = ReflectUtils.getInstance().getFieldNameWithAnnotation(type, EntityId.class); if (entityIdField == null) { try { ReflectUtils.getInstance().getFieldType(type, "id"); entityIdField = "id"; } catch (FieldnameNotFoundException e) { entityIdField = null; } } return entityIdField; }
/** * Get the types of the fields of a specific class type, * returns the method names without the "get"/"is" part and camelCased * @param type any class * @return a map of field name -> class type */ public Map<String, Class<?>> getFieldTypes(Class<?> type) { return getFieldTypes(type, null); }
public void copyBean(Object orig, Object dest, int maxDepth, String[] fieldNamesToSkip, boolean ignoreNulls) { ReflectUtils.getInstance().copy(orig, dest, maxDepth, fieldNamesToSkip, ignoreNulls); }
public List<String> populate(Object object, Map<String, Object> properties) { return ReflectUtils.getInstance().populate(object, properties); }
public <T> T cloneBean(T bean, int maxDepth, String[] propertiesToSkip) { return ReflectUtils.getInstance().clone(bean, maxDepth, propertiesToSkip); }
} else { map = ReflectUtils.getInstance().getObjectValues(object, FieldsFilter.SERIALIZABLE, includeClassField);
sb.append(" <table border='1'>\n"); ClassFields<?> cf = ReflectUtils.getInstance().analyzeClass(entityClass); Map<String, Object> fieldValues = ReflectUtils.getInstance().getObjectValues(entity); Map<String, Class<?>> readTypes = cf.getFieldTypes(FieldsFilter.SERIALIZABLE); Map<String, Class<?>> writeTypes = cf.getFieldTypes(FieldsFilter.WRITEABLE); String sVal = ""; if (value != null) { sVal = ReflectUtils.getInstance().convert(value, String.class); String sVal = ""; if (value != null) { sVal = ReflectUtils.getInstance().convert(value, String.class);
/** * @param object a persistent object * @return the value in the id property for the supplied object */ protected Object getIdValue(Object object) { Class<?> entityClass = object.getClass(); Object id = null; String idProp = getIdProperty(entityClass); if (idProp != null) { id = ReflectUtils.getInstance().getFieldValue(object, idProp); } return id; }
sb.append(" <fields>\n"); Map<String, Class<?>> readTypes = ReflectUtils.getInstance().getFieldTypes(entity.getClass(), FieldsFilter.SERIALIZABLE); Map<String, Class<?>> writeTypes = ReflectUtils.getInstance().getFieldTypes(entity.getClass(), FieldsFilter.WRITEABLE); Map<String, Class<?>> entityTypes = new HashMap<String, Class<?>>(readTypes); entityTypes.putAll(writeTypes); sb.append(" <tbody>\n"); ClassFields<?> cf = ReflectUtils.getInstance().analyzeClass(entity.getClass()); Map<String, Class<?>> readTypes = cf.getFieldTypes(FieldsFilter.SERIALIZABLE); Map<String, Class<?>> writeTypes = cf.getFieldTypes(FieldsFilter.WRITEABLE);
Map<String, Class<?>> types = ReflectUtils.getInstance().getFieldTypes(type, FieldsFilter.WRITEABLE); NamesRecord nr = getNamesRecord(type); entity = ReflectUtils.getInstance().constructClass(type); for (Entry<String, Object> entry : data.entrySet()) { String key = entry.getKey(); if (value != null && value.getClass().isAssignableFrom(pType)) { pValue = ReflectUtils.getInstance().getFieldValue(value, pId); value = ReflectUtils.getInstance().constructClass(pType); ReflectUtils.getInstance().setFieldValue(value, pId, pValue); ReflectUtils.getInstance().setFieldValue(entity, property, value);
Map<String, Class<?>> types = ReflectUtils.getInstance().getFieldTypes(type, FieldsFilter.ALL); // faster Map<String, Object> objectValues = ReflectUtils.getInstance().getObjectValues(entity, FieldsFilter.READABLE, false); NamesRecord nr = getNamesRecord(type); for (Entry<String, Object> entry : objectValues.entrySet()) { value = ReflectUtils.getInstance().getFieldValue(value, pId);
List<Object> existingObjects = new ArrayList<Object>(); for (Object object : entities) { Object id = ReflectUtils.getInstance().getFieldValue(object, idProp); if (id == null) { newObjects.add(object); Map<String, Class<?>> types = ReflectUtils.getInstance().getFieldTypes(type); StringBuilder update = new StringBuilder(); StringBuilder columns = new StringBuilder();
entity = current; try { ReflectUtils.getInstance().populateFromParams(entity, params); } catch (RuntimeException e) { throw new EntityEncodingException("Unable to populate bean for ref ("+ref+") from request: " + e.getMessage(), ref+"", e); ReflectUtils.getInstance().populate(entity, decoded); } catch (RuntimeException e) { throw new EntityEncodingException("Unable to populate bean for ref ("+ref+") from data: " + decoded + ":" + e.getMessage(), ref+"", e);
/** * Gets the id field value from an entity if possible * @param entity any entity object * @return the id value OR null if it cannot be found */ public static String getEntityId(Object entity) { String entityId = null; try { entityId = ReflectUtils.getInstance().getFieldValueAsString(entity, "entityId", EntityId.class); } catch (FieldnameNotFoundException e) { try { // try just id only as well entityId = ReflectUtils.getInstance().getFieldValueAsString(entity, "id", null); } catch (FieldnameNotFoundException e1) { entityId = null; } } return entityId; }
ReflectUtils.getInstance().setFieldValue(container.getContainer(), key, value); } catch (RuntimeException e) { throw new RuntimeException("Unknown container type ("+type+") and could not set field on container: " + container, e);
Class<?> idType = ReflectUtils.getInstance().getFieldType(type, idProperty); if (idValue == null) { throw new NullPointerException("Null value for new id retrieved from DB"); ReflectUtils.getInstance().setFieldValue(object, idProperty, idValue); } catch (RuntimeException e) { throw new IllegalStateException("Could not get back the autogenerated ID of the newly inserted object: " + object + " :" + e.getMessage(), e);
/** * This will determine the id property correctly (but fairly inefficiently) so this should be cached, * use {@link #getIdProperty(Class)} to get the id property for a class * @param type a persistent type * @return */ protected String findIdProperty(Class<?> type) { String idProp = null; DataMapper dm = getDataMapper(type); if (dm != null) { idProp = dm.getIdPropertyName(); } if (classes.contains(type)) { if (idProp == null) { // look for the annotation idProp = ReflectUtils.getInstance().getFieldNameWithAnnotation(type, PersistentId.class); } if (idProp == null) { idProp = "id"; } } return idProp; }
/** * Get the value of a field or getter method from an object allowing an annotation to override<br/> * Getting fields supports simple, nested, indexed, and mapped values:<br/> * <b>Simple:</b> Get/set a field in a bean (or map), Example: "title", "id"<br/> * <b>Nested:</b> Get/set a field in a bean which is contained in another bean, Example: "someBean.title", "someBean.id"<br/> * <b>Indexed:</b> Get/set a list/array item by index in a bean, Example: "myList[1]", "anArray[2]"<br/> * <b>Mapped:</b> Get/set a map entry by key in a bean, Example: "myMap(key)", "someMap(thing)"<br/> * * @param object any object * @param fieldName the name of the field (property) to get the value of or the getter method without the "get" and lowercase first char * @param annotationClass if the annotation class is set then we will attempt to get the value from the annotated field or getter method first * @return the value of the field OR null if the value is null * @throws FieldnameNotFoundException if this fieldName does not exist on the object and no annotation was found * @throws IllegalArgumentException if a failure occurs while getting the field value */ public Object getFieldValue(Object object, String fieldName, Class<? extends Annotation> annotationClass) { Object value; Class<?> elementClass = object.getClass(); if (annotationClass != null) { // try to find annotation first String annotatedField = getFieldNameWithAnnotation(elementClass, annotationClass); if (annotatedField != null) { fieldName = annotatedField; } } value = getFieldValue(object, fieldName); return value; }
/** * Create a tablename from a given class */ public static String makeTableNameFromClass(Class<?> pClass) { String tableName = null; // try to get from class annotations first ClassFields<?> classFields = ReflectUtils.getInstance().analyzeClass(pClass); if (classFields.getClassAnnotations().contains(PersistentTableName.class)) { for (Annotation classAnnote : classFields.getClassAnnotations()) { if (PersistentTableName.class.equals(classAnnote.annotationType())) { tableName = ((PersistentTableName)classAnnote).value(); break; } } } if (tableName == null) { String name = pClass.getSimpleName(); tableName = makeDBNameFromCamelCase(name); if (tableName.startsWith("_") && tableName.length() > 1) { tableName = tableName.substring(1); } } return tableName; }