From 7fd42bc2897d0c8b7fb087f96754b545ab891772 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 19 Sep 2007 17:52:18 +0000 Subject: [PATCH] getFullTextIndex() is not expected to return null, so throw an informative exception if it would rather than letting the caller NPE. Also switched to the Zell-style single-letter studly _pClass. --- .../jdbc/depot/DepotMarshaller.java | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java index 99a5ed1..2d646c8 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java @@ -68,17 +68,17 @@ public class DepotMarshaller /** * Creates a marshaller for the specified persistent object class. */ - public DepotMarshaller (Class pclass, PersistenceContext context) + public DepotMarshaller (Class pClass, PersistenceContext context) { - _pclass = pclass; + _pClass = pClass; - Entity entity = pclass.getAnnotation(Entity.class); + Entity entity = pClass.getAnnotation(Entity.class); // see if this is a computed entity - _computed = pclass.getAnnotation(Computed.class); + _computed = pClass.getAnnotation(Computed.class); if (_computed == null) { // if not, this class has a corresponding SQL table - _tableName = _pclass.getName(); + _tableName = _pClass.getName(); _tableName = _tableName.substring(_tableName.lastIndexOf(".")+1); // see if there are Entity values specified @@ -91,7 +91,7 @@ public class DepotMarshaller // if the entity defines a new TableGenerator, map that in our static table as those are // shared across all entities - TableGenerator generator = pclass.getAnnotation(TableGenerator.class); + TableGenerator generator = pClass.getAnnotation(TableGenerator.class); if (generator != null) { context.tableGenerators.put(generator.name(), generator); } @@ -100,7 +100,7 @@ public class DepotMarshaller // introspect on the class and create marshallers for persistent fields ArrayList fields = new ArrayList(); - for (Field field : _pclass.getFields()) { + for (Field field : _pClass.getFields()) { int mods = field.getModifiers(); // check for a static constant schema version @@ -110,7 +110,7 @@ public class DepotMarshaller _schemaVersion = (Integer)field.get(null); } catch (Exception e) { log.log(Level.WARNING, "Failed to read schema version " + - "[class=" + _pclass + "].", e); + "[class=" + _pClass + "].", e); } } @@ -183,7 +183,7 @@ public class DepotMarshaller _allFields = fields.toArray(new String[fields.size()]); // now check for @Entity and @Table annotations on the entire superclass chain - Class iterClass = pclass; + Class iterClass = pClass; do { Table table = iterClass.getAnnotation(Table.class); if (table != null) { @@ -231,7 +231,7 @@ public class DepotMarshaller */ public Class getPersistentClass () { - return _pclass; + return _pClass; } /** @@ -269,11 +269,19 @@ public class DepotMarshaller } /** - * Return the {@link FullTextIndex} registered under the given name, or null if none. + * Return the {@link FullTextIndex} registered under the given name. + * + * @exception IllegalArgumentException thrown if the requested full text index does not exist + * on this record. */ public FullTextIndex getFullTextIndex (String name) { - return _fullTextIndexes.get(name); + FullTextIndex fti = _fullTextIndexes.get(name); + if (fti == null) { + throw new IllegalStateException("Persistent class missing full text index " + + "[class=" + _pClass + ", index=" + name + "]"); + } + return fti; } /** @@ -335,7 +343,7 @@ public class DepotMarshaller if (!hasPrimaryKey()) { if (requireKey) { throw new UnsupportedOperationException( - _pclass.getName() + " does not define a primary key"); + _pClass.getName() + " does not define a primary key"); } return null; } @@ -382,7 +390,7 @@ public class DepotMarshaller for (int ii = 0; ii < _pkColumns.size(); ii++) { fields[ii] = _pkColumns.get(ii).getField().getName(); } - return new Key(_pclass, fields, values); + return new Key(_pClass, fields, values); } /** @@ -433,7 +441,7 @@ public class DepotMarshaller } // then create and populate the persistent object - T po = _pclass.newInstance(); + T po = _pClass.newInstance(); for (FieldMarshaller fm : _fields.values()) { if (!fields.contains(fm.getColumnName())) { // this field was not in the result set, make sure that's OK @@ -451,8 +459,8 @@ public class DepotMarshaller throw sqe; } catch (Exception e) { - String errmsg = "Failed to unmarshall persistent object [pclass=" + - _pclass.getName() + "]"; + String errmsg = "Failed to unmarshall persistent object [class=" + + _pClass.getName() + "]"; throw (SQLException)new SQLException(errmsg).initCause(e); } } @@ -486,7 +494,7 @@ public class DepotMarshaller } catch (Exception e) { throw new IllegalStateException( - "Failed to assign primary key [type=" + _pclass + "]", e); + "Failed to assign primary key [type=" + _pClass + "]", e); } } return idFields; @@ -512,11 +520,11 @@ public class DepotMarshaller { if (_initialized) { // sanity check throw new IllegalStateException( - "Cannot re-initialize marshaller [type=" + _pclass + "]."); + "Cannot re-initialize marshaller [type=" + _pClass + "]."); } _initialized = true; - final SQLBuilder builder = ctx.getSQLBuilder(new DepotTypes(ctx, _pclass)); + final SQLBuilder builder = ctx.getSQLBuilder(new DepotTypes(ctx, _pClass)); // perform the context-sensitive initialization of the field marshallers for (FieldMarshaller fm : _fields.values()) { @@ -548,7 +556,7 @@ public class DepotMarshaller // if we did not find a schema version field, complain if (_schemaVersion < 0) { - log.warning("Unable to read " + _pclass.getName() + "." + SCHEMA_VERSION_FIELD + + log.warning("Unable to read " + _pClass.getName() + "." + SCHEMA_VERSION_FIELD + ". Schema migration disabled."); } @@ -832,7 +840,7 @@ public class DepotMarshaller FieldMarshaller fm = _fields.get(fields[ii]); if (fm == null) { throw new IllegalArgumentException( - "Unknown field on record [field=" + fields[ii] + ", pClass=" + _pclass + "]"); + "Unknown field on record [field=" + fields[ii] + ", class=" + _pClass + "]"); } columns[ii] = fm.getColumnName(); } @@ -939,7 +947,7 @@ public class DepotMarshaller } /** The persistent object class that we manage. */ - protected Class _pclass; + protected Class _pClass; /** The name of our persistent object table. */ protected String _tableName;