Java Code Examples for com.badlogic.gdx.utils.reflect.Field

The following examples show how to use com.badlogic.gdx.utils.reflect.Field. These examples are extracted from open source projects. 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 Project: riiablo   Source File: Commands.java    License: Apache License 2.0 6 votes vote down vote up
private static Collection<Throwable> addTo(CommandManager commandManager, Class<?> clazz, Collection<Throwable> throwables) {
  for (Field field : ClassReflection.getFields(clazz)) {
    if (Command.class.isAssignableFrom(field.getType())) {
      try {
        commandManager.add((Command) field.get(null));
      } catch (Throwable t) {
        throwables.add(t);
      }
    }
  }

  for (Class<?> subclass : clazz.getClasses()) {
    addTo(commandManager, subclass, throwables);
  }

  return throwables;
}
 
Example 2
Source Project: riiablo   Source File: Keys.java    License: Apache License 2.0 6 votes vote down vote up
private static Collection<Throwable> addTo(KeyMapper keyMapper, Class<?> clazz, Collection<Throwable> throwables) {
  for (Field field : ClassReflection.getFields(clazz)) {
    if (MappedKey.class.isAssignableFrom(field.getType())) {
      try {
        keyMapper.add((MappedKey) field.get(null));
      } catch (Throwable t) {
        throwables.add(t);
      }
    }
  }

  for (Class<?> subclass : clazz.getClasses()) {
    addTo(keyMapper, subclass, throwables);
  }

  return throwables;
}
 
Example 3
Source Project: riiablo   Source File: Cvars.java    License: Apache License 2.0 6 votes vote down vote up
private static Collection<Throwable> addTo(CvarManager cvarManager, Class<?> clazz, Collection<Throwable> throwables) {
  for (Field field : ClassReflection.getFields(clazz)) {
    if (Cvar.class.isAssignableFrom(field.getType())) {
      try {
        cvarManager.add((Cvar) field.get(null));
      } catch (Throwable t) {
        throwables.add(t);
      }
    }
  }

  for (Class<?> subclass : clazz.getClasses()) {
    addTo(cvarManager, subclass, throwables);
  }

  return throwables;
}
 
Example 4
Source Project: gdx-ai   Source File: BehaviorTreeViewer.java    License: 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 5
Source Project: gdx-ai   Source File: BehaviorTreeParser.java    License: 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 6
Source Project: gdx-vfx   Source File: Scene2dUtils.java    License: 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 7
Source Project: gdx-fireapp   Source File: StringGeneratorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void dataToString() {
    // Given
    PowerMockito.mockStatic(ClassReflection.class);
    Mockito.when(ClassReflection.isPrimitive(Mockito.any(Class.class))).thenReturn(false);
    Mockito.when(ClassReflection.getDeclaredFields(Mockito.any(Class.class))).thenReturn(new Field[]{});
    Object arg = new TestClass();

    // When
    Object data = StringGenerator.dataToString(arg);

    // Then
    Assert.assertEquals("{}", data);
}
 
Example 8
Source Project: gdx-texture-packer-gui   Source File: Scene2dUtils.java    License: 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 9
Source Project: gdx-facebook   Source File: GDXFacebookLoaderUnitTests.java    License: Apache License 2.0 5 votes vote down vote up
private void androidPremocking() {

        Application mockApplication = mock(Application.class);
        when(mockApplication.getType()).thenReturn(Application.ApplicationType.Android);
        Gdx.app = mockApplication;

        try {
            mockConstructor = PowerMockito.mock(Constructor.class);
            mockFacebook = mock(AndroidGDXFacebook.class);
            mockField = mock(Field.class);
            mockObject = mock(Object.class);
            mockMethod = mock(Method.class);

            when(ClassReflection.forName(GDXFacebookVars.CLASSNAME_ANDROID)).thenReturn(AndroidGDXFacebook.class);
            when(ClassReflection.getConstructor(AndroidGDXFacebook.class, GDXFacebookConfig.class)).thenReturn(mockConstructor);
            when(mockConstructor.newInstance(anyObject())).thenReturn(mockFacebook);


            when(ClassReflection.forName("com.badlogic.gdx.Gdx")).thenReturn(Gdx.class);
            when(ClassReflection.getField(Gdx.class, "app")).thenReturn(mockField);
            when(mockField.get(null)).thenReturn(mockObject);


            when(ClassReflection.forName("com.badlogic.gdx.backends.android.AndroidEventListener")).thenReturn(AndroidEventListener.class);
            when(ClassReflection.forName("android.app.Activity")).thenReturn(Activity.class);

        } catch (ReflectionException e) {
            e.printStackTrace();
        }
    }
 
Example 10
Source Project: gdx-ai   Source File: BehaviorTreeViewer.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source Project: gdx-ai   Source File: BehaviorTreeParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected <E> boolean attribute (DefaultBehaviorTreeReader<E> reader, String name, Object value) {
	StackedTask<E> stackedTask = reader.getCurrentTask();
	AttrInfo ai = stackedTask.metadata.attributes.get(name);
	if (ai == null) return false;
	boolean isNew = reader.encounteredAttributes.add(name);
	if (!isNew) throw reader.stackedTaskException(stackedTask, "attribute '" + name + "' specified more than once");
	Field attributeField = reader.getField(stackedTask.task.getClass(), ai.fieldName);
	reader.setField(attributeField, stackedTask.task, value);
	return true;
}
 
