From 6ef713ee3fe2b94e553943ce64700b15149e2888 Mon Sep 17 00:00:00 2001 From: 6money <47844354+6money@users.noreply.github.com> Date: Fri, 17 Sep 2021 12:39:05 +1200 Subject: [PATCH] Use gdx ClassReflection methods so inGameConsole can be used with gwt backend (#59) * Use gdx ClassReflection methods so console can be used with gwt backend (cherry picked from commit 2096276187ba806d3c934c28f166e8630b856eb3) * Revert changes to overridden scrolled method to work with libgdx 1.9.9 --- .../strongjoshua/console/ConsoleContext.java | 9 +++-- .../strongjoshua/console/ConsoleUtils.java | 10 +++--- src/com/strongjoshua/console/GUIConsole.java | 34 ++++++++++++------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/com/strongjoshua/console/ConsoleContext.java b/src/com/strongjoshua/console/ConsoleContext.java index e95f3de..3438724 100644 --- a/src/com/strongjoshua/console/ConsoleContext.java +++ b/src/com/strongjoshua/console/ConsoleContext.java @@ -10,6 +10,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; +import com.badlogic.gdx.utils.reflect.ClassReflection; public class ConsoleContext { private Table root; @@ -18,15 +19,17 @@ public class ConsoleContext { ConsoleContext (Class tableClass, Class labelClass, Skin skin, String background) { try { - root = tableClass.newInstance(); + root = ClassReflection.newInstance(tableClass); } catch (Exception e) { throw new RuntimeException("Table class does not support empty constructor."); } try { - copy = labelClass.getConstructor(CharSequence.class, Skin.class).newInstance("Copy", skin); + copy = (Label) ClassReflection.getConstructor(labelClass, CharSequence.class, Skin.class) + .newInstance("Copy", skin); } catch (Exception e) { try { - copy = labelClass.getConstructor(CharSequence.class).newInstance("Copy"); + copy = (Label) ClassReflection.getConstructor(labelClass, CharSequence.class) + .newInstance("Copy"); } catch (Exception e2) { throw new RuntimeException( "Label class does not support either (, ) or () constructors."); diff --git a/src/com/strongjoshua/console/ConsoleUtils.java b/src/com/strongjoshua/console/ConsoleUtils.java index 946f90e..cf59191 100644 --- a/src/com/strongjoshua/console/ConsoleUtils.java +++ b/src/com/strongjoshua/console/ConsoleUtils.java @@ -33,12 +33,10 @@ public static String exceptionToString (final Throwable throwable) { Throwable cause = throwable; while (cause != null) { - if (result.length() == 0) { - result.append("\nException in thread \"").append(Thread.currentThread().getName()).append("\" "); - } else { - result.append("\nCaused by: "); - } - result.append(cause.getClass().getCanonicalName()).append(": ").append(cause.getMessage()); + result.append("\nCaused by: ") + .append(cause.getClass().getCanonicalName()) + .append(": ") + .append(cause.getMessage()); for (final StackTraceElement traceElement : cause.getStackTrace()) { result.append("\n\tat ").append(traceElement.toString()); diff --git a/src/com/strongjoshua/console/GUIConsole.java b/src/com/strongjoshua/console/GUIConsole.java index 78af05d..6265c52 100644 --- a/src/com/strongjoshua/console/GUIConsole.java +++ b/src/com/strongjoshua/console/GUIConsole.java @@ -30,6 +30,7 @@ import com.badlogic.gdx.scenes.scene2d.utils.Drawable; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.SnapshotArray; +import com.badlogic.gdx.utils.reflect.ClassReflection; /** * A simple console that allows live logging, and live execution of methods, from within an application. Please see the , ) or () constructors."); } @@ -410,7 +413,7 @@ private class ConsoleDisplay { ConsoleDisplay (Skin skin) { try { - root = tableClass.newInstance(); + root = ClassReflection.newInstance(tableClass); } catch (Exception e) { throw new RuntimeException("Table class does not support empty constructor."); } @@ -428,23 +431,26 @@ private class ConsoleDisplay { labels = new Array