Skip to content

Commit a63c573

Browse files
committed
util: use generics for Loader.getClassInstance(s) methods
1 parent 7b0727c commit a63c573

File tree

6 files changed

+19
-21
lines changed

6 files changed

+19
-21
lines changed

table/src/main/uk/ac/starlink/table/StoragePolicy.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ public abstract class StoragePolicy {
5757

5858
/**
5959
* Name of the system property which can be set to indicate the
60-
* initial setting of the default storage policy.
60+
* initial setting of the default storage policy ({@value}).
6161
* Currently recognised values are "adaptive", "memory", "disk",
6262
* "sideways", and "discard".
63+
* Alternatively, the classname of a StoragePolicy implementation
64+
* with a no-arg constructor may be supplied.
6365
*/
6466
public static final String PREF_PROPERTY = "startable.storage";
6567

@@ -89,7 +91,6 @@ else if ( "discard".equals( pref ) ) {
8991
}
9092
else {
9193
StoragePolicy named =
92-
(StoragePolicy)
9394
Loader.getClassInstance( pref, StoragePolicy.class );
9495
defaultInstance_ = named != null
9596
? named

topcat/src/main/uk/ac/starlink/topcat/ControlWindow.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,9 @@ public void mouseClicked( MouseEvent evt ) {
603603
toolBar.add( interopAct );
604604
}
605605
toolBar.add( MethodWindow.getWindowAction( this, false ) );
606-
List actList = Loader.getClassInstances( TOPCAT_TOOLS_PROP,
607-
TopcatToolAction.class );
608-
for ( Iterator it = actList.iterator(); it.hasNext(); ) {
609-
TopcatToolAction tact = (TopcatToolAction) it.next();
606+
for ( TopcatToolAction tact :
607+
Loader.getClassInstances( TOPCAT_TOOLS_PROP,
608+
TopcatToolAction.class ) ) {
610609
tact.setParent( this );
611610
toolBar.add( tact );
612611
}

topcat/src/main/uk/ac/starlink/topcat/LoadWindow.java

-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ public TableLoader createTableLoader() {
132132
/* Prepare actions for all known dialogues. */
133133
actList_ = new ArrayList<Action>();
134134
knownDialogs_ =
135-
(TableLoadDialog[])
136135
Loader.getClassInstances( DIALOG_CLASSES, LOAD_DIALOGS_PROPERTY,
137136
TableLoadDialog.class )
138137
.toArray( new TableLoadDialog[ 0 ] );

ttools/src/main/uk/ac/starlink/ttools/join/MatchEngineParameter.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,7 @@ else if ( cName.equalsIgnoreCase( "htm" ) ) {
498498
ARC_SECOND );
499499
}
500500
else {
501-
component =
502-
(MatchEngine) Loader.getClassInstance( cName,
503-
MatchEngine.class );
501+
component = Loader.getClassInstance( cName, MatchEngine.class );
504502
if ( component == null ) {
505503
throw new UsageException( "Unknown matcher element: "
506504
+ cName );

util/src/main/uk/ac/starlink/util/Loader.java

+11-9
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public static synchronized void loadProperties() {
252252
* from
253253
* @return new <tt>className</tt> instance, or <tt>null</tt>
254254
*/
255-
public static Object getClassInstance( String className, Class type ) {
255+
public static <T> T getClassInstance( String className, Class<T> type ) {
256256
if ( className == null || className.trim().length() == 0 ) {
257257
return null;
258258
}
@@ -277,7 +277,7 @@ public static Object getClassInstance( String className, Class type ) {
277277
return null;
278278
}
279279
try {
280-
return clazz.newInstance();
280+
return type.cast( clazz.newInstance() );
281281
}
282282
catch ( ExceptionInInitializerError e ) {
283283
warn( e.getCause() + " loading class " + className );
@@ -304,8 +304,9 @@ public static Object getClassInstance( String className, Class type ) {
304304
* @param type class which instantiated classes must be assignable from
305305
* @return list of new <tt>type</tt> instances (may be empty, but not null)
306306
*/
307-
public static List getClassInstances( String propertyName, Class type ) {
308-
List instances = new ArrayList();
307+
public static <T> List<T> getClassInstances( String propertyName,
308+
Class<T> type ) {
309+
List<T> instances = new ArrayList<T>();
309310

310311
/* Get the property value, if possible. */
311312
String propVal;
@@ -323,7 +324,7 @@ public static List getClassInstances( String propertyName, Class type ) {
323324
for ( StringTokenizer stok = new StringTokenizer( propVal, ":" );
324325
stok.hasMoreElements(); ) {
325326
String cname = stok.nextToken().trim();
326-
Object inst = getClassInstance( cname, type );
327+
T inst = getClassInstance( cname, type );
327328
if ( inst != null ) {
328329
instances.add( inst );
329330
}
@@ -342,12 +343,13 @@ public static List getClassInstances( String propertyName, Class type ) {
342343
*
343344
* @param defaultNames array of string
344345
*/
345-
public static List getClassInstances( String[] defaultNames,
346-
String propertyName, Class type ) {
346+
public static <T> List<T> getClassInstances( String[] defaultNames,
347+
String propertyName,
348+
Class<T> type ) {
347349
Loader.loadProperties();
348-
List instances = new ArrayList();
350+
List<T> instances = new ArrayList<T>();
349351
for ( int i = 0; i < defaultNames.length; i++ ) {
350-
Object instance = getClassInstance( defaultNames[ i ], type );
352+
T instance = getClassInstance( defaultNames[ i ], type );
351353
if ( instance != null ) {
352354
instances.add( instance );
353355
}

votable/src/main/uk/ac/starlink/votable/Namespacing.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ public static Namespacing getInstance() {
136136
}
137137
}
138138
if ( ns == null ) {
139-
ns = (Namespacing)
140-
Loader.getClassInstance( policy, Namespacing.class );
139+
ns = Loader.getClassInstance( policy, Namespacing.class );
141140
}
142141
if ( ns == null ) {
143142
StringBuffer sbuf = new StringBuffer()

0 commit comments

Comments
 (0)