11/*
2- * Copyright (c) 2009-2021 jMonkeyEngine
2+ * Copyright (c) 2009-2025 jMonkeyEngine
33 * All rights reserved.
44 *
55 * Redistribution and use in source and binary forms, with or without
3939import java .io .IOException ;
4040import java .io .InputStream ;
4141import java .io .OutputStream ;
42- import java .lang .reflect .InvocationTargetException ;
42+ import java .lang .reflect .Constructor ;
4343import java .net .URL ;
4444import java .nio .ByteBuffer ;
4545import java .util .function .BiFunction ;
5353public class JmeSystem {
5454
5555 private static final Logger logger = Logger .getLogger (JmeSystem .class .getName ());
56- public static enum StorageFolderType {
56+
57+ public enum StorageFolderType {
5758 Internal ,
5859 External ,
5960 }
@@ -116,6 +117,7 @@ public static void setSoftTextDialogInput(SoftTextDialogInput input) {
116117
117118 /**
118119 * Displays or hides the onscreen soft keyboard
120+ *
119121 * @param show If true, the keyboard is displayed, if false, the screen is hidden.
120122 */
121123 public static void showSoftKeyboard (boolean show ) {
@@ -130,16 +132,16 @@ public static SoftTextDialogInput getSoftTextDialogInput() {
130132
131133 /**
132134 * Compresses a raw image into a stream.
133- *
135+ * <p>
134136 * The encoding is performed via system libraries. On desktop, the encoding
135- * is performed via ImageIO, whereas on Android, is done via the
137+ * is performed via ImageIO, whereas on Android, is done via the
136138 * Bitmap class.
137- *
139+ *
138140 * @param outStream The stream where to write the image data.
139- * @param format The format to use, either "png" or "jpg".
141+ * @param format The format to use, either "png" or "jpg".
140142 * @param imageData The image data in {@link com.jme3.texture.Image.Format#RGBA8} format.
141- * @param width The width of the image.
142- * @param height The height of the image.
143+ * @param width The width of the image.
144+ * @param height The height of the image.
143145 * @throws IOException If outStream throws an exception while writing.
144146 */
145147 public static void writeImageFile (OutputStream outStream , String format , ByteBuffer imageData , int width , int height ) throws IOException {
@@ -157,8 +159,6 @@ public static AssetManager newAssetManager() {
157159 return systemDelegate .newAssetManager ();
158160 }
159161
160-
161-
162162 /**
163163 * Determine which Platform (operating system and architecture) the
164164 * application is running on.
@@ -184,83 +184,83 @@ public static URL getPlatformAssetConfigURL() {
184184 checkDelegate ();
185185 return systemDelegate .getPlatformAssetConfigURL ();
186186 }
187-
187+
188188 /**
189189 * Displays an error message to the user in whichever way the context
190190 * feels is appropriate. If this is a headless or an offscreen surface
191191 * context, this method should do nothing.
192192 *
193- * @deprecated Use JmeSystem.handleErrorMessage(String) instead
194193 * @param message The error message to display. May contain new line
195- * characters.
194+ * characters.
195+ * @deprecated Use JmeSystem.handleErrorMessage(String) instead
196196 */
197197 @ Deprecated
198- public static void showErrorDialog (String message ){
198+ public static void showErrorDialog (String message ) {
199199 handleErrorMessage (message );
200200 }
201201
202- public static void handleErrorMessage (String message ){
202+ public static void handleErrorMessage (String message ) {
203203 checkDelegate ();
204204 systemDelegate .handleErrorMessage (message );
205205 }
206206
207- public static void setErrorMessageHandler (Consumer <String > handler ){
207+ public static void setErrorMessageHandler (Consumer <String > handler ) {
208208 checkDelegate ();
209209 systemDelegate .setErrorMessageHandler (handler );
210210 }
211211
212-
213- public static void handleSettings (AppSettings sourceSettings , boolean loadFromRegistry ){
212+ public static void handleSettings (AppSettings sourceSettings , boolean loadFromRegistry ) {
214213 checkDelegate ();
215214 systemDelegate .handleSettings (sourceSettings , loadFromRegistry );
216215 }
217216
218- public static void setSettingsHandler (BiFunction <AppSettings ,Boolean ,Boolean > handler ){
217+ public static void setSettingsHandler (BiFunction <AppSettings , Boolean , Boolean > handler ) {
219218 checkDelegate ();
220219 systemDelegate .setSettingsHandler (handler );
221220 }
222221
223-
224222 @ Deprecated
225223 public static boolean showSettingsDialog (AppSettings sourceSettings , final boolean loadFromRegistry ) {
226224 checkDelegate ();
227225 return systemDelegate .showSettingsDialog (sourceSettings , loadFromRegistry );
228226 }
229227
230-
231228 public static void initialize (AppSettings settings ) {
232229 checkDelegate ();
233230 systemDelegate .initialize (settings );
234231 }
235232
236- private static JmeSystemDelegate tryLoadDelegate (String className ) throws InstantiationException , IllegalAccessException , NoSuchMethodException , IllegalArgumentException , InvocationTargetException {
237- try {
238- return (JmeSystemDelegate ) Class .forName (className ).getDeclaredConstructor ().newInstance ();
239- } catch (ClassNotFoundException ex ) {
240- return null ;
241- }
242- }
233+ private static final String [] delegateClassNames = {
234+ "com.jme3.system.JmeDesktopSystem" ,
235+ "com.jme3.system.android.JmeAndroidSystem" ,
236+ "com.jme3.system.ios.JmeIosSystem"
237+ };
243238
244- @ SuppressWarnings ("unchecked" )
245239 private static void checkDelegate () {
246240 if (systemDelegate == null ) {
247241 try {
248- systemDelegate = tryLoadDelegate ("com.jme3.system.JmeDesktopSystem" );
249- if (systemDelegate == null ) {
250- systemDelegate = tryLoadDelegate ("com.jme3.system.android.JmeAndroidSystem" );
251- if (systemDelegate == null ) {
252- systemDelegate = tryLoadDelegate ("com.jme3.system.ios.JmeIosSystem" );
253- if (systemDelegate == null ) {
254- // None of the system delegates were found.
255- Logger .getLogger (JmeSystem .class .getName ()).log (Level .SEVERE ,
256- "Failed to find a JmeSystem delegate!\n "
257- + "Ensure either desktop or android jME3 jar is in the classpath." );
258- }
242+ for (String className : delegateClassNames ) {
243+ systemDelegate = tryLoadDelegate (className );
244+ if (systemDelegate != null ) {
245+ return ; // Delegate found and loaded
259246 }
260247 }
261- } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | IllegalArgumentException | InvocationTargetException ex ) {
262- Logger .getLogger (JmeSystem .class .getName ()).log (Level .SEVERE , "Failed to create JmeSystem delegate:\n {0}" , ex );
248+ // None of the system delegates were found.
249+ logger .log (Level .SEVERE , "Failed to find a JmeSystem delegate!\n "
250+ + "Ensure either desktop or android jME3 jar is in the classpath." );
251+
252+ } catch (ReflectiveOperationException | IllegalArgumentException ex ) {
253+ logger .log (Level .SEVERE , "Failed to create JmeSystem delegate:\n {0}" , ex );
263254 }
264255 }
265256 }
257+
258+ private static JmeSystemDelegate tryLoadDelegate (String className ) throws ReflectiveOperationException , IllegalArgumentException {
259+ try {
260+ Constructor <?> c = Class .forName (className ).getDeclaredConstructor ();
261+ return (JmeSystemDelegate ) c .newInstance ();
262+ } catch (ClassNotFoundException ex ) {
263+ return null ;
264+ }
265+ }
266266}
0 commit comments