Java Code Examples for com.badlogic.gdx.utils.reflect.Field#getName()

The following examples show how to use com.badlogic.gdx.utils.reflect.Field#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BehaviorTreeViewer.java    From gdx-ai with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: BehaviorTreeParser.java    From gdx-ai with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: Scene2dUtils.java    From gdx-vfx with Apache License 2.0 5 votes vote down vote up
/** Injects actors from group into target's fields annotated with {@link InjectActor} using reflection. */
public static void injectActorFields(Object target, Group group) {
    Class<?> handledClass = target.getClass();
    while (handledClass != null && !handledClass.equals(Object.class)) {
        for (final Field field : ClassReflection.getDeclaredFields(handledClass)) {
            if (field != null && field.isAnnotationPresent(InjectActor.class)) {
                try {
                    InjectActor annotation = field.getDeclaredAnnotation(InjectActor.class).getAnnotation(InjectActor.class);
                    String actorName = annotation.value();
                    if (actorName.length() == 0) {
                        actorName = field.getName();
                    }
                    Actor actor = group.findActor(actorName);
                    if (actor == null && actorName.equals(group.getName())) {
                        actor = group;
                    }
                    if (actor == null) {
                        Gdx.app.error(TAG_INJECT_FIELDS, "Can't find actor with name: " + actorName + " in group: " + group + " to inject into: " + target);
                    } else {
                        field.setAccessible(true);
                        field.set(target, actor);
                    }
                } catch (final ReflectionException exception) {
                    Gdx.app.error(TAG_INJECT_FIELDS, "Unable to set value into field: " + field + " of object: " + target, exception);
                }
            }
        }
        handledClass = handledClass.getSuperclass();
    }
}
 
Example 4
Source File: Scene2dUtils.java    From gdx-texture-packer-gui with Apache License 2.0 5 votes vote down vote up
/** Injects actors from group into target's fields annotated with {@link InjectActor} using reflection. */
public static void injectActorFields(Object target, Group group) {
    Class<?> handledClass = target.getClass();
    while (handledClass != null && !handledClass.equals(Object.class)) {
        for (final Field field : ClassReflection.getDeclaredFields(handledClass)) {
            if (field != null && field.isAnnotationPresent(InjectActor.class)) {
                try {
                    InjectActor annotation = field.getDeclaredAnnotation(InjectActor.class).getAnnotation(InjectActor.class);
                    String actorName = annotation.value();
                    if (actorName.length() == 0) {
                        actorName = field.getName();
                    }
                    Actor actor = group.findActor(actorName);
                    if (actor == null && actorName.equals(group.getName())) {
                        actor = group;
                    }
                    if (actor == null) {
                        Gdx.app.error(TAG_INJECT_FIELDS, "Can't find actor with name: " + actorName + " in group: " + group + " to inject into: " + target);
                    } else {
                        field.setAccessible(true);
                        field.set(target, actor);
                    }
                } catch (final ReflectionException exception) {
                    Gdx.app.error(TAG_INJECT_FIELDS, "Unable to set value into field: " + field + " of object: " + target, exception);
                }
            }
        }
        handledClass = handledClass.getSuperclass();
    }
}
 
Example 5
Source File: GdxReflection.java    From Cardshifter with Apache License 2.0 5 votes vote down vote up
@Override
public ReflField[] getFields(Class<?> aClass) {
    Field[] fields = ClassReflection.getDeclaredFields(aClass);
    List<ReflField> result = new ArrayList<ReflField>(fields.length);
    for (Field field : fields) {
        String name = field.getName();
        if (fieldNameSkip.contains(name)) {
            continue;
        }
        result.add(new GdxField(field));
    }
    return result.toArray(new ReflField[result.size()]);
}
 
Example 6
Source File: StyleData.java    From skin-composer with MIT License 4 votes vote down vote up
private void newStyleProperties(Class clazz) {
    for (Field field : ClassReflection.getFields(clazz)) {
        StyleProperty styleProperty = new StyleProperty(field.getType(), field.getName(), true);
        properties.put(field.getName(), styleProperty);
    }
}