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.type;
018
019import java.lang.reflect.Modifier;
020import java.util.LinkedHashSet;
021
022import org.springframework.lang.Nullable;
023import org.springframework.util.Assert;
024import org.springframework.util.StringUtils;
025
026/**
027 * {@link ClassMetadata} implementation that uses standard reflection
028 * to introspect a given {@code Class}.
029 *
030 * @author Juergen Hoeller
031 * @since 2.5
032 */
033public class StandardClassMetadata implements ClassMetadata {
034
035        private final Class<?> introspectedClass;
036
037
038        /**
039         * Create a new StandardClassMetadata wrapper for the given Class.
040         * @param introspectedClass the Class to introspect
041         * @deprecated since 5.2 in favor of {@link StandardAnnotationMetadata}
042         */
043        @Deprecated
044        public StandardClassMetadata(Class<?> introspectedClass) {
045                Assert.notNull(introspectedClass, "Class must not be null");
046                this.introspectedClass = introspectedClass;
047        }
048
049        /**
050         * Return the underlying Class.
051         */
052        public final Class<?> getIntrospectedClass() {
053                return this.introspectedClass;
054        }
055
056
057        @Override
058        public String getClassName() {
059                return this.introspectedClass.getName();
060        }
061
062        @Override
063        public boolean isInterface() {
064                return this.introspectedClass.isInterface();
065        }
066
067        @Override
068        public boolean isAnnotation() {
069                return this.introspectedClass.isAnnotation();
070        }
071
072        @Override
073        public boolean isAbstract() {
074                return Modifier.isAbstract(this.introspectedClass.getModifiers());
075        }
076
077        @Override
078        public boolean isFinal() {
079                return Modifier.isFinal(this.introspectedClass.getModifiers());
080        }
081
082        @Override
083        public boolean isIndependent() {
084                return (!hasEnclosingClass() ||
085                                (this.introspectedClass.getDeclaringClass() != null &&
086                                                Modifier.isStatic(this.introspectedClass.getModifiers())));
087        }
088
089        @Override
090        @Nullable
091        public String getEnclosingClassName() {
092                Class<?> enclosingClass = this.introspectedClass.getEnclosingClass();
093                return (enclosingClass != null ? enclosingClass.getName() : null);
094        }
095
096        @Override
097        @Nullable
098        public String getSuperClassName() {
099                Class<?> superClass = this.introspectedClass.getSuperclass();
100                return (superClass != null ? superClass.getName() : null);
101        }
102
103        @Override
104        public String[] getInterfaceNames() {
105                Class<?>[] ifcs = this.introspectedClass.getInterfaces();
106                String[] ifcNames = new String[ifcs.length];
107                for (int i = 0; i < ifcs.length; i++) {
108                        ifcNames[i] = ifcs[i].getName();
109                }
110                return ifcNames;
111        }
112
113        @Override
114        public String[] getMemberClassNames() {
115                LinkedHashSet<String> memberClassNames = new LinkedHashSet<>(4);
116                for (Class<?> nestedClass : this.introspectedClass.getDeclaredClasses()) {
117                        memberClassNames.add(nestedClass.getName());
118                }
119                return StringUtils.toStringArray(memberClassNames);
120        }
121
122}