如何让 XPathFactory newInstance 在 Java 中失败?
How can I make XPathFactory newInstance fail in Java?
XPathFactory.newInstance(字符串 uri)的文档是 here。
我正试图使它像这样失败:
println(System.getProperty("DEFAULT_PROPERTY_NAME"));
System.setProperty("DEFAULT_PROPERTY_NAME", "dummy");
println(System.getProperty("DEFAULT_PROPERTY_NAME"));
XPathFactory xPathFactory = XPathFactory.newInstance();
println(xPathFactory.getClass());
我得到的输出是:
null
dummy
class org.apache.xpath.jaxp.XPathFactoryImpl
我预计 ClassLoader 会因要点而失败:
If the system property DEFAULT_PROPERTY_NAME + ":uri" is present,
where uri is the parameter to this method, then its value is read as a
class name. The method will try to create a new instance of this class
by using the class loader, and returns it if it is successfully
created.
但事实并非如此,我显然遗漏了一些东西。
我做错了什么?
I am expecting that the ClassLoader will fail due to the bullet point:
这个要点指的是方法:
public static final XPathFactory newInstance(String uri)
但是您正在调用:
public static final XPathFactory newInstance()
其中指出:
This method is functionally equivalent to:
newInstance(DEFAULT_OBJECT_MODEL_URI)
Since the implementation for the W3C DOM is always available, this method will never fail.
请更改
System.setProperty("DEFAULT_PROPERTY_NAME", "dummy");
到
System.setProperty(XPathFactory.DEFAULT_OBJECT_MODEL_URI, "dummy");
XPathFactory.newInstance(字符串 uri)的文档是 here。
我正试图使它像这样失败:
println(System.getProperty("DEFAULT_PROPERTY_NAME"));
System.setProperty("DEFAULT_PROPERTY_NAME", "dummy");
println(System.getProperty("DEFAULT_PROPERTY_NAME"));
XPathFactory xPathFactory = XPathFactory.newInstance();
println(xPathFactory.getClass());
我得到的输出是:
null
dummy
class org.apache.xpath.jaxp.XPathFactoryImpl
我预计 ClassLoader 会因要点而失败:
If the system property DEFAULT_PROPERTY_NAME + ":uri" is present, where uri is the parameter to this method, then its value is read as a class name. The method will try to create a new instance of this class by using the class loader, and returns it if it is successfully created.
但事实并非如此,我显然遗漏了一些东西。
我做错了什么?
I am expecting that the ClassLoader will fail due to the bullet point:
这个要点指的是方法:
public static final XPathFactory newInstance(String uri)
但是您正在调用:
public static final XPathFactory newInstance()
其中指出:
This method is functionally equivalent to:
newInstance(DEFAULT_OBJECT_MODEL_URI)
Since the implementation for the W3C DOM is always available, this method will never fail.
请更改
System.setProperty("DEFAULT_PROPERTY_NAME", "dummy");
到
System.setProperty(XPathFactory.DEFAULT_OBJECT_MODEL_URI, "dummy");