Search in sources :

Example 16 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) {
                    // 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);
                if (consoleTrace) {
                    log(e, 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)

Example 17 with ReflectionException

use of com.badlogic.gdx.utils.reflect.ReflectionException in project skin-composer by raeleus.

the class DialogSceneComposerModel method primeStyles.

private void primeStyles(SimActor simActor) {
    for (var field : ClassReflection.getFields(simActor.getClass())) {
        if (field.getType() == StyleData.class) {
            try {
                var style = (StyleData) field.get(simActor);
                if (style != null) {
                    StyleData foundStyle = jsonData.findStyle(style.clazz, style.name);
                    if (foundStyle == null)
                        foundStyle = jsonData.findStyle(style.clazz, "default");
                    if (foundStyle == null)
                        foundStyle = jsonData.findStyle(style.clazz, "default-horizontal");
                    field.set(simActor, foundStyle);
                }
            } catch (ReflectionException e) {
                e.printStackTrace(System.out);
            }
        } else if (field.getType() == DrawableData.class) {
            try {
                var drawable = (DrawableData) field.get(simActor);
                if (drawable != null) {
                    var foundDrawable = atlasData.getDrawable(drawable.name);
                    field.set(simActor, foundDrawable);
                }
            } catch (ReflectionException e) {
                e.printStackTrace(System.out);
            }
        }
    }
    if (simActor instanceof SimMultipleChildren) {
        for (var child : ((SimMultipleChildren) simActor).getChildren()) {
            if (child != null)
                primeStyles(child);
        }
    }
    if (simActor instanceof SimSingleChild) {
        var child = ((SimSingleChild) simActor).getChild();
        if (child != null)
            primeStyles(child);
    }
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) DrawableData(com.ray3k.skincomposer.data.DrawableData) SimSingleChild(com.ray3k.stripe.scenecomposer.SimSingleChild) StyleData(com.ray3k.skincomposer.data.StyleData) SimMultipleChildren(com.ray3k.stripe.scenecomposer.SimMultipleChildren)

Aggregations

ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)17 Field (com.badlogic.gdx.utils.reflect.Field)5 FileHandle (com.badlogic.gdx.files.FileHandle)4 JsonValue (com.badlogic.gdx.utils.JsonValue)4 SerializationException (com.badlogic.gdx.utils.SerializationException)4 IOException (java.io.IOException)4 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)3 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)3 Json (com.badlogic.gdx.utils.Json)3 Action (com.bladecoder.engine.actions.Action)3 AccessControlException (java.security.AccessControlException)3 Color (com.badlogic.gdx.graphics.Color)2 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)2 NinePatchDrawable (com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)2 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)2 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)2 TiledDrawable (com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable)2 Array (com.badlogic.gdx.utils.Array)2 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)2 ReadOnlySerializer (com.badlogic.gdx.utils.Json.ReadOnlySerializer)2