commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1088081 - in /commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools: DefaultClassHolder.java DiscoverClass.java DiscoverSingleton.java SPInterface.java Service.java
Date Sat, 02 Apr 2011 18:46:43 GMT
Author: simonetripodi
Date: Sat Apr  2 18:46:43 2011
New Revision: 1088081

URL: http://svn.apache.org/viewvc?rev=1088081&view=rev
Log:
DiscoverClass and DiscoverSingleton (and related dependencies) now use generics, no more casts
needed by clients

Modified:
    commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DefaultClassHolder.java
    commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DiscoverClass.java
    commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DiscoverSingleton.java
    commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/SPInterface.java
    commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/Service.java

Modified: commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DefaultClassHolder.java
URL: http://svn.apache.org/viewvc/commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DefaultClassHolder.java?rev=1088081&r1=1088080&r2=1088081&view=diff
==============================================================================
--- commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DefaultClassHolder.java
(original)
+++ commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DefaultClassHolder.java
Sat Apr  2 18:46:43 2011
@@ -30,11 +30,11 @@ import org.apache.commons.discovery.reso
  * 
  * @author Richard A. Sitze
  */
-public class DefaultClassHolder {
-    private Class        defaultClass;
+public class DefaultClassHolder<T> {
+    private Class<? extends T>        defaultClass;
     private final String defaultName;
     
-    public DefaultClassHolder(Class defaultClass) {
+    public <S extends T> DefaultClassHolder(Class<S> defaultClass) {
         this.defaultClass = defaultClass;
         this.defaultName = defaultClass.getName();
     }
@@ -52,12 +52,12 @@ public class DefaultClassHolder {
      *         and verify that it implements the SPI.
      *         (this forces the check, no way out..).
      */
-    public Class getDefaultClass(SPInterface spi, ClassLoaders loaders) {
+    public <S extends T> Class<S> getDefaultClass(SPInterface<T> spi, ClassLoaders
loaders) {
         if (defaultClass == null) {
-            DiscoverClasses classDiscovery = new DiscoverClasses(loaders);
-            ResourceClassIterator classes = classDiscovery.findResourceClasses(getDefaultName());
+            DiscoverClasses<T> classDiscovery = new DiscoverClasses<T>(loaders);
+            ResourceClassIterator<T> classes = classDiscovery.findResourceClasses(getDefaultName());
             if (classes.hasNext()) {
-                ResourceClass info = classes.nextResourceClass();
+                ResourceClass<T> info = classes.nextResourceClass();
                 try {
                     defaultClass = info.loadClass();
                 } catch (Exception e) {
@@ -70,7 +70,9 @@ public class DefaultClassHolder {
             spi.verifyAncestory(defaultClass);
         }
 
-        return defaultClass;
+        @SuppressWarnings("unchecked") // the SPInterface.verifyAncestory already asserted
+        Class<S> returned = (Class<S>) defaultClass;
+        return returned;
     }
 
     public String getDefaultName() {

Modified: commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DiscoverClass.java
URL: http://svn.apache.org/viewvc/commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DiscoverClass.java?rev=1088081&r1=1088080&r2=1088081&view=diff
==============================================================================
--- commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DiscoverClass.java
(original)
+++ commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DiscoverClass.java
Sat Apr  2 18:46:43 2011
@@ -17,8 +17,9 @@
 package org.apache.commons.discovery.tools;
 
 import java.lang.reflect.InvocationTargetException;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Properties;
-import java.util.Vector;
 
 import org.apache.commons.discovery.DiscoveryException;
 import org.apache.commons.discovery.ResourceClass;
@@ -136,11 +137,6 @@ public class DiscoverClass {
     /**
      * Readable placeholder for a null value.
      */
-    public static final DefaultClassHolder nullDefaultImpl = null;
-
-    /**
-     * Readable placeholder for a null value.
-     */
     public static final PropertiesHolder nullProperties = null;
     
     
@@ -168,7 +164,7 @@ public class DiscoverClass {
     }
     
     
-    public ClassLoaders getClassLoaders(Class spiClass) {
+    public ClassLoaders getClassLoaders(Class<?> spiClass) {
         return classLoaders;
     }
 
@@ -184,13 +180,13 @@ public class DiscoverClass {
      *            the SPI cannot be found, if the class cannot be loaded, or if
      *            the resulting class does not implement (or extend) the SPI.
      */
-    public Class find(Class spiClass)
+    public <T, S extends T> Class<S> find(Class<T> spiClass)
         throws DiscoveryException
     {
         return find(getClassLoaders(spiClass),
-                    new SPInterface(spiClass),
+                    new SPInterface<T>(spiClass),
                     nullProperties,
-                    nullDefaultImpl);
+                    (DefaultClassHolder<T>) null);
     }
 
     /**
@@ -206,13 +202,13 @@ public class DiscoverClass {
      *            the SPI cannot be found, if the class cannot be loaded, or if
      *            the resulting class does not implement (or extend) the SPI.
      */
-    public Class find(Class spiClass, Properties properties)
+    public <T, S extends T> Class<S> find(Class<T> spiClass, Properties
properties)
         throws DiscoveryException
     {
         return find(getClassLoaders(spiClass),
-                    new SPInterface(spiClass),
+                    new SPInterface<T>(spiClass),
                     new PropertiesHolder(properties),
-                    nullDefaultImpl);
+                    (DefaultClassHolder<T>) null);
     }
 
     /**
@@ -228,13 +224,13 @@ public class DiscoverClass {
      *            the SPI cannot be found, if the class cannot be loaded, or if
      *            the resulting class does not implement (or extend) the SPI.
      */
-    public Class find(Class spiClass, String defaultImpl)
+    public <T, S extends T> Class<S> find(Class<T> spiClass, String defaultImpl)
         throws DiscoveryException
     {
         return find(getClassLoaders(spiClass),
-                    new SPInterface(spiClass),
+                    new SPInterface<T>(spiClass),
                     nullProperties,
-                    new DefaultClassHolder(defaultImpl));
+                    new DefaultClassHolder<T>(defaultImpl));
     }
 
     /**
@@ -252,13 +248,13 @@ public class DiscoverClass {
      *            the SPI cannot be found, if the class cannot be loaded, or if
      *            the resulting class does not implement (or extend) the SPI.
      */
-    public Class find(Class spiClass, Properties properties, String defaultImpl)
+    public <T, S extends T> Class<S> find(Class<T> spiClass, Properties
properties, String defaultImpl)
         throws DiscoveryException
     {
         return find(getClassLoaders(spiClass),
-                    new SPInterface(spiClass),
+                    new SPInterface<T>(spiClass),
                     new PropertiesHolder(properties),
-                    new DefaultClassHolder(defaultImpl));
+                    new DefaultClassHolder<T>(defaultImpl));
     }
 
     /**
@@ -276,13 +272,13 @@ public class DiscoverClass {
      *            the SPI cannot be found, if the class cannot be loaded, or if
      *            the resulting class does not implement (or extend) the SPI.
      */
-    public Class find(Class spiClass, String propertiesFileName, String defaultImpl)
+    public <T, S extends T> Class<S> find(Class<T> spiClass, String propertiesFileName,
String defaultImpl)
         throws DiscoveryException
     {
         return find(getClassLoaders(spiClass),
-                    new SPInterface(spiClass),
+                    new SPInterface<T>(spiClass),
                     new PropertiesHolder(propertiesFileName),
-                    new DefaultClassHolder(defaultImpl));
+                    new DefaultClassHolder<T>(defaultImpl));
     }
 
     /**
@@ -300,10 +296,10 @@ public class DiscoverClass {
      *            the SPI cannot be found, if the class cannot be loaded, or if
      *            the resulting class does not implement (or extend) the SPI.
      */
-    public static Class find(ClassLoaders loaders,
-                             SPInterface spi,
+    public static <T, S extends T> Class<S> find(ClassLoaders loaders,
+                             SPInterface<T> spi,
                              PropertiesHolder properties,
-                             DefaultClassHolder defaultImpl)
+                             DefaultClassHolder<T> defaultImpl)
         throws DiscoveryException
     {
         if (loaders == null) {
@@ -319,14 +315,14 @@ public class DiscoverClass {
         String[] classNames = discoverClassNames(spi, props);
         
         if (classNames.length > 0) {
-            DiscoverClasses classDiscovery = new DiscoverClasses(loaders);
+            DiscoverClasses<T> classDiscovery = new DiscoverClasses<T>(loaders);
             
-            ResourceClassIterator classes =
+            ResourceClassIterator<T> classes =
                 classDiscovery.findResourceClasses(classNames[0]);
             
             // If it's set as a property.. it had better be there!
             if (classes.hasNext()) {
-                ResourceClass info = classes.nextResourceClass();
+                ResourceClass<T> info = classes.nextResourceClass();
                 try {
                     return info.loadClass();
                 } catch (Exception e) {
@@ -337,8 +333,8 @@ public class DiscoverClass {
             ResourceNameIterator classIter =
                 (new DiscoverServiceNames(loaders)).findResourceNames(spi.getSPName());
 
-            ResourceClassIterator classes =
-                (new DiscoverClasses(loaders)).findResourceClasses(classIter);
+            ResourceClassIterator<T> classes =
+                (new DiscoverClasses<T>(loaders)).findResourceClasses(classIter);
                 
             
             if (!classes.hasNext()  &&  defaultImpl != null) {
@@ -347,7 +343,7 @@ public class DiscoverClass {
             
             // Services we iterate through until we find one that loads..
             while (classes.hasNext()) {
-                ResourceClass info = classes.nextResourceClass();
+                ResourceClass<T> info = classes.nextResourceClass();
                 try {
                     return info.loadClass();
                 } catch (Exception e) {
@@ -372,7 +368,7 @@ public class DiscoverClass {
      *            instantiated, or if the resulting class does not implement
      *            (or extend) the SPI.
      */
-    public Object newInstance(Class spiClass)
+    public <T> T newInstance(Class<T> spiClass)
         throws DiscoveryException,
                InstantiationException,
                IllegalAccessException,
@@ -380,9 +376,9 @@ public class DiscoverClass {
                InvocationTargetException
     {
         return newInstance(getClassLoaders(spiClass),
-                           new SPInterface(spiClass),
+                           new SPInterface<T>(spiClass),
                            nullProperties,
-                           nullDefaultImpl);
+                           (DefaultClassHolder<T>) null);
     }
 
     /**
@@ -401,7 +397,7 @@ public class DiscoverClass {
      *            instantiated, or if the resulting class does not implement
      *            (or extend) the SPI.
      */
-    public Object newInstance(Class spiClass, Properties properties)
+    public <T> T newInstance(Class<T> spiClass, Properties properties)
         throws DiscoveryException,
                InstantiationException,
                IllegalAccessException,
@@ -409,9 +405,9 @@ public class DiscoverClass {
                InvocationTargetException
     {
         return newInstance(getClassLoaders(spiClass),
-                           new SPInterface(spiClass),
+                           new SPInterface<T>(spiClass),
                            new PropertiesHolder(properties),
-                           nullDefaultImpl);
+                           (DefaultClassHolder<T>) null);
     }
 
     /**
@@ -428,7 +424,7 @@ public class DiscoverClass {
      *            instantiated, or if the resulting class does not implement
      *            (or extend) the SPI.
      */
-    public Object newInstance(Class spiClass, String defaultImpl)
+    public <T> T newInstance(Class<T> spiClass, String defaultImpl)
         throws DiscoveryException,
                InstantiationException,
                IllegalAccessException,
@@ -436,9 +432,9 @@ public class DiscoverClass {
                InvocationTargetException
     {
         return newInstance(getClassLoaders(spiClass),
-                           new SPInterface(spiClass),
+                           new SPInterface<T>(spiClass),
                            nullProperties,
-                           new DefaultClassHolder(defaultImpl));
+                           new DefaultClassHolder<T>(defaultImpl));
     }
 
     /**
@@ -459,7 +455,7 @@ public class DiscoverClass {
      *            instantiated, or if the resulting class does not implement
      *            (or extend) the SPI.
      */
-    public Object newInstance(Class spiClass, Properties properties, String defaultImpl)
+    public <T> T newInstance(Class<T> spiClass, Properties properties, String
defaultImpl)
         throws DiscoveryException,
                InstantiationException,
                IllegalAccessException,
@@ -467,9 +463,9 @@ public class DiscoverClass {
                InvocationTargetException
     {
         return newInstance(getClassLoaders(spiClass),
-                           new SPInterface(spiClass),
+                           new SPInterface<T>(spiClass),
                            new PropertiesHolder(properties),
-                           new DefaultClassHolder(defaultImpl));
+                           new DefaultClassHolder<T>(defaultImpl));
     }
 
     /**
@@ -490,7 +486,7 @@ public class DiscoverClass {
      *            instantiated, or if the resulting class does not implement
      *            (or extend) the SPI.
      */
-    public Object newInstance(Class spiClass, String propertiesFileName, String defaultImpl)
+    public <T> T newInstance(Class<T> spiClass, String propertiesFileName, String
defaultImpl)
         throws DiscoveryException,
                InstantiationException,
                IllegalAccessException,
@@ -498,9 +494,9 @@ public class DiscoverClass {
                InvocationTargetException
     {
         return newInstance(getClassLoaders(spiClass),
-                           new SPInterface(spiClass),
+                           new SPInterface<T>(spiClass),
                            new PropertiesHolder(propertiesFileName),
-                           new DefaultClassHolder(defaultImpl));
+                           new DefaultClassHolder<T>(defaultImpl));
     }
 
     /**
@@ -521,10 +517,10 @@ public class DiscoverClass {
      *            instantiated, or if the resulting class does not implement
      *            (or extend) the SPI.
      */
-    public static Object newInstance(ClassLoaders loaders,
-                                     SPInterface spi,
+    public static <T> T newInstance(ClassLoaders loaders,
+                                     SPInterface<T> spi,
                                      PropertiesHolder properties,
-                                     DefaultClassHolder defaultImpl)
+                                     DefaultClassHolder<T> defaultImpl)
         throws DiscoveryException,
                InstantiationException,
                IllegalAccessException,
@@ -551,10 +547,10 @@ public class DiscoverClass {
      * @exception DiscoveryException Thrown if the name of a class implementing
      *            the SPI cannot be found.
      */
-    public static String[] discoverClassNames(SPInterface spi,
+    public static <T> String[] discoverClassNames(SPInterface<T> spi,
                                               Properties properties)
     {
-        Vector names = new Vector();
+        List<String> names = new LinkedList<String>();
         
         String spiName = spi.getSPName();
         String propertyName = spi.getPropertyName();
@@ -563,28 +559,28 @@ public class DiscoverClass {
         
         // Try the (managed) system property spiName
         String className = getManagedProperty(spiName);
-        if (className != null) names.addElement(className);
+        if (className != null) names.add(className);
         
         if (includeAltProperty) {
             // Try the (managed) system property propertyName
             className = getManagedProperty(propertyName);
-            if (className != null) names.addElement(className);
+            if (className != null) names.add(className);
         }
 
         if (properties != null) {
             // Try the properties parameter spiName
             className = properties.getProperty(spiName);
-            if (className != null) names.addElement(className);
+            if (className != null) names.add(className);
 
             if (includeAltProperty) {
                 // Try the properties parameter propertyName
                 className = properties.getProperty(propertyName);
-                if (className != null) names.addElement(className);
+                if (className != null) names.add(className);
             }
         }
 
         String[] results = new String[names.size()];
-        names.copyInto(results);        
+        names.toArray(results);
 
         return results;
     }

Modified: commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DiscoverSingleton.java
URL: http://svn.apache.org/viewvc/commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DiscoverSingleton.java?rev=1088081&r1=1088080&r2=1088081&view=diff
==============================================================================
--- commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DiscoverSingleton.java
(original)
+++ commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/DiscoverSingleton.java
Sat Apr  2 18:46:43 2011
@@ -17,6 +17,7 @@
 package org.apache.commons.discovery.tools;
 
 import java.util.HashMap;
+import java.util.Map;
 import java.util.Properties;
 
 import org.apache.commons.discovery.DiscoveryException;
@@ -221,13 +222,13 @@ public class DiscoverSingleton {
      *            instantiated, or if the resulting class does not implement
      *            (or extend) the SPI.
      */
-    public static Object find(Class spiClass)
+    public static <T> T find(Class<T> spiClass)
         throws DiscoveryException
     {
         return find(null,
-                    new SPInterface(spiClass),
+                    new SPInterface<T>(spiClass),
                     DiscoverClass.nullProperties,
-                    DiscoverClass.nullDefaultImpl);
+                    (DefaultClassHolder<T>) null);
     }
 
     /**
@@ -246,13 +247,13 @@ public class DiscoverSingleton {
      *            instantiated, or if the resulting class does not implement
      *            (or extend) the SPI.
      */
-    public static Object find(Class spiClass, Properties properties)
+    public static <T> T find(Class<T> spiClass, Properties properties)
         throws DiscoveryException
     {
         return find(null,
-                    new SPInterface(spiClass),
+                    new SPInterface<T>(spiClass),
                     new PropertiesHolder(properties),
-                    DiscoverClass.nullDefaultImpl);
+                    (DefaultClassHolder<T>) null);
     }
 
     /**
@@ -269,13 +270,13 @@ public class DiscoverSingleton {
      *            instantiated, or if the resulting class does not implement
      *            (or extend) the SPI.
      */
-    public static Object find(Class spiClass, String defaultImpl)
+    public static <T> T find(Class<T> spiClass, String defaultImpl)
         throws DiscoveryException
     {
         return find(null,
-                    new SPInterface(spiClass),
+                    new SPInterface<T>(spiClass),
                     DiscoverClass.nullProperties,
-                    new DefaultClassHolder(defaultImpl));
+                    new DefaultClassHolder<T>(defaultImpl));
     }
 
     /**
@@ -296,15 +297,15 @@ public class DiscoverSingleton {
      *            instantiated, or if the resulting class does not implement
      *            (or extend) the SPI.
      */
-    public static Object find(Class spiClass,
+    public static <T> T find(Class<T> spiClass,
                               Properties properties,
                               String defaultImpl)
         throws DiscoveryException
     {
         return find(null,
-                    new SPInterface(spiClass),
+                    new SPInterface<T>(spiClass),
                     new PropertiesHolder(properties),
-                    new DefaultClassHolder(defaultImpl));
+                    new DefaultClassHolder<T>(defaultImpl));
     }
 
     /**
@@ -325,15 +326,15 @@ public class DiscoverSingleton {
      *            instantiated, or if the resulting class does not implement
      *            (or extend) the SPI.
      */
-    public static Object find(Class spiClass,
+    public static <T> T find(Class<T> spiClass,
                               String propertiesFileName,
                               String defaultImpl)
         throws DiscoveryException
     {
         return find(null,
-                    new SPInterface(spiClass),
+                    new SPInterface<T>(spiClass),
                     new PropertiesHolder(propertiesFileName),
-                    new DefaultClassHolder(defaultImpl));
+                    new DefaultClassHolder<T>(defaultImpl));
     }
     
     /*************** FINDERS FOR USE IN FACTORY/HELPER METHODS ***************
@@ -358,15 +359,15 @@ public class DiscoverSingleton {
      *            instantiated, or if the resulting class does not implement
      *            (or extend) the SPI.
      */
-    public static Object find(ClassLoaders loaders,
-                              SPInterface spi,
+    public static <T> T find(ClassLoaders loaders,
+                              SPInterface<T> spi,
                               PropertiesHolder properties,
-                              DefaultClassHolder defaultImpl)
+                              DefaultClassHolder<T> defaultImpl)
         throws DiscoveryException
     {
         ClassLoader contextLoader = JDKHooks.getJDKHooks().getThreadContextClassLoader();
 
-        Object obj = get(contextLoader, spi.getSPName());
+        T obj = get(contextLoader, spi.getSPName());
 
         if (obj == null) {
             try {
@@ -408,8 +409,8 @@ public class DiscoverSingleton {
      * If the SPI instance implements <code>Service</code>, then call
      * <code>release()</code>.
      */
-    public static synchronized void release(Class spiClass) {
-        HashMap spis = (HashMap)EnvironmentCache.get(JDKHooks.getJDKHooks().getThreadContextClassLoader());
+    public static synchronized void release(Class<?> spiClass) {
+        Map<String, Object> spis = EnvironmentCache.get(JDKHooks.getJDKHooks().getThreadContextClassLoader());
         
         if (spis != null) {
             spis.remove(spiClass.getName());
@@ -454,14 +455,17 @@ public class DiscoverSingleton {
     /**
      * Get service keyed by spi & classLoader.
      */
-    private static synchronized Object get(ClassLoader classLoader,
+    private static synchronized <T> T get(ClassLoader classLoader,
                                            String spiName)
     {
-        HashMap spis = (HashMap)EnvironmentCache.get(classLoader);
+        Map<String, Object> spis = EnvironmentCache.get(classLoader);
         
-        return (spis != null)
-               ? spis.get(spiName)
-               : null;
+        if (spis != null) {
+            @SuppressWarnings("unchecked") // spiName is assignable from stored object class
+            T t = (T) spis.get(spiName);
+            return t;
+        }
+        return null;
     }
     
     /**
@@ -473,10 +477,10 @@ public class DiscoverSingleton {
     {
         if (service != null)
         {
-            HashMap spis = (HashMap)EnvironmentCache.get(classLoader);
+            Map<String, Object> spis = EnvironmentCache.get(classLoader);
             
             if (spis == null) {
-                spis = new HashMap(EnvironmentCache.smallHashSize);
+                spis = new HashMap<String, Object>(EnvironmentCache.smallHashSize);
                 EnvironmentCache.put(classLoader, spis);
             }
             

Modified: commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/SPInterface.java
URL: http://svn.apache.org/viewvc/commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/SPInterface.java?rev=1088081&r1=1088080&r2=1088081&view=diff
==============================================================================
--- commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/SPInterface.java
(original)
+++ commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/SPInterface.java
Sat Apr  2 18:46:43 2011
@@ -34,13 +34,13 @@ import org.apache.commons.discovery.Disc
  * 
  * @author Richard A. Sitze
  */
-public class SPInterface {
+public class SPInterface<T> {
     /**
      * The service programming interface: intended to be
      * an interface or abstract class, but not limited
      * to those two.
      */        
-    private final Class spi;
+    private final Class<T> spi;
     
     /**
      * The property name to be used for finding the name of
@@ -49,7 +49,7 @@ public class SPInterface {
     private final String propertyName;
     
     
-    private Class  paramClasses[] = null;
+    private Class<?>  paramClasses[] = null;
     private Object params[] = null;
 
 
@@ -58,7 +58,7 @@ public class SPInterface {
      * 
      * @param provider The SPI class
      */
-    public SPInterface(Class provider) {
+    public SPInterface(Class<T> provider) {
         this(provider, provider.getName());
     }
     
@@ -72,7 +72,7 @@ public class SPInterface {
      *        (system or other) properties having either the name of the class
      *        (provider) or the <code>propertyName</code>.
      */
-    public SPInterface(Class spi, String propertyName) {
+    public SPInterface(Class<T> spi, String propertyName) {
         this.spi = spi;
         this.propertyName = propertyName;
     }
@@ -88,8 +88,8 @@ public class SPInterface {
      * @param constructorParams objects representing the
      *        constructor arguments.
      */
-    public SPInterface(Class provider,
-                       Class constructorParamClasses[],
+    public SPInterface(Class<T> provider,
+                       Class<?> constructorParamClasses[],
                        Object constructorParams[])
     {
         this(provider,
@@ -114,9 +114,9 @@ public class SPInterface {
      * @param constructorParams objects representing the
      *        constructor arguments.
      */
-    public SPInterface(Class spi,
+    public SPInterface(Class<T> spi,
                        String propertyName,
-                       Class constructorParamClasses[],
+                       Class<?> constructorParamClasses[],
                        Object constructorParams[])
     {
         this.spi = spi;
@@ -129,7 +129,7 @@ public class SPInterface {
         return spi.getName();
     }
 
-    public Class getSPClass() {
+    public Class<T> getSPClass() {
         return spi;
     }
     
@@ -140,7 +140,7 @@ public class SPInterface {
     /**
      * Instantiate a new 
      */    
-    public Object newInstance(Class impl)
+    public <S extends T> S newInstance(Class<S> impl)
         throws DiscoveryException,
                InstantiationException,
                IllegalAccessException,
@@ -152,7 +152,7 @@ public class SPInterface {
         return ClassUtils.newInstance(impl, paramClasses, params);
     }
     
-    public void verifyAncestory(Class impl) {
+    public <S extends T> void verifyAncestory(Class<S> impl) {
         ClassUtils.verifyAncestory(spi, impl);
     }
 }

Modified: commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/Service.java
URL: http://svn.apache.org/viewvc/commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/Service.java?rev=1088081&r1=1088080&r2=1088081&view=diff
==============================================================================
--- commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/Service.java
(original)
+++ commons/proper/discovery/trunk/src/java/org/apache/commons/discovery/tools/Service.java
Sat Apr  2 18:46:43 2011
@@ -63,8 +63,8 @@ public class Service
      * 
      * @return Enumeration of class instances (<code>Object</code>)
      */
-    public static Enumeration providers(Class spiClass) {
-        return providers(new SPInterface(spiClass), null);
+    public static <T, S extends T> Enumeration<S> providers(Class<T> spiClass)
{
+        return providers(new SPInterface<T>(spiClass), null);
     }
     
     /**
@@ -74,7 +74,7 @@ public class Service
      * @param loaders loaders to use in search.
      *        If <code>null</code> then use ClassLoaders.getAppLoaders().
      */
-    public static Enumeration providers(final SPInterface spi,
+    public static <T, S extends T> Enumeration<S> providers(final SPInterface<T>
spi,
                                         ClassLoaders loaders)
     {
         if (loaders == null) {
@@ -86,11 +86,11 @@ public class Service
         ResourceNameIterator servicesIter =
             (new DiscoverServiceNames(loaders)).findResourceNames(spi.getSPName());
 
-        final ResourceClassIterator services =
-            (new DiscoverClasses(loaders)).findResourceClasses(servicesIter);
+        final ResourceClassIterator<T> services =
+            (new DiscoverClasses<T>(loaders)).findResourceClasses(servicesIter);
         
-        return new Enumeration() {
-            private Object object = null;
+        return new Enumeration<S>() {
+            private S object = null;
             
             public boolean hasMoreElements() {
                 if (object == null) {
@@ -99,15 +99,15 @@ public class Service
                 return object != null;
             }
             
-            public Object nextElement() {
-                Object obj = object;
+            public S nextElement() {
+                S obj = object;
                 object = null;
                 return obj;
             }
 
-            private Object getNextClassInstance() {
+            private S getNextClassInstance() {
                 while (services.hasNext()) {
-                    ResourceClass info = services.nextResourceClass();
+                    ResourceClass<S> info = services.nextResourceClass();
                     try {
                         return spi.newInstance(info.loadClass());
                     } catch (Exception e) {



Mime
View raw message