1 package com.fasterxml.jackson.databind; 2 3 import java.util.*; 4 5 import com.fasterxml.jackson.databind.util.ClassUtil; 6 7 /** 8 * Abstract class that defines API for objects that provide value to 9 * "inject" during deserialization. An instance of this object 10 */ 11 public abstract class InjectableValues 12 { 13 /** 14 * Method called to find value identified by id <code>valueId</code> to 15 * inject as value of specified property during deserialization, passing 16 * POJO instance in which value will be injected if it is available 17 * (will be available when injected via field or setter; not available 18 * when injected via constructor or factory method argument). 19 * 20 * @param valueId Object that identifies value to inject; may be a simple 21 * name or more complex identifier object, whatever provider needs 22 * @param ctxt Deserialization context 23 * @param forProperty Bean property in which value is to be injected 24 * @param beanInstance Bean instance that contains property to inject, 25 * if available; null if bean has not yet been constructed. 26 */ findInjectableValue(Object valueId, DeserializationContext ctxt, BeanProperty forProperty, Object beanInstance)27 public abstract Object findInjectableValue(Object valueId, DeserializationContext ctxt, 28 BeanProperty forProperty, Object beanInstance) throws JsonMappingException; 29 30 /* 31 /********************************************************** 32 /* Standard implementation 33 /********************************************************** 34 */ 35 36 /** 37 * Simple standard implementation which uses a simple Map to 38 * store values to inject, identified by simple String keys. 39 */ 40 public static class Std 41 extends InjectableValues 42 implements java.io.Serializable 43 { 44 private static final long serialVersionUID = 1L; 45 46 protected final Map<String,Object> _values; 47 Std()48 public Std() { 49 this(new HashMap<String,Object>()); 50 } 51 Std(Map<String,Object> values)52 public Std(Map<String,Object> values) { 53 _values = values; 54 } 55 addValue(String key, Object value)56 public Std addValue(String key, Object value) { 57 _values.put(key, value); 58 return this; 59 } 60 addValue(Class<?> classKey, Object value)61 public Std addValue(Class<?> classKey, Object value) { 62 _values.put(classKey.getName(), value); 63 return this; 64 } 65 66 @Override findInjectableValue(Object valueId, DeserializationContext ctxt, BeanProperty forProperty, Object beanInstance)67 public Object findInjectableValue(Object valueId, DeserializationContext ctxt, 68 BeanProperty forProperty, Object beanInstance) throws JsonMappingException 69 { 70 if (!(valueId instanceof String)) { 71 ctxt.reportBadDefinition(ClassUtil.classOf(valueId), 72 String.format( 73 "Unrecognized inject value id type (%s), expecting String", 74 ClassUtil.classNameOf(valueId))); 75 } 76 String key = (String) valueId; 77 Object ob = _values.get(key); 78 if (ob == null && !_values.containsKey(key)) { 79 throw new IllegalArgumentException("No injectable id with value '"+key+"' found (for property '"+forProperty.getName()+"')"); 80 } 81 return ob; 82 } 83 } 84 } 85