1 package com.fasterxml.jackson.databind.exc; 2 3 import java.util.Collection; 4 import java.util.Collections; 5 import java.util.Iterator; 6 7 import com.fasterxml.jackson.core.JsonLocation; 8 import com.fasterxml.jackson.core.JsonParser; 9 import com.fasterxml.jackson.databind.JsonMappingException; 10 11 /** 12 * Base class for {@link JsonMappingException}s that are specifically related 13 * to problems related to binding an individual property. 14 * 15 * @since 2.3 16 */ 17 @SuppressWarnings("serial") 18 public abstract class PropertyBindingException 19 extends MismatchedInputException // since 2.9 20 { 21 /** 22 * Class that does not contain mapping for the unrecognized property. 23 */ 24 protected final Class<?> _referringClass; 25 26 /** 27 *<p> 28 * Note: redundant information since it is also included in the 29 * reference path. 30 */ 31 protected final String _propertyName; 32 33 /** 34 * Set of ids of properties that are known for the type, if this 35 * can be statically determined. 36 */ 37 protected final Collection<Object> _propertyIds; 38 39 /** 40 * Lazily constructed description of known properties, used for 41 * constructing actual message if and as needed. 42 */ 43 protected transient String _propertiesAsString; 44 45 /** 46 * @since 2.7 47 */ PropertyBindingException(JsonParser p, String msg, JsonLocation loc, Class<?> referringClass, String propName, Collection<Object> propertyIds)48 protected PropertyBindingException(JsonParser p, String msg, JsonLocation loc, 49 Class<?> referringClass, String propName, 50 Collection<Object> propertyIds) 51 { 52 super(p, msg, loc); 53 _referringClass = referringClass; 54 _propertyName = propName; 55 _propertyIds = propertyIds; 56 } 57 58 /** 59 * @deprecated Since 2.7 60 */ 61 @Deprecated // since 2.7 PropertyBindingException(String msg, JsonLocation loc, Class<?> referringClass, String propName, Collection<Object> propertyIds)62 protected PropertyBindingException(String msg, JsonLocation loc, 63 Class<?> referringClass, String propName, 64 Collection<Object> propertyIds) 65 { 66 this(null, msg, loc, referringClass, propName, propertyIds); 67 } 68 69 /* 70 /********************************************************** 71 /* Overrides 72 /********************************************************** 73 */ 74 75 /** 76 * Somewhat arbitrary limit, but let's try not to create uselessly 77 * huge error messages 78 */ 79 private final static int MAX_DESC_LENGTH = 1000; 80 81 @Override getMessageSuffix()82 public String getMessageSuffix() 83 { 84 String suffix = _propertiesAsString; 85 if (suffix == null && _propertyIds != null) { 86 StringBuilder sb = new StringBuilder(100); 87 int len = _propertyIds.size(); 88 if (len == 1) { 89 sb.append(" (one known property: \""); 90 sb.append(String.valueOf(_propertyIds.iterator().next())); 91 sb.append('"'); 92 } else { 93 sb.append(" (").append(len).append(" known properties: "); 94 Iterator<Object> it = _propertyIds.iterator(); 95 while (it.hasNext()) { 96 sb.append('"'); 97 sb.append(String.valueOf(it.next())); 98 sb.append('"'); 99 // one other thing: limit max length 100 if (sb.length() > MAX_DESC_LENGTH) { 101 sb.append(" [truncated]"); 102 break; 103 } 104 if (it.hasNext()) { 105 sb.append(", "); 106 } 107 } 108 } 109 sb.append("])"); 110 _propertiesAsString = suffix = sb.toString(); 111 } 112 return suffix; 113 } 114 115 /* 116 /********************************************************** 117 /* Extended API 118 /********************************************************** 119 */ 120 121 /** 122 * Method for accessing type (class) that is missing definition to allow 123 * binding of the unrecognized property. 124 */ getReferringClass()125 public Class<?> getReferringClass() { 126 return _referringClass; 127 } 128 129 /** 130 * Convenience method for accessing logical property name that could 131 * not be mapped. Note that it is the last path reference in the 132 * underlying path. 133 */ getPropertyName()134 public String getPropertyName() { 135 return _propertyName; 136 } 137 getKnownPropertyIds()138 public Collection<Object> getKnownPropertyIds() 139 { 140 if (_propertyIds == null) { 141 return null; 142 } 143 return Collections.unmodifiableCollection(_propertyIds); 144 } 145 } 146