Example usage for com.badlogic.gdx.utils.reflect Field get

List of usage examples for com.badlogic.gdx.utils.reflect Field get

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils.reflect Field get.

Prototype

public Object get(Object obj) throws ReflectionException 

Source Link

Document

Returns the value of the field on the supplied object.

Usage

From source file:es.eucm.utils.apache.commons.lang3.JsonEscapeUtils.java

License:Open Source License

private static void escapeOrUnescape(Object objectToEscape, Class type, boolean escape) {
    if (objectToEscape == null) {
        return;/* w  w w  .  j a  va  2 s. c o  m*/
    }

    if (type == null) {
        type = objectToEscape.getClass();
    }

    // If the objectToEscape is from primitive type, do not search
    if (type.isEnum() || type == Float.class || type == Double.class || type == Boolean.class
            || type == Integer.class || type == Byte.class || type == Character.class || type == Long.class
            || type == Short.class || type == String.class) {
        return;
    }

    // Iterate through fields
    for (Field field : ClassReflection.getDeclaredFields(type)) {
        field.setAccessible(true);

        Object value = null;
        try {
            value = field.get(objectToEscape);
        } catch (ReflectionException e) {
            e.printStackTrace();
        }
        if (value == null) {
            continue;
        }

        // String field
        if (ClassReflection.isAssignableFrom(String.class, field.getType())) {
            try {
                field.set(objectToEscape, escapeOrUnescapeJsonString((String) value, escape));
            } catch (ReflectionException e) {
                e.printStackTrace();
            }
        }
        // Recursive search: array, list, map,
        else if (ClassReflection.isArray(field.getType())) {
            int length = java.lang.reflect.Array.getLength(value);
            for (int i = 0; i < length; i++) {
                Object child = java.lang.reflect.Array.get(value, i);
                if (child == null) {
                    continue;
                }
                if (child.getClass() == String.class) {
                    java.lang.reflect.Array.set(value, i, escapeOrUnescapeJsonString((String) child, escape));
                } else {
                    escapeOrUnescape(child, child.getClass(), escape);
                }
            }
        }

        else if (ClassReflection.isAssignableFrom(Array.class, field.getType())) {
            Array array = (Array) value;
            for (int i = 0; i < array.size; i++) {
                Object child = array.get(i);
                if (child == null) {
                    continue;
                }
                if (child.getClass() == String.class) {
                    array.set(i, escapeOrUnescapeJsonString((String) child, escape));
                } else {
                    escapeOrUnescape(child, child.getClass(), escape);
                }
            }
        }

        else if (ClassReflection.isAssignableFrom(List.class, field.getType())) {
            List list = (List) value;
            for (int i = 0; i < list.size(); i++) {
                Object child = list.get(i);
                if (child == null) {
                    continue;
                }
                if (child.getClass() == String.class) {
                    list.set(i, escapeOrUnescapeJsonString((String) child, escape));
                } else {
                    escapeOrUnescape(child, child.getClass(), escape);
                }
            }
        }

        else if (ClassReflection.isAssignableFrom(Map.class, field.getType())) {
            Map map = (Map) value;
            for (Object key : map.keySet()) {
                Object child = map.get(key);
                if (child == null) {
                    continue;
                }
                if (child.getClass() == String.class) {
                    map.put(key, escapeOrUnescapeJsonString((String) child, escape));
                } else {
                    escapeOrUnescape(child, child.getClass(), escape);
                }
            }
        }

        // Recursive search
        else {
            escapeOrUnescape(value, value.getClass(), escape);
        }
    }

    if (type.getSuperclass() != null) {
        escapeOrUnescape(objectToEscape, type.getSuperclass(), escape);
    }
}

From source file:es.eucm.ead.editor.utils.ProjectUtils.java

License:Open Source License

