Throw informative error messages on bad enum field

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@911530 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2010-02-18 19:11:36 +00:00
parent 87b284b52b
commit 2f3da18be0
4 changed files with 25 additions and 12 deletions

View File

@ -26,7 +26,9 @@ import org.apache.openjpa.jdbc.meta.ValueMapping;
import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.jdbc.schema.ColumnIO;
import org.apache.openjpa.jdbc.sql.DBDictionary;
import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.meta.JavaTypes;
import org.apache.openjpa.util.Exceptions;
import org.apache.openjpa.util.MetaDataException;
/**
@ -34,11 +36,13 @@ import org.apache.openjpa.util.MetaDataException;
*
* @nojavadoc
*/
@SuppressWarnings("serial")
public class EnumValueHandler
extends AbstractValueHandler {
private Enum[] _vals = null;
private Enum<?>[] _vals = null;
private boolean _ordinal = false;
private static final Localizer _loc = Localizer.forPackage(EnumValueHandler.class);
/**
* Whether to store the enum value as its ordinal.
@ -72,7 +76,9 @@ public class EnumValueHandler
Method m = vm.getType().getMethod("values", (Class[]) null);
_vals = (Enum[]) m.invoke(null, (Object[]) null);
} catch (Exception e) {
throw new MetaDataException().setCause(e);
throw new MetaDataException(_loc.get("not-enum-field",
vm.getFieldMapping().getFullName(true), Exceptions.toClassName(vm.getType()))).setCause(e);
}
Column col = new Column();
@ -96,8 +102,7 @@ public class EnumValueHandler
return true;
}
public Object toDataStoreValue(ValueMapping vm, Object val,
JDBCStore store) {
public Object toDataStoreValue(ValueMapping vm, Object val, JDBCStore store) {
if (val == null)
return null;
if (_ordinal)

View File

@ -137,3 +137,4 @@ unmapped-datastore-value: Instances of type "{0}" are not valid query \
multi-column-version-unsupported:You have specified more than one column for \
version data, but the "{1}" version strategy does not support multi-column \
versioning.
not-enum-field: Field "{0}" is expected to be a enum but actually is of type "{1}".

View File

@ -265,4 +265,11 @@ public class Exceptions {
else
return pc.pcFetchObjectId();
}
public static String toClassName(Class<?> cls) {
if (cls == null) return "";
if (cls.isArray())
return toClassName(cls.getComponentType())+"[]";
return cls.getName();
}
}

View File

@ -48,11 +48,12 @@ import java.util.concurrent.CopyOnWriteArraySet;
public class Localizer {
// static cache of package+loc name to localizer mappings
private static final Map _localizers = new ConcurrentHashMap();
private static final Map<String,Localizer> _localizers = new ConcurrentHashMap<String,Localizer>();
// list of resource providers to delegate to when locating resources
private static final Collection _providers = new CopyOnWriteArraySet
(Arrays.asList(new Object[]{
private static final Collection<ResourceBundleProvider> _providers =
new CopyOnWriteArraySet<ResourceBundleProvider>
(Arrays.asList(new ResourceBundleProvider[]{
new SimpleResourceBundleProvider(),
new StreamResourceBundleProvider(),
new ZipResourceBundleProvider(), }));
@ -63,7 +64,7 @@ public class Localizer {
*
* @see #forPackage(Class,Locale)
*/
public static Localizer forPackage(Class cls) {
public static Localizer forPackage(Class<?> cls) {
return forPackage(cls, null);
}
@ -77,7 +78,7 @@ public class Localizer {
* @param locale the locale to which strings should be localized; if
* null, the system default will be assumed
*/
public static Localizer forPackage(Class cls, Locale locale) {
public static Localizer forPackage(Class<?> cls, Locale locale) {
if (locale == null)
locale = Locale.getDefault();
@ -138,10 +139,9 @@ public class Localizer {
// no locking; it's ok to create multiple bundles
if (_bundle == null) {
// find resource bundle
for (Iterator itr = _providers.iterator();
for (Iterator<ResourceBundleProvider> itr = _providers.iterator();
itr.hasNext() && _bundle == null; ) {
_bundle = ((ResourceBundleProvider) itr.next())
.findResource(_file, _locale, _loader);
_bundle = itr.next().findResource(_file, _locale, _loader);
}
}
return _bundle;