-
Bug
-
Resolution: Unresolved
-
P5
-
None
-
5.0
-
x86
-
windows_xp
FULL PRODUCT VERSION :
java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
The implementation of javax.xml.transform.TransformerFactory.newInstance fails under certain classloader constellations. The reason is the erroneous implementation of the newInstance method of javax.xml.transform.FactoryFinder. The provider class is loaded by the following code snippet:
Class providerClass;
if (cl == null) {
// If classloader is null Use the bootstrap ClassLoader.
// Thus Class.forName(String) will use the current
// ClassLoader which will be the bootstrap ClassLoader.
providerClass = Class.forName(className);
} else {
try {
providerClass = cl.loadClass(className);
} catch (ClassNotFoundException x) {
if (doFallback) {
// Fall back to current classloader
cl = FactoryFinder.class.getClassLoader();
providerClass = cl.loadClass(className);
} else {
throw x;
}
}
}
If the cl argument passed to the method is not null, that is, there is a valid context classloader, the provider is loaded by cl. However, if cl fails to load the class, a fallback is attempted, by using the classloader of FactoryFinder. However, if FactoryFinder has been loaded by the bootstrap classloader, the line
providerClass = cl.loadClass(className);
will fail with a NullPointerException. A solution to this problem would be a modification of the above code as follows:
instead of:
cl = FactoryFinder.class.getClassLoader();
providerClass = cl.loadClass(className);
the code should read:
cl = FactoryFinder.class.getClassLoader();
if (cl != null)
providerClass = cl.loadClass(className);
else
providerClass = Class.forName (className);
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run a small Java code containing TransformerFactory.newInstance () using the <java> task of an Ant buildfile.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
[java] java.lang.NullPointerException
[java] at javax.xml.transform.FactoryFinder.newInstance(FactoryFinder.j
ava:93)
[java] at javax.xml.transform.FactoryFinder.find(FactoryFinder.java:195
)
[java] at javax.xml.transform.TransformerFactory.newInstance(Transforme
rFactory.java:103)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
Test.java
=======
import javax.xml.transform.*;
public class Test {
public static void main (String[] args) {
TransformerFactory tf = TransformerFactory.newInstance ();
}
}
build.xml
=======
<project name="test" default="run">
<target name="run">
<java classname="Test" classpath="."/>
</target>
</project>
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Described above.
java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
The implementation of javax.xml.transform.TransformerFactory.newInstance fails under certain classloader constellations. The reason is the erroneous implementation of the newInstance method of javax.xml.transform.FactoryFinder. The provider class is loaded by the following code snippet:
Class providerClass;
if (cl == null) {
// If classloader is null Use the bootstrap ClassLoader.
// Thus Class.forName(String) will use the current
// ClassLoader which will be the bootstrap ClassLoader.
providerClass = Class.forName(className);
} else {
try {
providerClass = cl.loadClass(className);
} catch (ClassNotFoundException x) {
if (doFallback) {
// Fall back to current classloader
cl = FactoryFinder.class.getClassLoader();
providerClass = cl.loadClass(className);
} else {
throw x;
}
}
}
If the cl argument passed to the method is not null, that is, there is a valid context classloader, the provider is loaded by cl. However, if cl fails to load the class, a fallback is attempted, by using the classloader of FactoryFinder. However, if FactoryFinder has been loaded by the bootstrap classloader, the line
providerClass = cl.loadClass(className);
will fail with a NullPointerException. A solution to this problem would be a modification of the above code as follows:
instead of:
cl = FactoryFinder.class.getClassLoader();
providerClass = cl.loadClass(className);
the code should read:
cl = FactoryFinder.class.getClassLoader();
if (cl != null)
providerClass = cl.loadClass(className);
else
providerClass = Class.forName (className);
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run a small Java code containing TransformerFactory.newInstance () using the <java> task of an Ant buildfile.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
[java] java.lang.NullPointerException
[java] at javax.xml.transform.FactoryFinder.newInstance(FactoryFinder.j
ava:93)
[java] at javax.xml.transform.FactoryFinder.find(FactoryFinder.java:195
)
[java] at javax.xml.transform.TransformerFactory.newInstance(Transforme
rFactory.java:103)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
Test.java
=======
import javax.xml.transform.*;
public class Test {
public static void main (String[] args) {
TransformerFactory tf = TransformerFactory.newInstance ();
}
}
build.xml
=======
<project name="test" default="run">
<target name="run">
<java classname="Test" classpath="."/>
</target>
</project>
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Described above.