如果从未使用过,声明中的泛型类型如何真正影响方法?
How does generic type in declaration really affects the method if it's never used?
我正在查看 HTMLUnit 源代码,但不太明白泛型类型在这里应该如何工作。它从未在方法内部使用过,使方法通用的意义何在?
public <T> List<T> getByXPath(final String xpathExpr) {
PrefixResolver prefixResolver = null;
if (hasFeature(XPATH_SELECTION_NAMESPACES)) {
/*
* See if the document has the SelectionNamespaces property defined. If so, then
* create a PrefixResolver that resolves the defined namespaces.
*/
final Document doc = getOwnerDocument();
if (doc instanceof XmlPage) {
final ScriptableObject scriptable = ((XmlPage) doc).getScriptableObject();
if (ScriptableObject.hasProperty(scriptable, "getProperty")) {
final Object selectionNS =
ScriptableObject.callMethod(scriptable, "getProperty", new Object[]{"SelectionNamespaces"});
if (selectionNS != null && !selectionNS.toString().isEmpty()) {
final Map<String, String> namespaces = parseSelectionNamespaces(selectionNS.toString());
if (namespaces != null) {
prefixResolver = new PrefixResolver() {
@Override
public String getBaseIdentifier() {
return namespaces.get("");
}
@Override
public String getNamespaceForPrefix(final String prefix) {
return namespaces.get(prefix);
}
@Override
public String getNamespaceForPrefix(final String prefix, final Node node) {
throw new UnsupportedOperationException();
}
@Override
public boolean handlesNullPrefixes() {
return false;
}
};
}
}
}
}
}
return XPathUtils.getByXPath(this, xpathExpr, prefixResolver);
}
它用于 return 类型 (List<T>
),因此 returned List
将具有特定的通用类型。这也意味着 XPathUtils.getByXPath(…)
return 是一个 List<T>
,因为它是 return 在方法底部的内容,所以它在方法内部使用。
这里的泛型是用来infer类型T
到XPathUtils.getByXPath
方法中的,签名是
public static <T> List<T> getByXPath(DomNode node, String xpathExpr, PrefixResolver resolver)
当编译器查看这条语句时
return XPathUtils.getByXPath(this, xpathExpr, prefixResolver);
它从外部 getByXPath
方法推断出 T
类型,后者又从其调用推断出 T
。
因此,如果您调用 List<HtmlDivision> list = page.getByXPath(expr);
,那么 HtmlDivision
将被推断为 getByXPath
,然后推断为 XPathUtils.getByXPath
我正在查看 HTMLUnit 源代码,但不太明白泛型类型在这里应该如何工作。它从未在方法内部使用过,使方法通用的意义何在?
public <T> List<T> getByXPath(final String xpathExpr) {
PrefixResolver prefixResolver = null;
if (hasFeature(XPATH_SELECTION_NAMESPACES)) {
/*
* See if the document has the SelectionNamespaces property defined. If so, then
* create a PrefixResolver that resolves the defined namespaces.
*/
final Document doc = getOwnerDocument();
if (doc instanceof XmlPage) {
final ScriptableObject scriptable = ((XmlPage) doc).getScriptableObject();
if (ScriptableObject.hasProperty(scriptable, "getProperty")) {
final Object selectionNS =
ScriptableObject.callMethod(scriptable, "getProperty", new Object[]{"SelectionNamespaces"});
if (selectionNS != null && !selectionNS.toString().isEmpty()) {
final Map<String, String> namespaces = parseSelectionNamespaces(selectionNS.toString());
if (namespaces != null) {
prefixResolver = new PrefixResolver() {
@Override
public String getBaseIdentifier() {
return namespaces.get("");
}
@Override
public String getNamespaceForPrefix(final String prefix) {
return namespaces.get(prefix);
}
@Override
public String getNamespaceForPrefix(final String prefix, final Node node) {
throw new UnsupportedOperationException();
}
@Override
public boolean handlesNullPrefixes() {
return false;
}
};
}
}
}
}
}
return XPathUtils.getByXPath(this, xpathExpr, prefixResolver);
}
它用于 return 类型 (List<T>
),因此 returned List
将具有特定的通用类型。这也意味着 XPathUtils.getByXPath(…)
return 是一个 List<T>
,因为它是 return 在方法底部的内容,所以它在方法内部使用。
这里的泛型是用来infer类型T
到XPathUtils.getByXPath
方法中的,签名是
public static <T> List<T> getByXPath(DomNode node, String xpathExpr, PrefixResolver resolver)
当编译器查看这条语句时
return XPathUtils.getByXPath(this, xpathExpr, prefixResolver);
它从外部 getByXPath
方法推断出 T
类型,后者又从其调用推断出 T
。
因此,如果您调用 List<HtmlDivision> list = page.getByXPath(expr);
,那么 HtmlDivision
将被推断为 getByXPath
,然后推断为 XPathUtils.getByXPath