JAL-4152 Make return type of getInstance a generic type
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Wed, 22 Mar 2023 11:50:47 +0000 (12:50 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Wed, 22 Mar 2023 11:50:47 +0000 (12:50 +0100)
src/jalview/bin/ApplicationSingletonProvider.java

index 450809a..1367fab 100644 (file)
@@ -106,7 +106,8 @@ public class ApplicationSingletonProvider
    * @param c
    * @return
    */
-  public static ApplicationSingletonI getInstance(Class<? extends ApplicationSingletonI> c)
+  @SuppressWarnings("unchecked")
+  public static <T extends ApplicationSingletonI> T getInstance(Class<T> c)
   {
     Map<Class<? extends ApplicationSingletonI>, ApplicationSingletonI> map = getContextMap();
     if (map.containsKey(c))
@@ -114,17 +115,16 @@ public class ApplicationSingletonProvider
       /*
        * singleton already created _or_ creation failed (null value stored)
        */
-      return map.get(c);
+      return (T) map.get(c);
     }
 
     /*
      * create and save the singleton
      */
-    ApplicationSingletonI o = map.get(c);
+    T o = null;
     try
     {
-      Constructor<? extends ApplicationSingletonI> con = c
-              .getDeclaredConstructor();
+      Constructor<T> con = c.getDeclaredConstructor();
       con.setAccessible(true);
       o = con.newInstance();
     } catch (IllegalAccessException | InstantiationException