001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
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 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
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 */
016package io.jboot.utils;
017
018import java.io.Serializable;
019
020public class ClassType implements Serializable {
021
022    private Class<?> mainClass; //类
023    private ClassType[] genericTypes;//泛型
024
025    public ClassType() {
026    }
027
028    public ClassType(Class<?> mainClass) {
029        this.mainClass = mainClass;
030    }
031
032    public ClassType(Class<?> mainClass, Class<?>[] genericClasses) {
033        this.mainClass = mainClass;
034        if (genericClasses != null && genericClasses.length > 0) {
035            genericTypes = new ClassType[genericClasses.length];
036            for (int i = 0; i < genericClasses.length; i++) {
037                genericTypes[i] = new ClassType(genericClasses[i]);
038            }
039        }
040    }
041
042    public ClassType(Class<?> mainClass, ClassType[] genericTypes) {
043        this.mainClass = mainClass;
044        this.genericTypes = genericTypes;
045    }
046
047    public Class<?> getMainClass() {
048        return mainClass;
049    }
050
051    public void setMainClass(Class<?> mainClass) {
052        this.mainClass = mainClass;
053    }
054
055    public ClassType[] getGenericTypes() {
056        return genericTypes;
057    }
058
059    public void setGenericTypes(ClassType[] genericTypes) {
060        this.genericTypes = genericTypes;
061    }
062
063    public String getDataType() {
064        return mainClass.getSimpleName();
065    }
066
067    public boolean isGeneric() {
068        return genericTypes != null && genericTypes.length > 0;
069    }
070
071    public boolean  isVoid(){
072        return mainClass == void.class;
073    }
074
075    @Override
076    public String toString() {
077        StringBuilder sb = new StringBuilder(mainClass.getSimpleName());
078        if (isGeneric()) {
079            sb.append("<");
080            for (int i = 0; i < genericTypes.length; i++) {
081                sb.append(genericTypes[i].toString());
082                if (i != genericTypes.length - 1) {
083                    sb.append(",");
084                }
085            }
086            sb.append(">");
087        }
088        return sb.toString();
089    }
090}