private Metadata findMetadata (Class<?> clazz) { Metadata metadata = metadataCache.get(clazz); if (metadata == null) { Annotation tca = ClassReflection.getAnnotation(clazz, TaskConstraint.class); if (tca != null) { TaskConstraint taskConstraint = tca.getAnnotation(TaskConstraint.class); ObjectMap<String, AttrInfo> taskAttributes = new ObjectMap<String, AttrInfo>(); Field[] fields = ClassReflection.getFields(clazz); for (Field f : fields) { Annotation a = f.getDeclaredAnnotation(TaskAttribute.class); if (a != null) { AttrInfo ai = new AttrInfo(f.getName(), a.getAnnotation(TaskAttribute.class)); taskAttributes.put(ai.name, ai); } } metadata = new Metadata(taskConstraint.minChildren(), taskConstraint.maxChildren(), taskAttributes); metadataCache.put(clazz, metadata); } } return metadata; }
private static void appendFieldString (StringBuilder sb, Task<?> task, TaskAttribute ann, Field field) { // prefer name from annotation if there is one String name = ann.name(); if (name == null || name.length() == 0) name = field.getName(); Object value; try { field.setAccessible(true); value = field.get(task); } catch (ReflectionException e) { Gdx.app.error("", "Failed to get field", e); return; } sb.append(name).append(":"); Class<?> fieldType = field.getType(); if (fieldType.isEnum() || fieldType == String.class) { sb.append('\"').append(value).append('\"'); } else if (Distribution.class.isAssignableFrom(fieldType)) { sb.append('\"').append(DAs.toString((Distribution)value)).append('\"'); } else sb.append(value); }
private static StringBuilder appendTaskAttributes (StringBuilder attrs, Task<?> task) { Class<?> aClass = task.getClass(); Field[] fields = ClassReflection.getFields(aClass); for (Field f : fields) { Annotation a = f.getDeclaredAnnotation(TaskAttribute.class); if (a == null) continue; TaskAttribute annotation = a.getAnnotation(TaskAttribute.class); attrs.append('\n'); appendFieldString(attrs, task, annotation, f); } return attrs; }
AttrInfo (String fieldName, TaskAttribute annotation) { this(annotation.name(), fieldName, annotation.required()); }