DATACMNS-283 - MappingInstantiationException captures more context now.
If an exception occurs in ReflectionEntityInstantiator we now capture more context about the failed instantiation.
This commit is contained in:
@@ -17,6 +17,7 @@ package org.springframework.data.convert;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.BeanInstantiationException;
|
||||
@@ -60,7 +61,7 @@ public enum ReflectionEntityInstantiator implements EntityInstantiator {
|
||||
return BeanUtils.instantiateClass(entity.getType());
|
||||
}
|
||||
} catch (BeanInstantiationException e) {
|
||||
throw new MappingInstantiationException(e.getMessage(), e);
|
||||
new MappingInstantiationException(entity, Collections.emptyList(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +75,7 @@ public enum ReflectionEntityInstantiator implements EntityInstantiator {
|
||||
try {
|
||||
return BeanUtils.instantiateClass(constructor.getConstructor(), params.toArray());
|
||||
} catch (BeanInstantiationException e) {
|
||||
throw new MappingInstantiationException(e.getMessage(), e);
|
||||
throw new MappingInstantiationException(entity, params, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
* Copyright 2011-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -13,21 +13,102 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PreferredConstructor;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
* Exception being thrown in case an entity could not be instantiated in the process of a to-object-mapping.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Jon Brisbin
|
||||
*/
|
||||
public class MappingInstantiationException extends RuntimeException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 822211065035487628L;
|
||||
private static final String TEXT_TEMPLATE = "Failed to instantiate %s using constructor %s with arguments %s";
|
||||
|
||||
public MappingInstantiationException(String s, Throwable throwable) {
|
||||
super(s, throwable);
|
||||
private final Class<?> entityType;
|
||||
private final Constructor<?> constructor;
|
||||
private final List<Object> constructorArguments;
|
||||
|
||||
/**
|
||||
* Creates a {@link MappingInstantiationException} using the given message and cause.
|
||||
*
|
||||
* @deprecated use {@link #MappingInstantiationException(PersistentEntity, List, String, Exception)} instead.
|
||||
* @param message
|
||||
* @param cause
|
||||
*/
|
||||
@Deprecated
|
||||
public MappingInstantiationException(String message, Exception cause) {
|
||||
this(null, null, message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link MappingInstantiationException} for the given {@link PersistentEntity}, constructor arguments
|
||||
* and the causing exception.
|
||||
*
|
||||
* @param entity
|
||||
* @param arguments
|
||||
* @param cause
|
||||
*/
|
||||
public MappingInstantiationException(PersistentEntity<?, ?> entity, List<Object> arguments, Exception cause) {
|
||||
this(entity, arguments, null, cause);
|
||||
}
|
||||
|
||||
private MappingInstantiationException(PersistentEntity<?, ?> entity, List<Object> arguments, String message,
|
||||
Exception cause) {
|
||||
|
||||
super(buildExceptionMessage(entity, arguments, null), cause);
|
||||
|
||||
this.entityType = entity == null ? null : entity.getType();
|
||||
this.constructor = entity == null || entity.getPersistenceConstructor() == null ? null : entity
|
||||
.getPersistenceConstructor().getConstructor();
|
||||
this.constructorArguments = arguments;
|
||||
}
|
||||
|
||||
private static final String buildExceptionMessage(PersistentEntity<?, ?> entity, List<Object> arguments,
|
||||
String defaultMessage) {
|
||||
|
||||
if (entity == null) {
|
||||
return defaultMessage;
|
||||
}
|
||||
|
||||
PreferredConstructor<?, ?> constructor = entity.getPersistenceConstructor();
|
||||
|
||||
return String.format(TEXT_TEMPLATE, entity.getType().getName(), constructor == null ? "NO_CONSTRUCTOR"
|
||||
: constructor.getConstructor().toString(), StringUtils.collectionToCommaDelimitedString(arguments));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the entity that was attempted to instantiate.
|
||||
*
|
||||
* @return the entityType
|
||||
*/
|
||||
public Class<?> getEntityType() {
|
||||
return entityType;
|
||||
}
|
||||
|
||||
/**
|
||||
* The constructor used during the instantiation attempt.
|
||||
*
|
||||
* @return the constructor
|
||||
*/
|
||||
public Constructor<?> getConstructor() {
|
||||
return constructor;
|
||||
}
|
||||
|
||||
/**
|
||||
* The constructor arguments used to invoke the constructor.
|
||||
*
|
||||
* @return the constructorArguments
|
||||
*/
|
||||
public List<Object> getConstructorArguments() {
|
||||
return constructorArguments;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user