60 lines
1.7 KiB
Java
60 lines
1.7 KiB
Java
package fr.doap.jdb.impl;
|
|
|
|
import fr.doap.slog.*;
|
|
|
|
import java.util.*;
|
|
|
|
public class ClassInfos {
|
|
|
|
static public Map<String, Object> getProps(Object aObject) {
|
|
Map<String, Object> lMap = new HashMap<>();
|
|
ClassInfos lCI = get(aObject.getClass());
|
|
if (lCI != null) lCI.mPropsGetterSetter.forEach((aKey, aGS) -> lMap.put(aKey, aGS.get(aObject)));
|
|
return lMap;
|
|
}
|
|
|
|
static public void setProps(Map<String, Object> aMap, Object aObject) {
|
|
ClassInfos lCI = get(aObject.getClass());
|
|
if (lCI != null) lCI.mPropsGetterSetter.forEach((aKey, aGS) -> aGS.set(aObject, aMap.get(aKey)));
|
|
}
|
|
|
|
static public ClassInfos get(Class<?> aClass) {
|
|
if ((aClass != null)&&(aClass != Object.class)) {
|
|
ClassInfos lCI = sInfosMap.get(aClass);
|
|
if (lCI == null) lCI = new ClassInfos(aClass);
|
|
return lCI;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
ClassInfos(Class<?> aClass) {
|
|
try {
|
|
sInfosMap.put(aClass, this);
|
|
ClassAnalyzer lCA = null;
|
|
for (ClassAnalyzer iCA: sClassAnalyzers) {
|
|
if (iCA.isApplicable(aClass)) {
|
|
lCA = iCA;
|
|
break;
|
|
}
|
|
}
|
|
if (lCA != null) {
|
|
if (lCA.processSuperclass(aClass)) {
|
|
Class<?> lSuper = aClass.getSuperclass();
|
|
if ((lSuper != null)&&(lSuper != Object.class)) {
|
|
ClassInfos lCI = get(lSuper);
|
|
mPropsGetterSetter.putAll(lCI.mPropsGetterSetter);
|
|
}
|
|
}
|
|
lCA.analyze(aClass, mPropsGetterSetter);
|
|
}
|
|
} catch (Exception aE) { L.t(aE); }
|
|
}
|
|
|
|
public Map<String, PropGetterSetter> getGS() { return mPropsGetterSetter; }
|
|
|
|
private Map<String, PropGetterSetter> mPropsGetterSetter = new HashMap<>();
|
|
|
|
static Map<Class<?>, ClassInfos> sInfosMap = new HashMap<>();
|
|
static List<ClassAnalyzer> sClassAnalyzers = new ArrayList<>(List.of(new ClassAnalyzer.CollectionCA(), new ClassAnalyzer.FieldCA()));
|
|
}
|