001/*
002 * Copyright 2002-2019 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 *      https://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
017package org.springframework.core.annotation;
018
019import java.lang.annotation.Annotation;
020import java.lang.reflect.Constructor;
021import java.lang.reflect.Executable;
022import java.lang.reflect.Method;
023import java.lang.reflect.Parameter;
024
025import org.springframework.core.MethodParameter;
026
027/**
028 * A {@link MethodParameter} variant which synthesizes annotations that
029 * declare attribute aliases via {@link AliasFor @AliasFor}.
030 *
031 * @author Juergen Hoeller
032 * @author Sam Brannen
033 * @since 4.2
034 * @see AnnotationUtils#synthesizeAnnotation
035 * @see AnnotationUtils#synthesizeAnnotationArray
036 */
037public class SynthesizingMethodParameter extends MethodParameter {
038
039        /**
040         * Create a new {@code SynthesizingMethodParameter} for the given method,
041         * with nesting level 1.
042         * @param method the Method to specify a parameter for
043         * @param parameterIndex the index of the parameter: -1 for the method
044         * return type; 0 for the first method parameter; 1 for the second method
045         * parameter, etc.
046         */
047        public SynthesizingMethodParameter(Method method, int parameterIndex) {
048                super(method, parameterIndex);
049        }
050
051        /**
052         * Create a new {@code SynthesizingMethodParameter} for the given method.
053         * @param method the Method to specify a parameter for
054         * @param parameterIndex the index of the parameter: -1 for the method
055         * return type; 0 for the first method parameter; 1 for the second method
056         * parameter, etc.
057         * @param nestingLevel the nesting level of the target type
058         * (typically 1; e.g. in case of a List of Lists, 1 would indicate the
059         * nested List, whereas 2 would indicate the element of the nested List)
060         */
061        public SynthesizingMethodParameter(Method method, int parameterIndex, int nestingLevel) {
062                super(method, parameterIndex, nestingLevel);
063        }
064
065        /**
066         * Create a new {@code SynthesizingMethodParameter} for the given constructor,
067         * with nesting level 1.
068         * @param constructor the Constructor to specify a parameter for
069         * @param parameterIndex the index of the parameter
070         */
071        public SynthesizingMethodParameter(Constructor<?> constructor, int parameterIndex) {
072                super(constructor, parameterIndex);
073        }
074
075        /**
076         * Create a new {@code SynthesizingMethodParameter} for the given constructor.
077         * @param constructor the Constructor to specify a parameter for
078         * @param parameterIndex the index of the parameter
079         * @param nestingLevel the nesting level of the target type
080         * (typically 1; e.g. in case of a List of Lists, 1 would indicate the
081         * nested List, whereas 2 would indicate the element of the nested List)
082         */
083        public SynthesizingMethodParameter(Constructor<?> constructor, int parameterIndex, int nestingLevel) {
084                super(constructor, parameterIndex, nestingLevel);
085        }
086
087        /**
088         * Copy constructor, resulting in an independent {@code SynthesizingMethodParameter}
089         * based on the same metadata and cache state that the original object was in.
090         * @param original the original SynthesizingMethodParameter object to copy from
091         */
092        protected SynthesizingMethodParameter(SynthesizingMethodParameter original) {
093                super(original);
094        }
095
096
097        @Override
098        protected <A extends Annotation> A adaptAnnotation(A annotation) {
099                return AnnotationUtils.synthesizeAnnotation(annotation, getAnnotatedElement());
100        }
101
102        @Override
103        protected Annotation[] adaptAnnotationArray(Annotation[] annotations) {
104                return AnnotationUtils.synthesizeAnnotationArray(annotations, getAnnotatedElement());
105        }
106
107        @Override
108        public SynthesizingMethodParameter clone() {
109                return new SynthesizingMethodParameter(this);
110        }
111
112
113        /**
114         * Create a new SynthesizingMethodParameter for the given method or constructor.
115         * <p>This is a convenience factory method for scenarios where a
116         * Method or Constructor reference is treated in a generic fashion.
117         * @param executable the Method or Constructor to specify a parameter for
118         * @param parameterIndex the index of the parameter
119         * @return the corresponding SynthesizingMethodParameter instance
120         * @since 5.0
121         */
122        public static SynthesizingMethodParameter forExecutable(Executable executable, int parameterIndex) {
123                if (executable instanceof Method) {
124                        return new SynthesizingMethodParameter((Method) executable, parameterIndex);
125                }
126                else if (executable instanceof Constructor) {
127                        return new SynthesizingMethodParameter((Constructor<?>) executable, parameterIndex);
128                }
129                else {
130                        throw new IllegalArgumentException("Not a Method/Constructor: " + executable);
131                }
132        }
133
134        /**
135         * Create a new SynthesizingMethodParameter for the given parameter descriptor.
136         * <p>This is a convenience factory method for scenarios where a
137         * Java 8 {@link Parameter} descriptor is already available.
138         * @param parameter the parameter descriptor
139         * @return the corresponding SynthesizingMethodParameter instance
140         * @since 5.0
141         */
142        public static SynthesizingMethodParameter forParameter(Parameter parameter) {
143                return forExecutable(parameter.getDeclaringExecutable(), findParameterIndex(parameter));
144        }
145
146}