use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.
the class IndexHelper method findField.
String findField(final MappedClass mc, final IndexOptions options, final List<String> path) {
String segment = path.get(0);
if (segment.equals("$**")) {
return segment;
}
MappedField mf = mc.getMappedField(segment);
if (mf == null) {
mf = mc.getMappedFieldByJavaField(segment);
}
if (mf == null && mc.isInterface()) {
for (final MappedClass mappedClass : mapper.getSubTypes(mc)) {
try {
return findField(mappedClass, options, new ArrayList<String>(path));
} catch (MappingException e) {
// try the next one
}
}
}
String namePath;
if (mf != null) {
namePath = mf.getNameToStore();
} else {
if (!options.disableValidation()) {
throw pathFail(mc, path);
} else {
return join(path, '.');
}
}
if (path.size() > 1) {
try {
Class concreteType = !mf.isSingleValue() ? mf.getSubClass() : mf.getConcreteType();
namePath += "." + findField(mapper.getMappedClass(concreteType), options, path.subList(1, path.size()));
} catch (MappingException e) {
if (!options.disableValidation()) {
throw pathFail(mc, path);
} else {
return join(path, '.');
}
}
}
return namePath;
}
use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.
the class IndexHelper method calculateKeys.
BsonDocument calculateKeys(final MappedClass mc, final Index index) {
BsonDocument keys = new BsonDocument();
for (Field field : index.fields()) {
String path;
try {
path = findField(mc, index.options(), new ArrayList<String>(asList(field.value().split("\\."))));
} catch (Exception e) {
path = field.value();
String message = format("The path '%s' can not be validated against '%s' and may represent an invalid index", path, mc.getClazz().getName());
if (!index.options().disableValidation()) {
throw new MappingException(message);
}
LOG.warning(message);
}
keys.putAll(toBsonDocument(path, field.type().toIndexValue()));
}
return keys;
}
use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.
the class Morphia method mapPackage.
/**
* Tries to map all classes in the package specified.
*
* @param packageName the name of the package to process
* @param ignoreInvalidClasses specifies whether to ignore classes in the package that cannot be mapped
* @return the Morphia instance
*/
public synchronized Morphia mapPackage(final String packageName, final boolean ignoreInvalidClasses) {
try {
for (final Class clazz : ReflectionUtils.getClasses(packageName, mapper.getOptions().isMapSubPackages())) {
try {
final Embedded embeddedAnn = ReflectionUtils.getClassEmbeddedAnnotation(clazz);
final Entity entityAnn = ReflectionUtils.getClassEntityAnnotation(clazz);
final boolean isAbstract = Modifier.isAbstract(clazz.getModifiers());
if ((entityAnn != null || embeddedAnn != null) && !isAbstract) {
map(clazz);
}
} catch (final MappingException ex) {
if (!ignoreInvalidClasses) {
throw ex;
}
}
}
return this;
} catch (IOException e) {
throw new MappingException("Could not get map classes from package " + packageName, e);
} catch (ClassNotFoundException e) {
throw new MappingException("Could not get map classes from package " + packageName, e);
}
}
use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.
the class Converters method fromDBObject.
/**
* Creates an entity and populates its state based on the dbObject given. This method is primarily an internal method. Reliance on
* this method may break your application in future releases.
*
* @param dbObj the object state to use
* @param mf the MappedField containing the metadata to use when decoding in to a field
* @param targetEntity then entity to hold the state from the database
*/
public void fromDBObject(final DBObject dbObj, final MappedField mf, final Object targetEntity) {
final Object object = mf.getDbObjectValue(dbObj);
if (object != null) {
final TypeConverter enc = getEncoder(mf);
final Object decodedValue = enc.decode(mf.getType(), object, mf);
try {
mf.setFieldValue(targetEntity, decodedValue);
} catch (IllegalArgumentException e) {
throw new MappingException(format("Error setting value from converter (%s) for %s to %s", enc.getClass().getSimpleName(), mf.getFullName(), decodedValue), e);
}
}
}
use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.
the class ReflectionUtils method getParameterizedClass.
/**
* Get the class that parameterizes the Field supplied, at the index supplied (field can be parameterized with multiple param classes).
*
* @param field the field
* @param index the index of the parameterizing class
* @return the class that parameterizes the field, or null if field is not parameterized
*/
public static Class getParameterizedClass(final Field field, final int index) {
if (field.getGenericType() instanceof ParameterizedType) {
final ParameterizedType type = (ParameterizedType) field.getGenericType();
if ((type.getActualTypeArguments() != null) && (type.getActualTypeArguments().length <= index)) {
return null;
}
final Type paramType = type.getActualTypeArguments()[index];
if (paramType instanceof GenericArrayType) {
final Class arrayType = (Class) ((GenericArrayType) paramType).getGenericComponentType();
return Array.newInstance(arrayType, 0).getClass();
} else {
if (paramType instanceof ParameterizedType) {
final ParameterizedType paramPType = (ParameterizedType) paramType;
return (Class) paramPType.getRawType();
} else {
if (paramType instanceof TypeVariable) {
// from the T/V/X
throw new MappingException("Generic Typed Class not supported: <" + ((TypeVariable) paramType).getName() + "> = " + ((TypeVariable) paramType).getBounds()[0]);
} else if (paramType instanceof Class) {
return (Class) paramType;
} else {
throw new MappingException("Unknown type... pretty bad... call for help, wave your hands... yeah!");
}
}
}
}
return getParameterizedClass(field.getType());
}
Aggregations