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

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

Introduction

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

Prototype

public Class getType() 

Source Link

Document

Returns a Class object that identifies the declared type for the field.

Usage

From source file:com.dongbat.invasion.registry.ReflectionUtil.java

static void setFieldValue(Class clazz, String fieldName, Object obj, String value) {
    try {/*  w  w w . jav a 2s . com*/
        Field field = ClassReflection.getDeclaredField(clazz, fieldName);
        field.setAccessible(true);
        Class type = field.getType();
        if (type.equals(float.class)) {
            field.set(obj, Float.parseFloat(value));
        } else if (type.equals(int.class)) {
            field.set(obj, Integer.parseInt(value));
        } else if (type.equals(boolean.class)) {
            field.set(obj, Boolean.parseBoolean(value));
        } else {
            field.set(obj, value);
        }
    } catch (ReflectionException ex) {
        Logger.getLogger(EnemyRegistry.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.dongbat.game.registry.ReflectionUtil.java

/**
 * Set object value for declared field//  w  w  w  .  j  a  v a 2 s . c  om
 *
 * @param clazz class name
 * @param fieldName field name
 * @param obj object type
 * @param value field value
 */
static void setFieldValue(Class clazz, String fieldName, Object obj, Object value) {

    try {
        Field field = ClassReflection.getDeclaredField(clazz, fieldName);
        field.setAccessible(true);
        Class type = field.getType();
        field.set(obj, value);
    } catch (ReflectionException ex) {
        Logger.getLogger(ReflectionUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.dongbat.game.registry.ReflectionUtil.java

/**
 * Set value for declared field Field data type is String, float, int, long,
 * boolean/*www. j a va  2 s  . co m*/
 *
 * @param clazz class name
 * @param fieldName field name
 * @param obj object type
 * @param value field value
 */
static void setFieldValue(Class clazz, String fieldName, Object obj, String value) {

    try {
        Field field = ClassReflection.getDeclaredField(clazz, fieldName);
        field.setAccessible(true);
        Class type = field.getType();
        if (type.equals(float.class)) {
            field.set(obj, Float.parseFloat(value));
        } else if (type.equals(int.class)) {
            field.set(obj, Integer.parseInt(value));
        } else if (type.equals(long.class)) {
            field.set(obj, Long.parseLong(value));
        } else if (type.equals(boolean.class)) {
            field.set(obj, Boolean.parseBoolean(value));
        } else {
            field.set(obj, value);
        }
    } catch (ReflectionException ex) {
        Logger.getLogger(ReflectionUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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 . j av a 2  s  . co m*/
    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: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 www . j  av a  2s  .c om
    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 isResInUse(Skin skin, T target) {
    if (skin == null || target == null) {
        throw new IllegalArgumentException("skin and target can not null");
    }/*www .j a va  2s . co  m*/
    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: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  ww  .j  ava2s  .  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: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;//from   ww w  .ja v a2 s .  c  om
    }

    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();//from www  .  ja  va2s.  com
    }

    // 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:com.github.antag99.retinazer.resolvers.MapperWireResolver.java

License:Open Source License

@SuppressWarnings("unchecked")
private Class<? extends Component> getType(Field field) {
    if (field.getType() != Mapper.class)
        return null;
    return field.getElementType(0);
}