Browse Source

use the same logic as getResource in getResourceAsStream. PR 44103

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@799650 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
44b6fcc184
3 changed files with 94 additions and 6 deletions
  1. +5
    -0
      WHATSNEW
  2. +6
    -6
      src/main/org/apache/tools/ant/AntClassLoader.java
  3. +83
    -0
      src/tests/antunit/core/classloader-test.xml

+ 5
- 0
WHATSNEW View File

@@ -415,6 +415,11 @@ Fixed bugs:
* AntClassLoader didn't set the proper CodeSource for loaded classes.
Bugzilla Report 20174.

* AntClassLoader.getResourceAsStream would return streams to
resources it didn't return with getResource and to classes it
failed to load.
Bugzilla Report 44103.

Other changes:
--------------
* The get task now also follows redirects from http to https


+ 6
- 6
src/main/org/apache/tools/ant/AntClassLoader.java View File

@@ -705,6 +705,7 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
InputStream resourceStream = null;
if (isParentFirst(name)) {
resourceStream = loadBaseResource(name);
}
if (resourceStream != null) {
log("ResourceStream for " + name
+ " loaded from parent loader", Project.MSG_DEBUG);
@@ -715,17 +716,16 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
+ " loaded from ant loader", Project.MSG_DEBUG);
}
}
} else {
resourceStream = loadResource(name);
if (resourceStream != null) {
log("ResourceStream for " + name + " loaded from ant loader", Project.MSG_DEBUG);
if (resourceStream == null && !isParentFirst(name)) {
if (ignoreBase) {
resourceStream = getRootLoader() == null ? null : getRootLoader().getResourceAsStream(name);
} else {
resourceStream = loadBaseResource(name);
}
if (resourceStream != null) {
log("ResourceStream for " + name + " loaded from parent loader",
Project.MSG_DEBUG);
}
}
}
if (resourceStream == null) {
log("Couldn't load ResourceStream for " + name, Project.MSG_DEBUG);
@@ -766,7 +766,7 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
* the resource cannot be found.
*/
private InputStream loadBaseResource(String name) {
return parent == null ? getSystemResourceAsStream(name) : parent.getResourceAsStream(name);
return parent == null ? super.getResourceAsStream(name) : parent.getResourceAsStream(name);
}

/**


+ 83
- 0
src/tests/antunit/core/classloader-test.xml View File

@@ -0,0 +1,83 @@
<?xml version="1.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.
-->
<project xmlns:au="antlib:org.apache.ant.antunit" default="antunit">

<import file="../antunit-base.xml"/>

<target name="setUp">
<mkdir dir="${input}"/>
<mkdir dir="${output}"/>
</target>

<target name="testGetResource"
description="https://issues.apache.org/bugzilla/show_bug.cgi?id=44103"
depends="setUp">
<echo file="${input}/A.java"><![CDATA[
public class A {
public static void main(String[] args) {
if (A.class.getClassLoader().getResource("org/apache/xerces/jaxp/DocumentBuilderFactoryImpl.class") != null) {
throw new RuntimeException("Didn't expect to find DocumenBuilderImpl");
}
}
}
]]></echo>
<javac srcdir="${input}" destdir="${output}"/>
<java classname="A" failonerror="true" fork="false">
<classpath location="${output}"/>
</java>
</target>

<target name="testLoadClass"
description="https://issues.apache.org/bugzilla/show_bug.cgi?id=44103"
depends="setUp">
<echo file="${input}/A.java"><![CDATA[
public class A {
public static void main(String[] args) {
try {
A.class.getClassLoader().loadClass("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
throw new RuntimeException("Didn't expect to find DocumenBuilderImpl");
} catch (ClassNotFoundException cnfe) {
}
}
}
]]></echo>
<javac srcdir="${input}" destdir="${output}"/>
<java classname="A" failonerror="true" fork="false">
<classpath location="${output}"/>
</java>
</target>

<target name="testGetResourceAsStream"
description="https://issues.apache.org/bugzilla/show_bug.cgi?id=44103"
depends="setUp">
<echo file="${input}/A.java"><![CDATA[
public class A {
public static void main(String[] args) {
if (A.class.getClassLoader().getResourceAsStream("org/apache/xerces/jaxp/DocumentBuilderFactoryImpl.class") != null) {
throw new RuntimeException("Didn't expect to find DocumenBuilderImpl");
}
}
}
]]></echo>
<javac srcdir="${input}" destdir="${output}"/>
<java classname="A" failonerror="true" fork="false">
<classpath location="${output}"/>
</java>
</target>

</project>

Loading…
Cancel
Save