001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.metrics; 021 022import com.puppycrawl.tools.checkstyle.api.TokenTypes; 023 024/** 025 * <div> 026 * Measures the number of distinct classes that are instantiated 027 * within the given class or record. This type of coupling is not caused by inheritance or 028 * the object-oriented paradigm. Generally speaking, any data type with other 029 * data types as members or local variable that is an instantiation (object) 030 * of another class has data abstraction coupling (DAC). The higher the DAC, 031 * the more complex the structure of the class. 032 * </div> 033 * 034 * <p> 035 * This check processes files in the following way: 036 * </p> 037 * <ol> 038 * <li> 039 * Iterates over the list of tokens (defined below) and counts all mentioned classes. 040 * <ul> 041 * <li> 042 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT"> 043 * PACKAGE_DEF</a> 044 * </li> 045 * <li> 046 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT"> 047 * IMPORT</a> 048 * </li> 049 * <li> 050 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF"> 051 * CLASS_DEF</a> 052 * </li> 053 * <li> 054 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF"> 055 * INTERFACE_DEF</a> 056 * </li> 057 * <li> 058 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF"> 059 * ENUM_DEF</a> 060 * </li> 061 * <li> 062 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_NEW"> 063 * LITERAL_NEW</a> 064 * </li> 065 * <li> 066 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF"> 067 * RECORD_DEF</a> 068 * </li> 069 * </ul> 070 * </li> 071 * <li> 072 * If a class was imported with direct import (i.e. {@code import java.math.BigDecimal}), 073 * or the class was referenced with the package name (i.e. {@code java.math.BigDecimal value}) 074 * and the package was added to the {@code excludedPackages} parameter, the class 075 * does not increase complexity. 076 * </li> 077 * <li> 078 * If a class name was added to the {@code excludedClasses} parameter, 079 * the class does not increase complexity. 080 * </li> 081 * </ol> 082 * <ul> 083 * <li> 084 * Property {@code excludeClassesRegexps} - Specify user-configured regular 085 * expressions to ignore classes. 086 * Type is {@code java.util.regex.Pattern[]}. 087 * Default value is {@code ^$}. 088 * Since version 7.7 089 * </li> 090 * <li> 091 * Property {@code excludedClasses} - Specify user-configured class names to ignore. 092 * Type is {@code java.lang.String[]}. 093 * Default value is {@code ArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte, 094 * Character, Class, Collection, Deprecated, Deque, Double, DoubleStream, EnumSet, Exception, 095 * Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException, 096 * IndexOutOfBoundsException, IntStream, Integer, LinkedHashMap, LinkedHashSet, LinkedList, List, 097 * Long, LongStream, Map, NullPointerException, Object, Optional, OptionalDouble, OptionalInt, 098 * OptionalLong, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short, 099 * SortedMap, SortedSet, Stream, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable, 100 * TreeMap, TreeSet, UnsupportedOperationException, Void, boolean, byte, char, double, 101 * float, int, long, short, var, void}. 102 * Since version 5.7 103 * </li> 104 * <li> 105 * Property {@code excludedPackages} - Specify user-configured packages to ignore. 106 * Type is {@code java.lang.String[]}. 107 * Default value is {@code ""}. 108 * Since version 7.7 109 * </li> 110 * <li> 111 * Property {@code max} - Specify the maximum threshold allowed. 112 * Type is {@code int}. 113 * Default value is {@code 7}. 114 * </li> 115 * </ul> 116 * 117 * <p> 118 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 119 * </p> 120 * 121 * <p> 122 * Violation Message Keys: 123 * </p> 124 * <ul> 125 * <li> 126 * {@code classDataAbstractionCoupling} 127 * </li> 128 * </ul> 129 * 130 * @since 3.4 131 * 132 */ 133public final class ClassDataAbstractionCouplingCheck 134 extends AbstractClassCouplingCheck { 135 136 /** 137 * A key is pointing to the warning message text in "messages.properties" 138 * file. 139 */ 140 public static final String MSG_KEY = "classDataAbstractionCoupling"; 141 142 /** Default allowed complexity. */ 143 private static final int DEFAULT_MAX = 7; 144 145 /** Creates bew instance of the check. */ 146 public ClassDataAbstractionCouplingCheck() { 147 super(DEFAULT_MAX); 148 } 149 150 @Override 151 public int[] getRequiredTokens() { 152 return new int[] { 153 TokenTypes.PACKAGE_DEF, 154 TokenTypes.IMPORT, 155 TokenTypes.CLASS_DEF, 156 TokenTypes.INTERFACE_DEF, 157 TokenTypes.ENUM_DEF, 158 TokenTypes.LITERAL_NEW, 159 TokenTypes.RECORD_DEF, 160 }; 161 } 162 163 @Override 164 public int[] getAcceptableTokens() { 165 return getRequiredTokens(); 166 } 167 168 @Override 169 protected String getLogMessageId() { 170 return MSG_KEY; 171 } 172 173}