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  * Object that represents method parameters, mostly so that associated
10  * annotations can be processed conveniently. Note that many of accessors
11  * cannot return meaningful values since parameters do not have stand-alone
12  * JDK objects associated; so access should mostly be limited to checking
13  * annotation values which are properly aggregated and included.
14  */

15 public final class AnnotatedParameter
16     extends AnnotatedMember
17 {
18     private static final long serialVersionUID = 1L;
19
20     /**
21      * Member (method, constructor) that this parameter belongs to
22      */

23     protected final AnnotatedWithParams _owner;
24     
25     /**
26      * JDK type of the parameter, possibly contains generic type information
27      */

28     protected final JavaType _type;
29     
30     /**
31      * Index of the parameter within argument list
32      */

33     protected final int _index;
34
35     /*
36     /**********************************************************
37     /* Life-cycle
38     /**********************************************************
39      */

40
41     public AnnotatedParameter(AnnotatedWithParams owner, JavaType type,
42             TypeResolutionContext typeContext,
43             AnnotationMap annotations, int index)
44     {
45         super(typeContext, annotations);
46         _owner = owner;
47         _type = type;
48         _index = index;
49     }
50
51     @Override
52     public AnnotatedParameter withAnnotations(AnnotationMap ann) {
53         if (ann == _annotations) {
54             return this;
55         }
56         return _owner.replaceParameterAnnotations(_index, ann);
57     }
58
59     /*
60     /**********************************************************
61     /* Annotated impl
62     /**********************************************************
63      */

64
65     /**
66      * Since there is no matching JDK element, this method will
67      * always return null
68      */

69     @Override
70     public AnnotatedElement getAnnotated() { return null; }
71
72     /**
73      * Returns modifiers of the constructor, as parameters do not
74      * have independent modifiers.
75      */

76     @Override
77     public int getModifiers() { return _owner.getModifiers(); }
78
79     /**
80      * Parameters have no names in bytecode (unlike in source code),
81      * will always return empty String ("").
82      */

83     @Override
84     public String getName() { return ""; }
85
86     @Override
87     public Class<?> getRawType() {
88         return _type.getRawClass();
89     }
90
91     @Override
92     public JavaType getType() {
93         return _type;
94     }
95
96     /*
97     /**********************************************************
98     /* AnnotatedMember extras
99     /**********************************************************
100      */

101
102     @Override
103     public Class<?> getDeclaringClass() {
104         return _owner.getDeclaringClass();
105     }
106
107     @Override
108     public Member getMember() {
109         // This is bit tricky: since there is no JDK equivalent; can either
110         // return null or owner... let's do latter, for now.
111         return _owner.getMember();
112     }
113
114     @Override
115     public void setValue(Object pojo, Object value) throws UnsupportedOperationException
116     {
117         throw new UnsupportedOperationException("Cannot call setValue() on constructor parameter of "
118                 +getDeclaringClass().getName());
119     }
120
121     @Override
122     public Object getValue(Object pojo) throws UnsupportedOperationException
123     {
124         throw new UnsupportedOperationException("Cannot call getValue() on constructor parameter of "
125                 +getDeclaringClass().getName());
126     }
127
128     /*
129     /**********************************************************
130     /* Extended API
131     /**********************************************************
132      */

133
134     public Type getParameterType() { return _type; }
135
136     /**
137      * Accessor for 'owner' of this parameter; method or constructor that
138      * has this parameter as member of its argument list.
139      * 
140      * @return Owner (member or creator) object of this parameter
141      */

142     public AnnotatedWithParams getOwner() { return _owner; }
143     
144     /**
145      * Accessor for index of this parameter within argument list
146      * 
147      * @return Index of this parameter within argument list
148      */

149     public int getIndex() { return _index; }
150
151     /*
152     /********************************************************
153     /* Other
154     /********************************************************
155      */

156     
157     @Override
158     public int hashCode() {
159         return _owner.hashCode() + _index;
160     }
161     
162     @Override
163     public boolean equals(Object o) {
164         if (o == thisreturn true;
165         if (!ClassUtil.hasClass(o, getClass())) {
166             return false;
167         }
168         AnnotatedParameter other = (AnnotatedParameter) o;
169         return other._owner.equals(_owner) && (other._index == _index);
170     }
171     
172     @Override
173     public String toString() {
174         return "[parameter #"+getIndex()+", annotations: "+_annotations+"]";
175     }
176 }
177
178