private static void listRefBinaries(Object object, Class clazz, BinaryReferences binaryPaths) {
    if (clazz == null) {
        clazz = object.getClass();/* www.ja  v  a 2s  .co  m*/
    }

    // If the object is from primitive type, do not search
    if (clazz.isEnum() || clazz == Float.class || clazz == Double.class || clazz == Boolean.class
            || clazz == Integer.class || clazz == Byte.class || clazz == Character.class || clazz == Long.class
            || clazz == Short.class) {
        return;
    }

    // If the object is a String (leaf)
    // Leaf: String
    if (ClassReflection.isAssignableFrom(String.class, clazz)) {
        String strValue = (String) object;
        binaryPaths.checkAndAdd(strValue);
    }
    // Special case: SpineAnimation does not store extension, and therefore
    // it needs to be treated differently
    else if (ClassReflection.isAssignableFrom(SpineAnimation.class, clazz)) {
        SpineAnimation spineAnimation = (SpineAnimation) object;
        String baseUri = spineAnimation.getUri();
        if (baseUri != null) {
            if (baseUri.toLowerCase().endsWith(".json")) {
                baseUri = baseUri.substring(0, baseUri.length() - 5);
            }
            String pngUri = baseUri + ".png";
            String jsonUri = baseUri + ".json";
            String atlasUri = baseUri + ".atlas";
            // Avoid adding the same reference twice
            binaryPaths.checkAndAdd(pngUri);
            binaryPaths.checkAndAdd(jsonUri);
            binaryPaths.checkAndAdd(atlasUri);
        }
    }

    // Iterate through fields
    for (Field field : ClassReflection.getDeclaredFields(clazz)) {
        field.setAccessible(true);

        Object value = null;
        try {
            value = field.get(object);
        } catch (ReflectionException e) {
            e.printStackTrace();
        }
        if (value == null) {
            continue;
        }

        // Recursive search: array, list, map,
        if (ClassReflection.isAssignableFrom(Array.class, field.getType())) {
            Array array = (Array) value;
            for (Object child : array) {
                if (child == null) {
                    continue;
                }
                listRefBinaries(child, child.getClass(), binaryPaths);
            }
        }

        else if (ClassReflection.isAssignableFrom(List.class, field.getType())) {
            List list = (List) value;
            for (Object child : list) {
                if (child == null) {
                    continue;
                }
                listRefBinaries(child, child.getClass(), binaryPaths);
            }
        }

        else if (ClassReflection.isAssignableFrom(Map.class, field.getType())) {
            Map map = (Map) value;
            for (Object child : map.values()) {
                if (child == null) {
                    continue;
                }
                listRefBinaries(child, child.getClass(), binaryPaths);
            }
        }

        // Recursive search
        else {
            listRefBinaries(value, value.getClass(), binaryPaths);
        }
    }

    if (clazz.getSuperclass() != null) {
        listRefBinaries(object, clazz.getSuperclass(), binaryPaths);
    }
}

From source file:es.eucm.ead.editor.utils.ProjectUtils.java

License:Open Source License

