Skip to content

Commit

Permalink
refactor: 优化实体类id字段类型获取
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Aug 1, 2023
1 parent df35260 commit 04963ff
Showing 1 changed file with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.context.index.CandidateComponentsIndex;
import org.springframework.context.index.CandidateComponentsIndexLoader;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.io.Resource;
Expand All @@ -27,6 +28,7 @@
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.util.ReflectionUtils;

import javax.persistence.Table;
import java.io.IOException;
Expand Down Expand Up @@ -79,6 +81,34 @@ protected Set<String> scanEntities(String[] packageStr) {
.collect(Collectors.toSet());
}

private Class<?> findIdType(Class<?> entityType) {
Class<?> idType;
try {
if (GenericEntity.class.isAssignableFrom(entityType)) {
return GenericTypeResolver.resolveTypeArgument(entityType, GenericEntity.class);
}

Class<?>[] ref = new Class[1];
ReflectionUtils.doWithFields(entityType, field -> {
if (field.isAnnotationPresent(javax.persistence.Id.class)) {
ref[0] = field.getType();
}
});
idType = ref[0];

if (idType == null) {
Method getId = org.springframework.util.ClassUtils.getMethod(entityType, "getId");
idType = getId.getReturnType();
}
} catch (Throwable e) {
log.warn("unknown id type of entity:{}", entityType);
idType = String.class;
}

return idType;

}

@Override
@SneakyThrows
@SuppressWarnings("all")
Expand Down Expand Up @@ -106,18 +136,7 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B

Reactive reactive = AnnotationUtils.findAnnotation(entityType, Reactive.class);

Class idType = null;
try {
if (GenericEntity.class.isAssignableFrom(entityType)) {
idType = ClassUtils.getGenericType(entityType);
}
if (idType == null) {
Method getId = org.springframework.util.ClassUtils.getMethod(entityType, "getId");
idType = getId.getReturnType();
}
} catch (Exception e) {
idType = String.class;
}
Class idType = findIdType(entityType);

EntityInfo entityInfo = new EntityInfo(entityType,
entityType,
Expand Down

0 comments on commit 04963ff

Please sign in to comment.