|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2011 by the original author(s). |
| 2 | + * Copyright 2011-2013 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
6 | 6 | * You may obtain a copy of the License at
|
7 | 7 | *
|
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 | *
|
10 | 10 | * Unless required by applicable law or agreed to in writing, software
|
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 |
| - |
17 | 16 | package org.springframework.data.mapping.model;
|
18 | 17 |
|
| 18 | +import java.lang.reflect.Constructor; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.springframework.data.mapping.PersistentEntity; |
| 22 | +import org.springframework.data.mapping.PreferredConstructor; |
| 23 | +import org.springframework.util.StringUtils; |
| 24 | + |
19 | 25 | /**
|
20 |
| - * @author Jon Brisbin <[email protected]> |
| 26 | + * Exception being thrown in case an entity could not be instantiated in the process of a to-object-mapping. |
| 27 | + * |
| 28 | + * @author Oliver Gierke |
| 29 | + * @author Jon Brisbin |
21 | 30 | */
|
22 | 31 | public class MappingInstantiationException extends RuntimeException {
|
23 | 32 |
|
| 33 | + private static final long serialVersionUID = 822211065035487628L; |
| 34 | + private static final String TEXT_TEMPLATE = "Failed to instantiate %s using constructor %s with arguments %s"; |
| 35 | + |
| 36 | + private final Class<?> entityType; |
| 37 | + private final Constructor<?> constructor; |
| 38 | + private final List<Object> constructorArguments; |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a {@link MappingInstantiationException} using the given message and cause. |
| 42 | + * |
| 43 | + * @deprecated use {@link #MappingInstantiationException(PersistentEntity, List, String, Exception)} instead. |
| 44 | + * @param message |
| 45 | + * @param cause |
| 46 | + */ |
| 47 | + @Deprecated |
| 48 | + public MappingInstantiationException(String message, Exception cause) { |
| 49 | + this(null, null, message, cause); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Creates a new {@link MappingInstantiationException} for the given {@link PersistentEntity}, constructor arguments |
| 54 | + * and the causing exception. |
| 55 | + * |
| 56 | + * @param entity |
| 57 | + * @param arguments |
| 58 | + * @param cause |
| 59 | + */ |
| 60 | + public MappingInstantiationException(PersistentEntity<?, ?> entity, List<Object> arguments, Exception cause) { |
| 61 | + this(entity, arguments, null, cause); |
| 62 | + } |
| 63 | + |
| 64 | + private MappingInstantiationException(PersistentEntity<?, ?> entity, List<Object> arguments, String message, |
| 65 | + Exception cause) { |
| 66 | + |
| 67 | + super(buildExceptionMessage(entity, arguments, null), cause); |
| 68 | + |
| 69 | + this.entityType = entity == null ? null : entity.getType(); |
| 70 | + this.constructor = entity == null || entity.getPersistenceConstructor() == null ? null : entity |
| 71 | + .getPersistenceConstructor().getConstructor(); |
| 72 | + this.constructorArguments = arguments; |
| 73 | + } |
| 74 | + |
| 75 | + private static final String buildExceptionMessage(PersistentEntity<?, ?> entity, List<Object> arguments, |
| 76 | + String defaultMessage) { |
| 77 | + |
| 78 | + if (entity == null) { |
| 79 | + return defaultMessage; |
| 80 | + } |
| 81 | + |
| 82 | + PreferredConstructor<?, ?> constructor = entity.getPersistenceConstructor(); |
| 83 | + |
| 84 | + return String.format(TEXT_TEMPLATE, entity.getType().getName(), constructor == null ? "NO_CONSTRUCTOR" |
| 85 | + : constructor.getConstructor().toString(), StringUtils.collectionToCommaDelimitedString(arguments)); |
| 86 | + } |
| 87 | + |
24 | 88 | /**
|
25 |
| - * |
| 89 | + * Returns the type of the entity that was attempted to instantiate. |
| 90 | + * |
| 91 | + * @return the entityType |
26 | 92 | */
|
27 |
| - private static final long serialVersionUID = 1L; |
| 93 | + public Class<?> getEntityType() { |
| 94 | + return entityType; |
| 95 | + } |
28 | 96 |
|
29 |
| - public MappingInstantiationException(String s, Throwable throwable) { |
30 |
| - super(s, throwable); |
| 97 | + /** |
| 98 | + * The constructor used during the instantiation attempt. |
| 99 | + * |
| 100 | + * @return the constructor |
| 101 | + */ |
| 102 | + public Constructor<?> getConstructor() { |
| 103 | + return constructor; |
31 | 104 | }
|
32 | 105 |
|
| 106 | + /** |
| 107 | + * The constructor arguments used to invoke the constructor. |
| 108 | + * |
| 109 | + * @return the constructorArguments |
| 110 | + */ |
| 111 | + public List<Object> getConstructorArguments() { |
| 112 | + return constructorArguments; |
| 113 | + } |
33 | 114 | }
|
0 commit comments