use of boofcv.struct.Configuration in project BoofCV by lessthanoptimal.
the class ConfigSurfDescribe method main.
public static void main(String[] args) throws ClassNotFoundException {
Class c = Class.forName("boofcv.abst.feature.describe.ConfigSurfDescribe$Speed");
System.out.println("c = " + c);
Configuration a;
}
use of boofcv.struct.Configuration in project BoofCV by lessthanoptimal.
the class SerializeFieldsYamlBase method serialize.
/**
* Serializes the specified config. If a 'canonical' reference is provided then only what is not identical
* in value to the canonical is serialized.
*
* @param config Object that is to be serialized
* @param canonical Canonical object.
*/
public Map<String, Object> serialize(Object config, @Nullable Object canonical) {
Map<String, Object> state = new HashMap<>();
Class type = config.getClass();
Field[] fields = type.getFields();
// Get a list of active fields, if a list is specified by the configuration
List<String> active = new ArrayList<>();
if (config instanceof Configuration) {
active = ((Configuration) config).serializeActiveFields();
}
for (Field f : fields) {
if (!(active.isEmpty() || active.contains(f.getName())))
continue;
try {
if (f.getType().isEnum() || f.getType().isPrimitive() || f.getType().getName().equals("java.lang.String")) {
try {
// Only add if they are not identical
Object targetValue = f.get(config);
Object canonicalValue = canonical == null ? null : f.get(canonical);
if (canonicalValue == null && targetValue == null)
continue;
if (targetValue == null || !targetValue.equals(canonicalValue))
state.put(f.getName(), f.getType().isEnum() ? ((Enum<?>) targetValue).name() : targetValue);
} catch (RuntimeException e) {
errorHandler.handle(ErrorType.MISC, "class=" + type.getSimpleName() + " field=" + f.getName(), e);
}
continue;
}
// FastArray are a special case. Serialize each element in the list
if (FastAccess.class.isAssignableFrom(f.getType())) {
FastAccess<?> list = (FastAccess<?>) f.get(config);
// See if the lists are identical. If they are then skip them
escape: if (canonical != null) {
FastAccess<?> listCanon = (FastAccess<?>) f.get(canonical);
if (list.size() != listCanon.size())
break escape;
if (list.isEmpty())
continue;
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).equals(listCanon.get(i)))
break escape;
}
continue;
}
// Encode the list. Basic types are just copied. Everything else is serialized.
Class<?> itemType = list.type;
boolean basic = itemType.isEnum() || itemType.isPrimitive() || itemType.getName().equals("java.lang.String");
// Create the canonical if possible
Object canonicalOfData = null;
if (canonical != null && !basic) {
try {
canonicalOfData = list.type.getConstructor().newInstance();
} catch (Exception e) {
errorHandler.handle(ErrorType.MISC, "Failed to create instance of '" + list.type.getSimpleName() + "'", null);
}
}
List<Object> serializedList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
Object value = list.get(i);
if (basic) {
serializedList.add(itemType.isEnum() ? ((Enum<?>) value).name() : value);
} else {
serializedList.add(serialize(value, canonicalOfData));
}
}
state.put(f.getName(), serializedList);
continue;
}
if (List.class.isAssignableFrom(f.getType())) {
errorHandler.handle(ErrorType.UNSUPPORTED, "Can't serialize lists. Use FastArray instead " + "since it specifies the item type. name=" + f.getName(), null);
continue;
}
// handle primitive arrays
if (f.getType().isArray()) {
state.put(f.getName(), f.get(config));
continue;
}
try {
// All Configuration must have setTo()
// We don't check to see if implements Configuration to allow objects outside of BoofCV
// to work.
type.getMethod("setTo", type);
} catch (NoSuchMethodException e) {
// This is intentionally annoying as a custom class specific serialization solution
// needs to be created. For now, it will complain and skip
errorHandler.handle(ErrorType.UNSUPPORTED, "Referenced object which is not enum, primitive, " + "or a valid class. class='" + type.getSimpleName() + "' field_name='" + f.getName() + "'", null);
continue;
}
Map<String, Object> result = canonical != null ? serialize(f.get(config), f.get(canonical)) : serialize(f.get(config), null);
// If everything is identical then the returned object will be empty
if (!result.isEmpty())
state.put(f.getName(), result);
} catch (IllegalAccessException e) {
errorHandler.handle(ErrorType.REFLECTION, String.format("IllegalAccess. class='%s' field='%s'", type.getSimpleName(), f.getName()), new RuntimeException(e));
}
}
return state;
}
Aggregations