Class IllegalTypeCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public final class IllegalTypeCheck
    extends AbstractCheck
    Checks that particular class are never used as types in variable declarations, return values or parameters.

    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 only specified modifiers.

    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
      • DEFAULT_LEGAL_ABSTRACT_NAMES

        private static final java.lang.String[] DEFAULT_LEGAL_ABSTRACT_NAMES
        Abstract classes legal by default.
      • DEFAULT_ILLEGAL_TYPES

        private static final java.lang.String[] DEFAULT_ILLEGAL_TYPES
        Types illegal by default.
      • DEFAULT_IGNORED_METHOD_NAMES

        private static final java.lang.String[] DEFAULT_IGNORED_METHOD_NAMES
        Default ignored method names.
      • illegalClassNames

        private final java.util.Set<java.lang.String> illegalClassNames
        Illegal classes.
      • legalAbstractClassNames

        private final java.util.Set<java.lang.String> legalAbstractClassNames
        Legal abstract classes.
      • ignoredMethodNames

        private final java.util.Set<java.lang.String> ignoredMethodNames
        Methods which should be ignored.
      • memberModifiers

        private java.util.List<java.lang.Integer> memberModifiers
        Check methods and fields with only corresponding modifiers.
      • format

        private java.util.regex.Pattern format
        The regexp to match against.
      • validateAbstractClassNames

        private boolean validateAbstractClassNames
        Controls whether to validate abstract class names.
    • 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
      • 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
      • isVerifiable

        private boolean isVerifiable​(DetailAST methodOrVariableDef)
        Checks if current method's return type or variable's type is verifiable according to memberModifiers option.
        Parameters:
        methodOrVariableDef - METHOD_DEF or VARIABLE_DEF ast node.
        Returns:
        true if member is verifiable according to memberModifiers option.
      • isContainVerifiableType

        private boolean isContainVerifiableType​(DetailAST modifiers)
        Checks is modifiers contain verifiable type.
        Parameters:
        modifiers - parent node for all modifiers
        Returns:
        true if method or variable can be verified
      • visitMethodDef

        private void visitMethodDef​(DetailAST methodDef)
        Checks return type of a given method.
        Parameters:
        methodDef - method for check.
      • visitParameterDef

        private void visitParameterDef​(DetailAST parameterDef)
        Checks type of parameters.
        Parameters:
        parameterDef - parameter list for check.
      • visitVariableDef

        private void visitVariableDef​(DetailAST variableDef)
        Checks type of given variable.
        Parameters:
        variableDef - variable to check.
      • visitImport

        private void visitImport​(DetailAST importAst)
        Checks imported type (as static and star imports are not supported by Check, only type is in the consideration).
        If this type is illegal due to Check's options - puts violation on it.
        Parameters:
        importAst - Import
      • isStarImport

        private static boolean isStarImport​(DetailAST importAst)
        Checks if current import is star import. E.g.:

        import java.util.*;

        Parameters:
        importAst - Import
        Returns:
        true if it is star import
      • checkClassName

        private void checkClassName​(DetailAST ast)
        Checks type of given method, parameter or variable.
        Parameters:
        ast - node to check.
      • isMatchingClassName

        private boolean isMatchingClassName​(java.lang.String className)
        Returns true if given class name is one of illegal classes or else false.
        Parameters:
        className - class name to check.
        Returns:
        true if given class name is one of illegal classes or if it matches to abstract class names pattern.
      • extendIllegalClassNamesWithShortName

        private void extendIllegalClassNamesWithShortName​(java.lang.String canonicalName)
        Extends illegal class names set via imported short type name.
        Parameters:
        canonicalName - Canonical name of imported type.
      • getImportedTypeCanonicalName

        private static java.lang.String getImportedTypeCanonicalName​(DetailAST importAst)
        Gets imported type's canonical name.
        Parameters:
        importAst - Import
        Returns:
        Imported canonical type's name.
      • getNextSubTreeNode

        private static DetailAST getNextSubTreeNode​(DetailAST currentNodeAst,
                                                    DetailAST subTreeRootAst)
        Gets the next node of a syntactical tree (child of a current node or sibling of a current node, or sibling of a parent of a current node).
        Parameters:
        currentNodeAst - Current node in considering
        subTreeRootAst - SubTree root
        Returns:
        Current node after bypassing, if current node reached the root of a subtree method returns null
      • isCheckedMethod

        private boolean isCheckedMethod​(DetailAST ast)
        Returns true if method has to be checked or false.
        Parameters:
        ast - method def to check.
        Returns:
        true if we should check this method.
      • 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.