1 package com.fasterxml.jackson.databind.introspect; 2 3 import java.lang.annotation.Annotation; 4 import java.lang.reflect.Member; 5 import java.util.Collections; 6 7 import com.fasterxml.jackson.databind.util.ClassUtil; 8 9 /** 10 * Intermediate base class for annotated entities that are members of 11 * a class; fields, methods and constructors. This is a superset 12 * of things that can represent logical properties as it contains 13 * constructors in addition to fields and methods. 14 */ 15 public abstract class AnnotatedMember 16 extends Annotated 17 implements java.io.Serializable 18 { 19 private static final long serialVersionUID = 1L; // since 2.5 20 21 // 19-Dec-2014, tatu: Similarly, assumed NOT to be needed in cases where 22 // owning object (ObjectMapper or relatives) is being JDK-serialized 23 /** 24 * Context object needed for resolving generic type associated with this 25 * member (method parameter or return value, or field type). 26 * 27 * @since 2.7 28 */ 29 protected final transient TypeResolutionContext _typeContext; 30 31 // Transient since information not needed after construction, so 32 // no need to persist 33 protected final transient AnnotationMap _annotations; 34 AnnotatedMember(TypeResolutionContext ctxt, AnnotationMap annotations)35 protected AnnotatedMember(TypeResolutionContext ctxt, AnnotationMap annotations) { 36 super(); 37 _typeContext = ctxt; 38 _annotations = annotations; 39 } 40 41 /** 42 * Copy-constructor. 43 * 44 * @since 2.5 45 */ AnnotatedMember(AnnotatedMember base)46 protected AnnotatedMember(AnnotatedMember base) { 47 _typeContext = base._typeContext; 48 _annotations = base._annotations; 49 } 50 51 /** 52 * Fluent factory method that will construct a new instance that uses specified 53 * instance annotations instead of currently configured ones. 54 * 55 * @since 2.9 (promoted from `Annotated`) 56 */ withAnnotations(AnnotationMap fallback)57 public abstract Annotated withAnnotations(AnnotationMap fallback); 58 59 /** 60 * Actual physical class in which this memmber was declared. 61 */ getDeclaringClass()62 public abstract Class<?> getDeclaringClass(); 63 getMember()64 public abstract Member getMember(); 65 getFullName()66 public String getFullName() { 67 return getDeclaringClass().getName() + "#" + getName(); 68 } 69 70 /** 71 * Accessor for {@link TypeResolutionContext} that is used for resolving 72 * full generic type of this member. 73 * 74 * @since 2.7 75 * 76 * @deprecated Since 2.9 77 */ 78 @Deprecated getTypeContext()79 public TypeResolutionContext getTypeContext() { 80 return _typeContext; 81 } 82 83 @Override getAnnotation(Class<A> acls)84 public final <A extends Annotation> A getAnnotation(Class<A> acls) { 85 if (_annotations == null) { 86 return null; 87 } 88 return _annotations.get(acls); 89 } 90 91 @Override hasAnnotation(Class<?> acls)92 public final boolean hasAnnotation(Class<?> acls) { 93 if (_annotations == null) { 94 return false; 95 } 96 return _annotations.has(acls); 97 } 98 99 @Override hasOneOf(Class<? extends Annotation>[] annoClasses)100 public boolean hasOneOf(Class<? extends Annotation>[] annoClasses) { 101 if (_annotations == null) { 102 return false; 103 } 104 return _annotations.hasOneOf(annoClasses); 105 } 106 107 @Override 108 @Deprecated annotations()109 public Iterable<Annotation> annotations() { 110 if (_annotations == null) { 111 return Collections.emptyList(); 112 } 113 return _annotations.annotations(); 114 } 115 116 /** 117 *<p> 118 * NOTE: promoted in 2.9 from `Annotated` up 119 */ getAllAnnotations()120 public AnnotationMap getAllAnnotations() { // alas, used by at least one module, hence public 121 return _annotations; 122 } 123 124 /** 125 * Method that can be called to modify access rights, by calling 126 * {@link java.lang.reflect.AccessibleObject#setAccessible} on 127 * the underlying annotated element. 128 *<p> 129 * Note that caller should verify that 130 * {@link com.fasterxml.jackson.databind.MapperFeature#CAN_OVERRIDE_ACCESS_MODIFIERS} 131 * is enabled before calling this method; as well as pass 132 * <code>force</code> flag appropriately. 133 * 134 * @since 2.7 135 */ fixAccess(boolean force)136 public final void fixAccess(boolean force) { 137 Member m = getMember(); 138 if (m != null) { // may be null for virtual members 139 ClassUtil.checkAndFixAccess(m, force); 140 } 141 } 142 143 /** 144 * Optional method that can be used to assign value of 145 * this member on given object, if this is a supported 146 * operation for member type. 147 *<p> 148 * This is implemented for fields and single-argument 149 * member methods; but not for constructor parameters or 150 * other types of methods (like static methods) 151 */ setValue(Object pojo, Object value)152 public abstract void setValue(Object pojo, Object value) 153 throws UnsupportedOperationException, IllegalArgumentException; 154 155 /** 156 * Optional method that can be used to access the value of 157 * this member on given object, if this is a supported 158 * operation for member type. 159 *<p> 160 * This is implemented for fields and no-argument 161 * member methods; but not for constructor parameters or 162 * other types of methods (like static methods) 163 */ getValue(Object pojo)164 public abstract Object getValue(Object pojo) 165 throws UnsupportedOperationException, IllegalArgumentException; 166 } 167