Class 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
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      Key for error message.
    • Constructor Detail

      • ForbidWildcardAsReturnTypeCheck

        public ForbidWildcardAsReturnTypeCheck()
    • 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 class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST methodDefAst)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck