001/* 002 * Copyright (c) 2022-2023, Mybatis-Flex (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 com.mybatisflex.core.table; 017 018import com.mybatisflex.core.query.QueryColumn; 019import com.mybatisflex.core.util.ClassUtil; 020import com.mybatisflex.core.util.StringUtil; 021import org.apache.ibatis.io.ResolverUtil; 022 023import java.io.Serializable; 024import java.lang.reflect.Field; 025import java.lang.reflect.Modifier; 026import java.util.HashMap; 027import java.util.List; 028import java.util.Map; 029import java.util.Set; 030 031/** 032 * @author michael 033 */ 034public class TableDefs implements Serializable { 035 036 private static final Map<String, TableDef> TABLE_DEF_MAP = new HashMap<>(); 037 private static final Map<String, Map<String, QueryColumn>> QUERY_COLUMN_MAP = new HashMap<>(); 038 039 public static void init(String packageName) { 040 ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>(); 041 resolverUtil.find(new ResolverUtil.IsA(TableDef.class), packageName); 042 Set<Class<? extends Class<?>>> typeSet = resolverUtil.getClasses(); 043 for (Class<?> type : typeSet) { 044 if (!type.isAnonymousClass() && !type.isInterface() && !type.isMemberClass()) { 045 try { 046 registerTableDef(type); 047 } catch (IllegalAccessException e) { 048 e.printStackTrace(); 049 } 050 } 051 } 052 } 053 054 public static QueryColumn getQueryColumn(Class<?> entityClass, String key, String column) { 055 if (TABLE_DEF_MAP.isEmpty()) { 056 init(entityClass.getPackage().getName()); 057 } 058 Map<String, QueryColumn> queryColumnMap = QUERY_COLUMN_MAP.get(key); 059 return queryColumnMap != null ? queryColumnMap.get(column) : null; 060 } 061 062 063 public static void registerTableDef(Class<?> clazz) throws IllegalAccessException { 064 TableDef tableDef = (TableDef) ClassUtil.getFirstField(clazz, field -> { 065 int mod = Modifier.fieldModifiers(); 066 return Modifier.isPublic(mod) && Modifier.isStatic(mod); 067 }).get(null); 068 069 String key = StringUtil.buildSchemaWithTable(tableDef.getSchema(), tableDef.getTableName()); 070 071 TABLE_DEF_MAP.put(key, tableDef); 072 073 List<Field> allFields = ClassUtil.getAllFields(tableDef.getClass(), field -> field.getType() == QueryColumn.class); 074 075 Map<String, QueryColumn> columnMap = new HashMap<>(allFields.size()); 076 for (Field field : allFields) { 077 QueryColumn queryColumn = (QueryColumn) field.get(tableDef); 078 columnMap.put(queryColumn.getName(), queryColumn); 079 } 080 081 QUERY_COLUMN_MAP.put(key, columnMap); 082 } 083 084 085}