Revert "cleanup IDEA-382376: move com.siyeh.ig.naming package from java-impl to java-impl-inspections"

This reverts commit b9a43121223c211cf1fdf6f3edcebcebbb84a21a.


(cherry picked from commit 918b4ac78f2a6f7ad2b9ad7426e1e202fddeedeb)

IJ-MR-191549

GitOrigin-RevId: 9ee0641d3d348d9013ecb93ae811f5bb16164c1f
This commit is contained in:
Bartek Pacia
2026-02-13 08:29:30 +00:00
committed by intellij-monorepo-bot
parent 8941c9e307
commit 7c1f518efa
43 changed files with 0 additions and 2 deletions
@@ -1,50 +0,0 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.codeInspection.naming.NamingConventionWithFallbackBean;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiModifier;
import com.siyeh.InspectionGadgetsBundle;
import org.jetbrains.annotations.NonNls;
public final class AbstractClassNamingConvention extends NamingConvention<PsiClass> {
public static final @NonNls String ABSTRACT_CLASS_NAMING_CONVENTION_SHORT_NAME = "AbstractClassNamingConvention";
private static final int DEFAULT_MIN_LENGTH = 8;
private static final int DEFAULT_MAX_LENGTH = 64;
@Override
public NamingConventionBean createDefaultBean() {
return new NamingConventionWithFallbackBean("[A-Z][A-Za-z\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("abstract.class.naming.convention.element.description");
}
@Override
public boolean isApplicable(PsiClass aClass) {
return aClass.hasModifierProperty(PsiModifier.ABSTRACT);
}
@Override
public String getShortName() {
return ABSTRACT_CLASS_NAMING_CONVENTION_SHORT_NAME;
}
}
@@ -1,50 +0,0 @@
/*
* Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.codeInspection.naming.NamingConventionWithFallbackBean;
import com.intellij.psi.PsiClass;
import com.siyeh.InspectionGadgetsBundle;
import org.jetbrains.annotations.NonNls;
public final class AnnotationNamingConvention extends NamingConvention<PsiClass> {
private static final int DEFAULT_MIN_LENGTH = 8;
private static final int DEFAULT_MAX_LENGTH = 64;
@Override
public NamingConventionBean createDefaultBean() {
return new NamingConventionWithFallbackBean("[A-Z][A-Za-z\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public boolean isApplicable(PsiClass member) {
return member.isAnnotationType();
}
@Override
public @NonNls String getShortName() {
return "AnnotationNamingConvention";
}
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("annotation.naming.convention.element.description");
}
}
@@ -1,105 +0,0 @@
/*
* Copyright 2003-2022 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.options.OptPane;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiTypes;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.LibraryUtil;
import com.siyeh.ig.psiutils.MethodUtils;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import static com.intellij.codeInspection.options.OptPane.checkbox;
public final class BooleanMethodNameMustStartWithQuestionInspection extends NonBooleanMethodNameMayNotStartWithQuestionInspection {
@SuppressWarnings("PublicField")
public boolean ignoreInAnnotationInterface = true;
public BooleanMethodNameMustStartWithQuestionInspection() {}
@Override
public void writeSettings(@NotNull Element element) throws WriteExternalException {
// keep original order of settings written for compatibility
writeOption(element, "ignoreBooleanMethods");
writeOption(element, "ignoreInAnnotationInterface");
writeOption(element, "onlyWarnOnBaseMethods");
questionString = formatString(questionList);
writeOption(element, "questionString");
}
@Override
public @NotNull OptPane getOptionsPane() {
return super.getOptionsPane()
.append(checkbox("ignoreInAnnotationInterface", InspectionGadgetsBundle.message("ignore.boolean.methods.in.an.interface.option")));
}
@Override
public @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message("boolean.method.name.must.start.with.question.problem.descriptor");
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new BooleanMethodNameMustStartWithQuestionVisitor();
}
private class BooleanMethodNameMustStartWithQuestionVisitor extends BaseInspectionVisitor {
@Override
public void visitMethod(@NotNull PsiMethod method) {
final PsiType returnType = method.getReturnType();
if (returnType == null) {
return;
}
else if (!returnType.equals(PsiTypes.booleanType())) {
if (ignoreBooleanMethods || !returnType.equalsToText(CommonClassNames.JAVA_LANG_BOOLEAN)) {
return;
}
}
if (ignoreInAnnotationInterface) {
final PsiClass containingClass = method.getContainingClass();
if (containingClass != null && containingClass.isAnnotationType()) {
return;
}
}
final String name = method.getName();
if (startsWithQuestionWord(name) || isSpecialCase(name)) {
return;
}
if (onlyWarnOnBaseMethods) {
if (MethodUtils.hasSuper(method)) {
return;
}
}
else if (LibraryUtil.isOverrideOfLibraryMethod(method)) {
return;
}
registerMethodError(method, method);
}
private static boolean isSpecialCase(String name) {
return name.equals("add") || name.equals("remove") || name.equals("put");
}
}
}
@@ -1,92 +0,0 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiClass;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import com.siyeh.ig.psiutils.ClassUtils;
import org.jetbrains.annotations.NotNull;
import java.util.StringTokenizer;
public final class ClassNamePrefixedWithPackageNameInspection
extends BaseInspection {
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new RenameFix();
}
@Override
protected @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"class.name.prefixed.with.package.name.problem.descriptor");
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new ClassNameBePrefixedWithPackageNameVisitor();
}
private static class ClassNameBePrefixedWithPackageNameVisitor
extends BaseInspectionVisitor {
@Override
public void visitClass(@NotNull PsiClass aClass) {
// no call to super, so it doesn't drill down into inner classes
final String className = aClass.getName();
if (className == null) {
return;
}
final PsiClass outerClass =
ClassUtils.getOutermostContainingClass(aClass);
final String qualifiedName = outerClass.getQualifiedName();
if (qualifiedName == null) {
return;
}
if (className.equals(qualifiedName)) {
return;
}
final StringTokenizer tokenizer =
new StringTokenizer(qualifiedName, ".");
String currentPackageName = null;
String lastPackageName = null;
while (tokenizer.hasMoreTokens()) {
lastPackageName = currentPackageName;
currentPackageName = tokenizer.nextToken();
}
if (lastPackageName == null) {
return;
}
final String lowercaseClassName = StringUtil.toLowerCase(className);
final String lowercasePackageName = StringUtil.toLowerCase(lastPackageName);
if (!lowercaseClassName.startsWith(lowercasePackageName)) {
return;
}
registerClassError(aClass);
}
}
}
@@ -1,94 +0,0 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.psi.PsiClass;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Set;
public final class ClassNameSameAsAncestorNameInspection extends BaseInspection {
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new RenameFix();
}
@Override
protected @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"class.name.same.as.ancestor.name.problem.descriptor");
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new ClassNameSameAsAncestorNameVisitor();
}
private static class ClassNameSameAsAncestorNameVisitor
extends BaseInspectionVisitor {
@Override
public void visitClass(@NotNull PsiClass aClass) {
// no call to super, so it doesn't drill down into inner classes
final String className = aClass.getName();
if (className == null) {
return;
}
final Set<PsiClass> alreadyVisited = new HashSet<>(8);
final PsiClass[] supers = aClass.getSupers();
for (final PsiClass aSuper : supers) {
if (hasMatchingName(aSuper, className, alreadyVisited)) {
registerClassError(aClass);
}
}
}
private static boolean hasMatchingName(PsiClass aSuper,
String className,
Set<? super PsiClass> alreadyVisited) {
if (aSuper == null) {
return false;
}
if (alreadyVisited.contains(aSuper)) {
return false;
}
alreadyVisited.add(aSuper);
final String superName = aSuper.getName();
if (className.equals(superName)) {
return true;
}
final PsiClass[] supers = aSuper.getSupers();
for (PsiClass aSupers : supers) {
if (hasMatchingName(aSupers, className, alreadyVisited)) {
return true;
}
}
return false;
}
}
}
@@ -1,48 +0,0 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.psi.PsiClass;
import com.siyeh.InspectionGadgetsBundle;
import org.jetbrains.annotations.NonNls;
public final class ClassNamingConvention extends NamingConvention<PsiClass> {
public static final @NonNls String CLASS_NAMING_CONVENTION_SHORT_NAME = "ClassNamingConvention";
private static final int DEFAULT_MIN_LENGTH = 8;
private static final int DEFAULT_MAX_LENGTH = 64;
@Override
public NamingConventionBean createDefaultBean() {
return new NamingConventionBean("[A-Z][A-Za-z\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public boolean isApplicable(PsiClass member) {
return true;
}
@Override
public String getShortName() {
return CLASS_NAMING_CONVENTION_SHORT_NAME;
}
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("class.naming.convention.element.description");
}
}
@@ -1,11 +0,0 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.AbstractNamingConventionMerger;
import com.intellij.psi.PsiClass;
final class ClassNamingConventionMerger extends AbstractNamingConventionMerger<PsiClass> {
ClassNamingConventionMerger() {
super(new NewClassNamingConventionInspection());
}
}
@@ -1,75 +0,0 @@
/*
* Copyright 2003-2023 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiImplicitClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.util.PsiMethodUtil;
import com.siyeh.HardcodedMethodConstants;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import com.siyeh.ig.psiutils.MethodUtils;
import org.jetbrains.annotations.NotNull;
public final class ConfusingMainMethodInspection extends BaseInspection {
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new RenameFix();
}
@Override
protected @NotNull String buildErrorString(Object... infos) {
return infos.length > 0
? InspectionGadgetsBundle.message("unrunnable.main.method.problem.descriptor")
: InspectionGadgetsBundle.message("confusing.main.method.problem.descriptor");
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new ConfusingMainMethodVisitor();
}
private static class ConfusingMainMethodVisitor extends BaseInspectionVisitor {
@Override
public void visitMethod(@NotNull PsiMethod method) {
if (!HardcodedMethodConstants.MAIN.equals(method.getName()) ||
method.hasModifierProperty(PsiModifier.ABSTRACT) ||
MethodUtils.hasSuper(method)) {
return;
}
if (!PsiMethodUtil.isMainMethod(method)) {
registerMethodError(method);
return;
}
PsiClass containingClass = method.getContainingClass();
if (!(containingClass instanceof PsiImplicitClass) && containingClass != null && containingClass.getQualifiedName() == null) {
registerMethodError(method, Boolean.TRUE); // main method not runnable
}
}
}
}
@@ -1,53 +0,0 @@
/*
* Copyright 2003-2013 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiModifier;
import com.siyeh.InspectionGadgetsBundle;
import org.jetbrains.annotations.NonNls;
public final class ConstantNamingConvention extends NamingConvention<PsiField> {
static final @NonNls String CONSTANT_NAMING_CONVENTION_SHORT_NAME = "ConstantNamingConvention";
private static final int DEFAULT_MIN_LENGTH = 5;
private static final int DEFAULT_MAX_LENGTH = 32;
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("constant.naming.convention.element.description");
}
@Override
public String getShortName() {
return CONSTANT_NAMING_CONVENTION_SHORT_NAME;
}
@Override
public NamingConventionBean createDefaultBean() {
return new FieldNamingConventionInspection.FieldNamingConventionBean("[A-Z][A-Z_\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public boolean isApplicable(PsiField field) {
return field.hasModifierProperty(PsiModifier.STATIC) && field.hasModifierProperty(PsiModifier.FINAL);
}
}
@@ -1,63 +0,0 @@
/*
* Copyright 2003-2013 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.dataFlow.Mutability;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiModifier;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.psiutils.ClassUtils;
import org.jetbrains.annotations.NonNls;
public final class ConstantWithMutableFieldTypeNamingConvention extends NamingConvention<PsiField> {
static final @NonNls String CONSTANT_WITH_MUTABLE_FIELD_TYPE_NAMING_CONVENTION_SHORT_NAME = "ConstantWithMutableFieldTypeNamingConvention";
private static final int DEFAULT_MIN_LENGTH = 5;
private static final int DEFAULT_MAX_LENGTH = 32;
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("constant.with.mutable.field.naming.convention.element.description");
}
@Override
public String getShortName() {
return CONSTANT_WITH_MUTABLE_FIELD_TYPE_NAMING_CONVENTION_SHORT_NAME;
}
@Override
public NamingConventionBean createDefaultBean() {
return new FieldNamingConventionInspection.FieldNamingConventionBean("[A-Z][A-Z_\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public boolean isApplicable(PsiField field) {
if (field.hasModifierProperty(PsiModifier.STATIC) && field.hasModifierProperty(PsiModifier.FINAL)) {
if (Mutability.getMutability(field).isUnmodifiable()) {
return false;
}
if (!ClassUtils.isImmutable(field.getType())) {
return true;
}
}
return false;
}
}
@@ -1,142 +0,0 @@
/*
* Copyright 2003-2015 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.options.CommonOptionPanes;
import com.intellij.codeInspection.options.OptPane;
import com.intellij.codeInspection.options.OptRegularComponent;
import com.intellij.codeInspection.options.OptionController;
import com.intellij.openapi.util.InvalidDataException;
import com.siyeh.HardcodedMethodConstants;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public abstract class ConventionInspection extends BaseInspection {
/**
* @noinspection PublicField
*/
public String m_regex = getDefaultRegex();
/**
* @noinspection PublicField
*/
public int m_minLength = getDefaultMinLength();
/**
* @noinspection PublicField
*/
public int m_maxLength = getDefaultMaxLength();
protected Pattern m_regexPattern = Pattern.compile(m_regex);
@Override
protected final @NotNull String buildErrorString(Object... infos) {
final String name = (String)infos[0];
final int length = name.length();
if (length < getMinLength()) {
return InspectionGadgetsBundle.message("naming.convention.problem.descriptor.short", getElementDescription(),
Integer.valueOf(length), Integer.valueOf(getMinLength()));
}
else if (getMaxLength() > 0 && length > getMaxLength()) {
return InspectionGadgetsBundle.message("naming.convention.problem.descriptor.long", getElementDescription(),
Integer.valueOf(length), Integer.valueOf(getMaxLength()));
}
return InspectionGadgetsBundle.message("naming.convention.problem.descriptor.regex.mismatch", getElementDescription(), getRegex());
}
protected abstract String getElementDescription();
protected abstract @NonNls String getDefaultRegex();
protected abstract int getDefaultMinLength();
protected abstract int getDefaultMaxLength();
protected String getRegex() {
return m_regex;
}
protected int getMinLength() {
return m_minLength;
}
protected int getMaxLength() {
return m_maxLength;
}
protected boolean isValid(String name) {
final int length = name.length();
if (length < m_minLength) {
return false;
}
if (m_maxLength > 0 && length > m_maxLength) {
return false;
}
if (HardcodedMethodConstants.SERIAL_VERSION_UID.equals(name)) {
return true;
}
final Matcher matcher = m_regexPattern.matcher(name);
return matcher.matches();
}
@Override
public void readSettings(@NotNull Element element) throws InvalidDataException {
super.readSettings(element);
m_regexPattern = Pattern.compile(m_regex);
}
public OptRegularComponent @NotNull [] createExtraOptions() {
return new OptRegularComponent[0];
}
@Override
public @NotNull OptPane getOptionsPane() {
return CommonOptionPanes.conventions(
"m_minLength", "m_maxLength", "m_regex", createExtraOptions()
);
}
@Override
public @NotNull OptionController getOptionController() {
return super.getOptionController().onValueSet("m_regex", value -> {
try {
m_regexPattern = Pattern.compile(m_regex);
}
catch (PatternSyntaxException ignore) {
m_regex = getDefaultRegex();
m_regexPattern = Pattern.compile(m_regex);
}
});
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new RenameFix();
}
}
@@ -1,89 +0,0 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiVariable;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import org.jetbrains.annotations.NotNull;
public final class DollarSignInNameInspection extends BaseInspection {
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new RenameFix();
}
@Override
protected @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"dollar.sign.in.name.problem.descriptor");
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new DollarSignInNameVisitor();
}
private static class DollarSignInNameVisitor extends BaseInspectionVisitor {
@Override
public void visitVariable(@NotNull PsiVariable variable) {
super.visitVariable(variable);
final String name = variable.getName();
if (name == null) {
return;
}
if (name.indexOf('$') < 0) {
return;
}
registerVariableError(variable);
}
@Override
public void visitMethod(@NotNull PsiMethod method) {
super.visitMethod(method);
final String name = method.getName();
if (name.indexOf('$') < 0) {
return;
}
registerMethodError(method);
}
@Override
public void visitClass(@NotNull PsiClass aClass) {
//note: no call to super, to avoid drill-down
final String name = aClass.getName();
if (name == null) {
return;
}
if (name.indexOf('$') < 0) {
return;
}
registerClassError(aClass);
}
}
}
@@ -1,48 +0,0 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.codeInspection.naming.NamingConventionWithFallbackBean;
import com.intellij.psi.PsiClass;
import com.siyeh.InspectionGadgetsBundle;
public final class EnumeratedClassNamingConvention extends NamingConvention<PsiClass> {
private static final int DEFAULT_MIN_LENGTH = 8;
private static final int DEFAULT_MAX_LENGTH = 64;
@Override
public NamingConventionBean createDefaultBean() {
return new NamingConventionWithFallbackBean("[A-Z][A-Za-z\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public boolean isApplicable(PsiClass member) {
return member.isEnum();
}
@Override
public String getShortName() {
return "EnumeratedClassNamingConvention";
}
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("enumerated.class.naming.convention.element.description");
}
}
@@ -1,48 +0,0 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.psi.PsiEnumConstant;
import com.intellij.psi.PsiField;
import com.siyeh.InspectionGadgetsBundle;
public final class EnumeratedConstantNamingConvention extends NamingConvention<PsiField> {
private static final int DEFAULT_MIN_LENGTH = 5;
private static final int DEFAULT_MAX_LENGTH = 32;
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("enumerated.constant.naming.convention.element.description");
}
@Override
public NamingConventionBean createDefaultBean() {
return new FieldNamingConventionInspection.FieldNamingConventionBean("[A-Z][A-Z_\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public boolean isApplicable(PsiField member) {
return member instanceof PsiEnumConstant;
}
@Override
public String getShortName() {
return "EnumeratedConstantNamingConvention";
}
}
@@ -1,83 +0,0 @@
/*
* Copyright 2003-2011 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiTypeParameter;
import com.intellij.psi.util.InheritanceUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.fixes.RenameFix;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public final class ExceptionNameDoesntEndWithExceptionInspection
extends BaseInspection {
@Override
protected LocalQuickFix buildFix(Object... infos) {
return new RenameFix();
}
@Override
public @NotNull String getID() {
return "ExceptionClassNameDoesntEndWithException";
}
@Override
protected @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"exception.name.doesnt.end.with.exception.problem.descriptor");
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new ExceptionNameDoesntEndWithExceptionVisitor();
}
private static class ExceptionNameDoesntEndWithExceptionVisitor
extends BaseInspectionVisitor {
@Override
public void visitClass(@NotNull PsiClass aClass) {
// no call to super, so it doesn't drill down into inner classes
if (aClass instanceof PsiTypeParameter) {
return;
}
final String className = aClass.getName();
if (className == null) {
return;
}
final @NonNls String exception = "Exception";
if (className.endsWith(exception)) {
return;
}
if (!InheritanceUtil.isInheritor(aClass,
CommonClassNames.JAVA_LANG_EXCEPTION)) {
return;
}
registerClassError(aClass);
}
}
}
@@ -1,47 +0,0 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.naming;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.naming.AbstractNamingConventionInspection;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.psi.JavaElementVisitor;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiField;
import com.siyeh.HardcodedMethodConstants;
import com.siyeh.ig.fixes.RenameFix;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class FieldNamingConventionInspection extends AbstractNamingConventionInspection<PsiField> {
public static final ExtensionPointName<NamingConvention<PsiField>> EP_NAME = ExtensionPointName.create("com.intellij.naming.convention.field");
public FieldNamingConventionInspection() {
super(EP_NAME.getExtensionList(), null);
registerConventionsListener(EP_NAME);
}
@Override
protected @Nullable LocalQuickFix createRenameFix() {
return new RenameFix();
}
@Override
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new JavaElementVisitor() {
@Override
public void visitField(@NotNull PsiField field) {
String name = field.getName();
checkName(field, name, holder);
}
};
}
static class FieldNamingConventionBean extends NamingConventionBean {
FieldNamingConventionBean(@NonNls String regex, int minLength, int maxLength) {
super(regex, minLength, maxLength, HardcodedMethodConstants.SERIAL_VERSION_UID);
}
}
}
@@ -1,47 +0,0 @@
// Copyright 2000-2017 JetBrains s.r.o.
// Use of this source code is governed by the Apache 2.0 license that can be
// found in the LICENSE file.
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.AbstractNamingConventionMerger;
import com.intellij.psi.PsiField;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
public final class FieldNamingConventionMerger extends AbstractNamingConventionMerger<PsiField> {
public FieldNamingConventionMerger() {
super(new FieldNamingConventionInspection());
}
@Override
protected Element getSourceElement(@NotNull Map<String, Element> inspectionElements, @NotNull String sourceToolName) {
if (sourceToolName.equals(ConstantWithMutableFieldTypeNamingConvention.CONSTANT_WITH_MUTABLE_FIELD_TYPE_NAMING_CONVENTION_SHORT_NAME)) {
Element element = inspectionElements.get(ConstantNamingConvention.CONSTANT_NAMING_CONVENTION_SHORT_NAME);
if (element != null) {
for (@NonNls Element option : element.getChildren("option")) {
if ("onlyCheckImmutables".equals(option.getAttributeValue("name"))) {
if (!Boolean.parseBoolean(option.getAttributeValue("value"))) {
return element;
}
break;
}
}
}
element = inspectionElements.get(StaticVariableNamingConvention.STATIC_VARIABLE_NAMING_CONVENTION_SHORT_NAME);
if (element != null) {
for (@NonNls Element option : element.getChildren("option")) {
if ("checkMutableFinals".equals(option.getAttributeValue("name"))) {
return Boolean.parseBoolean(option.getAttributeValue("value")) ? element : null;
}
}
}
return null;
}
return super.getSourceElement(inspectionElements, sourceToolName);
}
}
@@ -1,50 +0,0 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.siyeh.InspectionGadgetsBundle;
import org.jetbrains.annotations.NonNls;
public final class InstanceMethodNamingConvention extends NamingConvention<PsiMethod> {
private static final int DEFAULT_MIN_LENGTH = 4;
private static final int DEFAULT_MAX_LENGTH = 32;
public static final @NonNls String INSTANCE_METHOD_NAMING_CONVENTION = "InstanceMethodNamingConvention";
@Override
public NamingConventionBean createDefaultBean() {
return new NamingConventionBean("[a-z][A-Za-z\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("instance.method.naming.convention.element.description");
}
@Override
public boolean isApplicable(PsiMethod member) {
return !member.hasModifierProperty(PsiModifier.STATIC);
}
@Override
public String getShortName() {
return INSTANCE_METHOD_NAMING_CONVENTION;
}
}
@@ -1,49 +0,0 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiModifier;
import com.siyeh.InspectionGadgetsBundle;
public final class InstanceVariableNamingConvention extends NamingConvention<PsiField> {
private static final int DEFAULT_MIN_LENGTH = 5;
private static final int DEFAULT_MAX_LENGTH = 32;
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("instance.variable.naming.convention.element.description");
}
@Override
public NamingConventionBean createDefaultBean() {
return new FieldNamingConventionInspection.FieldNamingConventionBean("m_[a-z][A-Za-z\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public boolean isApplicable(PsiField field) {
return !field.hasModifierProperty(PsiModifier.STATIC);
}
@Override
public String getShortName() {
return "InstanceVariableNamingConvention";
}
}
@@ -1,48 +0,0 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.codeInspection.naming.NamingConventionWithFallbackBean;
import com.intellij.psi.PsiClass;
import com.siyeh.InspectionGadgetsBundle;
public final class InterfaceNamingConvention extends NamingConvention<PsiClass> {
private static final int DEFAULT_MIN_LENGTH = 8;
private static final int DEFAULT_MAX_LENGTH = 64;
@Override
public NamingConventionBean createDefaultBean() {
return new NamingConventionWithFallbackBean("[A-Z][A-Za-z\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("interface.naming.convention.element.description");
}
@Override
public boolean isApplicable(PsiClass member) {
return member.isInterface();
}
@Override
public String getShortName() {
return "InterfaceNamingConvention";
}
}
@@ -1,74 +0,0 @@
/*
* Copyright 2003-2025 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiLambdaExpression;
import com.intellij.psi.PsiParameter;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspectionVisitor;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
*/
public final class LambdaParameterNamingConventionInspection extends ConventionInspection {
private static final int DEFAULT_MIN_LENGTH = 1;
private static final int DEFAULT_MAX_LENGTH = 20;
@Override
protected String getElementDescription() {
return InspectionGadgetsBundle.message("lambda.parameter.naming.convention.element.description");
}
@Override
protected String getDefaultRegex() {
return "[a-z][A-Za-z\\d]*";
}
@Override
protected int getDefaultMinLength() {
return DEFAULT_MIN_LENGTH;
}
@Override
protected int getDefaultMaxLength() {
return DEFAULT_MAX_LENGTH;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new NamingConventionsVisitor();
}
private class NamingConventionsVisitor extends BaseInspectionVisitor {
@Override
public void visitParameter(@NotNull PsiParameter variable) {
if (variable.isUnnamed()) return;
final PsiElement scope = variable.getDeclarationScope();
if (!(scope instanceof PsiLambdaExpression)) {
return;
}
final String name = variable.getName();
if (isValid(name)) {
return;
}
registerVariableError(variable, name);
}
}
}
@@ -1,151 +0,0 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.naming;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.psi.LambdaUtil;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiParameterList;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiTypes;
import com.intellij.psi.util.PsiSuperMethodUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.fixes.RenameFix;
import com.siyeh.ig.psiutils.MethodUtils;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
/**
* @author Bas Leijdekkers
*/
public final class LambdaUnfriendlyMethodOverloadInspection extends BaseInspection {
@Override
protected @Nullable LocalQuickFix buildFix(Object... infos) {
final PsiMethod method = (PsiMethod)infos[0];
if (method.isConstructor()) {
return null;
}
return new RenameFix();
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
protected @NotNull String buildErrorString(Object... infos) {
final PsiMethod method = (PsiMethod)infos[0];
return InspectionGadgetsBundle.message(method.isConstructor()
? "lambda.unfriendly.constructor.overload.problem.descriptor"
: "lambda.unfriendly.method.overload.problem.descriptor");
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new LambdaUnfriendlyMethodOverloadVisitor();
}
private static class LambdaUnfriendlyMethodOverloadVisitor extends BaseInspectionVisitor {
@Override
public void visitMethod(@NotNull PsiMethod method) {
super.visitMethod(method);
final PsiParameterList parameterList = method.getParameterList();
final int parametersCount = parameterList.getParametersCount();
if (parametersCount == 0) {
return;
}
final PsiParameter[] parameters = parameterList.getParameters();
final IntList functionalIndices = new IntArrayList(2);
for (int i = 0; i < parameters.length; i++) {
final PsiParameter parameter = parameters[i];
if (LambdaUtil.isFunctionalType(parameter.getType())) {
functionalIndices.add(i);
}
}
if (functionalIndices.isEmpty()) {
return;
}
final PsiClass containingClass = method.getContainingClass();
if (containingClass == null) {
return;
}
if (MethodUtils.hasSuper(method)) {
return;
}
final String name = method.getName();
outer:
for (PsiMethod sameNameMethod : containingClass.findMethodsByName(name, true)) {
if (method.equals(sameNameMethod) || PsiSuperMethodUtil.isSuperMethod(method, sameNameMethod)) {
continue;
}
final PsiParameterList otherParameterList = sameNameMethod.getParameterList();
if (parametersCount != otherParameterList.getParametersCount()) {
continue;
}
final PsiParameter[] otherParameters = otherParameterList.getParameters();
if (!areOtherParameterTypesConvertible(parameters, otherParameters, functionalIndices)) {
continue;
}
final int max = functionalIndices.size();
boolean equalTypes = true;
for (int i = 0; i < max; i++) {
final int index = functionalIndices.getInt(i);
final PsiType otherFunctionalType = otherParameters[index].getType();
if (!LambdaUtil.isFunctionalType(otherFunctionalType)) {
continue outer;
}
final PsiType functionalType = parameters[index].getType();
if (!areSameShapeFunctionalTypes(functionalType, otherFunctionalType)) {
continue outer;
}
equalTypes &= Objects.equals(TypeConversionUtil.erasure(functionalType), TypeConversionUtil.erasure(otherFunctionalType));
}
if (equalTypes) {
continue;
}
registerMethodError(method, method);
return;
}
}
private static boolean areSameShapeFunctionalTypes(PsiType one, PsiType two) {
final PsiMethod method1 = LambdaUtil.getFunctionalInterfaceMethod(one);
final PsiMethod method2 = LambdaUtil.getFunctionalInterfaceMethod(two);
if (method1 == null || method2 == null) {
return false;
}
final PsiType returnType1 = method1.getReturnType();
final PsiType returnType2 = method2.getReturnType();
if (PsiTypes.voidType().equals(returnType1) ^ PsiTypes.voidType().equals(returnType2)) {
return false;
}
return method1.getParameterList().getParametersCount() == method2.getParameterList().getParametersCount();
}
private static boolean areOtherParameterTypesConvertible(PsiParameter[] parameters, PsiParameter[] otherParameters,
IntList ignores) {
for (int i = 0; i < parameters.length; i++) {
if (ignores.contains(i)) {
continue;
}
final PsiType type = TypeConversionUtil.erasure(parameters[i].getType());
final PsiType otherType = TypeConversionUtil.erasure(otherParameters[i].getType());
if (!type.isAssignableFrom(otherType) && !otherType.isAssignableFrom(type)) {
return false;
}
}
return true;
}
}
}
@@ -1,124 +0,0 @@
/*
* Copyright 2003-2025 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.options.OptPane;
import com.intellij.codeInspection.options.OptRegularComponent;
import com.intellij.psi.PsiCatchSection;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiForStatement;
import com.intellij.psi.PsiForeachStatement;
import com.intellij.psi.PsiLocalVariable;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiStatement;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspectionVisitor;
import org.jetbrains.annotations.NotNull;
public final class LocalVariableNamingConventionInspection extends ConventionInspection {
private static final int DEFAULT_MIN_LENGTH = 1;
private static final int DEFAULT_MAX_LENGTH = 20;
/**
* @noinspection PublicField
*/
public boolean m_ignoreForLoopParameters = false;
/**
* @noinspection PublicField
*/
public boolean m_ignoreCatchParameters = false;
@Override
public OptRegularComponent @NotNull [] createExtraOptions() {
return new OptRegularComponent[] {
OptPane.checkbox("m_ignoreForLoopParameters", InspectionGadgetsBundle.message("local.variable.naming.convention.ignore.option")),
OptPane.checkbox("m_ignoreCatchParameters", InspectionGadgetsBundle.message("local.variable.naming.convention.ignore.catch.option"))
};
}
@Override
protected String getElementDescription() {
return InspectionGadgetsBundle.message("local.variable.naming.convention.element.description");
}
@Override
protected String getDefaultRegex() {
return "[a-z][A-Za-z\\d]*";
}
@Override
protected int getDefaultMinLength() {
return DEFAULT_MIN_LENGTH;
}
@Override
protected int getDefaultMaxLength() {
return DEFAULT_MAX_LENGTH;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new NamingConventionsVisitor();
}
private class NamingConventionsVisitor extends BaseInspectionVisitor {
@Override
public void visitLocalVariable(@NotNull PsiLocalVariable variable) {
super.visitLocalVariable(variable);
if (variable.isUnnamed()) return;
if (m_ignoreForLoopParameters) {
final PsiElement parent = variable.getParent();
if (parent != null) {
final PsiElement grandparent = parent.getParent();
if (grandparent instanceof PsiForStatement forLoop) {
final PsiStatement initialization = forLoop.getInitialization();
if (parent.equals(initialization)) {
return;
}
}
}
}
final String name = variable.getName();
if (isValid(name)) {
return;
}
registerVariableError(variable, name);
}
@Override
public void visitParameter(@NotNull PsiParameter variable) {
if (variable.isUnnamed()) return;
final PsiElement scope = variable.getDeclarationScope();
final boolean isCatchParameter = scope instanceof PsiCatchSection;
final boolean isForeachParameter = scope instanceof PsiForeachStatement;
if (!isCatchParameter && !isForeachParameter) {
return;
}
if (m_ignoreCatchParameters && isCatchParameter) {
return;
}
if (m_ignoreForLoopParameters && isForeachParameter) {
return;
}
final String name = variable.getName();
if (isValid(name)) {
return;
}
registerVariableError(variable, name);
}
}
}
@@ -1,125 +0,0 @@
/*
* Copyright 2003-2009 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.modcommand.ModPsiUpdater;
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiImplicitClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.PsiSubstitutor;
import com.intellij.psi.PsiTypeElement;
import com.intellij.psi.util.MethodSignature;
import com.intellij.psi.util.MethodSignatureUtil;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.fixes.RenameFix;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public final class MethodNameSameAsClassNameInspection extends BaseInspection {
private static final Set<String> MODIFIERS_ALLOWED_ON_CONSTRUCTORS = Set.of(
// JLS 8.8.3
PsiModifier.PUBLIC, PsiModifier.PROTECTED, PsiModifier.PRIVATE
);
@Override
protected LocalQuickFix @NotNull [] buildFixes(Object... infos) {
final Boolean onTheFly = (Boolean)infos[0];
final Boolean canBeConvertedToConstructor = (Boolean)infos[1];
List<LocalQuickFix> fixes = new ArrayList<>();
if (onTheFly) {
fixes.add(new RenameFix());
}
if (canBeConvertedToConstructor) {
fixes.add(new MethodNameSameAsClassNameFix());
}
return fixes.toArray(LocalQuickFix.EMPTY_ARRAY);
}
@Override
protected @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"method.name.same.as.class.name.problem.descriptor");
}
@Override
public boolean isEnabledByDefault() {
return true;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new MethodNameSameAsClassNameVisitor();
}
private static class MethodNameSameAsClassNameFix
extends PsiUpdateModCommandQuickFix {
@Override
public @NotNull String getFamilyName() {
return InspectionGadgetsBundle.message("make.method.ctr.quickfix");
}
@Override
protected void applyFix(@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) {
final PsiMethod method = ObjectUtils.tryCast(element.getParent(), PsiMethod.class);
if (method == null) return;
final PsiTypeElement returnTypeElement = method.getReturnTypeElement();
if (returnTypeElement == null) return;
PsiModifierList modifiers = method.getModifierList();
for (String modifier : PsiModifier.MODIFIERS) {
if (!MODIFIERS_ALLOWED_ON_CONSTRUCTORS.contains(modifier)) {
modifiers.setModifierProperty(modifier, false);
}
}
returnTypeElement.delete();
}
}
private static class MethodNameSameAsClassNameVisitor
extends BaseInspectionVisitor {
@Override
public void visitMethod(@NotNull PsiMethod method) {
// no call to super, so it doesn't drill down into inner classes
if (method.isConstructor()) return;
final String methodName = method.getName();
final PsiClass containingClass = method.getContainingClass();
if (containingClass == null) return;
if (containingClass instanceof PsiImplicitClass) return;
final String className = containingClass.getName();
if (!methodName.equals(className)) return;
MethodSignature signature = method.getSignature(PsiSubstitutor.EMPTY);
boolean canReplaceWithConstructor =
method.getBody() != null && !containingClass.isInterface() &&
!ContainerUtil.exists(containingClass.getConstructors(), ctor -> MethodSignatureUtil.areErasedParametersEqual(signature, ctor.getSignature(PsiSubstitutor.EMPTY)));
registerMethodError(method, isOnTheFly(), canReplaceWithConstructor);
}
}
}
@@ -1,81 +0,0 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import org.jetbrains.annotations.NotNull;
public final class MethodNameSameAsParentNameInspection extends BaseInspection {
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new RenameFix();
}
@Override
protected @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"method.name.same.as.parent.name.problem.descriptor");
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new MethodNameSameAsParentClassNameVisitor();
}
private static class MethodNameSameAsParentClassNameVisitor
extends BaseInspectionVisitor {
@Override
public void visitMethod(@NotNull PsiMethod method) {
// no call to super, so it doesn't drill down into inner classes
if (method.isConstructor()) {
return;
}
if (method.getNameIdentifier() == null) {
return;
}
final PsiClass containingClass = method.getContainingClass();
if (containingClass == null) {
return;
}
final PsiClass parent = containingClass.getSuperClass();
if (parent == null) {
return;
}
final String parentName = parent.getName();
if (parentName == null) {
return;
}
final String methodName = method.getName();
if (!methodName.equals(parentName)) {
return;
}
registerMethodError(method);
}
}
}
@@ -1,25 +0,0 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.AbstractNamingConventionMerger;
import com.intellij.psi.PsiMethod;
public final class MethodNamingConventionMerger extends AbstractNamingConventionMerger<PsiMethod> {
public MethodNamingConventionMerger() {
super(new NewMethodNamingConventionInspection());
}
}
@@ -1,115 +0,0 @@
/*
* Copyright 2003-2013 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.options.OptPane;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiIdentifier;
import com.intellij.psi.PsiMethod;
import com.intellij.util.containers.CollectionFactory;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import com.siyeh.ig.psiutils.MethodUtils;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
import static com.intellij.codeInspection.options.OptPane.checkbox;
import static com.intellij.codeInspection.options.OptPane.pane;
public final class MisspelledMethodNameInspection extends BaseInspection {
@SuppressWarnings("PublicField")
public boolean ignoreIfMethodIsOverride = true;
@Override
public @NotNull OptPane getOptionsPane() {
return pane(
checkbox("ignoreIfMethodIsOverride", InspectionGadgetsBundle.message("ignore.methods.overriding.super.method")));
}
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new RenameFix((String)infos[0]);
}
@Override
public @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message("method.names.differ.only.by.case.problem.descriptor", infos[0]);
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new MethodNamesDifferOnlyByCaseVisitor();
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
private class MethodNamesDifferOnlyByCaseVisitor extends BaseInspectionVisitor {
@Override
public void visitClass(@NotNull PsiClass aClass) {
super.visitClass(aClass);
PsiMethod[] methods = aClass.getAllMethods();
Map<String, PsiMethod> methodNames = CollectionFactory.createCaseInsensitiveStringMap();
Map<PsiIdentifier, String> errorNames = new HashMap<>();
for (PsiMethod method : methods) {
ProgressManager.checkCanceled();
if (method.isConstructor()) continue;
if (ignoreIfMethodIsOverride && MethodUtils.hasSuper(method)) {
continue;
}
String name = method.getName();
PsiMethod existing = methodNames.get(name);
if (existing == null) {
methodNames.put(name, method);
}
else {
PsiClass methodClass = method.getContainingClass();
PsiClass existingMethodClass = existing.getContainingClass();
String existingName = existing.getName();
if (!name.equals(existingName)) {
if (existingMethodClass == aClass) {
PsiIdentifier identifier = existing.getNameIdentifier();
if (identifier != null) {
errorNames.put(identifier, name);
}
}
if (methodClass == aClass) {
PsiIdentifier identifier = method.getNameIdentifier();
if (identifier != null && identifier.isPhysical()) {
errorNames.put(identifier, existingName);
}
}
}
}
}
for (Map.Entry<PsiIdentifier, String> entry : errorNames.entrySet()) {
PsiIdentifier identifier = entry.getKey();
String otherName = entry.getValue();
registerError(identifier, otherName);
}
}
}
}
@@ -1,48 +0,0 @@
/*
* Copyright 2003-2014 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.siyeh.InspectionGadgetsBundle;
public final class NativeMethodNamingConvention extends NamingConvention<PsiMethod>{
private static final int DEFAULT_MIN_LENGTH = 4;
private static final int DEFAULT_MAX_LENGTH = 32;
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("native.method.naming.convention.element.description");
}
@Override
public NamingConventionBean createDefaultBean() {
return new NamingConventionBean("[a-z][A-Za-z\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public boolean isApplicable(PsiMethod member) {
return member.hasModifierProperty(PsiModifier.NATIVE);
}
@Override
public String getShortName() {
return "NativeMethodNamingConvention";
}
}
@@ -1,57 +0,0 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.naming.AbstractNamingConventionInspection;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.psi.JavaElementVisitor;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassOwner;
import com.intellij.psi.PsiElementVisitor;
import com.siyeh.ig.fixes.RenameFix;
import org.jetbrains.annotations.NotNull;
public class NewClassNamingConventionInspection extends AbstractNamingConventionInspection<PsiClass> {
public static final ExtensionPointName<NamingConvention<PsiClass>> EP_NAME = ExtensionPointName.create("com.intellij.naming.convention.class");
public NewClassNamingConventionInspection() {
super(EP_NAME.getExtensionList(), ClassNamingConvention.CLASS_NAMING_CONVENTION_SHORT_NAME);
registerConventionsListener(EP_NAME);
}
@Override
protected LocalQuickFix createRenameFix() {
return new RenameFix();
}
@Override
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!(holder.getFile() instanceof PsiClassOwner)) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@Override
public void visitClass(@NotNull PsiClass aClass) {
final String name = aClass.getName();
if (name == null) return;
checkName(aClass, name, holder);
}
};
}
}
@@ -1,56 +0,0 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.naming;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.naming.AbstractNamingConventionInspection;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.psi.JavaElementVisitor;
import com.intellij.psi.PsiClassOwner;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiMethod;
import com.siyeh.ig.fixes.RenameFix;
import com.siyeh.ig.psiutils.LibraryUtil;
import com.siyeh.ig.psiutils.MethodUtils;
import org.jetbrains.annotations.NotNull;
public final class NewMethodNamingConventionInspection extends AbstractNamingConventionInspection<PsiMethod> {
public static final ExtensionPointName<NamingConvention<PsiMethod>> EP_NAME = ExtensionPointName.create("com.intellij.naming.convention.method");
public NewMethodNamingConventionInspection() {
super(EP_NAME.getExtensionList(), InstanceMethodNamingConvention.INSTANCE_METHOD_NAMING_CONVENTION);
registerConventionsListener(EP_NAME);
}
@Override
protected LocalQuickFix createRenameFix() {
return new RenameFix();
}
@Override
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!(holder.getFile() instanceof PsiClassOwner)) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@Override
public void visitMethod(@NotNull PsiMethod method) {
if (method.isConstructor()) {
return;
}
if (!isOnTheFly && MethodUtils.hasSuper(method)) {
return;
}
if (LibraryUtil.isOverrideOfLibraryMethod(method)) {
return;
}
String name = method.getName();
checkName(method, name, holder);
}
};
}
}
@@ -1,148 +0,0 @@
/*
* Copyright 2003-2021 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.options.OptPane;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiTypes;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import com.siyeh.ig.fixes.SuppressForTestsScopeFix;
import com.siyeh.ig.psiutils.LibraryUtil;
import com.siyeh.ig.psiutils.MethodUtils;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import static com.intellij.codeInspection.options.OptPane.checkbox;
import static com.intellij.codeInspection.options.OptPane.pane;
import static com.intellij.codeInspection.options.OptPane.stringList;
public class NonBooleanMethodNameMayNotStartWithQuestionInspection extends BaseInspection {
public static final @NonNls String DEFAULT_QUESTION_WORDS =
"are,can,check,contains,could,endsWith,equals,has,is,matches,must,shall,should,startsWith,was,were,will,would";
@SuppressWarnings("PublicField") public @NonNls String questionString = DEFAULT_QUESTION_WORDS;
@SuppressWarnings("PublicField")
public boolean ignoreBooleanMethods = false;
@SuppressWarnings("PublicField")
public boolean onlyWarnOnBaseMethods = true;
List<String> questionList = new ArrayList<>(32);
public NonBooleanMethodNameMayNotStartWithQuestionInspection() {
parseString(questionString, questionList);
}
@Override
public @NotNull OptPane getOptionsPane() {
return pane(
stringList("questionList", InspectionGadgetsBundle.message("boolean.method.name.must.start.with.question.table.label")),
checkbox("ignoreBooleanMethods", InspectionGadgetsBundle.message("ignore.methods.with.boolean.return.type.option")),
checkbox("onlyWarnOnBaseMethods", InspectionGadgetsBundle.message("ignore.methods.overriding.super.method"))
);
}
@Override
protected LocalQuickFix @NotNull [] buildFixes(Object... infos) {
final PsiElement context = (PsiElement)infos[0];
final LocalQuickFix suppressFix = SuppressForTestsScopeFix.build(this, context);
if (suppressFix == null) {
return new InspectionGadgetsFix[]{new RenameFix()};
}
return new LocalQuickFix[]{new RenameFix(), suppressFix};
}
@Override
public @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message("non.boolean.method.name.must.not.start.with.question.problem.descriptor");
}
@Override
public void readSettings(@NotNull Element element) throws InvalidDataException {
super.readSettings(element);
parseString(questionString, questionList);
}
@Override
public void writeSettings(@NotNull Element element) throws WriteExternalException {
questionString = formatString(questionList);
super.writeSettings(element);
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new NonBooleanMethodNameMayNotStartWithQuestionVisitor();
}
private class NonBooleanMethodNameMayNotStartWithQuestionVisitor extends BaseInspectionVisitor {
@Override
public void visitMethod(@NotNull PsiMethod method) {
super.visitMethod(method);
final PsiType returnType = method.getReturnType();
if (returnType == null || returnType.equals(PsiTypes.booleanType())) {
return;
}
if (ignoreBooleanMethods && returnType.equalsToText(CommonClassNames.JAVA_LANG_BOOLEAN)) {
return;
}
if (!startsWithQuestionWord(method.getName())) {
return;
}
if (onlyWarnOnBaseMethods) {
if (MethodUtils.hasSuper(method)) {
return;
}
}
else if (LibraryUtil.isOverrideOfLibraryMethod(method)) {
return;
}
registerMethodError(method, method);
}
}
protected boolean startsWithQuestionWord(String name) {
for (String question : questionList) {
if (name.startsWith(question)) {
if (name.length() == question.length()) {
return true;
}
final char nextChar = name.charAt(question.length());
if (Character.isUpperCase(nextChar) || nextChar == '_') {
return true;
}
}
}
return false;
}
}
@@ -1,136 +0,0 @@
/*
* Copyright 2003-2018 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.modcommand.ModPsiUpdater;
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
import com.intellij.openapi.project.Project;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementFactory;
import com.intellij.psi.PsiJavaCodeReferenceElement;
import com.intellij.psi.PsiReferenceList;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.InheritanceUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.fixes.RenameFix;
import com.siyeh.ig.psiutils.CommentTracker;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public final class NonExceptionNameEndsWithExceptionInspection extends BaseInspection {
@Override
protected LocalQuickFix @NotNull [] buildFixes(Object... infos) {
final String name = (String)infos[0];
final Boolean onTheFly = (Boolean)infos[1];
if (onTheFly.booleanValue()) {
return new LocalQuickFix[]{new RenameFix(),
new ExtendExceptionFix(name)};
}
else {
return new LocalQuickFix[]{
new ExtendExceptionFix(name)};
}
}
@Override
protected @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"non.exception.name.ends.with.exception.problem.descriptor");
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new NonExceptionNameEndsWithExceptionVisitor();
}
private static class ExtendExceptionFix extends PsiUpdateModCommandQuickFix {
private final String name;
ExtendExceptionFix(String name) {
this.name = name;
}
@Override
public @NotNull String getName() {
return InspectionGadgetsBundle.message(
"non.exception.name.ends.with.exception.quickfix", name);
}
@Override
public @NotNull String getFamilyName() {
return InspectionGadgetsBundle.message("extend.exception.fix.family.name");
}
@Override
protected void applyFix(@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) {
final PsiElement parent = element.getParent();
if (!(parent instanceof PsiClass aClass)) {
return;
}
final PsiReferenceList extendsList = aClass.getExtendsList();
if (extendsList == null) {
return;
}
final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
final PsiElementFactory factory = facade.getElementFactory();
final GlobalSearchScope scope = aClass.getResolveScope();
final PsiJavaCodeReferenceElement reference =
factory.createReferenceElementByFQClassName(CommonClassNames.JAVA_LANG_EXCEPTION, scope);
CommentTracker tracker = new CommentTracker();
final PsiJavaCodeReferenceElement[] referenceElements = extendsList.getReferenceElements();
for (PsiJavaCodeReferenceElement referenceElement : referenceElements) {
tracker.delete(referenceElement);
}
tracker.insertCommentsBefore(extendsList.add(reference));
}
}
private static class NonExceptionNameEndsWithExceptionVisitor
extends BaseInspectionVisitor {
@Override
public void visitClass(@NotNull PsiClass aClass) {
// no call to super, so it doesn't drill down into inner classes
final String className = aClass.getName();
if (className == null) {
return;
}
final @NonNls String exception = "Exception";
if (!className.endsWith(exception)) {
return;
}
if (InheritanceUtil.isInheritor(aClass,
CommonClassNames.JAVA_LANG_EXCEPTION)) {
return;
}
registerClassError(aClass, className,
Boolean.valueOf(isOnTheFly()));
}
}
}
@@ -1,178 +0,0 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.naming;
import com.intellij.analysis.AnalysisScope;
import com.intellij.codeInspection.CommonProblemDescriptor;
import com.intellij.codeInspection.GlobalInspectionContext;
import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.options.CommonOptionPanes;
import com.intellij.codeInspection.options.OptPane;
import com.intellij.codeInspection.options.OptionController;
import com.intellij.codeInspection.reference.RefPackage;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiJavaCodeReferenceElement;
import com.intellij.psi.PsiPackageStatement;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.BaseSharedLocalInspection;
import com.siyeh.ig.PackageGlobalInspection;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public final class PackageNamingConventionInspection extends PackageGlobalInspection {
private static final int DEFAULT_MIN_LENGTH = 3;
private static final int DEFAULT_MAX_LENGTH = 16;
private static final String DEFAULT_NAME = "<default>";
private static final String DEFAULT_REGEX = "[a-z]*";
/**
* @noinspection PublicField
*/
public @NonNls String m_regex = DEFAULT_REGEX;
/**
* @noinspection PublicField
*/
public int m_minLength = DEFAULT_MIN_LENGTH;
/**
* @noinspection PublicField
*/
public int m_maxLength = DEFAULT_MAX_LENGTH;
private Pattern m_regexPattern = Pattern.compile(m_regex);
@Override
public CommonProblemDescriptor @Nullable [] checkPackage(@NotNull RefPackage refPackage,
@NotNull AnalysisScope analysisScope,
@NotNull InspectionManager inspectionManager,
@NotNull GlobalInspectionContext globalInspectionContext) {
final @NonNls String name = StringUtil.getShortName(refPackage.getQualifiedName());
if (DEFAULT_NAME.equals(name)) {
return null;
}
final int length = name.length();
if (length == 0) {
return null;
}
if (length < m_minLength) {
final String errorString = InspectionGadgetsBundle.message("package.naming.convention.problem.descriptor.short", name);
return new CommonProblemDescriptor[]{inspectionManager.createProblemDescriptor(errorString)};
}
if (length > m_maxLength) {
final String errorString = InspectionGadgetsBundle.message("package.naming.convention.problem.descriptor.long", name);
return new CommonProblemDescriptor[]{inspectionManager.createProblemDescriptor(errorString)};
}
final Matcher matcher = m_regexPattern.matcher(name);
if (matcher.matches()) {
return null;
}
else {
final String errorString =
InspectionGadgetsBundle.message("package.naming.convention.problem.descriptor.regex.mismatch", name, m_regex);
return new CommonProblemDescriptor[]{inspectionManager.createProblemDescriptor(errorString)};
}
}
@Override
public void readSettings(@NotNull Element element) throws InvalidDataException {
super.readSettings(element);
m_regexPattern = Pattern.compile(m_regex);
}
@Override
public @NotNull OptPane getOptionsPane() {
return CommonOptionPanes.conventions("m_minLength", "m_maxLength", "m_regex");
}
@Override
public @NotNull OptionController getOptionController() {
return super.getOptionController().onValueSet("m_regex", value -> {
try {
m_regexPattern = Pattern.compile(m_regex);
}
catch (PatternSyntaxException ignore) {
m_regex = DEFAULT_REGEX;
m_regexPattern = Pattern.compile(m_regex);
}
});
}
boolean isValid(String name) {
final int length = name.length();
if (length < m_minLength) {
return false;
}
if (m_maxLength > 0 && length > m_maxLength) {
return false;
}
final Matcher matcher = m_regexPattern.matcher(name);
return matcher.matches();
}
@Override
public @Nullable LocalInspectionTool getSharedLocalInspectionTool() {
return new LocalPackageNamingConventionInspection(this);
}
@SuppressWarnings("InspectionDescriptionNotFoundInspection") // TODO IJPL-166089
private static class LocalPackageNamingConventionInspection extends BaseSharedLocalInspection<PackageNamingConventionInspection> {
LocalPackageNamingConventionInspection(PackageNamingConventionInspection inspection) {
super(inspection);
}
@Override
protected @NotNull String buildErrorString(Object... infos) {
final String name = (String)infos[0];
if (name.length() < mySettingsDelegate.m_minLength) {
return InspectionGadgetsBundle.message("package.naming.convention.problem.descriptor.short", name);
}
else if (name.length() > mySettingsDelegate.m_maxLength) {
return InspectionGadgetsBundle.message("package.naming.convention.problem.descriptor.long", name);
}
else {
return InspectionGadgetsBundle.message("package.naming.convention.problem.descriptor.regex.mismatch",
name, mySettingsDelegate.m_regex);
}
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new BaseInspectionVisitor() {
@Override
public void visitPackageStatement(@NotNull PsiPackageStatement statement) {
final PsiJavaCodeReferenceElement reference = statement.getPackageReference();
if (reference == null) {
return;
}
final String text = reference.getText();
int start = 0;
int index = text.indexOf('.', start);
while (index > 0) {
final String name = text.substring(start, index);
if (!mySettingsDelegate.isValid(name)) {
registerErrorAtOffset(reference, start, index - start, name);
}
start = index + 1;
index = text.indexOf('.', start);
}
final String lastName = text.substring(start);
if (!lastName.isEmpty() && !mySettingsDelegate.isValid(lastName)) {
registerErrorAtOffset(reference, start, lastName.length(), lastName);
}
}
};
}
}
}
@@ -1,183 +0,0 @@
/*
* Copyright 2003-2023 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.options.OptPane;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiCompiledElement;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiExpressionList;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiMethodCallExpression;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiParameterList;
import com.intellij.psi.PsiReferenceExpression;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.JavaPsiConstructorUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import com.siyeh.ig.psiutils.LibraryUtil;
import com.siyeh.ig.psiutils.MethodUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import static com.intellij.codeInspection.options.OptPane.checkbox;
import static com.intellij.codeInspection.options.OptPane.pane;
public final class ParameterNameDiffersFromOverriddenParameterInspection extends BaseInspection {
/**
* @noinspection PublicField
*/
public boolean m_ignoreSingleCharacterNames = false;
/**
* @noinspection PublicField
*/
public boolean m_ignoreOverridesOfLibraryMethods = false;
@Override
public @NotNull OptPane getOptionsPane() {
return pane(
checkbox("m_ignoreSingleCharacterNames",
InspectionGadgetsBundle.message("parameter.name.differs.from.overridden.parameter.ignore.character.option")),
checkbox("m_ignoreOverridesOfLibraryMethods",
InspectionGadgetsBundle.message("parameter.name.differs.from.overridden.parameter.ignore.library.option")));
}
@Override
protected @Nullable InspectionGadgetsFix buildFix(Object... infos) {
return new RenameFix((String)infos[0], false, false);
}
@Override
public @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message("parameter.name.differs.from.overridden.parameter.problem.descriptor", infos[0], infos[1]);
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new ParameterNameDiffersFromOverriddenParameterVisitor();
}
private class ParameterNameDiffersFromOverriddenParameterVisitor extends BaseInspectionVisitor {
private static final int SUPER_METHOD = 1, OVERLOADED_METHOD = 2, SUPER_CONSTRUCTOR = 3, OVERLOADED_CONSTRUCTOR = 4;
@Override
public void visitMethod(@NotNull PsiMethod method) {
final PsiParameterList parameterList = method.getParameterList();
if (parameterList.isEmpty()) {
return;
}
final PsiMethod superMethod = MethodUtils.getSuper(method);
if (superMethod == null) {
return;
}
checkParameters(superMethod, parameterList.getParameters());
}
@Override
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
final PsiExpressionList argumentList = expression.getArgumentList();
if (argumentList.isEmpty()) {
return;
}
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final boolean constructorCall = JavaPsiConstructorUtil.isConstructorCall(expression);
final boolean superCall = PsiUtil.isJavaToken(methodExpression.getReferenceNameElement(), JavaTokenType.SUPER_KEYWORD);
final PsiMethod method = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true);
if (method == null || !constructorCall && !method.getName().equals(methodExpression.getReferenceName())) {
return;
}
final PsiMethod targetMethod = expression.resolveMethod();
if (targetMethod == null) {
return;
}
final PsiParameterList parameterList = targetMethod.getParameterList();
if (!superCall && parameterList.getParametersCount() <= method.getParameterList().getParametersCount()) {
return;
}
final PsiClass containingClass = targetMethod.getContainingClass();
final PsiClass aClass = method.getContainingClass();
if (containingClass == null || aClass == null || containingClass != aClass && !aClass.isInheritor(containingClass, true)) {
return;
}
if (m_ignoreOverridesOfLibraryMethods && targetMethod instanceof PsiCompiledElement) {
return;
}
final PsiParameter[] parameters = parameterList.getParameters();
final PsiExpression[] arguments = argumentList.getExpressions();
for (int i = 0, length = Math.min(arguments.length, parameters.length); i < length; i++) {
final PsiExpression argument = PsiUtil.skipParenthesizedExprDown(arguments[i]);
if (!(argument instanceof PsiReferenceExpression referenceExpression)) {
continue;
}
if (referenceExpression.getQualifierExpression() != null) {
continue;
}
final PsiElement target = referenceExpression.resolve();
if (!(target instanceof PsiParameter targetParameter) || !(targetParameter.getDeclarationScope() instanceof PsiMethod)) {
continue;
}
final PsiParameter parameter = parameters[i];
final String parameterName = parameter.getName();
if (m_ignoreSingleCharacterNames && parameterName.length() == 1) {
continue;
}
if (targetParameter.getName().equals(parameterName) || !targetParameter.getType().equals(parameter.getType())) {
continue;
}
int type = constructorCall
? superCall ? SUPER_CONSTRUCTOR : OVERLOADED_CONSTRUCTOR
: OVERLOADED_METHOD;
registerVariableError(targetParameter, parameterName, type);
}
}
private void checkParameters(@NotNull PsiMethod superMethod, PsiParameter[] parameters) {
if (m_ignoreOverridesOfLibraryMethods) {
final PsiClass containingClass = superMethod.getContainingClass();
if (containingClass != null && LibraryUtil.classIsInLibrary(containingClass)) {
return;
}
}
final PsiParameterList superParameterList = superMethod.getParameterList();
final PsiParameter[] superParameters = superParameterList.getParameters();
for (int i = 0; i < parameters.length; i++) {
final PsiParameter parameter = parameters[i];
final PsiParameter superParameter = superParameters[i];
final String superParameterName = superParameter.getName();
if (superParameterName.equals(parameter.getName())) {
continue;
}
if (m_ignoreSingleCharacterNames && superParameterName.length() == 1) {
continue;
}
if (!parameter.getType().equals(superParameter.getType())) {
continue;
}
registerVariableError(parameter, superParameterName, SUPER_METHOD);
}
}
}
}
@@ -1,80 +0,0 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.psi.PsiCatchSection;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiForeachStatement;
import com.intellij.psi.PsiLambdaExpression;
import com.intellij.psi.PsiParameter;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspectionVisitor;
import org.jetbrains.annotations.NotNull;
public final class ParameterNamingConventionInspection extends
ConventionInspection {
private static final int DEFAULT_MIN_LENGTH = 1;
private static final int DEFAULT_MAX_LENGTH = 20;
@Override
public @NotNull String getID() {
return "MethodParameterNamingConvention";
}
@Override
protected String getElementDescription() {
return InspectionGadgetsBundle.message("parameter.naming.convention.element.description");
}
@Override
protected String getDefaultRegex() {
return "[a-z][A-Za-z\\d]*";
}
@Override
protected int getDefaultMinLength() {
return DEFAULT_MIN_LENGTH;
}
@Override
protected int getDefaultMaxLength() {
return DEFAULT_MAX_LENGTH;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new NamingConventionsVisitor();
}
private class NamingConventionsVisitor extends BaseInspectionVisitor {
@Override
public void visitParameter(@NotNull PsiParameter variable) {
final PsiElement scope = variable.getDeclarationScope();
if (scope instanceof PsiCatchSection ||
scope instanceof PsiForeachStatement ||
scope instanceof PsiLambdaExpression) {
return;
}
final String name = variable.getName();
if (isValid(name)) {
return;
}
registerVariableError(variable, name);
}
}
}
@@ -1,122 +0,0 @@
/*
* Copyright 2003-2021 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.options.OptPane;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiVariable;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static com.intellij.codeInspection.options.OptPane.pane;
public final class QuestionableNameInspection extends BaseInspection {
/**
* @noinspection PublicField
*/
public @NonNls String nameString = "aa,abc,bad,bar,bar2,baz,baz1,baz2," +
"baz3,bb,blah,bogus,bool,cc,dd,defau1t,dummy,dummy2,ee,fa1se," +
"ff,foo,foo1,foo2,foo3,foobar,four,fred,fred1,fred2,gg,hh,hello," +
"hello1,hello2,hello3,ii,nu11,one,silly,silly2,string,two,that," +
"then,three,whi1e,var";
List<String> nameList = new ArrayList<>(32);
public QuestionableNameInspection() {
parseString(this.nameString, this.nameList);
}
@Override
public @NotNull OptPane getOptionsPane() {
return pane(OptPane.stringList("nameList", InspectionGadgetsBundle.message("questionable.name.list.label")));
}
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new RenameFix();
}
@Override
public @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"questionable.name.problem.descriptor");
}
@Override
public void readSettings(@NotNull Element element) throws InvalidDataException {
super.readSettings(element);
parseString(nameString, nameList);
}
@Override
public void writeSettings(@NotNull Element element) throws WriteExternalException {
nameString = formatString(nameList);
super.writeSettings(element);
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new QuestionableNameVisitor();
}
private class QuestionableNameVisitor extends BaseInspectionVisitor {
private final Set<String> nameSet = new HashSet<>(nameList);
@Override
public void visitVariable(@NotNull PsiVariable variable) {
final String name = variable.getName();
if (nameSet.contains(name)) {
registerVariableError(variable);
}
}
@Override
public void visitMethod(@NotNull PsiMethod method) {
final String name = method.getName();
if (nameSet.contains(name)) {
registerMethodError(method);
}
}
@Override
public void visitClass(@NotNull PsiClass aClass) {
final String name = aClass.getName();
if (nameSet.contains(name)) {
registerClassError(aClass);
}
}
}
}
@@ -1,170 +0,0 @@
/*
* Copyright 2003-2024 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.''
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.options.OptPane;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiPrimitiveType;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiVariable;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
import static com.intellij.codeInspection.options.OptPane.checkbox;
import static com.intellij.codeInspection.options.OptPane.pane;
public final class StandardVariableNamesInspection extends BaseInspection {
static final @NonNls Map<String, String> s_expectedTypes = Map.ofEntries(
Map.entry("b", "byte"),
Map.entry("c", "char"),
Map.entry("ch", "char"),
Map.entry("d", "double"),
Map.entry("f", "float"),
Map.entry("i", "int"),
Map.entry("j", "int"),
Map.entry("k", "int"),
Map.entry("m", "int"),
Map.entry("n", "int"),
Map.entry("l", "long"),
Map.entry("s", CommonClassNames.JAVA_LANG_STRING),
Map.entry("str", CommonClassNames.JAVA_LANG_STRING)
);
static final @NonNls Map<String, String> s_boxingClasses = Map.of(
"int", CommonClassNames.JAVA_LANG_INTEGER,
"short", CommonClassNames.JAVA_LANG_SHORT,
"boolean", CommonClassNames.JAVA_LANG_BOOLEAN,
"long", CommonClassNames.JAVA_LANG_LONG,
"byte", CommonClassNames.JAVA_LANG_BYTE,
"float", CommonClassNames.JAVA_LANG_FLOAT,
"double", CommonClassNames.JAVA_LANG_DOUBLE,
"char", CommonClassNames.JAVA_LANG_CHARACTER
);
@SuppressWarnings("PublicField")
public boolean ignoreParameterNameSameAsSuper = false;
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new RenameFix();
}
@Override
public @NotNull OptPane getOptionsPane() {
return pane(
checkbox("ignoreParameterNameSameAsSuper", InspectionGadgetsBundle.message(
"standard.variable.names.ignore.override.option")));
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
public @NotNull String buildErrorString(Object... infos) {
final PsiVariable variable = (PsiVariable)infos[0];
final String name = variable.getName();
final String expectedType = s_expectedTypes.get(name);
if (PsiUtil.isLanguageLevel5OrHigher(variable)) {
final String boxedType = s_boxingClasses.get(expectedType);
if (boxedType != null) {
return InspectionGadgetsBundle.message(
"standard.variable.names.problem.descriptor2",
expectedType, boxedType);
}
}
return InspectionGadgetsBundle.message(
"standard.variable.names.problem.descriptor", expectedType);
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new StandardVariableNamesVisitor();
}
private class StandardVariableNamesVisitor
extends BaseInspectionVisitor {
@Override
public void visitVariable(@NotNull PsiVariable variable) {
super.visitVariable(variable);
final String variableName = variable.getName();
if (variableName == null) return;
final String expectedType = s_expectedTypes.get(variableName);
if (expectedType == null) {
return;
}
final PsiType type = variable.getType();
final String typeText = type.getCanonicalText();
if (expectedType.equals(typeText)) {
return;
}
if (PsiUtil.isLanguageLevel5OrHigher(variable)) {
final PsiPrimitiveType unboxedType =
PsiPrimitiveType.getUnboxedType(type);
if (unboxedType != null) {
final String unboxedTypeText =
unboxedType.getCanonicalText();
if (expectedType.equals(unboxedTypeText)) {
return;
}
}
}
if (ignoreParameterNameSameAsSuper &&
isVariableNamedSameAsSuper(variable)) {
return;
}
registerVariableError(variable, variable);
}
private static boolean isVariableNamedSameAsSuper(PsiVariable variable) {
if (!(variable instanceof PsiParameter parameter)) {
return false;
}
final PsiElement scope = parameter.getDeclarationScope();
if (!(scope instanceof PsiMethod method)) {
return false;
}
final String variableName = variable.getName();
final int index =
method.getParameterList().getParameterIndex(parameter);
final PsiMethod[] superMethods = method.findSuperMethods();
for (PsiMethod superMethod : superMethods) {
final PsiParameter[] parameters =
superMethod.getParameterList().getParameters();
final PsiParameter overriddenParameter = parameters[index];
if (overriddenParameter.getName().equals(variableName)) {
return true;
}
}
return false;
}
}
}
@@ -1,50 +0,0 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.siyeh.InspectionGadgetsBundle;
public final class StaticMethodNamingConvention extends NamingConvention<PsiMethod> {
private static final int DEFAULT_MIN_LENGTH = 4;
private static final int DEFAULT_MAX_LENGTH = 32;
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("static.method.naming.convention.element.description");
}
@Override
public NamingConventionBean createDefaultBean() {
return new NamingConventionBean("[a-z][A-Za-z\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public boolean isApplicable(PsiMethod member) {
return member.hasModifierProperty(PsiModifier.STATIC);
}
@Override
public String getShortName() {
return "StaticMethodNamingConvention";
}
}
@@ -1,49 +0,0 @@
/*
* Copyright 2003-2013 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiModifier;
import com.siyeh.InspectionGadgetsBundle;
public final class StaticVariableNamingConvention extends NamingConvention<PsiField> {
static final String STATIC_VARIABLE_NAMING_CONVENTION_SHORT_NAME = "StaticVariableNamingConvention";
private static final int DEFAULT_MIN_LENGTH = 5;
private static final int DEFAULT_MAX_LENGTH = 32;
@Override
public NamingConventionBean createDefaultBean() {
return new FieldNamingConventionInspection.FieldNamingConventionBean("[A-Z][A-Z_\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("static.variable.naming.convention.element.description");
}
@Override
public String getShortName() {
return STATIC_VARIABLE_NAMING_CONVENTION_SHORT_NAME;
}
@Override
public boolean isApplicable(PsiField field) {
return field.hasModifierProperty(PsiModifier.STATIC);
}
}
@@ -1,48 +0,0 @@
/*
* Copyright 2003-2009 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.codeInspection.naming.NamingConvention;
import com.intellij.codeInspection.naming.NamingConventionBean;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiTypeParameter;
import com.siyeh.InspectionGadgetsBundle;
public final class TypeParameterNamingConvention extends NamingConvention<PsiClass> {
private static final int DEFAULT_MIN_LENGTH = 1;
private static final int DEFAULT_MAX_LENGTH = 1;
@Override
public NamingConventionBean createDefaultBean() {
return new NamingConventionBean("[A-Z][A-Za-z\\d]*", DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH);
}
@Override
public String getElementDescription() {
return InspectionGadgetsBundle.message("type.parameter.naming.convention.element.description");
}
@Override
public boolean isApplicable(PsiClass member) {
return member instanceof PsiTypeParameter;
}
@Override
public String getShortName() {
return "TypeParameterNamingConvention";
}
}
@@ -1,73 +0,0 @@
/*
* Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.naming;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiModifier;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.RenameFix;
import org.jetbrains.annotations.NotNull;
public final class UpperCaseFieldNameNotConstantInspection extends BaseInspection {
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new RenameFix();
}
@Override
public @NotNull String getID() {
return "NonConstantFieldWithUpperCaseName";
}
@Override
protected @NotNull String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"upper.case.field.name.not.constant.problem.descriptor");
}
@Override
protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
return true;
}
@Override
public @NotNull BaseInspectionVisitor buildVisitor() {
return new UpperCaseFieldNameNotConstantVisitor();
}
private static class UpperCaseFieldNameNotConstantVisitor
extends BaseInspectionVisitor {
@Override
public void visitField(@NotNull PsiField field) {
super.visitField(field);
if (field.hasModifierProperty(PsiModifier.STATIC) &&
field.hasModifierProperty(PsiModifier.FINAL)) {
return;
}
final String fieldName = field.getName();
if (!fieldName.equals(StringUtil.toUpperCase(fieldName))) {
return;
}
registerFieldError(field);
}
}
}