jkf 2005/05/23 14:52:35
Modified: src/main/org/apache/tools/ant/taskdefs/email EmailTask.java
src/main/org/apache/tools/ant Main.java
src/main/org/apache/tools/ant/taskdefs/optional/javah
JavahAdapterFactory.java
src/main/org/apache/tools/ant/listener MailLogger.java
src/main/org/apache/tools/ant/taskdefs/optional/script
ScriptDef.java
src/main/org/apache/tools/ant/util ClasspathUtils.java
src/main/org/apache/tools/ant/taskdefs/rmic
RmicAdapterFactory.java
src/main/org/apache/tools/ant/taskdefs/optional/native2ascii
Native2AsciiAdapterFactory.java
src/main/org/apache/tools/ant/util/regexp
RegexpMatcherFactory.java
src/main/org/apache/tools/ant/taskdefs/compilers
CompilerAdapterFactory.java
Log:
Improved the newInstance method in ClasspathUtils, and used this method from
the places where newInstances are made and Throwable is caught.
Revision Changes Path
1.31 +18 -11
ant/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java
Index: EmailTask.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -r1.30 -r1.31
--- EmailTask.java 2 Feb 2005 20:08:57 -0000 1.30
+++ EmailTask.java 23 May 2005 21:52:35 -0000 1.31
@@ -25,8 +25,10 @@
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
+import org.apache.tools.ant.listener.MailLogger;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.types.FileSet;
+import org.apache.tools.ant.util.ClasspathUtils;
/**
* A task to send SMTP email. This is a refactoring of the SendMail and
@@ -399,14 +401,16 @@
if (encoding.equals(MIME)
|| (encoding.equals(AUTO) && !autoFound)) {
try {
- mailer = (Mailer) Class.forName(
- "org.apache.tools.ant.taskdefs.email.MimeMailer")
- .newInstance();
+ mailer = (Mailer) ClasspathUtils.newInstance(
+ "org.apache.tools.ant.taskdefs.email.MimeMailer",
+ EmailTask.class.getClassLoader(), Mailer.class);
autoFound = true;
log("Using MIME mail", Project.MSG_VERBOSE);
- } catch (Throwable e) {
- log("Failed to initialise MIME mail: "
- + e.getMessage(), Project.MSG_WARN);
+ } catch (BuildException e) {
+ Throwable t = e.getCause() == null ? e : e.getCause();
+ log("Failed to initialise MIME mail: " + t.getMessage(),
+ Project.MSG_WARN);
+ return;
}
}
// SMTP auth only allowed with MIME mail
@@ -423,13 +427,16 @@
if (encoding.equals(UU)
|| (encoding.equals(AUTO) && !autoFound)) {
try {
- mailer =
- (Mailer)
Class.forName("org.apache.tools.ant.taskdefs.email.UUMailer")
- .newInstance();
+ mailer = (Mailer) ClasspathUtils.newInstance(
+ "org.apache.tools.ant.taskdefs.email.UUMailer",
+ EmailTask.class.getClassLoader(), Mailer.class);
autoFound = true;
log("Using UU mail", Project.MSG_VERBOSE);
- } catch (Throwable e) {
- log("Failed to initialise UU mail", Project.MSG_WARN);
+ } catch (BuildException e) {
+ Throwable t = e.getCause() == null ? e : e.getCause();
+ log("Failed to initialise UU mail: " + t.getMessage(),
+ Project.MSG_WARN);
+ return;
}
}
// try plain format
1.119 +17 -35 ant/src/main/org/apache/tools/ant/Main.java
Index: Main.java
===================================================================
RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/Main.java,v
retrieving revision 1.118
retrieving revision 1.119
diff -u -r1.118 -r1.119
--- Main.java 13 May 2005 21:57:23 -0000 1.118
+++ Main.java 23 May 2005 21:52:35 -0000 1.119
@@ -29,6 +29,7 @@
import org.apache.tools.ant.input.DefaultInputHandler;
import org.apache.tools.ant.input.InputHandler;
import org.apache.tools.ant.launch.AntMain;
+import org.apache.tools.ant.util.ClasspathUtils;
import org.apache.tools.ant.util.FileUtils;
@@ -698,17 +699,13 @@
for (int i = 0; i < listeners.size(); i++) {
String className = (String) listeners.elementAt(i);
- try {
- BuildListener listener =
- (BuildListener) Class.forName(className).newInstance();
- if (project != null) {
- project.setProjectReference(listener);
- }
- project.addBuildListener(listener);
- } catch (Throwable exc) {
- throw new BuildException("Unable to instantiate listener "
- + className, exc);
+ BuildListener listener =
+ (BuildListener) ClasspathUtils.newInstance(className,
+ Main.class.getClassLoader(),
BuildListener.class);
+ if (project != null) {
+ project.setProjectReference(listener);
}
+ project.addBuildListener(listener);
}
}
@@ -725,22 +722,11 @@
if (inputHandlerClassname == null) {
handler = new DefaultInputHandler();
} else {
- try {
- handler = (InputHandler)
- (Class.forName(inputHandlerClassname).newInstance());
- if (project != null) {
- project.setProjectReference(handler);
- }
- } catch (ClassCastException e) {
- String msg = "The specified input handler class "
- + inputHandlerClassname
- + " does not implement the InputHandler interface";
- throw new BuildException(msg);
- } catch (Exception e) {
- String msg = "Unable to instantiate specified input handler "
- + "class " + inputHandlerClassname + " : "
- + e.getClass().getName();
- throw new BuildException(msg);
+ handler = (InputHandler) ClasspathUtils.newInstance(
+ inputHandlerClassname, Main.class.getClassLoader(),
+ InputHandler.class);
+ if (project != null) {
+ project.setProjectReference(handler);
}
}
project.setInputHandler(handler);
@@ -760,17 +746,13 @@
BuildLogger logger = null;
if (loggerClassname != null) {
try {
- Class loggerClass = Class.forName(loggerClassname);
- logger = (BuildLogger) (loggerClass.newInstance());
- } catch (ClassCastException e) {
+ logger = (BuildLogger) ClasspathUtils.newInstance(
+ loggerClassname, Main.class.getClassLoader(),
+ BuildLogger.class);
+ } catch (BuildException e) {
System.err.println("The specified logger class "
+ loggerClassname
- + " does not implement the BuildLogger interface");
- throw new RuntimeException();
- } catch (Exception e) {
- System.err.println("Unable to instantiate specified logger "
- + "class " + loggerClassname + " : "
- + e.getClass().getName());
+ + " could not be used because " + e.getMessage());
throw new RuntimeException();
}
} else {
1.3 +4 -15
ant/src/main/org/apache/tools/ant/taskdefs/optional/javah/JavahAdapterFactory.java
Index: JavahAdapterFactory.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/javah/JavahAdapterFactory.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JavahAdapterFactory.java 7 Feb 2005 18:38:07 -0000 1.2
+++ JavahAdapterFactory.java 23 May 2005 21:52:35 -0000 1.3
@@ -18,6 +18,7 @@
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ProjectComponent;
+import org.apache.tools.ant.util.ClasspathUtils;
import org.apache.tools.ant.util.JavaEnvUtils;
/**
@@ -77,20 +78,8 @@
* isn't an instance of JavahAdapter.
*/
private static JavahAdapter resolveClassName(String className)
- throws BuildException {
- try {
- Class c = Class.forName(className);
- Object o = c.newInstance();
- return (JavahAdapter) o;
- } catch (ClassNotFoundException cnfe) {
- throw new BuildException("Can't load " + className, cnfe);
- } catch (ClassCastException cce) {
- throw new BuildException(className
- + " is not a Javah adapter", cce);
- } catch (Throwable t) {
- // for all other possibilities
- throw new BuildException(className + " caused an interesting "
- + "exception.", t);
- }
+ throws BuildException {
+ return (JavahAdapter) ClasspathUtils.newInstance(className,
+ JavahAdapterFactory.class.getClassLoader(),
JavahAdapter.class);
}
}
1.27 +11 -8
ant/src/main/org/apache/tools/ant/listener/MailLogger.java
Index: MailLogger.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/listener/MailLogger.java,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- MailLogger.java 9 Mar 2004 16:48:03 -0000 1.26
+++ MailLogger.java 23 May 2005 21:52:35 -0000 1.27
@@ -27,11 +27,13 @@
import java.util.StringTokenizer;
import org.apache.tools.ant.BuildEvent;
+import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.email.EmailAddress;
import org.apache.tools.ant.taskdefs.email.Message;
import org.apache.tools.ant.taskdefs.email.Mailer;
+import org.apache.tools.ant.util.ClasspathUtils;
import org.apache.tools.ant.util.DateUtils;
import org.apache.tools.ant.util.StringUtils;
import org.apache.tools.mail.MailMessage;
@@ -239,14 +241,15 @@
String message) {
// convert the replyTo string into a vector of emailaddresses
Mailer mailer = null;
- try {
- mailer =
- (Mailer)
Class.forName("org.apache.tools.ant.taskdefs.email.MimeMailer")
- .newInstance();
- } catch (Throwable e) {
- log("Failed to initialise MIME mail: " + e.getMessage());
- return;
- }
+ try {
+ mailer = (Mailer) ClasspathUtils.newInstance(
+ "org.apache.tools.ant.taskdefs.email.MimeMailer",
+ MailLogger.class.getClassLoader(), Mailer.class);
+ } catch (BuildException e) {
+ Throwable t = e.getCause() == null ? e : e.getCause();
+ log("Failed to initialise MIME mail: " + t.getMessage());
+ return;
+ }
Vector replyToList = vectorizeEmailAddresses(replyToString);
mailer.setHost(host);
mailer.setPort(port);
1.19 +6 -19
ant/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java
Index: ScriptDef.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- ScriptDef.java 17 May 2005 10:45:29 -0000 1.18
+++ ScriptDef.java 23 May 2005 21:52:35 -0000 1.19
@@ -34,6 +34,7 @@
import java.util.HashSet;
import java.io.File;
+import org.apache.tools.ant.util.ClasspathUtils;
import org.apache.tools.ant.util.ScriptRunner;
/**
@@ -272,27 +273,13 @@
*/
ClassLoader loader = createLoader();
- Class instanceClass = null;
- try {
- instanceClass = Class.forName(classname, true, loader);
- } catch (Throwable e) {
- // try normal method
- try {
- instanceClass = Class.forName(classname);
- } catch (Throwable e2) {
- throw new BuildException("scriptdef: Unable to load "
- + "class " + classname + " for nested element <"
- + elementName + ">", e2);
- }
+ try
+ {
+ instance = ClasspathUtils.newInstance(classname, loader);
+ } catch (BuildException e) {
+ instance = ClasspathUtils.newInstance(classname,
ScriptDef.class.getClassLoader());
}
- try {
- instance = instanceClass.newInstance();
- } catch (Throwable e) {
- throw new BuildException("scriptdef: Unable to create "
- + "element of class " + classname + " for nested "
- + "element <" + elementName + ">", e);
- }
getProject().setProjectReference(instance);
}
1.16 +60 -5
ant/src/main/org/apache/tools/ant/util/ClasspathUtils.java
Index: ClasspathUtils.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/util/ClasspathUtils.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- ClasspathUtils.java 20 May 2005 22:51:37 -0000 1.15
+++ ClasspathUtils.java 23 May 2005 21:52:35 -0000 1.16
@@ -236,17 +236,49 @@
* @throws BuildException when loading or instantiation failed.
*/
public static Object newInstance(
+ String className,
+ ClassLoader userDefinedLoader) {
+ return newInstance(className, userDefinedLoader, Object.class);
+ }
+
+
+
+ /**
+ * Creates a fresh object instance of the specified classname.
+ *
+ * <p> This uses the userDefinedLoader to load the specified class,
+ * and then makes an instance using the default no-argument constructor.
+ * </p>
+ *
+ * @param className the full qualified class name to load.
+ * @param userDefinedLoader the classloader to use.
+ * @param expectedType the Class that the result should be assignment
+ * compatible with. (No ClassCastException will be thrown in case
+ * the result of this method is casted to the expectedType)
+ * @return The fresh object instance
+ * @throws BuildException when loading or instantiation failed.
+ * @since Ant 1.7
+ */
+ public static Object newInstance(
String className,
- ClassLoader userDefinedLoader) {
+ ClassLoader userDefinedLoader,
+ Class expectedType) {
try {
- Class clazz = userDefinedLoader.loadClass(className);
+ Class clazz = Class.forName(className, true, userDefinedLoader);
Object o = clazz.newInstance();
+ if (!expectedType.isInstance(o))
+ {
+ throw new BuildException(
+ "Class of unexpected Type: "
+ + className
+ + " expected :"
+ + expectedType);
+ }
return o;
} catch (ClassNotFoundException e) {
throw new BuildException(
- "Class "
- + className
- + " not found by the specific classLoader.",
+ "Class not found: "
+ + className,
e);
} catch (InstantiationException e) {
throw new BuildException(
@@ -262,10 +294,33 @@
+ ". Specified class should have a "
+ "public constructor.",
e);
+ } catch (LinkageError e) {
+ throw new BuildException(
+ "Class "
+ + className
+ + " could not be loaded because of an invalid
dependency.",
+ e);
}
}
/**
+ * Creates a fresh object instance of the specified classname.
+ *
+ * <p> This uses the userDefinedLoader to load the specified class,
+ * and then makes an instance using the default no-argument constructor.
+ * </p>
+ *
+ * @param className the full qualified class name to load.
+ * @param userDefinedLoader the classloader to use.
+ * @param expectedType the Class that the result should be assignment
+ * compatible with. (No ClassCastException will be thrown in case
+ * the result of this method is casted to the expectedType)
+ * @return The fresh object instance
+ * @throws BuildException when loading or instantiation failed.
+ */
+
+
+ /**
* Obtains a delegate that helps out with classic classpath
configuration.
*
* @param component your projectComponent that needs the assistence
1.18 +5 -17
ant/src/main/org/apache/tools/ant/taskdefs/rmic/RmicAdapterFactory.java
Index: RmicAdapterFactory.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/rmic/RmicAdapterFactory.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- RmicAdapterFactory.java 14 Mar 2005 18:36:44 -0000 1.17
+++ RmicAdapterFactory.java 23 May 2005 21:52:35 -0000 1.18
@@ -19,6 +19,7 @@
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
+import org.apache.tools.ant.util.ClasspathUtils;
/**
@@ -28,10 +29,10 @@
*/
public final class RmicAdapterFactory {
/** The error message to be used when the compiler cannot be found. */
- public static final String ERROR_UNKNOWN_COMPILER = "Cannot find the
compiler or class: ";
+ public static final String ERROR_UNKNOWN_COMPILER = "Class not found: ";
/** The error message to be used when the class is not an rmic adapter.
*/
- public static final String ERROR_NOT_RMIC_ADAPTER = "Not an rmic
adapter: ";
+ public static final String ERROR_NOT_RMIC_ADAPTER = "Class of unexpected
Type: ";
/** If the compiler has this name use a default compiler. */
public static final String DEFAULT_COMPILER = "default";
@@ -93,20 +94,7 @@
*/
private static RmicAdapter resolveClassName(String className)
throws BuildException {
- try {
- Class c = Class.forName(className);
- Object o = c.newInstance();
- return (RmicAdapter) o;
- } catch (ClassNotFoundException cnfe) {
- throw new BuildException(ERROR_UNKNOWN_COMPILER + className,
- cnfe);
- } catch (ClassCastException cce) {
- throw new BuildException(ERROR_NOT_RMIC_ADAPTER + className,
- cce);
- } catch (Throwable t) {
- // for all other possibilities
- throw new BuildException(className + " caused an interesting "
- + "exception.", t);
- }
+ return (RmicAdapter) ClasspathUtils.newInstance(className,
+ RmicAdapterFactory.class.getClassLoader(), RmicAdapter.class);
}
}
1.2 +6 -15
ant/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/Native2AsciiAdapterFactory.java
Index: Native2AsciiAdapterFactory.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/Native2AsciiAdapterFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Native2AsciiAdapterFactory.java 31 Jan 2005 11:48:25 -0000 1.1
+++ Native2AsciiAdapterFactory.java 23 May 2005 21:52:35 -0000 1.2
@@ -18,6 +18,7 @@
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ProjectComponent;
+import org.apache.tools.ant.util.ClasspathUtils;
import org.apache.tools.ant.util.JavaEnvUtils;
/**
@@ -77,20 +78,10 @@
* isn't an instance of Native2AsciiAdapter.
*/
private static Native2AsciiAdapter resolveClassName(String className)
- throws BuildException {
- try {
- Class c = Class.forName(className);
- Object o = c.newInstance();
- return (Native2AsciiAdapter) o;
- } catch (ClassNotFoundException cnfe) {
- throw new BuildException("Can't load " + className, cnfe);
- } catch (ClassCastException cce) {
- throw new BuildException(className
- + " is not a Native2Ascii adapter",
cce);
- } catch (Throwable t) {
- // for all other possibilities
- throw new BuildException(className + " caused an interesting "
- + "exception.", t);
- }
+ throws BuildException
+ {
+ return (Native2AsciiAdapter) ClasspathUtils.newInstance(className,
+ Native2AsciiAdapterFactory.class.getClassLoader(),
+ Native2AsciiAdapter.class);
}
}
\ No newline at end of file
1.18 +5 -7
ant/src/main/org/apache/tools/ant/util/regexp/RegexpMatcherFactory.java
Index: RegexpMatcherFactory.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/util/regexp/RegexpMatcherFactory.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- RegexpMatcherFactory.java 17 Dec 2004 19:46:04 -0000 1.17
+++ RegexpMatcherFactory.java 23 May 2005 21:52:35 -0000 1.18
@@ -19,6 +19,7 @@
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
+import org.apache.tools.ant.util.ClasspathUtils;
/**
* Simple Factory Class that produces an implementation of
@@ -99,13 +100,10 @@
* @exception BuildException if an error occurs
*/
protected RegexpMatcher createInstance(String className)
- throws BuildException {
- try {
- Class implClass = Class.forName(className);
- return (RegexpMatcher) implClass.newInstance();
- } catch (Throwable t) {
- throw new BuildException(t);
- }
+ throws BuildException
+ {
+ return (RegexpMatcher) ClasspathUtils.newInstance(className,
+ RegexpMatcherFactory.class.getClassLoader(), RegexpMatcher
.class);
}
/**
1.31 +4 -15
ant/src/main/org/apache/tools/ant/taskdefs/compilers/CompilerAdapterFactory.java
Index: CompilerAdapterFactory.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/compilers/CompilerAdapterFactory.java,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -r1.30 -r1.31
--- CompilerAdapterFactory.java 13 May 2005 16:58:55 -0000 1.30
+++ CompilerAdapterFactory.java 23 May 2005 21:52:35 -0000 1.31
@@ -20,6 +20,7 @@
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
+import org.apache.tools.ant.util.ClasspathUtils;
import org.apache.tools.ant.util.JavaEnvUtils;
/**
@@ -162,21 +163,9 @@
*/
private static CompilerAdapter resolveClassName(String className)
throws BuildException {
- try {
- Class c = Class.forName(className);
- Object o = c.newInstance();
- return (CompilerAdapter) o;
- } catch (ClassNotFoundException cnfe) {
- throw new BuildException("Compiler Adapter '" + className
- + "' can\'t be found.", cnfe);
- } catch (ClassCastException cce) {
- throw new BuildException(className + " isn\'t the classname of "
- + "a compiler adapter.", cce);
- } catch (Throwable t) {
- // for all other possibilities
- throw new BuildException("Compiler Adapter " + className
- + " caused an interesting exception.", t);
- }
+ return (CompilerAdapter) ClasspathUtils.newInstance(className,
+ CompilerAdapterFactory.class.getClassLoader(),
+ CompilerAdapter.class);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]