Browse Source

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.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274352 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
161579a6d0
5 changed files with 193 additions and 59 deletions
  1. +4
    -18
      src/main/org/apache/tools/ant/Diagnostics.java
  2. +22
    -19
      src/main/org/apache/tools/ant/taskdefs/optional/ANTLR.java
  3. +22
    -21
      src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
  4. +66
    -1
      src/main/org/apache/tools/ant/util/LoaderUtils.java
  5. +79
    -0
      src/testcases/org/apache/tools/ant/util/LoaderUtilsTest.java

+ 4
- 18
src/main/org/apache/tools/ant/Diagnostics.java View File

@@ -1,7 +1,7 @@
/* /*
* The Apache Software License, Version 1.1 * 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. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -53,6 +53,7 @@
*/ */
package org.apache.tools.ant; package org.apache.tools.ant;


import org.apache.tools.ant.util.LoaderUtils;


import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
@@ -230,23 +231,8 @@ public final class Diagnostics {
*/ */


private static String getClassLocation( Class clazz) { 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();
} }






+ 22
- 19
src/main/org/apache/tools/ant/taskdefs/optional/ANTLR.java View File

@@ -68,6 +68,7 @@ import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.CommandlineJava; import org.apache.tools.ant.types.CommandlineJava;
import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.JavaEnvUtils; import org.apache.tools.ant.util.JavaEnvUtils;
import org.apache.tools.ant.util.LoaderUtils;


/** /**
* Invokes the ANTLR Translator generator on a grammar file. * Invokes the ANTLR Translator generator on a grammar file.
@@ -239,25 +240,27 @@ public class ANTLR extends Task {
* getResource doesn't contain the name of the archive.</p> * getResource doesn't contain the name of the archive.</p>
*/ */
protected void addClasspathEntry(String resource) { 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 { } else {
log("Couldn\'t find " + resource, Project.MSG_DEBUG); log("Couldn\'t find " + resource, Project.MSG_DEBUG);
} }


+ 22
- 21
src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java View File

@@ -77,6 +77,7 @@ import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.types.Environment; import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.LoaderUtils;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestResult; import junit.framework.TestResult;
@@ -965,27 +966,27 @@ public class JUnitTask extends Task {
* @since Ant 1.4 * @since Ant 1.4
*/ */
protected void addClasspathEntry(String resource) { 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 { } else {
log("Couldn\'t find " + resource, Project.MSG_DEBUG); log("Couldn\'t find " + resource, Project.MSG_DEBUG);
} }


+ 66
- 1
src/main/org/apache/tools/ant/util/LoaderUtils.java View File

@@ -1,7 +1,7 @@
/* /*
* The Apache Software License, Version 1.1 * 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. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -53,8 +53,10 @@
*/ */
package org.apache.tools.ant.util; package org.apache.tools.ant.util;


import java.io.File;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URL;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;


/** /**
@@ -144,5 +146,68 @@ public class LoaderUtils {
return getContextClassLoader != null && return getContextClassLoader != null &&
setContextClassLoader != 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;

} }



+ 79
- 0
src/testcases/org/apache/tools/ant/util/LoaderUtilsTest.java View File

@@ -0,0 +1,79 @@
/*
* 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 apache@apache.org.
*
* 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());
}

}

Loading…
Cancel
Save