com.fastchar.core.FastEntities Maven / Gradle / Ivy
package com.fastchar.core;
import com.fastchar.annotation.AFastEntity;
import com.fastchar.asm.FastMethodRead;
import com.fastchar.database.info.FastTableInfo;
import com.fastchar.exception.FastEntityException;
import com.fastchar.utils.FastClassUtils;
import com.fastchar.utils.FastStringUtils;
import java.util.ArrayList;
import java.util.List;
public final class FastEntities {
private FastMethodRead methodConverter = new FastMethodRead();
private List entityInfos = new ArrayList<>();
public FastEntities addEntity(Class targetClass) throws Exception {
if (!FastClassUtils.checkNewInstance(targetClass)) {
return this;
}
if (targetClass.isAnnotationPresent(AFastEntity.class)) {
AFastEntity aFastEntity = targetClass.getAnnotation(AFastEntity.class);
if (!aFastEntity.value()) {
return this;
}
}
EntityInfo entityInfo = new EntityInfo();
List lineNumber = methodConverter.getMethodLineNumber(targetClass, "getTableName");
FastEntity fastEntity = targetClass.newInstance();
String tableName = fastEntity.getTableName();
if (FastStringUtils.isEmpty(tableName)) {
StackTraceElement stackTraceElement = new StackTraceElement(targetClass.getName(),
"getTableName", targetClass.getSimpleName() + ".java",
lineNumber.get(0).getLastLine());
throw new FastEntityException(FastChar.getLocal().getInfo("Entity_Error1") +
"\n\tat " + stackTraceElement);
}
entityInfo.setMethodLine(lineNumber.get(0))
.setTableName(tableName)
.setDatabaseName(fastEntity.getDatabase())
.setTargetClass(targetClass);
entityInfos.add(entityInfo);
return this;
}
private void checkTableExists() throws Exception {
for (EntityInfo entityInfo : entityInfos) {
FastTableInfo tableInfo = FastChar.getDatabases().get(entityInfo.getDatabaseName()).getTableInfo(entityInfo.getTableName());
if (tableInfo == null) {
Class aClass = entityInfo.getTargetClass();
StackTraceElement stackTraceElement = new StackTraceElement(aClass.getName(),
"getTableName", aClass.getSimpleName() + ".java",
entityInfo.getMethodLine().getLastLine());
throw new FastEntityException(FastChar.getLocal().getInfo("Entity_Error2", "'" + entityInfo.getTableName() + "'") +
"\n\tat " + stackTraceElement);
}
}
}
public EntityInfo getEntityInfo(Class targetClass) {
for (EntityInfo entityInfo : entityInfos) {
if (entityInfo.getTargetClass() == targetClass) {
return entityInfo;
}
}
return null;
}
public List getEntityInfo(String tableName) {
List entityInfoList = new ArrayList<>();
for (EntityInfo entityInfo : entityInfos) {
if (entityInfo.getTableName().equalsIgnoreCase(tableName)) {
entityInfoList.add(entityInfo);
}
}
return entityInfoList;
}
public void onDatabaseInfoChange() throws Exception {
checkTableExists();
}
public static class EntityInfo {
private String databaseName;
private String tableName;
private FastMethodRead.MethodLine methodLine;
private Class targetClass;
public String getTableName() {
return tableName;
}
public EntityInfo setTableName(String tableName) {
this.tableName = tableName;
return this;
}
public FastMethodRead.MethodLine getMethodLine() {
return methodLine;
}
public EntityInfo setMethodLine(FastMethodRead.MethodLine methodLine) {
this.methodLine = methodLine;
return this;
}
public Class getTargetClass() {
return targetClass;
}
public EntityInfo setTargetClass(Class targetClass) {
this.targetClass = targetClass;
return this;
}
public String getDatabaseName() {
return databaseName;
}
public EntityInfo setDatabaseName(String databaseName) {
this.databaseName = databaseName;
return this;
}
}
}