|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.beans.factory; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | +import java.lang.reflect.AnnotatedElement; |
| 21 | +import java.lang.reflect.Field; |
| 22 | +import java.lang.reflect.Member; |
| 23 | + |
| 24 | +import org.springframework.core.MethodParameter; |
| 25 | +import org.springframework.util.Assert; |
| 26 | + |
| 27 | +/** |
| 28 | + * A simple descriptor for an injection point, pointing to a method/constructor |
| 29 | + * parameter or a field. Exposed by {@link UnsatisfiedDependencyException}. |
| 30 | + * |
| 31 | + * @author Juergen Hoeller |
| 32 | + * @since 4.3 |
| 33 | + * @see UnsatisfiedDependencyException#getInjectionPoint() |
| 34 | + * @see org.springframework.beans.factory.config.DependencyDescriptor |
| 35 | + */ |
| 36 | +public class InjectionPoint { |
| 37 | + |
| 38 | + protected MethodParameter methodParameter; |
| 39 | + |
| 40 | + protected Field field; |
| 41 | + |
| 42 | + private volatile Annotation[] fieldAnnotations; |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * Create an injection point descriptor for a method or constructor parameter. |
| 47 | + * @param methodParameter the MethodParameter to wrap |
| 48 | + */ |
| 49 | + public InjectionPoint(MethodParameter methodParameter) { |
| 50 | + Assert.notNull(methodParameter, "MethodParameter must not be null"); |
| 51 | + this.methodParameter = methodParameter; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Create an injection point descriptor for a field. |
| 56 | + * @param field the field to wrap |
| 57 | + */ |
| 58 | + public InjectionPoint(Field field) { |
| 59 | + Assert.notNull(field, "Field must not be null"); |
| 60 | + this.field = field; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Copy constructor. |
| 65 | + * @param original the original descriptor to create a copy from |
| 66 | + */ |
| 67 | + protected InjectionPoint(InjectionPoint original) { |
| 68 | + this.methodParameter = (original.methodParameter != null ? |
| 69 | + new MethodParameter(original.methodParameter) : null); |
| 70 | + this.field = original.field; |
| 71 | + this.fieldAnnotations = original.fieldAnnotations; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Just available for serialization purposes in subclasses. |
| 76 | + */ |
| 77 | + protected InjectionPoint() { |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + /** |
| 82 | + * Return the wrapped MethodParameter, if any. |
| 83 | + * <p>Note: Either MethodParameter or Field is available. |
| 84 | + * @return the MethodParameter, or {@code null} if none |
| 85 | + */ |
| 86 | + public MethodParameter getMethodParameter() { |
| 87 | + return this.methodParameter; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Return the wrapped Field, if any. |
| 92 | + * <p>Note: Either MethodParameter or Field is available. |
| 93 | + * @return the Field, or {@code null} if none |
| 94 | + */ |
| 95 | + public Field getField() { |
| 96 | + return this.field; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Obtain the annotations associated with the wrapped field or method/constructor parameter. |
| 101 | + */ |
| 102 | + public Annotation[] getAnnotations() { |
| 103 | + if (this.field != null) { |
| 104 | + if (this.fieldAnnotations == null) { |
| 105 | + this.fieldAnnotations = this.field.getAnnotations(); |
| 106 | + } |
| 107 | + return this.fieldAnnotations; |
| 108 | + } |
| 109 | + else { |
| 110 | + return this.methodParameter.getParameterAnnotations(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Return the type declared by the underlying field or method/constructor parameter, |
| 116 | + * indicating the injection type. |
| 117 | + */ |
| 118 | + public Class<?> getDeclaredType() { |
| 119 | + return (this.field != null ? this.field.getType() : this.methodParameter.getParameterType()); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Returns the wrapped member, containing the injection point. |
| 124 | + * @return the Field / Method / Constructor as Member |
| 125 | + */ |
| 126 | + public Member getMember() { |
| 127 | + return (this.field != null ? this.field : this.methodParameter.getMember()); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Return the wrapped annotated element. |
| 132 | + * <p>Note: In case of a method/constructor parameter, this exposes |
| 133 | + * the annotations declared on the method or constructor itself |
| 134 | + * (i.e. at the method/constructor level, not at the parameter level). |
| 135 | + * Use {@link #getAnnotations()} to obtain parameter-level annotations in |
| 136 | + * such a scenario, transparently with corresponding field annotations. |
| 137 | + * @return the Field / Method / Constructor as AnnotatedElement |
| 138 | + */ |
| 139 | + public AnnotatedElement getAnnotatedElement() { |
| 140 | + return (this.field != null ? this.field : this.methodParameter.getAnnotatedElement()); |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + @Override |
| 145 | + public boolean equals(Object other) { |
| 146 | + if (this == other) { |
| 147 | + return true; |
| 148 | + } |
| 149 | + if (getClass() != other.getClass()) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + InjectionPoint otherPoint = (InjectionPoint) other; |
| 153 | + return (this.field != null ? this.field.equals(otherPoint.field) : |
| 154 | + this.methodParameter.equals(otherPoint.methodParameter)); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public int hashCode() { |
| 159 | + return (this.field != null ? this.field.hashCode() : this.methodParameter.hashCode()); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String toString() { |
| 164 | + return (this.field != null ? "field '" + this.field.getName() + "'" : this.methodParameter.toString()); |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments