You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rs...@apache.org on 2002/10/08 23:58:14 UTC
cvs commit: jakarta-commons/discovery/src/java/org/apache/commons/discovery/tools ClassUtils.java SPInterface.java
rsitze 2002/10/08 14:58:14
Modified: discovery/src/java/org/apache/commons/discovery/tools
ClassUtils.java SPInterface.java
Log:
Minor refactoring
New resource name discovery helpers
Revision Changes Path
1.3 +43 -0 jakarta-commons/discovery/src/java/org/apache/commons/discovery/tools/ClassUtils.java
Index: ClassUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/tools/ClassUtils.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ClassUtils.java 3 Oct 2002 22:31:52 -0000 1.2
+++ ClassUtils.java 8 Oct 2002 21:58:14 -0000 1.3
@@ -61,9 +61,13 @@
package org.apache.commons.discovery.tools;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import org.apache.commons.discovery.DiscoveryException;
+
import org.apache.commons.discovery.log.DiscoveryLogFactory;
import org.apache.commons.logging.Log;
@@ -137,5 +141,44 @@
}
return method;
+ }
+
+ /**
+ * Instantiate a new
+ */
+ public static Object newInstance(Class impl, Class paramClasses[], Object params[])
+ throws DiscoveryException,
+ InstantiationException,
+ IllegalAccessException,
+ NoSuchMethodException,
+ InvocationTargetException
+ {
+ if (paramClasses == null || params == null) {
+ return impl.newInstance();
+ } else {
+ Constructor constructor = impl.getConstructor(paramClasses);
+ return constructor.newInstance(params);
+ }
+ }
+
+ /**
+ * Throws exception if <code>impl</code> does not
+ * implement or extend the SPI.
+ */
+ public static void verifyAncestory(Class spi, Class impl)
+ throws DiscoveryException
+ {
+ if (spi == null) {
+ throw new DiscoveryException("No interface defined!");
+ }
+
+ if (impl == null) {
+ throw new DiscoveryException("No implementation defined for " + spi.getName());
+ }
+
+ if (!spi.isAssignableFrom(impl)) {
+ throw new DiscoveryException("Class " + impl.getName() +
+ " does not implement " + spi.getName());
+ }
}
}
1.2 +4 -20 jakarta-commons/discovery/src/java/org/apache/commons/discovery/tools/SPInterface.java
Index: SPInterface.java
===================================================================
RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/tools/SPInterface.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SPInterface.java 23 Aug 2002 21:56:10 -0000 1.1
+++ SPInterface.java 8 Oct 2002 21:58:14 -0000 1.2
@@ -193,28 +193,12 @@
NoSuchMethodException,
InvocationTargetException
{
- if (impl == null) {
- throw new DiscoveryException("No implementation defined for " + getSPName());
- }
-
- verifyAncestory(impl);
-
- if (paramClasses == null || params == null) {
- return impl.newInstance();
- } else {
- Constructor constructor = impl.getConstructor(paramClasses);
- return constructor.newInstance(params);
- }
+ verifyAncestory(impl);
+
+ return ClassUtils.newInstance(impl, paramClasses, params);
}
- /**
- * Throws exception if <code>impl</code> does not
- * implement or extend the SPI.
- */
public void verifyAncestory(Class impl) {
- if (!getSPClass().isAssignableFrom(impl)) {
- throw new DiscoveryException("Class " + impl.getName() +
- " does not implement " + getSPName());
- }
+ ClassUtils.verifyAncestory(spi, impl);
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>