001/*
002 * Copyright 2002-2015 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.type;
018
019import java.lang.reflect.Method;
020import java.lang.reflect.Modifier;
021import java.util.Map;
022
023import org.springframework.core.annotation.AnnotatedElementUtils;
024import org.springframework.util.Assert;
025import org.springframework.util.MultiValueMap;
026
027/**
028 * {@link MethodMetadata} implementation that uses standard reflection
029 * to introspect a given {@code Method}.
030 *
031 * @author Juergen Hoeller
032 * @author Mark Pollack
033 * @author Chris Beams
034 * @author Phillip Webb
035 * @since 3.0
036 */
037public class StandardMethodMetadata implements MethodMetadata {
038
039        private final Method introspectedMethod;
040
041        private final boolean nestedAnnotationsAsMap;
042
043
044        /**
045         * Create a new StandardMethodMetadata wrapper for the given Method.
046         * @param introspectedMethod the Method to introspect
047         */
048        public StandardMethodMetadata(Method introspectedMethod) {
049                this(introspectedMethod, false);
050        }
051
052        /**
053         * Create a new StandardMethodMetadata wrapper for the given Method,
054         * providing the option to return any nested annotations or annotation arrays in the
055         * form of {@link org.springframework.core.annotation.AnnotationAttributes} instead
056         * of actual {@link java.lang.annotation.Annotation} instances.
057         * @param introspectedMethod the Method to introspect
058         * @param nestedAnnotationsAsMap return nested annotations and annotation arrays as
059         * {@link org.springframework.core.annotation.AnnotationAttributes} for compatibility
060         * with ASM-based {@link AnnotationMetadata} implementations
061         * @since 3.1.1
062         */
063        public StandardMethodMetadata(Method introspectedMethod, boolean nestedAnnotationsAsMap) {
064                Assert.notNull(introspectedMethod, "Method must not be null");
065                this.introspectedMethod = introspectedMethod;
066                this.nestedAnnotationsAsMap = nestedAnnotationsAsMap;
067        }
068
069
070        /**
071         * Return the underlying Method.
072         */
073        public final Method getIntrospectedMethod() {
074                return this.introspectedMethod;
075        }
076
077        @Override
078        public String getMethodName() {
079                return this.introspectedMethod.getName();
080        }
081
082        @Override
083        public String getDeclaringClassName() {
084                return this.introspectedMethod.getDeclaringClass().getName();
085        }
086
087        @Override
088        public String getReturnTypeName() {
089                return this.introspectedMethod.getReturnType().getName();
090        }
091
092        @Override
093        public boolean isAbstract() {
094                return Modifier.isAbstract(this.introspectedMethod.getModifiers());
095        }
096
097        @Override
098        public boolean isStatic() {
099                return Modifier.isStatic(this.introspectedMethod.getModifiers());
100        }
101
102        @Override
103        public boolean isFinal() {
104                return Modifier.isFinal(this.introspectedMethod.getModifiers());
105        }
106
107        @Override
108        public boolean isOverridable() {
109                return (!isStatic() && !isFinal() && !Modifier.isPrivate(this.introspectedMethod.getModifiers()));
110        }
111
112        @Override
113        public boolean isAnnotated(String annotationName) {
114                return AnnotatedElementUtils.isAnnotated(this.introspectedMethod, annotationName);
115        }
116
117        @Override
118        public Map<String, Object> getAnnotationAttributes(String annotationName) {
119                return getAnnotationAttributes(annotationName, false);
120        }
121
122        @Override
123        public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
124                return AnnotatedElementUtils.getMergedAnnotationAttributes(this.introspectedMethod,
125                                annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
126        }
127
128        @Override
129        public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
130                return getAllAnnotationAttributes(annotationName, false);
131        }
132
133        @Override
134        public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {
135                return AnnotatedElementUtils.getAllAnnotationAttributes(this.introspectedMethod,
136                                annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
137        }
138
139}