1 package com.fasterxml.jackson.databind.introspect; 2 3 import java.lang.reflect.*; 4 5 import com.fasterxml.jackson.databind.JavaType; 6 import com.fasterxml.jackson.databind.util.ClassUtil; 7 8 /** 9 * Placeholder used by virtual properties as placeholder for 10 * underlying {@link AnnotatedMember}. 11 * 12 * @since 2.5 13 */ 14 public class VirtualAnnotatedMember extends AnnotatedMember 15 implements java.io.Serializable 16 { 17 private static final long serialVersionUID = 1L; 18 19 protected final Class<?> _declaringClass; 20 21 /** 22 * @since 2.8 with this signature; had <code>_rawType</code> earlier 23 */ 24 protected final JavaType _type; 25 26 protected final String _name; 27 28 /* 29 /********************************************************** 30 /* Life-cycle 31 /********************************************************** 32 */ 33 VirtualAnnotatedMember(TypeResolutionContext typeContext, Class<?> declaringClass, String name, JavaType type)34 public VirtualAnnotatedMember(TypeResolutionContext typeContext, Class<?> declaringClass, 35 String name, JavaType type) 36 { 37 super(typeContext, /* AnnotationMap*/ null); 38 _declaringClass = declaringClass; 39 _type = type; 40 _name = name; 41 } 42 43 @Override withAnnotations(AnnotationMap fallback)44 public Annotated withAnnotations(AnnotationMap fallback) { 45 return this; 46 } 47 48 /* 49 /********************************************************** 50 /* Annotated impl 51 /********************************************************** 52 */ 53 54 @Override getAnnotated()55 public Field getAnnotated() { return null; } 56 57 @Override getModifiers()58 public int getModifiers() { return 0; } 59 60 @Override getName()61 public String getName() { return _name; } 62 63 @Override getRawType()64 public Class<?> getRawType() { 65 return _type.getRawClass(); 66 } 67 68 @Override getType()69 public JavaType getType() { 70 return _type; 71 } 72 73 /* 74 /********************************************************** 75 /* AnnotatedMember impl 76 /********************************************************** 77 */ 78 79 @Override getDeclaringClass()80 public Class<?> getDeclaringClass() { return _declaringClass; } 81 82 @Override getMember()83 public Member getMember() { return null; } 84 85 @Override setValue(Object pojo, Object value)86 public void setValue(Object pojo, Object value) throws IllegalArgumentException { 87 throw new IllegalArgumentException("Cannot set virtual property '"+_name+"'"); 88 } 89 90 @Override getValue(Object pojo)91 public Object getValue(Object pojo) throws IllegalArgumentException { 92 throw new IllegalArgumentException("Cannot get virtual property '"+_name+"'"); 93 } 94 95 /* 96 /********************************************************** 97 /* Extended API, generic 98 /********************************************************** 99 */ 100 getAnnotationCount()101 public int getAnnotationCount() { return 0; } 102 103 @Override hashCode()104 public int hashCode() { 105 return _name.hashCode(); 106 } 107 108 @Override equals(Object o)109 public boolean equals(Object o) { 110 if (o == this) return true; 111 if (!ClassUtil.hasClass(o, getClass())) { 112 return false; 113 } 114 VirtualAnnotatedMember other = (VirtualAnnotatedMember) o; 115 return (other._declaringClass == _declaringClass) 116 && other._name.equals(_name); 117 } 118 119 @Override toString()120 public String toString() { 121 return "[virtual "+getFullName()+"]"; 122 } 123 } 124