diff --git a/src/main/org/apache/tools/ant/AntClassLoader.java b/src/main/org/apache/tools/ant/AntClassLoader.java index 103b118e7..e6e648b65 100644 --- a/src/main/org/apache/tools/ant/AntClassLoader.java +++ b/src/main/org/apache/tools/ant/AntClassLoader.java @@ -18,6 +18,7 @@ package org.apache.tools.ant; import java.io.ByteArrayOutputStream; +import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -48,7 +49,6 @@ import org.apache.tools.ant.util.CollectionUtils; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.JavaEnvUtils; import org.apache.tools.ant.util.LoaderUtils; -import org.apache.tools.ant.util.ReflectUtil; import org.apache.tools.ant.util.VectorSet; import org.apache.tools.zip.ZipLong; @@ -69,7 +69,7 @@ import org.apache.tools.zip.ZipLong; *

* */ -public class AntClassLoader extends ClassLoader implements SubBuildListener { +public class AntClassLoader extends ClassLoader implements SubBuildListener, Closeable { private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); @@ -1539,20 +1539,15 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener { return "AntClassLoader[" + getClasspath() + "]"; } - private static Class subClassToLoad = null; - private static final Class[] CONSTRUCTOR_ARGS = new Class[] { - ClassLoader.class, Project.class, Path.class, Boolean.TYPE - }; + /** {@inheritDoc} */ + @Override + public Enumeration getResources(String name) throws IOException { + return getNamedResources(name); + } - static { - if (JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_1_5)) { - try { - subClassToLoad = - Class.forName("org.apache.tools.ant.loader.AntClassLoader5"); - } catch (final ClassNotFoundException e) { - // this is Java5 but the installation is lacking our subclass - } - } + /** {@inheritDoc} */ + public void close() { + cleanup(); } /** @@ -1562,15 +1557,6 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener { final Project project, final Path path, final boolean parentFirst) { - if (subClassToLoad != null) { - return (AntClassLoader) - ReflectUtil.newInstance(subClassToLoad, - CONSTRUCTOR_ARGS, - new Object[] { - parent, project, path, - Boolean.valueOf(parentFirst) - }); - } return new AntClassLoader(parent, project, path, parentFirst); } diff --git a/src/main/org/apache/tools/ant/loader/AntClassLoader5.java b/src/main/org/apache/tools/ant/loader/AntClassLoader5.java index a91ed41e1..15c29dfe3 100644 --- a/src/main/org/apache/tools/ant/loader/AntClassLoader5.java +++ b/src/main/org/apache/tools/ant/loader/AntClassLoader5.java @@ -18,20 +18,15 @@ package org.apache.tools.ant.loader; -import java.io.Closeable; -import java.io.IOException; -import java.net.URL; -import java.util.Enumeration; - import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Path; /** - * Overrides getResources which became non-final in Java5 and - * implements Closeable + * @deprecated since 1.9.7 + * Just use {@link AntClassLoader} itself. */ -public class AntClassLoader5 extends AntClassLoader implements Closeable { +public class AntClassLoader5 extends AntClassLoader { /** * Creates a classloader for the given project using the classpath given. * @@ -53,13 +48,4 @@ public class AntClassLoader5 extends AntClassLoader implements Closeable { super(parent, project, classpath, parentFirst); } - /** {@inheritDoc} */ - public Enumeration getResources(String name) throws IOException { - return getNamedResources(name); - } - - /** {@inheritDoc} */ - public void close() { - cleanup(); - } } diff --git a/src/tests/junit/org/apache/tools/ant/AntClassLoaderTest.java b/src/tests/junit/org/apache/tools/ant/AntClassLoaderTest.java index 8419037e1..e22bd05f2 100644 --- a/src/tests/junit/org/apache/tools/ant/AntClassLoaderTest.java +++ b/src/tests/junit/org/apache/tools/ant/AntClassLoaderTest.java @@ -19,16 +19,21 @@ package org.apache.tools.ant; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.File; +import java.io.IOException; import java.io.PrintStream; import java.net.URL; +import java.util.Enumeration; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.util.FileUtils; +import org.apache.tools.ant.util.CollectionUtils; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -40,8 +45,8 @@ import org.junit.Test; */ public class AntClassLoaderTest { - @Rule - public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public BuildFileRule buildRule = new BuildFileRule(); private AntClassLoader loader; @@ -199,6 +204,44 @@ public class AntClassLoaderTest { } } + /** + * Asserts that getResources won't return resources that cannot be + * seen by AntClassLoader but by ClassLoader.this.parent. + * + * @see + * https://issues.apache.org/bugzilla/show_bug.cgi?id=46752 + */ + @Test + public void testGetResources() throws IOException { + AntClassLoader acl = new AntClassLoader(new EmptyLoader(), null, + new Path(null), true); + assertNull(acl.getResource("META-INF/MANIFEST.MF")); + assertFalse(acl.getResources("META-INF/MANIFEST.MF").hasMoreElements()); + + // double check using system classloader as parent + acl = new AntClassLoader(null, null, new Path(null), true); + assertNotNull(acl.getResource("META-INF/MANIFEST.MF")); + assertTrue(acl.getResources("META-INF/MANIFEST.MF").hasMoreElements()); + } + + @Test + public void testGetResourcesUsingFactory() throws IOException { + AntClassLoader acl = + AntClassLoader.newAntClassLoader(new EmptyLoader(), null, + new Path(null), true); + assertNull(acl.getResource("META-INF/MANIFEST.MF")); + assertFalse(acl.getResources("META-INF/MANIFEST.MF").hasMoreElements()); + } + + private static class EmptyLoader extends ClassLoader { + public URL getResource(String n) { + return null; + } + public Enumeration getResources(String n) { + return new CollectionUtils.EmptyEnumeration(); + } + } + private static class GetPackageWrapper extends ClassLoader { GetPackageWrapper(ClassLoader parent) { super(parent); diff --git a/src/tests/junit/org/apache/tools/ant/loader/AntClassLoader5Test.java b/src/tests/junit/org/apache/tools/ant/loader/AntClassLoader5Test.java deleted file mode 100644 index a01a940a8..000000000 --- a/src/tests/junit/org/apache/tools/ant/loader/AntClassLoader5Test.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.tools.ant.loader; - -import java.io.IOException; -import java.net.URL; -import java.util.Enumeration; -import org.apache.tools.ant.AntClassLoader; -import org.apache.tools.ant.types.Path; -import org.apache.tools.ant.util.CollectionUtils; -import org.junit.Test; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -public class AntClassLoader5Test { - - /** - * Asserts that getResources won't return resources that cannot be - * seen by AntClassLoader but by ClassLoader.this.parent. - * - * @see - * https://issues.apache.org/bugzilla/show_bug.cgi?id=46752 - */ - @Test - public void testGetResources() throws IOException { - AntClassLoader acl = new AntClassLoader5(new EmptyLoader(), null, - new Path(null), true); - assertNull(acl.getResource("META-INF/MANIFEST.MF")); - assertFalse(acl.getResources("META-INF/MANIFEST.MF").hasMoreElements()); - - // double check using system classloader as parent - acl = new AntClassLoader5(null, null, new Path(null), true); - assertNotNull(acl.getResource("META-INF/MANIFEST.MF")); - assertTrue(acl.getResources("META-INF/MANIFEST.MF").hasMoreElements()); - } - - @Test - public void testGetResourcesUsingFactory() throws IOException { - AntClassLoader acl = - AntClassLoader.newAntClassLoader(new EmptyLoader(), null, - new Path(null), true); - assertNull(acl.getResource("META-INF/MANIFEST.MF")); - assertFalse(acl.getResources("META-INF/MANIFEST.MF").hasMoreElements()); - } - - private static class EmptyLoader extends ClassLoader { - public URL getResource(String n) { - return null; - } - public Enumeration getResources(String n) { - return new CollectionUtils.EmptyEnumeration(); - } - } -} \ No newline at end of file