Class ForbidWildcardAsReturnTypeCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.github.sevntu.checkstyle.checks.design.ForbidWildcardAsReturnTypeCheck
-
- All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable
,com.puppycrawl.tools.checkstyle.api.Contextualizable
public class ForbidWildcardAsReturnTypeCheck extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
Prevents using wildcards as return type of methods.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).
- Since:
- 1.9.0
- Author:
- Baratali Izmailov
-
-
Constructor Summary
Constructors Constructor Description ForbidWildcardAsReturnTypeCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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)
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
-
-
-
Field Detail
-
MSG_KEY
public static final String MSG_KEY
Key for error message.- See Also:
- Constant Field Values
-
-
Method Detail
-
setCheckPublicMethods
public void setCheckPublicMethods(boolean checkPublicMethods)
Setter for checkPublicMethods.- Parameters:
checkPublicMethods
- New value for the field.
-
setCheckProtectedMethods
public void setCheckProtectedMethods(boolean checkProtectedMethods)
Setter for checkProtectedMethods.- Parameters:
checkProtectedMethods
- New value for the field.
-
setCheckPackageMethods
public void setCheckPackageMethods(boolean checkPackageMethods)
Setter for checkPackageMethods.- Parameters:
checkPackageMethods
- New value for the field.
-
setCheckPrivateMethods
public void setCheckPrivateMethods(boolean checkPrivateMethods)
Setter for checkPrivateMethods.- Parameters:
checkPrivateMethods
- New value for the field.
-
setCheckOverrideMethods
public void setCheckOverrideMethods(boolean checkOverrideMethods)
Setter for checkOverrideMethods.- Parameters:
checkOverrideMethods
- New value for the field.
-
setCheckDeprecatedMethods
public void setCheckDeprecatedMethods(boolean checkDeprecatedMethods)
Setter for checkDeprecatedMethods.- Parameters:
checkDeprecatedMethods
- New value for the field.
-
setAllowReturnWildcardWithSuper
public void setAllowReturnWildcardWithSuper(boolean allowReturnWildcardWithSuper)
Setter for allowReturnWildcardWithSuper.- Parameters:
allowReturnWildcardWithSuper
- New value for the field.
-
setAllowReturnWildcardWithExtends
public void setAllowReturnWildcardWithExtends(boolean allowReturnWildcardWithExtends)
Setter for allowReturnWildcardWithExtends.- Parameters:
allowReturnWildcardWithExtends
- New value for the field.
-
setReturnTypeClassNamesIgnoreRegex
public void setReturnTypeClassNamesIgnoreRegex(String returnTypeClassNamesIgnoreRegex)
Setter for returnTypeClassNamesIgnoreRegex.- Parameters:
returnTypeClassNamesIgnoreRegex
- New value for the field.
-
getDefaultTokens
public int[] getDefaultTokens()
- Specified by:
getDefaultTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST methodDefAst)
- Overrides:
visitToken
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
-