1
1
package cn .aethli .mineauth .common .utils ;
2
2
3
- import cn .aethli .mineauth .Mineauth ;
4
- import cn .aethli .mineauth .annotation .MetadataScan ;
5
3
import cn .aethli .mineauth .common .model .EntityMapper ;
4
+ import cn .aethli .mineauth .entity .AuthPlayer ;
6
5
import cn .aethli .mineauth .entity .BaseEntity ;
7
- import com . google . common . collect . ImmutableSet ;
8
- import com . google . common . reflect . ClassPath ;
6
+ import org . apache . logging . log4j . LogManager ;
7
+ import org . apache . logging . log4j . Logger ;
9
8
10
9
import java .io .File ;
11
10
import java .io .IOException ;
15
14
import java .util .*;
16
15
import java .util .concurrent .ConcurrentHashMap ;
17
16
import java .util .concurrent .atomic .AtomicBoolean ;
18
- import java .util .stream .Collectors ;
19
17
20
18
public class MetadataUtils {
21
19
private static final Map <String , EntityMapper > ENTITY_MAPPER_MAP = new ConcurrentHashMap <>();
22
20
private static final AtomicBoolean initFlag = new AtomicBoolean (false );
21
+ private static final Logger LOGGER = LogManager .getLogger ();
23
22
24
23
/**
25
24
* init metadata, to cache entity-field map exclude abstract class and non-extends BaseEntity
@@ -37,7 +36,7 @@ public static void initMetadata(String packageName) throws IOException, ClassNot
37
36
classes .forEach (
38
37
aClass -> {
39
38
if (!Modifier .isAbstract (aClass .getModifiers ())
40
- /* && BaseEntity.class.isAssignableFrom(aClass)*/ ) {
39
+ && BaseEntity .class .isAssignableFrom (aClass )) {
41
40
Set <Field > fields = new HashSet <>(Arrays .asList (aClass .getDeclaredFields ()));
42
41
fields .addAll (Arrays .asList (baseEntityFields ));
43
42
EntityMapper entityMapper = ENTITY_MAPPER_MAP .get (aClass .getTypeName ());
@@ -56,12 +55,22 @@ public static void initMetadata() throws IOException, ClassNotFoundException {
56
55
if (initFlag .get ()) {
57
56
return ;
58
57
}
59
- Class <Mineauth > mineauthClass = Mineauth .class ;
60
- if (mineauthClass .isAnnotationPresent (MetadataScan .class )) {
61
- MetadataScan metadataScan = mineauthClass .getAnnotation (MetadataScan .class );
62
- for (String packageName : metadataScan .packageName ()) {
63
- MetadataUtils .initMetadata (packageName );
64
- }
58
+ // Class<Mineauth> mineauthClass = Mineauth.class;
59
+ // if (mineauthClass.isAnnotationPresent(MetadataScan.class)) {
60
+ // MetadataScan metadataScan = mineauthClass.getAnnotation(MetadataScan.class);
61
+ // for (String packageName : metadataScan.packageName()) {
62
+ // MetadataUtils.initMetadata(packageName);
63
+ // }
64
+ // }
65
+ Field [] baseEntityFields = BaseEntity .class .getDeclaredFields ();
66
+ HashSet <Field > fields = new HashSet <>(Arrays .asList (AuthPlayer .class .getDeclaredFields ()));
67
+ fields .addAll (Arrays .asList (baseEntityFields ));
68
+ EntityMapper entityMapper = ENTITY_MAPPER_MAP .get (AuthPlayer .class .getTypeName ());
69
+ if (entityMapper == null ) {
70
+ entityMapper = new EntityMapper ();
71
+ entityMapper .setClassName (AuthPlayer .class .getTypeName ());
72
+ entityMapper .setFields (fields );
73
+ ENTITY_MAPPER_MAP .put (AuthPlayer .class .getTypeName (), entityMapper );
65
74
}
66
75
initFlag .set (true );
67
76
}
@@ -87,26 +96,28 @@ public static EntityMapper getEntityMapperByTypeName(String typeName) {
87
96
*/
88
97
private static <T extends BaseEntity > List <Class <T >> getClasses (String packageName )
89
98
throws ClassNotFoundException , IOException {
99
+ // both not works with java -jar
100
+ // ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
101
+ // ClassPath classpath = ClassPath.from(classLoader); // scans the class path used by
102
+ // classloader
103
+ // ImmutableSet<ClassPath.ClassInfo> topLevelClasses =
104
+ // classpath.getTopLevelClasses(packageName);
105
+ // List<? extends Class<?>> collect =
106
+ // topLevelClasses.stream().map(ClassPath.ClassInfo::load).collect(Collectors.toList());
107
+ // return (List<Class<T>>) collect;
90
108
ClassLoader classLoader = Thread .currentThread ().getContextClassLoader ();
91
- ClassPath classpath = ClassPath .from (classLoader ); // scans the class path used by classloader
92
- ImmutableSet <ClassPath .ClassInfo > topLevelClasses = classpath .getTopLevelClasses (packageName );
93
- List <? extends Class <?>> collect =
94
- topLevelClasses .stream ().map (ClassPath .ClassInfo ::load ).collect (Collectors .toList ());
95
- return (List <Class <T >>) collect ;
96
- //not works on produce
97
- // ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
98
- // String path = packageName.replace('.', '/');
99
- // Enumeration<URL> resources = classLoader.getResources(path);
100
- // List<File> dirs = new ArrayList<>();
101
- // while (resources.hasMoreElements()) {
102
- // URL resource = resources.nextElement();
103
- // dirs.add(new File(resource.getFile()));
104
- // }
105
- // ArrayList<Class<T>> classes = new ArrayList<>();
106
- // for (File directory : dirs) {
107
- // classes.addAll(findClasses(directory, packageName));
108
- // }
109
- // return classes;
109
+ String path = packageName .replace ('.' , '/' );
110
+ Enumeration <URL > resources = classLoader .getResources (path );
111
+ List <File > dirs = new ArrayList <>();
112
+ while (resources .hasMoreElements ()) {
113
+ URL resource = resources .nextElement ();
114
+ dirs .add (new File (resource .getFile ()));
115
+ }
116
+ ArrayList <Class <T >> classes = new ArrayList <>();
117
+ for (File directory : dirs ) {
118
+ classes .addAll (findClasses (directory , packageName ));
119
+ }
120
+ return classes ;
110
121
}
111
122
112
123
/**
0 commit comments