Class IllegalTypeCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public final class IllegalTypeCheck
    extends AbstractCheck
    Checks that particular classes or interfaces are never used.

    Rationale: Helps reduce coupling on concrete classes.

    Check has following properties:

    format - Pattern for illegal class names.

    legalAbstractClassNames - Abstract classes that may be used as types.

    illegalClassNames - Classes that should not be used as types in variable declarations, return values or parameters. It is possible to set illegal class names via short or canonical name. Specifying illegal type invokes analyzing imports and Check puts violations at corresponding declarations (of variables, methods or parameters). This helps to avoid ambiguous cases, e.g.:

    java.awt.List was set as illegal class name, then, code like:

    import java.util.List;<br> ...<br> List list; //No violation here

    will be ok.

    validateAbstractClassNames - controls whether to validate abstract class names. Default value is false

    ignoredMethodNames - Methods that should not be checked.

    memberModifiers - To check only methods and fields with any of the specified modifiers. This property does not affect method calls nor method references.

    In most cases it's justified to put following classes to illegalClassNames:

    • GregorianCalendar
    • Hashtable
    • ArrayList
    • LinkedList
    • Vector

    as methods that are differ from interface methods are rear used, so in most cases user will benefit from checking for them.

    • Field Detail

      • MSG_KEY

        public static final java.lang.String MSG_KEY
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
    • Constructor Detail

      • IllegalTypeCheck

        public IllegalTypeCheck()
        Creates new instance of the check.
    • Method Detail

      • setFormat

        public void setFormat​(java.util.regex.Pattern pattern)
        Set the format for the specified regular expression.
        Parameters:
        pattern - a pattern.
      • setValidateAbstractClassNames

        public void setValidateAbstractClassNames​(boolean validateAbstractClassNames)
        Sets whether to validate abstract class names.
        Parameters:
        validateAbstractClassNames - whether abstract class names must be ignored.
      • getDefaultTokens

        public int[] getDefaultTokens()
        Description copied from class: AbstractCheck
        Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.
        Specified by:
        getDefaultTokens in class AbstractCheck
        Returns:
        the default tokens
        See Also:
        TokenTypes
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Description copied from class: AbstractCheck
        The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.
        Specified by:
        getAcceptableTokens in class AbstractCheck
        Returns:
        the token set this check is designed for.
        See Also:
        TokenTypes
      • beginTree

        public void beginTree​(DetailAST rootAST)
        Description copied from class: AbstractCheck
        Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.
        Overrides:
        beginTree in class AbstractCheck
        Parameters:
        rootAST - the root of the tree
      • getRequiredTokens

        public int[] getRequiredTokens()
        Description copied from class: AbstractCheck
        The tokens that this check must be registered for.
        Specified by:
        getRequiredTokens in class AbstractCheck
        Returns:
        the token set this must be registered for.
        See Also:
        TokenTypes
      • setIllegalClassNames

        public void setIllegalClassNames​(java.lang.String... classNames)
        Set the list of illegal variable types.
        Parameters:
        classNames - array of illegal variable types
      • setIgnoredMethodNames

        public void setIgnoredMethodNames​(java.lang.String... methodNames)
        Set the list of ignore method names.
        Parameters:
        methodNames - array of ignored method names
      • setLegalAbstractClassNames

        public void setLegalAbstractClassNames​(java.lang.String... classNames)
        Set the list of legal abstract class names.
        Parameters:
        classNames - array of legal abstract class names
      • setMemberModifiers

        public void setMemberModifiers​(java.lang.String modifiers)
        Set the list of member modifiers (of methods and fields) which should be checked.
        Parameters:
        modifiers - String contains modifiers.