Setting value of array using java reflection - java

I have created generic json parser using java reflection, but there is a error that i am not able to solve.
Method (at the bottom of this question), receives subclass of my custom Model class. I iterate through fields and set values from json. If subclass contains array property of some other class (which is again, subclass of Model), i've created small recursion to fill those objects.
Eg.
class UserModel extends Model
{
#JsonResponseParam(Name="userName")
public String Name;
#JsonResponseParam(Name="friends")
public FriendModel[] Friends;
}
At the end UserModel should be filled with Friends. (JsonResponseParam is custom anotation, and Name value is used as property for getting values from json)
Result of this method is IllegalArgumentException, and it is thrown on
field.set(t, values.toArray());
Here is the method:
protected <T extends Model> T getModel(T t)
{
Field[] fields = t.getClass().getFields();
for (Field field : fields) {
Annotation an = field.getAnnotation(JsonResponseParam.class);
if(an != null){
try
{
if(field.getType() == boolean.class)
field.setBoolean(t, t.getBool(((JsonResponseParam)an).Name()));
if(field.getType() == String.class)
field.set(t, t.getString(((JsonResponseParam)an).Name()));
if(field.getType() == int.class)
field.setInt(t, t.getInt(((JsonResponseParam)an).Name()));
if(field.getType() == Date.class)
field.set(t, t.getDate(((JsonResponseParam)an).Name()));
if(field.getType().isArray()){
ArrayList<Model> modelArray = t.getModelArray(((JsonResponseParam)an).Name());
ArrayList<Model> values = new ArrayList<Model>();
for (Model model : modelArray) {
Class<? extends Model> arrayType = field.getType().getComponentType().asSubclass(Model.class);
Model m = arrayType.newInstance();
m.jsonObject = model.jsonObject;
model.getModel(m);
values.add(m);
}
field.set(t, values.toArray());
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
return t;
}
I am suspecting on class type inconsistency between field and values..
Thank you for your time.

toArray() only return an Object[] so it cannot be assigned to any other array type.
What you want is
field.set(t, values.toArray(Array.newInstance(field.getType().getComponentType(), values.size()));
This will create an array of the type to match the field.
See Array.newInstance

Basically, the pattern is as below:
// First, create the array
Object myArray = Array.newInstance(field.getType().getComponentType(), arraySize);
// Then, adding value to that array
for (int i = 0; i < arraySize; i++) {
// value = ....
Array.set(myArray, i, value);
}
// Finally, set value for that array field
set(data, fieldName, myArray);
The set function is taken from this stackoverflow question:
public static boolean set(Object object, String fieldName, Object fieldValue) {
Class<?> clazz = object.getClass();
while (clazz != null) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(object, fieldValue);
return true;
} catch (NoSuchFieldException e) {
clazz = clazz.getSuperclass();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
return false;
}
Apply the above code, we have:
if(field.getType().isArray()){
// ....
int arraySize = modelArray.size();
Object values = Array.newInstance(field.getType().getComponentType(), modelArray.size());
for (int i = 0; i < arraySize; i++) {
// ......
Array.set(values, i, m);
}
field.set(t, values);
}

Related

Java working with .class

I am creating java app which will allow storing objects in database. What I want to do is generic implementation so it could load json and create java class from it. This is what a code should look like:
SomeClass someObject= data.getValue(SomeClass.class);
Lets say that data would be a json object. How should I implement getValue() method so it will allow me to create class from it. I don't want SomeClass to extend anything other then Object. I think that this should be done using generic classes but so far I have not worked with generic classes like this. Can you please point to a best way on how to acomplish this? Example code would be best.
Many thanks
You can consult the source code of Jackson library and look inside (or debug) the method BeanDeserializer#vanillaDeserialize(), there you'll find the loop which traverse through all json tokens, finds the corresponding fields and sets their values.
As a proof of concept, I've extracted part of the logic from Jacskson and wrapped it inside a naive (and fragile) object mapper and a naive (and fragile) json parser:
public static class NaiveObjectMapper {
private Map<String, Object> fieldsAndMethods;
private NaiveJsonParser parser;
public <T> T readValue(String content, Class<T> valueType) {
parser = new NaiveJsonParser(content);
try {
// aggregate all value type fields and methods inside a map
fieldsAndMethods = new HashMap<>();
for (Field field : valueType.getDeclaredFields()) {
fieldsAndMethods.put(field.getName(), field);
}
for (Method method : valueType.getMethods()) {
fieldsAndMethods.put(method.getName(), method);
}
// create an instance of value type by calling its default constructor
Constructor<T> constructor = valueType.getConstructor();
Object bean = constructor.newInstance(new Object[0]);
// loop through all json nodes
String propName;
while ((propName = parser.nextFieldName()) != null) {
// find the corresponding field
Field prop = (Field) fieldsAndMethods.get(propName);
// get and set field value
deserializeAndSet(prop, bean);
}
return (T) bean;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
private void deserializeAndSet(Field prop, Object bean) {
Class<?> propType = prop.getType();
Method setter = (Method) fieldsAndMethods.get(getFieldSetterName(prop));
try {
if (propType.isPrimitive()) {
if (propType.getName().equals("int")) {
setter.invoke(bean, parser.getIntValue());
}
} else if (propType == String.class) {
setter.invoke(bean, parser.getTextValue());
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
private String getFieldSetterName(Field prop) {
String propName = prop.getName();
return "set" + propName.substring(0, 1).toUpperCase() + propName.substring(1);
}
}
class NaiveJsonParser {
String[] nodes;
int currentNodeIdx = -1;
String currentProperty;
String currentValueStr;
public NaiveJsonParser(String content) {
// split the content into 'property:value' nodes
nodes = content.replaceAll("[{}]", "").split(",");
}
public String nextFieldName() {
if ((++currentNodeIdx) >= nodes.length) {
return null;
}
String[] propertyAndValue = nodes[currentNodeIdx].split(":");
currentProperty = propertyAndValue[0].replace("\"", "").trim();
currentValueStr = propertyAndValue[1].replace("\"", "").trim();
return currentProperty;
}
public String getTextValue() {
return String.valueOf(currentValueStr);
}
public int getIntValue() {
return Integer.valueOf(currentValueStr).intValue();
}
}
public static class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "id = " + id + ", name = \"" + name + "\"";
}
}
To see the deserialization in action run:
String json = "{\"id\":1, \"name\":\"jsmith\"}";
NaiveObjectMapper objectMapper = new NaiveObjectMapper();
User user = objectMapper.readValue(json, User.class);
System.out.println(user);
Or try online.
However I recommend not to reinvent the wheel and use Jackson and in case you need some custom actions you can use custom deserialization, see here and here.

How can I know the last object when I reflect?

I have a DTO like this,
ADto{
BDto bDto;
Cto cDto;
}
BDto{
String a1;
String b1;
int b1;
}
CDto{
String a2;
String b2;
int b2;
}
When I use reflect,I want to get the BDto and CDto in ADto Object.Code like this:
for (Field field : aObj.getClass().getDeclaredFields()) {
try {
Object fieldValue = field.get(object);
//todo how to collect all String value in `BDto` and `CDto` of aObj
if (fieldValue instanceof String) {
shouldCheckFieldValues.add((String) fieldValue);
}
} catch (Exception e) {
logger.error("some error has happened when fetch data in loop", e);
}
}
}
I want to collect all String value in BDto and CDto of aObj?How can I achieve this? Or how can I know the field which I have to recursive traversal with no hard code?
You directly try to get the String attributes from ADto class, you can't.
First get the BDto attribute, then retreive Strings attributes. Do the same for CDto attribute
for (Field field : aObj.getClass().getDeclaredFields()) {
try {
Object fieldValue = field.get(object);
//todo how to collect all String value in `BDto` and `CDto` of aObj
if (fieldValue instanceof BDto) {
for (Field field2 : fieldValue.getClass().getDeclaredFields())
if (field2 instanceof String) {
shouldCheckFieldValues.add((String) field2 );
Hope this helps
static void exploreFields(Object aObj) {
for (Field field : aObj.getClass().getDeclaredFields()) {
try {
Object instance_var = field.get(aObj);
if (instance_var instanceof String) {
System.out.println(instance_var);
} else if(!(instance_var instanceof Number)) {
exploreFields(instance_var);
}
} catch (Exception e) {
logger.error("some error has happened when fetch data in loop", e);
}
}
}
Edited based on comment. Be aware your objects should not have circular dependencies.

Copy object properties by direct field access

Is there an easy way to copy an object's property's onto another object of a different class which has the same field names using direct field access - i.e. when one of the classes does not have getters or setters for the fields? I can use org.springframework.beans.BeanUtils#copyProperties(Object source, Object target) when they both have getter and setter methods, but what can I do when they don't?
It may also be relevant that the fields are public.
I know that I can write my own code to do this using reflection, but I'm hoping that there's some library that provides a one-liner.
I didn't find a 3rd-party library to do this quite how I wanted. I'll paste my code here in case it is useful to anyone:
import java.lang.reflect.Field;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An alternative to Spring's BeanUtils#copyProperties for classes that don't have getters and setters.
*/
public class FieldCopier {
private static final Logger log = LoggerFactory.getLogger(FieldCopier.class);
/** Always use the same instance, so that we can cache the fields. */
private static final FieldCopier instance = new FieldCopier();
/** Caching the paired fields cuts the time taken by about 25% */
private final Map<Map.Entry<Class<?>, Class<?>>, Map<Field, Field>> PAIRED_FIELDS = new ConcurrentHashMap<>();
/** Caching the fields cuts the time taken by about 50% */
private final Map<Class<?>, Field[]> FIELDS = new ConcurrentHashMap<>();
public static FieldCopier instance() {
return instance;
}
private FieldCopier() {
// do not instantiate
}
public <S, T> T copyFields(S source, T target) {
Map<Field, Field> pairedFields = getPairedFields(source, target);
for (Field sourceField : pairedFields.keySet()) {
Field targetField = pairedFields.get(sourceField);
try {
Object value = getValue(source, sourceField);
setValue(target, targetField, value);
} catch(Throwable t) {
throw new RuntimeException("Failed to copy field value", t);
}
}
return target;
}
private <S, T> Map<Field, Field> getPairedFields(S source, T target) {
Class<?> sourceClass = source.getClass();
Class<?> targetClass = target.getClass();
Map.Entry<Class<?>, Class<?>> sourceToTarget = new AbstractMap.SimpleImmutableEntry<>(sourceClass, targetClass);
PAIRED_FIELDS.computeIfAbsent(sourceToTarget, st -> mapSourceFieldsToTargetFields(sourceClass, targetClass));
Map<Field, Field> pairedFields = PAIRED_FIELDS.get(sourceToTarget);
return pairedFields;
}
private Map<Field, Field> mapSourceFieldsToTargetFields(Class<?> sourceClass, Class<?> targetClass) {
Map<Field, Field> sourceFieldsToTargetFields = new HashMap<>();
Field[] sourceFields = getDeclaredFields(sourceClass);
Field[] targetFields = getDeclaredFields(targetClass);
for (Field sourceField : sourceFields) {
if (sourceField.getName().equals("serialVersionUID")) {
continue;
}
Field targetField = findCorrespondingField(targetFields, sourceField);
if (targetField == null) {
log.warn("No target field found for " + sourceField.getName());
continue;
}
if (Modifier.isFinal(targetField.getModifiers())) {
log.warn("The target field " + targetField.getName() + " is final, and so cannot be written to");
continue;
}
sourceFieldsToTargetFields.put(sourceField, targetField);
}
return Collections.unmodifiableMap(sourceFieldsToTargetFields);
}
private Field[] getDeclaredFields(Class<?> clazz) {
FIELDS.computeIfAbsent(clazz, Class::getDeclaredFields);
return FIELDS.get(clazz);
}
private <S> Object getValue(S source, Field sourceField) throws IllegalArgumentException, IllegalAccessException {
sourceField.setAccessible(true);
return sourceField.get(source);
}
private <T> void setValue(T target, Field targetField, Object value) throws IllegalArgumentException, IllegalAccessException {
targetField.setAccessible(true);
targetField.set(target, value);
}
private Field findCorrespondingField(Field[] targetFields, Field sourceField) {
for (Field targetField : targetFields) {
if (sourceField.getName().equals(targetField.getName())) {
if (sourceField.getType().equals(targetField.getType())) {
return targetField;
} else {
log.warn("Different types for field " + sourceField.getName()
+ " source " + sourceField.getType() + " and target " + targetField.getType());
return null;
}
}
}
return null;
}
}
Write a simple utility class for that and you got your one liner... this task is IMHO to easy to use a library for it.
Just keep in mind to make your fields accessible if they aren't by default. Here are two functions you could adapt from our codebase:
public void injectIntoObject(Object o, Object value) {
try {
getField().set(o, value);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Illegal argument while injecting property '"+name+"' of class '"+beanDef.getName()+"' in object '"+o+"' to '"+value+"'. Got one of type "+value.getClass().getCanonicalName()+" but needed one of "+type.getCanonicalName()+"!",e);
} catch (IllegalAccessException e) {
getField().setAccessible(true);
try {
getField().set(o, value);
} catch (IllegalArgumentException e1) {
throw new RuntimeException("Illegal argument while injecting property '"+name+"' of class '"+beanDef.getName()+"' in object '"+o+"' to '"+value+"'. Got one of type "+value.getClass().getCanonicalName()+" but needed one of "+type.getCanonicalName()+"!",e);
} catch (IllegalAccessException e1) {
throw new RuntimeException("Access exception while injecting property '"+name+"' of class '"+beanDef.getName()+"' in object '"+o+"' to '"+value+"'!",e);
}
} catch (Exception e) {
throw new RuntimeException("Exception while setting property '"+name+"' of class '"+beanDef.getName()+"' in object '"+o+"' to '"+value+"'!",e);
}
}
public Object extractFromObject(Object o) {
try {
return getField().get(o);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Illegal argument while read property '"+name+"' of class '"+beanDef.getName()+"' in object '"+o+"' but needed one of "+type.getCanonicalName()+"!",e);
} catch (IllegalAccessException e) {
getField().setAccessible(true);
try {
return getField().get(o);
} catch (IllegalArgumentException e1) {
throw new RuntimeException("Illegal argument while read property '"+name+"' of class '"+beanDef.getName()+"' in object '"+o+"' but needed one of "+type.getCanonicalName()+"!",e);
} catch (IllegalAccessException e1) {
throw new RuntimeException("Access exception while read property '"+name+"' of class '"+beanDef.getName()+"' in object '"+o+"'!",e);
}
} catch (Exception e) {
throw new RuntimeException("Exception while read property '"+name+"' of class '"+beanDef.getName()+"' in object '"+o+"'!",e);
}
}
getField() returns a java.lang.Field, should be easy to implement.
I would strongly suggest that you avoid using reflection for this, as it leads to code that is difficult to understand and maintain. (Reflection is ok for testing and when creating frameworks, other than this it probably creates more problems than it solves.)
Also, if a property of an object needs to be accessed by something other than the object, it needs a scope that is not private (or an accessor/getter that is not private). That is the whole point of variable scopes. Keeping a variable private without accessors, and then using it anyways through reflection is just wrong, and will just lead to problems, as you are creating code that lies to the reader.
public class MyClass {
private Integer someInt;
private String someString;
private List<Double> someList;
//...
}
public class MyOtherClass {
private Integer someInt;
private String someString;
private List<Double> someList;
private boolean somethingElse;
public copyPropertiesFromMyClass(final MyClass myClass) {
this.someInt = myClass.getSomeInt();
this.someString = myClass.getSomeString();
this.someList = new ArrayList<>(myClass.getSomeList());
}
}

How can I generate a map of all initialized variables from an object in Java?

I have a large list of java objects that all inherit from one shared object and each contains many field members (properties). However, not all fields on all of the objects are guaranteed to be initialized. There are also fields that are contained in the super class which should be included as well. I am looking to create a map that contains all of the initialized members with the member's identifier as the key and their value and the map value.
Is this even possible? I have looked briefly into reflection which looked promising but I do not have much experience with it. All of the values are primitive value types and could be stored in strings if necessary.
Use this
public void method method(Object obj) {
Map initializedFieldsMap = new HashMap();
for (Field field : obj.getClass().getDeclaredFields()) {
Boolean acessibleState = field.isAccessible();
field.setAccessible(true);
Object value;
try {
value = field.get(obj);
if (value != null) {
initializedFieldsMap.put(field.getName(), new WeakReference(value));
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
field.setAccessible(acessibleState);
}
return initializedFieldsMap;
}
It's using a WeakReference here so the object value won't get "stucked" (but it's still not ideal) and ineligible to GC, to access a value (String for instance) from the Map use:
String xxx = (String)map.get("value").get();
Combining the answers that I found:
public static Map<String, String> generatePropertiesMap(Object o)
{
Map<String, String> properties = new HashMap<String, String>();
for (Field field : getAllDeclaredFields(o)) {
Boolean acessibleState = field.isAccessible();
field.setAccessible(true);
Object value;
try {
value = field.get(o);
if (value != null) {
properties.put(field.getName(), value.toString());
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
field.setAccessible(acessibleState);
}
return properties;
}
private static List<Field> getAllDeclaredFields(Object o) {
List<Field> list = new ArrayList<Field>();
List<Field[]> fields = new ArrayList<Field[]>();
//work up from this class until Object
Class next = o.getClass();
while (true) {
Field[] f = next.getDeclaredFields();
fields.add(f);
next = next.getSuperclass();
if (next.equals(Object.class))
break;
}
for (Field[] f : fields) {
list.addAll(Arrays.asList(f));
}
return list;
}

ORMLite JOINs, or rawQuery auto mapping

I am looking for a way to do a query that requires a JOIN. Is there any way to do this in a prepared statement, or is the rawQuery the only option that I have. If rawQuery is the only option, then is there some way to automatically map the returned objects to the objects of the Dao being implemented.
I've dug through the documents and examples but cannot find anything that will allow me to map the raw database result to an ORM object class.
I am looking for a way to do a query that requires a JOIN.
ORMLite supports simple JOIN queries. You can also use raw-queries to accomplish this.
You can use the Dao.getRawRowMapper() to map the queries as you found or you can create a custom mapper. The documentation has the following sample code which shows how to map the String[] into your object:
GenericRawResults<Foo> rawResults =
orderDao.queryRaw(
"select account_id,sum(amount) from orders group by account_id",
new RawRowMapper<Foo>() {
public Foo mapRow(String[] columnNames,
String[] resultColumns) {
return new Foo(Long.parseLong(resultColumns[0]),
Integer.parseInt(resultColumns[1]));
}
});
I've found a way to auto map a result set to a model object.
// return the orders with the sum of their amounts per account
GenericRawResults<Order> rawResults =
orderDao.queryRaw(query, orderDao.getRawRowMapper(), param1)
// page through the results
for (Order order : rawResults) {
System.out.println("Account-id " + order.accountId + " has "
+ order.totalOrders + " total orders");
}
rawResults.close();
The key is to pull the row mapper from your object Dao using getRawRowMapper(), which will handle the mapping for you. I hope this helps anyone who finds it.
I still would love the ability to do joins within the QueryBuilder but until that is supported, this is the next best thing in my opinion.
Raw query auto mapping
I had problem of mapping fields from custom SELECT which return columns that are not present in any table model. So I made custom RawRowMapper which can map fields from custom query to custom model. This is useful when you have query which has fields that doesn't corresponds to any table maping model.
This is RowMapper which performs query auto mapping:
public class GenericRowMapper<T> implements RawRowMapper<T> {
private Class<T> entityClass;
private Set<Field> fields = new HashSet<>();
private Map<String, Field> colNameFieldMap = new HashMap<>();
public GenericRowMapper(Class<T> entityClass) {
this.dbType = dbType;
this.entityClass = entityClass;
Class cl = entityClass;
do {
for (Field field : cl.getDeclaredFields()) {
if (field.isAnnotationPresent(DatabaseField.class)) {
DatabaseField an = field.getAnnotation(DatabaseField.class);
fields.add(field);
colNameFieldMap.put(an.columnName(), field);
}
}
cl = cl.getSuperclass();
} while (cl != Object.class);
}
#Override
public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException {
try {
T entity = entityClass.newInstance();
for (int i = 0; i < columnNames.length; i++) {
Field f = colNameFieldMap.get(columnNames[i]);
boolean accessible = f.isAccessible();
f.setAccessible(true);
f.set(entity, stringToJavaObject(f.getType(), resultColumns[i]));
f.setAccessible(accessible);
}
return entity;
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public Object stringToJavaObject(Class cl, String result) {
if (result == null){
return null;
}else if (cl == Integer.class || int.class == cl) {
return Integer.parseInt(result);
} else if (cl == Float.class || float.class == cl) {
return Float.parseFloat(result);
} else if (cl == Double.class || double.class == cl) {
return Double.parseDouble(result);
} else if (cl == Boolean.class || cl == boolean.class) {
try{
return Integer.valueOf(result) > 0;
}catch (NumberFormatException e){
return Boolean.parseBoolean(result);
}
} else if (cl == Date.class) {
DateLongType lType = DateLongType.getSingleton();
DateStringType sType = DateStringType.getSingleton();
try {
return lType.resultStringToJava(null, result, -1);
} catch (NumberFormatException e) {
try {
return sType.resultStringToJava(null, result, -1);
} catch (SQLException e2) {
throw new RuntimeException(e);
}
}
} else {
return result;
}
}
}
And here is the usage:
class Model{
#DatabaseField(columnName = "account_id")
String accId;
#DatabaseField(columnName = "amount")
int amount;
}
String sql = "select account_id,sum(amount) amount from orders group by account_id"
return queryRaw(sql,new GenericRowMapper<>(Model.class)).getResults()
This will return List<Model> with mapped result rows to Model if query column names and #DatabaseField(columnName are the same

Categories

Resources