From b0eca5e4bbae7c254ba66eac900378c25c585fdd Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 28 Jul 2009 04:42:28 +0000 Subject: [PATCH] print where core and optional classes have been found. PR 47574. Make check for optional classes work again after the test task has been removed. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@798399 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/Diagnostics.java | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/main/org/apache/tools/ant/Diagnostics.java b/src/main/org/apache/tools/ant/Diagnostics.java index 4fdf00994..35ef35f5d 100644 --- a/src/main/org/apache/tools/ant/Diagnostics.java +++ b/src/main/org/apache/tools/ant/Diagnostics.java @@ -36,6 +36,7 @@ import java.io.InputStream; import java.io.IOException; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.net.URL; import java.util.Enumeration; import java.util.Properties; import java.util.Calendar; @@ -71,7 +72,7 @@ public final class Diagnostics { private static final int SECONDS_PER_MILLISECOND = 1000; private static final int SECONDS_PER_MINUTE = 60; private static final int MINUTES_PER_HOUR = 60; - private static final String TEST_CLASS = "org.apache.tools.ant.taskdefs.optional.Test"; + private static final String TEST_CLASS = "org.apache.tools.ant.taskdefs.optional.EchoProperties"; /** * The error text when a security manager blocks access to a property. @@ -112,8 +113,11 @@ public final class Diagnostics { if (coreVersion != null && !coreVersion.equals(optionalVersion)) { throw new BuildException("Invalid implementation version " - + "between Ant core and Ant optional tasks.\n" + " core : " - + coreVersion + "\n" + " optional: " + optionalVersion); + + "between Ant core and Ant optional tasks.\n" + + " core : " + coreVersion + " in " + + getClassLocation(Main.class) + + "\n" + " optional: " + optionalVersion + " in " + + getClassLocation(optional)); } } catch (ClassNotFoundException e) { // ignore @@ -170,6 +174,15 @@ public final class Diagnostics { return clazz.getPackage().getImplementationVersion(); } + /** + * Helper method to get the location. + * @param clazz the class to get the information from. + * @since Ant 1.8.0 + */ + private static URL getClassLocation(Class clazz) { + return clazz.getProtectionDomain().getCodeSource().getLocation(); + } + /** * what parser are we using. * @return the classname of the parser @@ -245,8 +258,8 @@ public final class Diagnostics { if (saxParser == null) { return null; } - String location = getClassLocation(saxParser.getClass()); - return location; + URL location = getClassLocation(saxParser.getClass()); + return location != null ? location.toString() : null; } private static String getNamespaceParserName() { @@ -263,7 +276,8 @@ public final class Diagnostics { private static String getNamespaceParserLocation() { try { XMLReader reader = JAXPUtils.getNamespaceXMLReader(); - return getClassLocation(reader.getClass()); + URL location = getClassLocation(reader.getClass()); + return location != null ? location.toString() : null; } catch (BuildException e) { //ignore ignoreThrowable(e); @@ -280,8 +294,8 @@ public final class Diagnostics { if (transformer == null) { return null; } - String location = getClassLocation(transformer.getClass()); - return location; + URL location = getClassLocation(transformer.getClass()); + return location != null ? location.toString() : null; } /** @@ -292,17 +306,6 @@ public final class Diagnostics { private static void ignoreThrowable(Throwable thrown) { } - /** - * get the location of a class. Stolen from axis/webapps/happyaxis.jsp - * @param clazz - * @return the jar file or path where a class was found, or null - */ - - private static String getClassLocation(Class clazz) { - File f = LoaderUtils.getClassSource(clazz); - return f == null ? null : f.getAbsolutePath(); - } - /** * Print a report to the given stream. @@ -323,12 +326,14 @@ public final class Diagnostics { out.println(Main.getAntVersion()); header(out, "Implementation Version"); - out.println("core tasks : " + getImplementationVersion(Main.class)); + out.println("core tasks : " + getImplementationVersion(Main.class) + + " in " + getClassLocation(Main.class)); Class optional = null; try { optional = Class.forName(TEST_CLASS); - out.println("optional tasks : " + getImplementationVersion(optional)); + out.println("optional tasks : " + getImplementationVersion(optional) + + " in " + getClassLocation(optional)); } catch (ClassNotFoundException e) { ignoreThrowable(e); out.println("optional tasks : not available");