Return-Path: Delivered-To: apmail-poi-commits-archive@minotaur.apache.org Received: (qmail 1048 invoked from network); 16 Mar 2009 07:03:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 16 Mar 2009 07:03:37 -0000 Received: (qmail 89184 invoked by uid 500); 16 Mar 2009 07:03:36 -0000 Delivered-To: apmail-poi-commits-archive@poi.apache.org Received: (qmail 89146 invoked by uid 500); 16 Mar 2009 07:03:36 -0000 Mailing-List: contact commits-help@poi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@poi.apache.org Delivered-To: mailing list commits@poi.apache.org Received: (qmail 89137 invoked by uid 99); 16 Mar 2009 07:03:36 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 Mar 2009 00:03:36 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 Mar 2009 07:03:35 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id CDAB52388999; Mon, 16 Mar 2009 07:03:15 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r754828 - /poi/trunk/src/java/org/apache/poi/hssf/record/RecordFactory.java Date: Mon, 16 Mar 2009 07:03:15 -0000 To: commits@poi.apache.org From: josh@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090316070315.CDAB52388999@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: josh Date: Mon Mar 16 07:03:13 2009 New Revision: 754828 URL: http://svn.apache.org/viewvc?rev=754828&view=rev Log: Abstracted record construction Modified: poi/trunk/src/java/org/apache/poi/hssf/record/RecordFactory.java Modified: poi/trunk/src/java/org/apache/poi/hssf/record/RecordFactory.java URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/RecordFactory.java?rev=754828&r1=754827&r2=754828&view=diff ============================================================================== --- poi/trunk/src/java/org/apache/poi/hssf/record/RecordFactory.java (original) +++ poi/trunk/src/java/org/apache/poi/hssf/record/RecordFactory.java Mon Mar 16 07:03:13 2009 @@ -45,6 +45,37 @@ */ public final class RecordFactory { private static final int NUM_RECORDS = 512; + + private interface I_RecordCreator { + Record create(RecordInputStream in); + + String getRecordClassName(); + } + private static final class ReflectionRecordCreator implements I_RecordCreator { + + private final Constructor _c; + public ReflectionRecordCreator(Constructor c) { + _c = c; + } + public Record create(RecordInputStream in) { + Object[] args = { in, }; + try { + return _c.newInstance(args); + } catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RecordFormatException("Unable to construct record instance" , e.getTargetException()); + } + } + public String getRecordClassName() { + return _c.getDeclaringClass().getName(); + } + } + private static final Class[] CONSTRUCTOR_ARGS = { RecordInputStream.class, }; @@ -189,7 +220,7 @@ /** * cache of the recordsToMap(); */ - private static Map> recordsMap = recordsToMap(recordClasses); + private static Map recordsMap = recordsToMap(recordClasses); private static short[] _allKnownRecordSIDs; @@ -213,24 +244,14 @@ return new Record[] { record, }; } - private static Record createSingleRecord(RecordInputStream in) { - Constructor constructor = recordsMap.get(new Short(in.getSid())); + static Record createSingleRecord(RecordInputStream in) { + I_RecordCreator constructor = recordsMap.get(new Short(in.getSid())); if (constructor == null) { return new UnknownRecord(in); } - try { - return constructor.newInstance(new Object[] { in }); - } catch (InvocationTargetException e) { - throw new RecordFormatException("Unable to construct record instance" , e.getTargetException()); - } catch (IllegalArgumentException e) { - throw new RuntimeException(e); - } catch (InstantiationException e) { - throw new RuntimeException(e); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } + return constructor.create(in); } /** @@ -290,8 +311,8 @@ * @return map of SIDs to short,short,byte[] constructors for Record classes * most of org.apache.poi.hssf.record.* */ - private static Map> recordsToMap(Class [] records) { - Map> result = new HashMap>(); + private static Map recordsToMap(Class [] records) { + Map result = new HashMap(); Set> uniqueRecClasses = new HashSet>(records.length * 3 / 2); for (int i = 0; i < records.length; i++) { @@ -318,11 +339,11 @@ } Short key = new Short(sid); if (result.containsKey(key)) { - Class prev = result.get(key).getDeclaringClass(); + String prevClassName = result.get(key).getRecordClassName(); throw new RuntimeException("duplicate record sid 0x" + Integer.toHexString(sid).toUpperCase() - + " for classes (" + recClass.getName() + ") and (" + prev.getName() + ")"); + + " for classes (" + recClass.getName() + ") and (" + prevClassName + ")"); } - result.put(key, constructor); + result.put(key, new ReflectionRecordCreator(constructor)); } return result; } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org For additional commands, e-mail: commits-help@poi.apache.org