Search in sources :

Example 6 with ReflectionException

use of com.badlogic.gdx.utils.reflect.ReflectionException in project libgdx by libgdx.

the class Json method readFields.

public void readFields(Object object, JsonValue jsonMap) {
    Class type = object.getClass();
    ObjectMap<String, FieldMetadata> fields = getFields(type);
    for (JsonValue child = jsonMap.child; child != null; child = child.next) {
        FieldMetadata metadata = fields.get(child.name);
        if (metadata == null) {
            if (child.name.equals(typeName))
                continue;
            if (ignoreUnknownFields) {
                if (debug)
                    System.out.println("Ignoring unknown field: " + child.name + " (" + type.getName() + ")");
                continue;
            } else {
                SerializationException ex = new SerializationException("Field not found: " + child.name + " (" + type.getName() + ")");
                ex.addTrace(child.trace());
                throw ex;
            }
        }
        Field field = metadata.field;
        try {
            field.set(object, readValue(field.getType(), metadata.elementType, child));
        } catch (ReflectionException ex) {
            throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
        } catch (SerializationException ex) {
            ex.addTrace(field.getName() + " (" + type.getName() + ")");
            throw ex;
        } catch (RuntimeException runtimeEx) {
            SerializationException ex = new SerializationException(runtimeEx);
            ex.addTrace(child.trace());
            ex.addTrace(field.getName() + " (" + type.getName() + ")");
            throw ex;
        }
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Example 7 with ReflectionException

use of com.badlogic.gdx.utils.reflect.ReflectionException in project libgdx by libgdx.

the class Json method writeField.

/** Writes the specified field to the current JSON object.
	 * @param elementType May be null if the type is unknown. */
public void writeField(Object object, String fieldName, String jsonName, Class elementType) {
    Class type = object.getClass();
    ObjectMap<String, FieldMetadata> fields = getFields(type);
    FieldMetadata metadata = fields.get(fieldName);
    if (metadata == null)
        throw new SerializationException("Field not found: " + fieldName + " (" + type.getName() + ")");
    Field field = metadata.field;
    if (elementType == null)
        elementType = metadata.elementType;
    try {
        if (debug)
            System.out.println("Writing field: " + field.getName() + " (" + type.getName() + ")");
        writer.name(jsonName);
        writeValue(field.get(object), field.getType(), elementType);
    } catch (ReflectionException ex) {
        throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
    } catch (SerializationException ex) {
        ex.addTrace(field + " (" + type.getName() + ")");
        throw ex;
    } catch (Exception runtimeEx) {
        SerializationException ex = new SerializationException(runtimeEx);
        ex.addTrace(field + " (" + type.getName() + ")");
        throw ex;
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Example 8 with ReflectionException

use of com.badlogic.gdx.utils.reflect.ReflectionException in project libgdx-inGameConsole by StrongJoshua.

the class AbstractConsole method execCommand.

/* (non-Javadoc)
	 * @see com.strongjoshua.console.Console#execCommand(java.lang.String)
	 */
@Override
public void execCommand(String command) {
    if (disabled)
        return;
    log(command, LogLevel.COMMAND);
    String[] parts = command.split(" ");
    String methodName = parts[0];
    String[] sArgs = null;
    if (parts.length > 1) {
        sArgs = new String[parts.length - 1];
        for (int i = 1; i < parts.length; i++) {
            sArgs[i - 1] = parts[i];
        }
    }
    Class<? extends CommandExecutor> clazz = exec.getClass();
    Method[] methods = ClassReflection.getMethods(clazz);
    Array<Integer> possible = new Array<Integer>();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (method.getName().equalsIgnoreCase(methodName) && ConsoleUtils.canExecuteCommand(this, method)) {
            possible.add(i);
        }
    }
    if (possible.size <= 0) {
        log("No such method found.", LogLevel.ERROR);
        return;
    }
    int size = possible.size;
    int numArgs = sArgs == null ? 0 : sArgs.length;
    for (int i = 0; i < size; i++) {
        Method m = methods[possible.get(i)];
        Class<?>[] params = m.getParameterTypes();
        if (numArgs == params.length) {
            try {
                Object[] args = null;
                try {
                    if (sArgs != null) {
                        args = new Object[numArgs];
                        for (int j = 0; j < params.length; j++) {
                            Class<?> param = params[j];
                            final String value = sArgs[j];
                            if (param.equals(String.class)) {
                                args[j] = value;
                            } else if (param.equals(Boolean.class) || param.equals(boolean.class)) {
                                args[j] = Boolean.parseBoolean(value);
                            } else if (param.equals(Byte.class) || param.equals(byte.class)) {
                                args[j] = Byte.parseByte(value);
                            } else if (param.equals(Short.class) || param.equals(short.class)) {
                                args[j] = Short.parseShort(value);
                            } else if (param.equals(Integer.class) || param.equals(int.class)) {
                                args[j] = Integer.parseInt(value);
                            } else if (param.equals(Long.class) || param.equals(long.class)) {
                                args[j] = Long.parseLong(value);
                            } else if (param.equals(Float.class) || param.equals(float.class)) {
                                args[j] = Float.parseFloat(value);
                            } else if (param.equals(Double.class) || param.equals(double.class)) {
                                args[j] = Double.parseDouble(value);
                            }
                        }
                    }
                } catch (Exception e) {
                    // Error occurred trying to parse parameter, continue to next function
                    continue;
                }
                m.setAccessible(true);
                m.invoke(exec, args);
                return;
            } catch (ReflectionException e) {
                String msg = e.getMessage();
                if (msg == null || msg.length() <= 0 || msg.equals("")) {
                    msg = "Unknown Error";
                    e.printStackTrace();
                }
                log(msg, LogLevel.ERROR);
                return;
            }
        }
    }
    log("Bad parameters. Check your code.", LogLevel.ERROR);
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) Method(com.badlogic.gdx.utils.reflect.Method) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) Array(com.badlogic.gdx.utils.Array)

Aggregations

ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)8 Field (com.badlogic.gdx.utils.reflect.Field)4 IOException (java.io.IOException)3 AccessControlException (java.security.AccessControlException)3 FileHandle (com.badlogic.gdx.files.FileHandle)2 Color (com.badlogic.gdx.graphics.Color)2 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)2 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)2 Array (com.badlogic.gdx.utils.Array)2 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)2 Json (com.badlogic.gdx.utils.Json)2 ReadOnlySerializer (com.badlogic.gdx.utils.Json.ReadOnlySerializer)2 JsonValue (com.badlogic.gdx.utils.JsonValue)2 SerializationException (com.badlogic.gdx.utils.SerializationException)2 BitmapFontData (com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData)1 BaseDrawable (com.badlogic.gdx.scenes.scene2d.utils.BaseDrawable)1 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)1 NinePatchDrawable (com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)1 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)1 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)1