From 69ac6942a3a5baf5b5a1d3296f76430d94b23ea3 Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Wed, 11 Nov 2009 06:33:11 +0000 Subject: [PATCH] SOLR-1551 Provide DocumentObjectBinder.getBean() method git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@834780 13f79535-47bb-0310-9956-ffa450edef68 --- .../solrj/beans/DocumentObjectBinder.java | 33 ++++++++++++------- .../solrj/beans/TestDocumentObjectBinder.java | 7 +++- 2 files changed, 27 insertions(+), 13 deletions(-) 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 {