private static void replaceBinaryRef(Object object, Class clazz, String oldRef, String newRef) {
    if (clazz == null) {
        clazz = object.getClass();//www. ja  v a  2 s.  c om
    }

    // If the object is from primitive type or null, do not search
    if (object == null || clazz.isEnum() || clazz == Float.class || clazz == Double.class
            || clazz == Boolean.class || clazz == Integer.class || clazz == Byte.class
            || clazz == Character.class || clazz == Long.class || clazz == Short.class) {
        return;
    }

    // Special case: SpineAnimation does not store extension, and therefore
    // it needs to be treated differently
    else if (ClassReflection.isAssignableFrom(SpineAnimation.class, clazz)) {
        SpineAnimation spineAnimation = (SpineAnimation) object;
        String baseUri = spineAnimation.getUri();
        if (baseUri != null) {
            baseUri = baseUri.toLowerCase();
            oldRef = oldRef.toLowerCase();
            if (baseUri.endsWith(".json")) {
                baseUri = baseUri.substring(0, baseUri.length() - 5);
            }
            if (oldRef.endsWith(".json")) {
                oldRef = oldRef.substring(0, oldRef.length() - 5);
            }
            if (baseUri.equals(oldRef)) {
                spineAnimation.setUri(newRef);
            }
        }
    }

    // Iterate through fields
    for (Field field : ClassReflection.getDeclaredFields(clazz)) {
        field.setAccessible(true);

        Object value = null;
        try {
            value = field.get(object);
        } catch (ReflectionException e) {
            e.printStackTrace();
        }
        if (value == null) {
            continue;
        }

        // Recursive search: array, list, map,
        if (ClassReflection.isAssignableFrom(Array.class, field.getType())) {
            Array array = (Array) value;
            for (int i = 0; i < array.size; i++) {
                Object child = array.get(i);
                if (child == null) {
                    continue;
                } else if (child instanceof String) {
                    String strValue = (String) child;
                    if (strValue.toLowerCase().equals(oldRef.toLowerCase())) {
                        array.set(i, newRef);
                    }
                } else {
                    replaceBinaryRef(child, child.getClass(), oldRef, newRef);
                }
            }
        }

        else if (ClassReflection.isAssignableFrom(List.class, field.getType())) {
            List list = (List) value;
            for (int i = 0; i < list.size(); i++) {
                Object child = list.get(i);
                if (child == null) {
                    continue;
                } else if (child instanceof String) {
                    String strValue = (String) child;
                    if (strValue.toLowerCase().equals(oldRef.toLowerCase())) {
                        list.set(i, newRef);
                    }
                } else {
                    replaceBinaryRef(child, child.getClass(), oldRef, newRef);
                }
            }
        }

        else if (ClassReflection.isAssignableFrom(Map.class, field.getType())) {
            Map map = (Map) value;
            for (Object key : map.keySet()) {
                Object child = map.get(key);
                if (child == null) {
                    continue;
                } else if (child instanceof String) {
                    String strValue = (String) child;
                    if (strValue.toLowerCase().equals(oldRef.toLowerCase())) {
                        map.put(key, newRef);
                    }
                } else {
                    replaceBinaryRef(child, child.getClass(), oldRef, newRef);
                }
            }
        } else if (ClassReflection.isAssignableFrom(String.class, field.getType())) {
            String strValue = (String) value;
            // Check if value matches oldRef
            if (strValue.toLowerCase().equals(oldRef.toLowerCase())) {
                try {
                    field.set(object, newRef);
                } catch (ReflectionException e) {
                    Gdx.app.error("Error setting binary ref in field " + field.getName(), "", e);
                }
            }
        }
        // Recursive search
        else {
            replaceBinaryRef(value, value.getClass(), oldRef, newRef);
        }
    }

    if (clazz.getSuperclass() != null) {
        replaceBinaryRef(object, clazz.getSuperclass(), oldRef, newRef);
    }
}

From source file:org.shadebob.skineditor.utils.scenes.scene2d.ui.CustomSkin.java

License:Apache License

