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);
}
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);
}
}
Aggregations