diff --git a/src/solrj/org/apache/solr/client/solrj/beans/DocumentObjectBinder.java b/src/solrj/org/apache/solr/client/solrj/beans/DocumentObjectBinder.java index 56269c7602b..3706251cdf5 100644 --- a/src/solrj/org/apache/solr/client/solrj/beans/DocumentObjectBinder.java +++ b/src/solrj/org/apache/solr/client/solrj/beans/DocumentObjectBinder.java @@ -43,21 +43,30 @@ public class DocumentObjectBinder { for(int j=0;j T getBean(Class clazz, SolrDocument solrDoc) { + return getBean(clazz, null,solrDoc); + } + + private T getBean(Class clazz, List 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 ) { diff --git a/src/test/org/apache/solr/client/solrj/beans/TestDocumentObjectBinder.java b/src/test/org/apache/solr/client/solrj/beans/TestDocumentObjectBinder.java index 9d995d40786..38a33419b44 100644 --- a/src/test/org/apache/solr/client/solrj/beans/TestDocumentObjectBinder.java +++ b/src/test/org/apache/solr/client/solrj/beans/TestDocumentObjectBinder.java @@ -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 {