Example 12
Source Project: gdx-ai   Source File: BehaviorTreeParser.java    License: Apache License 2.0 5 votes vote down vote up
private Field getField (Class<?> clazz, String name) {
	try {
		return ClassReflection.getField(clazz, name);
	} catch (ReflectionException e) {
		throw new GdxRuntimeException(e);
	}
}
 
Example 13
Source Project: gdx-ai   Source File: BehaviorTreeParser.java    License: Apache License 2.0 5 votes vote down vote up
private void setField (Field field, Task<E> task, Object value) {
	field.setAccessible(true);
	Object valueObject = castValue(field, value);
	if (valueObject == null) throwAttributeTypeException(getCurrentTask().name, field.getName(), field.getType().getSimpleName());
	try {
		field.set(task, valueObject);
	} catch (ReflectionException e) {
		throw new GdxRuntimeException(e);
	}
}
 
Example 14
Source Project: gdx-skineditor   Source File: ColorPickerDialog.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Check if the color is already in use somewhere else in the skin
 */
public boolean isColorInUse(Color color) {

	try {
		// Check if it is already in use somewhere!
		for (String widget : SkinEditorGame.widgets) {
			String widgetStyle = "com.badlogic.gdx.scenes.scene2d.ui." + widget + "$" + widget + "Style";
			Class<?> style = Class.forName(widgetStyle);
			ObjectMap<String, ?> styles = game.skinProject.getAll(style);
			Iterator<String> it = styles.keys().iterator();
			while (it.hasNext()) {
				Object item = styles.get((String) it.next());
				Field[] fields = ClassReflection.getFields(item.getClass());
				for (Field field : fields) {

					if (field.getType() == Color.class) {

						Color c = (Color) field.get(item);
						if (color.equals(c)) {
							return true;
						}

					}

				}

			}

		}
	} catch (Exception e) {
		e.printStackTrace();

	}

	return false;
}
 
Example 15
Source Project: gdx-skineditor   Source File: FontPickerDialog.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Is font is already in use somewhere else?
 */
public boolean isFontInUse(BitmapFont font) {

	try {
		// Check if it is already in use somewhere!
		for (String widget : SkinEditorGame.widgets) {
			String widgetStyle = "com.badlogic.gdx.scenes.scene2d.ui." + widget + "$" + widget + "Style";
			Class<?> style = Class.forName(widgetStyle);
			ObjectMap<String, ?> styles = game.skinProject.getAll(style);
			Iterator<String> it = styles.keys().iterator();
			while (it.hasNext()) {
				Object item = styles.get((String) it.next());
				Field[] fields = ClassReflection.getFields(item.getClass());
				for (Field field : fields) {

					if (field.getType() == BitmapFont.class) {

						BitmapFont f = (BitmapFont) field.get(item);
						if (font.equals(f)) {
							return true;
						}

					}

				}

			}

		}
	} catch (Exception e) {
		e.printStackTrace();

	}

	return false;
}
 
Example 16
Source Project: Cardshifter   Source File: GdxReflection.java    License: 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 17
Source Project: skin-composer   Source File: StyleData.java    License: 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);
    }
}
 
Example 18
Source Project: gdx-skineditor   Source File: OptionsPane.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Show color picker dialog
 */
public void showColorPickerDialog(final Field field) {

	ColorPickerDialog dlg = new ColorPickerDialog(game, field);
	dlg.show(getStage());
}
 
Example 19
Source Project: gdx-skineditor   Source File: OptionsPane.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Show font picker dialog
 */
public void showFontPickerDialog(final Field field) {

	FontPickerDialog dlg = new FontPickerDialog(game, field);
	dlg.show(getStage());
}
 
Example 20
Source Project: gdx-skineditor   Source File: OptionsPane.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Show drawable picker dialog
 * @param field
 */
public void showDrawableDialog(final Field field) {

	DrawablePickerDialog dlg = new DrawablePickerDialog(game, field);
	dlg.show(getStage());
}
 
Example 21
Source Project: Cardshifter   Source File: GdxField.java    License: Apache License 2.0 4 votes vote down vote up
public GdxField(Field field) {
    this.field = field;
}
 
Example 22
Source Project: gdx-skineditor   Source File: FontPickerDialog.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 
 */
public FontPickerDialog(final SkinEditorGame game, Field field) {

	super("Bitmap Font Picker", game.skin);

	this.game = game;
	this.field = field;

	tableFonts = new Table(game.skin);
	tableFonts.left().top().pad(5);
	tableFonts.defaults().pad(5);

	fonts = game.skinProject.getAll(BitmapFont.class);

	updateTable();

	TextButton buttonNewFont = new TextButton("New Font", game.skin);
	buttonNewFont.addListener(new ChangeListener() {

		@Override
		public void changed(ChangeEvent event, Actor actor) {

			showNewFontDialog();

		}

	});


	ScrollPane scrollPane = new ScrollPane(tableFonts, game.skin);
	scrollPane.setFlickScroll(false);
	scrollPane.setFadeScrollBars(false);
	scrollPane.setScrollbarsOnTop(true);

	getContentTable().add(scrollPane).width(720).height(420).pad(20);
	getButtonTable().add(buttonNewFont);
	getButtonTable().padBottom(15);
	button("Cancel", false);
	key(com.badlogic.gdx.Input.Keys.ESCAPE, false);

}