001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.config;
018:
019: import java.lang.reflect.Field;
020:
021: import org.springframework.core.GenericCollectionTypeResolver;
022: import org.springframework.core.JdkVersion;
023: import org.springframework.core.MethodParameter;
024: import org.springframework.util.Assert;
025:
026: /**
027: * Descriptor for a specific dependency that is about to be injected.
028: * Wraps a constructor parameter, a method parameter or a field,
029: * allowing unified access to their metadata.
030: *
031: * @author Juergen Hoeller
032: * @since 2.5
033: */
034: public class DependencyDescriptor {
035:
036: private MethodParameter methodParameter;
037:
038: private Field field;
039:
040: private final boolean required;
041:
042: /**
043: * Create a new descriptor for a method or constructor parameter.
044: * @param methodParameter the MethodParameter to wrap
045: * @param required whether the dependency is required
046: */
047: public DependencyDescriptor(MethodParameter methodParameter,
048: boolean required) {
049: Assert.notNull(methodParameter,
050: "MethodParameter must not be null");
051: this .methodParameter = methodParameter;
052: this .required = required;
053: }
054:
055: /**
056: * Create a new descriptor for a field.
057: * @param field the field to wrap
058: * @param required whether the dependency is required
059: */
060: public DependencyDescriptor(Field field, boolean required) {
061: Assert.notNull(field, "Field must not be null");
062: this .field = field;
063: this .required = required;
064: }
065:
066: /**
067: * Return the wrapped MethodParameter, if any.
068: * <p>Note: Either MethodParameter or Field is available.
069: * @return the MethodParameter, or <code>null</code> if none
070: */
071: public MethodParameter getMethodParameter() {
072: return this .methodParameter;
073: }
074:
075: /**
076: * Return the wrapped Field, if any.
077: * <p>Note: Either MethodParameter or Field is available.
078: * @return the Field, or <code>null</code> if none
079: */
080: public Field getField() {
081: return this .field;
082: }
083:
084: /**
085: * Return whether this dependency is required.
086: */
087: public boolean isRequired() {
088: return this .required;
089: }
090:
091: /**
092: * Determine the declared (non-generic) type of the wrapped parameter/field.
093: * @return the declared type (never <code>null</code>)
094: */
095: public Class getDependencyType() {
096: return (this .field != null ? this .field.getType()
097: : this .methodParameter.getParameterType());
098: }
099:
100: /**
101: * Determine the generic element type of the wrapped Collection parameter/field, if any.
102: * @return the generic type, or <code>null</code> if none
103: */
104: public Class getCollectionType() {
105: if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_15) {
106: return null;
107: }
108: return (this .field != null ? GenericCollectionTypeResolver
109: .getCollectionFieldType(this .field)
110: : GenericCollectionTypeResolver
111: .getCollectionParameterType(this .methodParameter));
112: }
113:
114: /**
115: * Determine the generic key type of the wrapped Map parameter/field, if any.
116: * @return the generic type, or <code>null</code> if none
117: */
118: public Class getMapKeyType() {
119: if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_15) {
120: return null;
121: }
122: return (this .field != null ? GenericCollectionTypeResolver
123: .getMapKeyFieldType(this .field)
124: : GenericCollectionTypeResolver
125: .getMapKeyParameterType(this .methodParameter));
126: }
127:
128: /**
129: * Determine the generic value type of the wrapped Map parameter/field, if any.
130: * @return the generic type, or <code>null</code> if none
131: */
132: public Class getMapValueType() {
133: if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_15) {
134: return null;
135: }
136: return (this .field != null ? GenericCollectionTypeResolver
137: .getMapValueFieldType(this .field)
138: : GenericCollectionTypeResolver
139: .getMapValueParameterType(this .methodParameter));
140: }
141:
142: /**
143: * Obtain the annotations associated with the wrapped parameter/field, if any.
144: * @return the parameter/field annotations, or <code>null</code> if there is
145: * no annotation support (on JDK < 1.5). The return value is an Object array
146: * instead of an Annotation array simply for compatibility with older JDKs;
147: * feel free to cast it to <code>Annotation[]</code> on JDK 1.5 or higher.
148: */
149: public Object[] getAnnotations() {
150: if (this.field != null) {
151: if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_15) {
152: return null;
153: }
154: return this.field.getAnnotations();
155: } else {
156: return this.methodParameter.getParameterAnnotations();
157: }
158: }
159:
160: }
|