/** ?? */
public static <T> boolean isResInUse(Skin skin, T target) {
    if (skin == null || target == null) {
        throw new IllegalArgumentException("skin and target can not null");
    }//from  w  w w .java  2s. c om
    try {
        Class<?> clazzTarget = target.getClass();
        for (String widget : SkinEditorGame.widgets) {
            Class<?> resType = Class
                    .forName("com.badlogic.gdx.scenes.scene2d.ui." + widget + "$" + widget + "Style");
            // ?
            if (resType == clazzTarget) {
                continue;
            }
            ObjectMap<String, ?> typeResources = skin.getAll(resType);
            if (emptyMap(typeResources)) {
                continue;
            }
            //  styleName ?.???
            Array<String> styleNames = typeResources.keys().toArray();
            // ??? resType ? ?
            for (String styleName : styleNames) {
                Object objStyle = typeResources.get(styleName);
                Field[] fields = ClassReflection.getFields(objStyle.getClass());
                for (Field field : fields) {
                    //  field
                    if (field.isFinal() || field.isStatic() || field.isTransient()) {
                        continue;
                    }
                    if (field.getType() == clazzTarget) {
                        @SuppressWarnings("unchecked")
                        T f = (T) field.get(objStyle);
                        if (target.equals(f)) {
                            return true;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.libgdx.skin.editor.utils.scene2d.CustomSkin.java

License:Apache License

/** ?? */
public static <T> boolean isResInUse(Skin skin, T target) {
    if (skin == null || target == null) {
        throw new IllegalArgumentException("skin and target can not null");
    }//from   w ww  . j  av  a2 s.c om
    try {
        Class<?> clazzTarget = target.getClass();
        for (String widget : widgets) {
            Class<?> resType = Class
                    .forName("com.badlogic.gdx.scenes.scene2d.ui." + widget + "$" + widget + "Style");
            // ?
            if (resType == clazzTarget) {
                continue;
            }
            ObjectMap<String, ?> typeResources = skin.getAll(resType);
            if (emptyMap(typeResources)) {
                continue;
            }
            //  styleName ?.???
            Array<String> styleNames = typeResources.keys().toArray();
            // ??? resType ? ?
            for (String styleName : styleNames) {
                Object objStyle = typeResources.get(styleName);
                Field[] fields = ClassReflection.getFields(objStyle.getClass());
                for (Field field : fields) {
                    //  field
                    if (field.isFinal() || field.isStatic() || field.isTransient()) {
                        continue;
                    }
                    if (field.getType() == clazzTarget) {
                        @SuppressWarnings("unchecked")
                        T f = (T) field.get(objStyle);
                        if (target.equals(f)) {
                            return true;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:org.shadebob.skineditor.utils.scenes.scene2d.ui.CustomSkin.java

License:Apache License

/** ?? */
public static <T> boolean isStyleInUse(Skin skin, String targetStyleName, Class<T> targetStyleClazz) {
    if (skin == null || targetStyleName == null || targetStyleName.trim().length() == 0
            || targetStyleClazz == null) {
        throw new IllegalArgumentException("skin?targetStyleName and targetStyleClazz can not null");
    }/*from w  w w.  j  a v  a 2s  . c o m*/
    try {
        for (String widget : SkinEditorGame.widgets) {
            Class<?> resType = Class
                    .forName("com.badlogic.gdx.scenes.scene2d.ui." + widget + "$" + widget + "Style");
            // ?
            if (resType == targetStyleClazz) {
                continue;
            }
            ObjectMap<String, ?> typeResources = skin.getAll(resType);
            if (emptyMap(typeResources)) {
                continue;
            }
            //  styleName ?.???
            Array<String> styleNames = typeResources.keys().toArray();
            // ??? resType ? ?
            for (String styleName : styleNames) {
                Object objStyle = typeResources.get(styleName);
                Field[] fields = ClassReflection.getFields(objStyle.getClass());
                for (Field field : fields) {
                    //  field
                    if (field.isFinal() || field.isStatic() || field.isTransient()) {
                        continue;
                    }
                    if (field.getType() == targetStyleClazz) {
                        @SuppressWarnings("unchecked")
                        T fieldObj = (T) field.get(objStyle);
                        String fieldObjStyleName = skin.find(fieldObj);
                        if (targetStyleName.equals(fieldObjStyleName)) {
                            return true;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.libgdx.skin.editor.utils.scene2d.CustomSkin.java

License:Apache License

/** ?? */
public static <T> boolean isStyleInUse(Skin skin, String targetStyleName, Class<T> targetStyleClazz) {
    if (skin == null || targetStyleName == null || targetStyleName.trim().length() == 0
            || targetStyleClazz == null) {
        throw new IllegalArgumentException("skin?targetStyleName and targetStyleClazz can not null");
    }//from   w w w .  j a  va 2s  .  co m
    try {
        for (String widget : widgets) {
            Class<?> resType = Class
                    .forName("com.badlogic.gdx.scenes.scene2d.ui." + widget + "$" + widget + "Style");
            // ?
            if (resType == targetStyleClazz) {
                continue;
            }
            ObjectMap<String, ?> typeResources = skin.getAll(resType);
            if (emptyMap(typeResources)) {
                continue;
            }
            //  styleName ?.???
            Array<String> styleNames = typeResources.keys().toArray();
            // ??? resType ? ?
            for (String styleName : styleNames) {
                Object objStyle = typeResources.get(styleName);
                Field[] fields = ClassReflection.getFields(objStyle.getClass());
                for (Field field : fields) {
                    //  field
                    if (field.isFinal() || field.isStatic() || field.isTransient()) {
                        continue;
                    }
                    if (field.getType() == targetStyleClazz) {
                        @SuppressWarnings("unchecked")
                        T fieldObj = (T) field.get(objStyle);
                        String fieldObjStyleName = skin.find(fieldObj);
                        if (targetStyleName.equals(fieldObjStyleName)) {
                            return true;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.mygdx.game.ui.BooleanSettingsMenu.java

License:Apache License

public BooleanSettingsMenu(String buttonText, Skin skin, Class<?> booleanSettingsClass) {
    final Table innerTable = new Table();

    Field[] fields = ClassReflection.getDeclaredFields(booleanSettingsClass);
    for (final Field field : fields) {
        boolean fieldValueBoolean = false;
        try {//from   w w  w  . ja  v a  2 s .  c  om
            fieldValueBoolean = (Boolean) field.get(field);
        } catch (Exception e) {
            Gdx.app.debug(TAG, "Cannot parse value for boolean " + field.getName());
        }
        final CheckBox checkBox = new CheckBox(field.getName(), skin);
        checkBox.setChecked(fieldValueBoolean);
        innerTable.add(checkBox).pad(1).align(Align.left);
        innerTable.row();
        checkBox.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                try {
                    field.set(field, (Boolean) checkBox.isChecked());
                } catch (ReflectionException e) {
                    Gdx.app.debug(TAG, "Cannot set value for " + field.getName());
                }
            }
        });
    }
    innerTable.setVisible(false);
    final TextButton button = new TextButton(buttonText, skin);
    button.addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            innerTable.setVisible(!innerTable.isVisible());
            return true;
        }
    });
    add(innerTable).fillX();
    row();
    add(button).fillX();
    row();
}

From source file:de.longri.cachebox3.develop.tools.skin_editor.validation.Validate_Abstract_Icons.java

License:Open Source License

@Override
public void runValidation() {

    errorMsg = "";
    warnMsg = "";

    try {/*from ww  w  . j  a  v  a 2  s. c o  m*/
        Field[] fields = ClassReflection.getFields(tClass);
        Object instance = validationSkin.get(tClass);

        for (Field field : fields) {
            Object object = field.get(instance);

            if (object instanceof Integer) {
                continue;
            }

            if (object != null) {
                TextureRegionDrawable trd = (TextureRegionDrawable) object;
                String svgName = trd.getName();

                ScaledSvg scaledSvg = validationSkin.get(svgName, ScaledSvg.class);
                if (scaledSvg.path.toLowerCase().contains("missingicon")) {
                    isMissingIconList.add(field.getName());
                }

                checkSize(object, field.getName());

            } else {
                isNullList.add(field.getName());
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
        errorMsg = "Error with validation task";
    }

    if (isNullList.size > 0) {
        StringBuilder sb = new StringBuilder();
        sb.append("Some Icons are NULL:\n\n");
        for (String name : isNullList) {
            sb.append(name);
            sb.append("\n");
        }

        errorMsg += sb.toString();
    }

    if (isMissingIconList.size > 0) {
        StringBuilder sb = new StringBuilder();
        sb.append("Some Icons are dummy Icon (\"missingIcon\") :\n\n");
        for (String name : isMissingIconList) {
            sb.append(name);
            sb.append("\n");
        }
        warnMsg = sb.toString();
    }

    if (wrongBitmapsSize.length > 0) {
        warnMsg += "\n\nWrong Sizes:\n\n" + wrongBitmapsSize.toString();
    }
}

From source file:es.eucm.ead.engine.processors.RefProcessor.java

License:Open Source License

@Override
public Component getComponent(T component) {
    try {/*  w  w w.  java2  s . co  m*/
        Field field = ClassReflection.getDeclaredField(component.getClass(), "uri");
        field.setAccessible(true);
        loadedComponent = null;
        gameAssets.get(field.get(component) + "", Object.class, this);
        return componentLoader.toEngineComponent(loadedComponent);
    } catch (ReflectionException e) {
        Gdx.app.error("RefProcessor", "No uri field in " + component);
        return null;
    }
}