Skip to content

Commit be9840c

Browse files
committed
JAVA-324: Supporing object class (but not internal classes) for findAndModify
1 parent 3d5458e commit be9840c

File tree

2 files changed

+80
-11
lines changed

2 files changed

+80
-11
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,43 @@ public DBObject findAndModify(DBObject query, DBObject fields, DBObject sort, bo
378378
throw new MongoException("FindAndModify: Remove cannot be mixed with the Update, or returnNew params!");
379379

380380
CommandResult res = this._db.command( cmd );
381-
if (res.ok() || res.getErrorMessage().equals( "No matching object found" ))
382-
return (DBObject) res.get( "value" );
381+
if (res.ok() || res.getErrorMessage().equals( "No matching object found" )) {
382+
return replaceWithObjectClass((DBObject) res.get( "value" ));
383+
}
383384
res.throwOnError();
384385
return null;
385386
}
386387

388+
/**
389+
* Doesn't yet handle internal classes properly, so this method only does something if object class is set but
390+
* no internal classes are set.
391+
*
392+
* @param oldObj the original value from the command result
393+
* @return replaced object if necessary, or oldObj
394+
*/
395+
private DBObject replaceWithObjectClass(DBObject oldObj) {
396+
if (oldObj == null || getObjectClass() == null & _internalClass.isEmpty()) {
397+
return oldObj;
398+
}
399+
400+
DBObject newObj = instantiateObjectClassInstance();
401+
402+
for (String key : oldObj.keySet()) {
403+
newObj.put(key, oldObj.get(key));
404+
}
405+
return newObj;
406+
}
407+
408+
private DBObject instantiateObjectClassInstance() {
409+
try {
410+
return (DBObject) getObjectClass().newInstance();
411+
} catch (InstantiationException e) {
412+
throw new MongoInternalException("can't create instance of type " + getObjectClass(), e);
413+
} catch (IllegalAccessException e) {
414+
throw new MongoInternalException("can't create instance of type " + getObjectClass(), e);
415+
}
416+
}
417+
387418

388419
/**
389420
* calls {@link DBCollection#findAndModify(com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.DBObject, boolean, com.mongodb.DBObject, boolean, boolean)}

src/test/com/mongodb/ReflectionTest.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ public class ReflectionTest extends TestCase {
2727
public static class Person extends ReflectionDBObject {
2828

2929
public Person(){
30-
31-
}
32-
33-
Person( String name ){
34-
_name = name;
3530
}
3631

3732
public String getName(){
@@ -55,16 +50,17 @@ public ReflectionTest()
5550
@Test
5651
public void test1()
5752
throws MongoException {
58-
DBCollection c = _db.getCollection( "persen.test1" );
53+
DBCollection c = _db.getCollection( "person.test1" );
5954
c.drop();
6055
c.setObjectClass( Person.class );
6156

62-
Person p = new Person( "eliot" );
57+
Person p = new Person();
58+
p.setName( "eliot" );
6359
c.save( p );
6460

6561
DBObject out = c.findOne();
6662
assertEquals( "eliot" , out.get( "Name" ) );
67-
assertTrue( out instanceof Person , "didn't come out as Person" );
63+
assertEquals(Person.class, out.getClass());
6864
}
6965

7066
public static class Outer extends ReflectionDBObject {
@@ -105,12 +101,54 @@ public void test2()
105101

106102
DBObject out = c.findOne();
107103
assertEquals( "eliot" , out.get( "Name" ) );
108-
assertTrue( out instanceof Outer , "didn't come out as Person" );
104+
assertEquals(Outer.class, out.getClass());
109105
o = (Outer)out;
110106
assertEquals( "eliot" , o.getName() );
111107
assertEquals( 17 , o.getInner().getNumber() );
112108
}
113109

110+
static class Process extends ReflectionDBObject {
111+
112+
public Process() {}
113+
114+
public String getName() {
115+
return name;
116+
}
117+
118+
public void setName(String name) {
119+
this.name = name;
120+
}
121+
122+
public int getStatus() {
123+
return status;
124+
}
125+
126+
public void setStatus(int status) {
127+
this.status = status;
128+
}
129+
130+
String name;
131+
int status;
132+
}
133+
134+
@Test
135+
public void testFindAndModify() {
136+
DBCollection c = _db.getCollection( "findAndModify" );
137+
c.drop();
138+
c.setObjectClass( Process.class );
139+
140+
Process p = new Process();
141+
p.setName("test");
142+
p.setStatus(0);
143+
c.save(p, WriteConcern.SAFE);
144+
145+
DBObject obj = c.findAndModify(new BasicDBObject(), new BasicDBObject("$set", new BasicDBObject("status", 1)));
146+
assertEquals(Process.class, obj.getClass());
147+
Process pModified = (Process) obj;
148+
assertEquals(0, pModified.getStatus());
149+
assertEquals("test", pModified.getName());
150+
}
151+
114152
final DB _db;
115153

116154
public static void main( String args[] )

0 commit comments

Comments
 (0)