com.liferay.source.formatter.checkstyle.check.CapsNameCheck Maven / Gradle / Ivy
The newest version!
/**
* SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.source.formatter.checkstyle.check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
/**
* @author Alan Huang
*/
public class CapsNameCheck extends BaseCheck {
@Override
public int[] getDefaultTokens() {
return new int[] {
TokenTypes.CLASS_DEF, TokenTypes.ENUM_DEF, TokenTypes.INTERFACE_DEF,
TokenTypes.METHOD_DEF, TokenTypes.PARAMETER_DEF,
TokenTypes.RESOURCE, TokenTypes.VARIABLE_DEF
};
}
@Override
protected void doVisitToken(DetailAST detailAST) {
if (detailAST.getType() == TokenTypes.METHOD_DEF) {
if (AnnotationUtil.containsAnnotation(detailAST, "Override")) {
return;
}
String absolutePath = getAbsolutePath();
if (absolutePath.contains("/taglib/") &&
absolutePath.endsWith("Tag.java")) {
return;
}
}
String name = getName(detailAST);
String tokenTypeName = getTokenTypeName(detailAST);
for (String[] array : _ALL_CAPS_STRINGS) {
String s = array[1];
int x = -1;
while (true) {
x = name.indexOf(s, x + 1);
if (x == -1) {
break;
}
int y = x + s.length();
if ((y != name.length()) &&
!Character.isUpperCase(name.charAt(y))) {
continue;
}
String newName =
name.substring(0, x) + array[0] + name.substring(y);
log(detailAST, _MSG_RENAME, tokenTypeName, name, newName);
}
}
}
private static final String[][] _ALL_CAPS_STRINGS = {
{"DDL", "Ddl"}, {"DDM", "Ddm"}, {"DL", "Dl"}, {"PK", "Pk"}
};
private static final String _MSG_RENAME = "rename";
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy