public class ForbidWildcardAsReturnTypeCheck
extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
Joshua Bloch, "Effective Java (2nd edition)" Item 28: page 137 :
"Do not use wildcard types as return types. Rather than providing additional flexibility for your users, it would force them to use wildcard types in client code. Properly used, wildcard types are nearly invisible to users of a class. They cause methods to accept the parameters they should accept and reject those they should reject. If the user of a class has to think about wildcard types, there is probably something wrong with the classâs API." Attention: some JDK classes have public methods with "?"(wildcard) in return type so it might not always possible to avoid wildcards in return type, as they do not demand user to bother about it (invisible for user or method). So suppressions should be used.
Examples: JDK Collectors, so usage of methods that return wildcard could force user customizations over Collectors use wildcard in public methods
// custom util method, wildcard come from Collectors.toList()
public <T> Collector<T, ?, T> singleResult(Function<? super Iterable<T>, T> collector) {
return Collectors.collectingAndThen(Collectors.toList(), collected -> collected.get(0));
}
If suppressions become too wide spread and annoying it might be reasonable to update Check with option to ignore wildcard if used with another type (not alone).
| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
Key for error message.
|
| Constructor and Description |
|---|
ForbidWildcardAsReturnTypeCheck() |
| Modifier and Type | Method and Description |
|---|---|
int[] |
getAcceptableTokens() |
int[] |
getDefaultTokens() |
int[] |
getRequiredTokens() |
void |
setAllowReturnWildcardWithExtends(boolean allowReturnWildcardWithExtends)
Setter for allowReturnWildcardWithExtends.
|
void |
setAllowReturnWildcardWithSuper(boolean allowReturnWildcardWithSuper)
Setter for allowReturnWildcardWithSuper.
|
void |
setCheckDeprecatedMethods(boolean checkDeprecatedMethods)
Setter for checkDeprecatedMethods.
|
void |
setCheckOverrideMethods(boolean checkOverrideMethods)
Setter for checkOverrideMethods.
|
void |
setCheckPackageMethods(boolean checkPackageMethods)
Setter for checkPackageMethods.
|
void |
setCheckPrivateMethods(boolean checkPrivateMethods)
Setter for checkPrivateMethods.
|
void |
setCheckProtectedMethods(boolean checkProtectedMethods)
Setter for checkProtectedMethods.
|
void |
setCheckPublicMethods(boolean checkPublicMethods)
Setter for checkPublicMethods.
|
void |
setReturnTypeClassNamesIgnoreRegex(String returnTypeClassNamesIgnoreRegex)
Setter for returnTypeClassNamesIgnoreRegex.
|
void |
visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST methodDefAst) |
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeveritypublic static final String MSG_KEY
public void setCheckPublicMethods(boolean checkPublicMethods)
checkPublicMethods - New value for the field.public void setCheckProtectedMethods(boolean checkProtectedMethods)
checkProtectedMethods - New value for the field.public void setCheckPackageMethods(boolean checkPackageMethods)
checkPackageMethods - New value for the field.public void setCheckPrivateMethods(boolean checkPrivateMethods)
checkPrivateMethods - New value for the field.public void setCheckOverrideMethods(boolean checkOverrideMethods)
checkOverrideMethods - New value for the field.public void setCheckDeprecatedMethods(boolean checkDeprecatedMethods)
checkDeprecatedMethods - New value for the field.public void setAllowReturnWildcardWithSuper(boolean allowReturnWildcardWithSuper)
allowReturnWildcardWithSuper - New value for the field.public void setAllowReturnWildcardWithExtends(boolean allowReturnWildcardWithExtends)
allowReturnWildcardWithExtends - New value for the field.public void setReturnTypeClassNamesIgnoreRegex(String returnTypeClassNamesIgnoreRegex)
returnTypeClassNamesIgnoreRegex - New value for the field.public int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST methodDefAst)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.