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

The following examples show how to use com.badlogic.gdx.utils.reflect.Field. 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: 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 #2
Source File: Commands.java    From riiablo with 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 #3
Source File: Keys.java    From riiablo with 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 #4
Source File: Cvars.java    From riiablo with 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 #5
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 #6
Source File: StringGeneratorTest.java    From gdx-fireapp with 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 #7
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 #8
Source File: FontPickerDialog.java    From gdx-skineditor with 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 #9
Source File: ColorPickerDialog.java    From gdx-skineditor with 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 #10
Source File: BehaviorTreeParser.java    From gdx-ai with 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 #11
Source File: BehaviorTreeParser.java    From gdx-ai with 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 #12
Source File: BehaviorTreeParser.java    From gdx-ai with 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 #13
Source File: BehaviorTreeViewer.java    From gdx-ai with 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 #14
Source File: GDXFacebookLoaderUnitTests.java    From gdx-facebook with 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 #15
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 #16
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 #17
Source File: OptionsPane.java    From gdx-skineditor with 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 #18
Source File: OptionsPane.java    From gdx-skineditor with 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 #19
Source File: OptionsPane.java    From gdx-skineditor with 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 #20
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);
    }
}
 
Example #21
Source File: GdxField.java    From Cardshifter with Apache License 2.0 4 votes vote down vote up
public GdxField(Field field) {
    this.field = field;
}
 
Example #22
Source File: FontPickerDialog.java    From gdx-skineditor with 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);

}