bodewig 2003/03/31 05:46:18
Modified: src/main/org/apache/tools/ant Diagnostics.java
src/main/org/apache/tools/ant/taskdefs/optional ANTLR.java
src/main/org/apache/tools/ant/taskdefs/optional/junit
JUnitTask.java
src/main/org/apache/tools/ant/util LoaderUtils.java
Added: src/testcases/org/apache/tools/ant/util LoaderUtilsTest.java
Log:
Move "where has this class been loaded from?" logic to LoaderUtils.
Use it in <junit>, <antlr> and Diagnostics.
As side effects:
* fix PR: 15131
* make Diagnostics compile using JDK 1.1
this doesn't mean CVS HEAD will build on JDK 1.1, still to fix are
Sync, SQLExec and EscapeUnicode.
Revision Changes Path
1.8 +4 -18 ant/src/main/org/apache/tools/ant/Diagnostics.java
Index: Diagnostics.java
===================================================================
RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/Diagnostics.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- Diagnostics.java 10 Feb 2003 14:13:30 -0000 1.7
+++ Diagnostics.java 31 Mar 2003 13:46:18 -0000 1.8
@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2002 The Apache Software Foundation. All rights
+ * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -53,6 +53,7 @@
*/
package org.apache.tools.ant;
+import org.apache.tools.ant.util.LoaderUtils;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
@@ -230,23 +231,8 @@
*/
private static String getClassLocation( Class clazz) {
- try {
- java.net.URL url =
clazz.getProtectionDomain().getCodeSource().getLocation();
- String location = url.toString();
- if (location.startsWith("jar")) {
- url = ((java.net.JarURLConnection)
url.openConnection()).getJarFileURL();
- location = url.toString();
- }
-
- if (location.startsWith("file")) {
- java.io.File file = new java.io.File(url.getFile());
- return file.getAbsolutePath();
- } else {
- return url.toString();
- }
- } catch (Throwable t) {
- }
- return null;
+ File f = LoaderUtils.getClassSource(clazz);
+ return f == null ? null : f.getAbsolutePath();
}
1.20 +22 -19
ant/src/main/org/apache/tools/ant/taskdefs/optional/ANTLR.java
Index: ANTLR.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/ANTLR.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- ANTLR.java 10 Feb 2003 14:13:45 -0000 1.19
+++ ANTLR.java 31 Mar 2003 13:46:18 -0000 1.20
@@ -68,6 +68,7 @@
import org.apache.tools.ant.types.CommandlineJava;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.JavaEnvUtils;
+import org.apache.tools.ant.util.LoaderUtils;
/**
* Invokes the ANTLR Translator generator on a grammar file.
@@ -239,25 +240,27 @@
* getResource doesn't contain the name of the archive.</p>
*/
protected void addClasspathEntry(String resource) {
- URL url = getClass().getResource(resource);
- if (url != null) {
- String u = url.toString();
- if (u.startsWith("jar:file:")) {
- int pling = u.indexOf("!");
- String jarName = u.substring(9, pling);
- log("Implicitly adding " + jarName + " to classpath",
- Project.MSG_DEBUG);
- createClasspath().setLocation(new File((new
File(jarName)).getAbsolutePath()));
- } else if (u.startsWith("file:")) {
- int tail = u.indexOf(resource);
- String dirName = u.substring(5, tail);
- log("Implicitly adding " + dirName + " to classpath",
- Project.MSG_DEBUG);
- createClasspath().setLocation(new File((new
File(dirName)).getAbsolutePath()));
- } else {
- log("Don\'t know how to handle resource URL " + u,
- Project.MSG_DEBUG);
- }
+ /*
+ * pre Ant 1.6 this method used to call getClass().getResource
+ * while Ant 1.6 will call ClassLoader.getResource().
+ *
+ * The difference is that Class.getResource expects a leading
+ * slash for "absolute" resources and will strip it before
+ * delegating to ClassLoader.getResource - so we now have to
+ * emulate Class's behavior.
+ */
+ if (resource.startsWith("/")) {
+ resource = resource.substring(1);
+ } else {
+ resource = "org/apache/tools/ant/taskdefs/optional/"
+ + resource;
+ }
+
+ File f = LoaderUtils.getResourceSource(getClass().getClassLoader(),
+ resource);
+ if (f != null) {
+ log("Found " + f.getAbsolutePath(), Project.MSG_DEBUG);
+ createClasspath().setLocation(f);
} else {
log("Couldn\'t find " + resource, Project.MSG_DEBUG);
}
1.60 +23 -22
ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
Index: JUnitTask.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java,v
retrieving revision 1.59
retrieving revision 1.60
diff -u -r1.59 -r1.60
--- JUnitTask.java 31 Mar 2003 12:27:14 -0000 1.59
+++ JUnitTask.java 31 Mar 2003 13:46:18 -0000 1.60
@@ -77,6 +77,7 @@
import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.FileUtils;
+import org.apache.tools.ant.util.LoaderUtils;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestResult;
@@ -965,27 +966,27 @@
* @since Ant 1.4
*/
protected void addClasspathEntry(String resource) {
- URL url = getClass().getResource(resource);
- if (url != null) {
- String u = url.toString();
- if (u.startsWith("jar:file:")) {
- int pling = u.indexOf("!");
- String jarName = u.substring(9, pling);
- log("Found " + jarName, Project.MSG_DEBUG);
- antRuntimeClasses.createPath()
- .setLocation(new File((new File(jarName))
- .getAbsolutePath()));
- } else if (u.startsWith("file:")) {
- int tail = u.indexOf(resource);
- String dirName = u.substring(5, tail);
- log("Found " + dirName, Project.MSG_DEBUG);
- antRuntimeClasses.createPath()
- .setLocation(new File((new File(dirName))
- .getAbsolutePath()));
- } else {
- log("Don\'t know how to handle resource URL " + u,
- Project.MSG_DEBUG);
- }
+ /*
+ * pre Ant 1.6 this method used to call getClass().getResource
+ * while Ant 1.6 will call ClassLoader.getResource().
+ *
+ * The difference is that Class.getResource expects a leading
+ * slash for "absolute" resources and will strip it before
+ * delegating to ClassLoader.getResource - so we now have to
+ * emulate Class's behavior.
+ */
+ if (resource.startsWith("/")) {
+ resource = resource.substring(1);
+ } else {
+ resource = "org/apache/tools/ant/taskdefs/optional/junit/"
+ + resource;
+ }
+
+ File f = LoaderUtils.getResourceSource(getClass().getClassLoader(),
+ resource);
+ if (f != null) {
+ log("Found " + f.getAbsolutePath(), Project.MSG_DEBUG);
+ antRuntimeClasses.createPath().setLocation(f);
} else {
log("Couldn\'t find " + resource, Project.MSG_DEBUG);
}
1.7 +66 -1 ant/src/main/org/apache/tools/ant/util/LoaderUtils.java
Index: LoaderUtils.java
===================================================================
RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/util/LoaderUtils.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- LoaderUtils.java 10 Feb 2003 14:14:38 -0000 1.6
+++ LoaderUtils.java 31 Mar 2003 13:46:18 -0000 1.7
@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2002 The Apache Software Foundation. All rights
+ * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -53,8 +53,10 @@
*/
package org.apache.tools.ant.util;
+import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.net.URL;
import org.apache.tools.ant.BuildException;
/**
@@ -144,5 +146,68 @@
return getContextClassLoader != null &&
setContextClassLoader != null;
}
+
+ /**
+ * Find the directory or jar file the class has been loaded from.
+ *
+ * @return null if we cannot determine the location.
+ *
+ * @since Ant 1.6
+ */
+ public static File getClassSource(Class c) {
+ String classFile = c.getName().replace('.', '/') + ".class";
+ return getResourceSource(c.getClassLoader(), classFile);
+ }
+
+ /**
+ * Find the directory or a give resource has been loaded from.
+ *
+ * @return null if we cannot determine the location.
+ *
+ * @since Ant 1.6
+ */
+ public static File getResourceSource(ClassLoader c, String resource) {
+ FileUtils fileUtils = FileUtils.newFileUtils();
+ if (c == null) {
+ c = LoaderUtils.class.getClassLoader();
+ }
+
+ URL url = c.getResource(resource);
+ if (url != null) {
+ String u = url.toString();
+ if (u.startsWith("jar:file:")) {
+ int pling = u.indexOf("!");
+ String jarName = u.substring(4, pling);
+ return new File(fileUtils.fromURI(jarName));
+ } else if (u.startsWith("file:")) {
+ int tail = u.indexOf(resource);
+ String dirName = u.substring(0, tail);
+ return new File(fileUtils.fromURI(dirName));
+ }
+ }
+ return null;
+ }
+
+ // if we want to drop JDK 1.1, here is code that does something similar
+ // - stolen from Diagnostics, stolen from Axis, stolen from somewhere
else
+ //
+ // try {
+ // java.net.URL url =
clazz.getProtectionDomain().getCodeSource().getLocation();
+ // String location = url.toString();
+ // if (location.startsWith("jar")) {
+ // url = ((java.net.JarURLConnection)
url.openConnection()).getJarFileURL();
+ // location = url.toString();
+ // }
+ //
+ // if (location.startsWith("file")) {
+ // java.io.File file = new java.io.File(url.getFile());
+ // return file.getAbsolutePath();
+ // } else {
+ // return url.toString();
+ // }
+ // } catch (Throwable t) {
+ // }
+ // return null;
+
}
1.1
ant/src/testcases/org/apache/tools/ant/util/LoaderUtilsTest.java
Index: LoaderUtilsTest.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "Ant" and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.tools.ant.util;
import java.io.File;
import junit.framework.TestCase;
/**
* @since Ant 1.6
*/
public class LoaderUtilsTest extends TestCase {
public LoaderUtilsTest(String name) {
super(name);
}
public void testGetXyzSource() {
File f1 = LoaderUtils.getClassSource(LoaderUtils.class);
assertNotNull(f1);
File f2 = LoaderUtils.getResourceSource(null,
"org/apache/tools/ant/taskdefs/defaults.properties");
assertNotNull(f2);
assertEquals(f1.getAbsolutePath(), f2.getAbsolutePath());
}
}