Skip to content

Commit

Permalink
fixed #928
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed Aug 21, 2015
1 parent 0415fcb commit a8ef839
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/org/nutz/lang/inject/InjectBySetter.java
@@ -1,8 +1,12 @@
package org.nutz.lang.inject;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;

import org.nutz.castor.Castors;
import org.nutz.json.Json;
import org.nutz.lang.Lang;
import org.nutz.log.Log;
import org.nutz.log.Logs;
Expand All @@ -13,16 +17,25 @@ public class InjectBySetter implements Injecting {

private Method setter;
private Class<?> valueType;
private Type type;
private boolean isMapCollection;

public InjectBySetter(Method setter) {
this.setter = setter;
valueType = setter.getParameterTypes()[0];
type = setter.getGenericParameterTypes()[0];
isMapCollection = Map.class.isAssignableFrom(valueType) ||
Collection.class.isAssignableFrom(valueType);
}

public void inject(Object obj, Object value) {
Object v = null;
try {
v = Castors.me().castTo(value, valueType);
if (isMapCollection && value != null && value instanceof String) {
v = Json.fromJson(type, value.toString());
} else {
v = Castors.me().castTo(value, valueType);
}
setter.invoke(obj, v);
}
catch (Exception e) {
Expand Down
43 changes: 43 additions & 0 deletions test/org/nutz/dao/test/meta/issue928/BeanWithSet.java
@@ -0,0 +1,43 @@
package org.nutz.dao.test.meta.issue928;

import java.util.Set;

import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Table;

@Table("t_bean_with_set")
public class BeanWithSet {

@Id
private long id;

private Set<Long> uids;

private Set<String> names;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public Set<Long> getUids() {
return uids;
}

public void setUids(Set<Long> uids) {
this.uids = uids;
}

public Set<String> getNames() {
return names;
}

public void setNames(Set<String> names) {
this.names = names;
}


}
25 changes: 25 additions & 0 deletions test/org/nutz/dao/test/normal/SimpleDaoTest.java
Expand Up @@ -9,7 +9,9 @@
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Test;
import org.nutz.Nutz;
Expand Down Expand Up @@ -45,6 +47,7 @@
import org.nutz.dao.test.meta.issue726.Issue726;
import org.nutz.dao.test.meta.issue901.XPlace;
import org.nutz.dao.test.meta.issue918.Region;
import org.nutz.dao.test.meta.issue928.BeanWithSet;
import org.nutz.dao.util.Daos;
import org.nutz.dao.util.blob.SimpleBlob;
import org.nutz.dao.util.blob.SimpleClob;
Expand Down Expand Up @@ -476,4 +479,26 @@ public void test_xxx() {
public void test_issue_918() {
dao.create(Region.class, true);
}

@Test
public void test_issue_928() {
dao.create(BeanWithSet.class, true);
BeanWithSet s = new BeanWithSet();
Set<Long> uids = new HashSet<Long>();
Long UID = 1234455656L;
uids.add(UID);
s.setUids(uids);

Set<String> names = new HashSet<String>();
names.add("wendal");
s.setNames(names);
System.out.println(names);

dao.insert(s);

BeanWithSet out = dao.fetch(BeanWithSet.class);
assertEquals(Long.class, out.getUids().iterator().next().getClass());
assertEquals(UID, out.getUids().iterator().next());

}
}

0 comments on commit a8ef839

Please sign in to comment.