SOLR-1551 Provide DocumentObjectBinder.getBean() method

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@834780 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Noble Paul 2009-11-11 06:33:11 +00:00
parent bb19fb01d8
commit 69ac6942a3
2 changed files with 27 additions and 13 deletions

View File

@ -43,21 +43,30 @@ public class DocumentObjectBinder {
for(int j=0;j<solrDocList.size();j++) {
SolrDocument sdoc = solrDocList.get(j);
T obj = null;
try {
obj = clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException("Could not instantiate object of " + clazz,e);
}
for (int i = 0; i < fields.size(); i++) {
DocField docField = fields.get(i);
docField.inject(obj, sdoc);
}
result.add(obj);
result.add(getBean(clazz, fields, sdoc));
}
return result;
}
public <T> T getBean(Class<T> clazz, SolrDocument solrDoc) {
return getBean(clazz, null,solrDoc);
}
private <T> T getBean(Class<T> clazz, List<DocField> fields, SolrDocument solrDoc) {
if (fields == null) {
fields = getDocFields(clazz);
}
T obj = null;
try {
obj = clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException("Could not instantiate object of " + clazz, e);
}
for (int i = 0; i < fields.size(); i++) {
DocField docField = fields.get(i);
docField.inject(obj, solrDoc);
}
return obj;
}
public SolrInputDocument toSolrInputDocument( Object obj )
{

View File

@ -106,12 +106,17 @@ public class TestDocumentObjectBinder extends TestCase
SolrDocumentList docs = new SolrDocumentList();
docs.add( ClientUtils.toSolrDocument(doc) );
Item out = binder.getBeans( Item.class, docs ).get( 0 );
Item singleOut = binder.getBean(Item.class, ClientUtils.toSolrDocument(doc));
// make sure it came out the same
Assert.assertEquals( item.id, out.id );
Assert.assertEquals( item.inStock, out.inStock );
Assert.assertEquals( item.categories.length, out.categories.length );
Assert.assertEquals( item.features, out.features );
Assert.assertEquals( item.id, singleOut.id );
Assert.assertEquals( item.inStock, singleOut.inStock );
Assert.assertEquals( item.categories.length, singleOut.categories.length );
Assert.assertEquals( item.features, singleOut.features );
}
public static class Item {