From 88c9371a4dabeae1a79a325c8129fa49200aec33 Mon Sep 17 00:00:00 2001 From: Stephen Gold Date: Wed, 15 Apr 2020 16:55:29 -0700 Subject: [PATCH] avoid unchecked casts due by premature casting of Class objects --- .../main/java/com/jme3/app/AndroidHarness.java | 5 ++--- .../java/com/jme3/app/AndroidHarnessFragment.java | 5 ++--- .../src/main/java/com/jme3/app/AppletHarness.java | 4 ++-- .../java/com/jme3/system/JmeDesktopSystem.java | 15 +++++++-------- .../src/main/java/jme3test/awt/AppHarness.java | 4 ++-- .../src/main/java/jme3test/awt/TestApplet.java | 4 ++-- .../src/main/java/jme3test/awt/TestCanvas.java | 4 ++-- 7 files changed, 19 insertions(+), 22 deletions(-) diff --git a/jme3-android/src/main/java/com/jme3/app/AndroidHarness.java b/jme3-android/src/main/java/com/jme3/app/AndroidHarness.java index 5700f01cc..b592ac06a 100644 --- a/jme3-android/src/main/java/com/jme3/app/AndroidHarness.java +++ b/jme3-android/src/main/java/com/jme3/app/AndroidHarness.java @@ -239,9 +239,8 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt // Create application instance try { if (app == null) { - @SuppressWarnings("unchecked") - Class clazz = (Class) Class.forName(appClass); - app = clazz.newInstance(); + Class clazz = Class.forName(appClass); + app = (LegacyApplication)clazz.newInstance(); } app.setSettings(settings); diff --git a/jme3-android/src/main/java/com/jme3/app/AndroidHarnessFragment.java b/jme3-android/src/main/java/com/jme3/app/AndroidHarnessFragment.java index 073feb3fe..9c2eef375 100644 --- a/jme3-android/src/main/java/com/jme3/app/AndroidHarnessFragment.java +++ b/jme3-android/src/main/java/com/jme3/app/AndroidHarnessFragment.java @@ -257,9 +257,8 @@ public class AndroidHarnessFragment extends Fragment implements // Create application instance try { if (app == null) { - @SuppressWarnings("unchecked") - Class clazz = (Class) Class.forName(appClass); - app = clazz.newInstance(); + Class clazz = Class.forName(appClass); + app = (LegacyApplication)clazz.newInstance(); } app.setSettings(settings); diff --git a/jme3-desktop/src/main/java/com/jme3/app/AppletHarness.java b/jme3-desktop/src/main/java/com/jme3/app/AppletHarness.java index 3dee23bec..8b675c25e 100644 --- a/jme3-desktop/src/main/java/com/jme3/app/AppletHarness.java +++ b/jme3-desktop/src/main/java/com/jme3/app/AppletHarness.java @@ -103,8 +103,8 @@ public class AppletHarness extends Applet { JmeSystem.setLowPermissions(true); try{ - Class clazz = (Class) Class.forName(appClass); - app = clazz.newInstance(); + Class clazz = Class.forName(appClass); + app = (LegacyApplication) clazz.newInstance(); }catch (ClassNotFoundException ex){ ex.printStackTrace(); }catch (InstantiationException ex){ diff --git a/jme3-desktop/src/main/java/com/jme3/system/JmeDesktopSystem.java b/jme3-desktop/src/main/java/com/jme3/system/JmeDesktopSystem.java index 864d492ee..d5f2e420b 100644 --- a/jme3-desktop/src/main/java/com/jme3/system/JmeDesktopSystem.java +++ b/jme3-desktop/src/main/java/com/jme3/system/JmeDesktopSystem.java @@ -193,22 +193,22 @@ public class JmeDesktopSystem extends JmeSystemDelegate { private JmeContext newContextLwjgl(AppSettings settings, JmeContext.Type type) { try { - Class ctxClazz = null; + Class ctxClazz = null; switch (type) { case Canvas: - ctxClazz = (Class) Class.forName("com.jme3.system.lwjgl.LwjglCanvas"); + ctxClazz = Class.forName("com.jme3.system.lwjgl.LwjglCanvas"); break; case Display: - ctxClazz = (Class) Class.forName("com.jme3.system.lwjgl.LwjglDisplay"); + ctxClazz = Class.forName("com.jme3.system.lwjgl.LwjglDisplay"); break; case OffscreenSurface: - ctxClazz = (Class) Class.forName("com.jme3.system.lwjgl.LwjglOffscreenBuffer"); + ctxClazz = Class.forName("com.jme3.system.lwjgl.LwjglOffscreenBuffer"); break; default: throw new IllegalArgumentException("Unsupported context type " + type); } - return ctxClazz.newInstance(); + return (JmeContext) ctxClazz.newInstance(); } catch (InstantiationException ex) { logger.log(Level.SEVERE, "Failed to create context", ex); } catch (IllegalAccessException ex) { @@ -225,9 +225,8 @@ public class JmeDesktopSystem extends JmeSystemDelegate { try { String className = settings.getRenderer().substring("CUSTOM".length()); - Class ctxClazz = null; - ctxClazz = (Class) Class.forName(className); - return ctxClazz.newInstance(); + Class ctxClazz = Class.forName(className); + return (JmeContext) ctxClazz.newInstance(); } catch (InstantiationException ex) { logger.log(Level.SEVERE, "Failed to create context", ex); } catch (IllegalAccessException ex) { diff --git a/jme3-examples/src/main/java/jme3test/awt/AppHarness.java b/jme3-examples/src/main/java/jme3test/awt/AppHarness.java index 34d6c0522..85e1ad1c3 100644 --- a/jme3-examples/src/main/java/jme3test/awt/AppHarness.java +++ b/jme3-examples/src/main/java/jme3test/awt/AppHarness.java @@ -79,8 +79,8 @@ public class AppHarness extends Applet { JmeSystem.setLowPermissions(true); try{ - Class clazz = (Class) Class.forName(appClass); - app = clazz.newInstance(); + Class clazz = Class.forName(appClass); + app = (LegacyApplication) clazz.newInstance(); }catch (ClassNotFoundException ex){ ex.printStackTrace(); }catch (InstantiationException ex){ diff --git a/jme3-examples/src/main/java/jme3test/awt/TestApplet.java b/jme3-examples/src/main/java/jme3test/awt/TestApplet.java index 2c5f11e65..03978620e 100644 --- a/jme3-examples/src/main/java/jme3test/awt/TestApplet.java +++ b/jme3-examples/src/main/java/jme3test/awt/TestApplet.java @@ -62,8 +62,8 @@ public class TestApplet extends Applet { JmeSystem.setLowPermissions(true); try{ - Class clazz = (Class) Class.forName(appClass); - app = clazz.newInstance(); + Class clazz = Class.forName(appClass); + app = (LegacyApplication) clazz.newInstance(); }catch (ClassNotFoundException ex){ ex.printStackTrace(); }catch (InstantiationException ex){ diff --git a/jme3-examples/src/main/java/jme3test/awt/TestCanvas.java b/jme3-examples/src/main/java/jme3test/awt/TestCanvas.java index 019fee406..617691e89 100644 --- a/jme3-examples/src/main/java/jme3test/awt/TestCanvas.java +++ b/jme3-examples/src/main/java/jme3test/awt/TestCanvas.java @@ -209,8 +209,8 @@ public class TestCanvas { settings.setHeight(480); try{ - Class clazz = (Class) Class.forName(appClass); - app = clazz.newInstance(); + Class clazz = Class.forName(appClass); + app = (LegacyApplication)clazz.newInstance(); }catch (ClassNotFoundException ex){ ex.printStackTrace(); }catch (InstantiationException ex){