![JAR search and dependency download from the Maven repository](/logo.png)
com.siyeh.ig.style.UnnecessaryConstructorInspection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-analysis-impl Show documentation
Show all versions of java-analysis-impl Show documentation
A packaging of the IntelliJ Community Edition java-analysis-impl library.
This is release number 1 of trunk branch 142.
The newest version!
/*
* 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.style;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
public class UnnecessaryConstructorInspection extends BaseInspection {
@SuppressWarnings("PublicField")
public boolean ignoreAnnotations = false;
@Override
@NotNull
public String getID() {
return "RedundantNoArgConstructor";
}
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"unnecessary.constructor.display.name");
}
@Override
public JComponent createOptionsPanel() {
return new SingleCheckboxOptionsPanel(
InspectionGadgetsBundle.message(
"unnecessary.constructor.annotation.option"),
this, "ignoreAnnotations");
}
@Override
@NotNull
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"unnecessary.constructor.problem.descriptor");
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new UnnecessaryConstructorVisitor();
}
@Override
public InspectionGadgetsFix buildFix(Object... infos) {
return new UnnecessaryConstructorFix();
}
private static class UnnecessaryConstructorFix
extends InspectionGadgetsFix {
@Override
@NotNull
public String getName() {
return InspectionGadgetsBundle.message(
"unnecessary.constructor.remove.quickfix");
}
@NotNull
@Override
public String getFamilyName() {
return getName();
}
@Override
public void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
final PsiElement nameIdentifier = descriptor.getPsiElement();
final PsiElement constructor = nameIdentifier.getParent();
assert constructor != null;
deleteElement(constructor);
}
}
private class UnnecessaryConstructorVisitor
extends BaseInspectionVisitor {
@Override
public void visitClass(@NotNull PsiClass aClass) {
final PsiMethod[] constructors = aClass.getConstructors();
if (constructors.length != 1) {
return;
}
final PsiMethod constructor = constructors[0];
if (!constructor.isPhysical() || constructor.getNameIdentifier() == null) {
return;
}
if (!constructor.hasModifierProperty(PsiModifier.PRIVATE) &&
aClass.hasModifierProperty(PsiModifier.PRIVATE)) {
return;
}
if (!constructor.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) &&
aClass.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
return;
}
if (!constructor.hasModifierProperty(PsiModifier.PROTECTED) &&
aClass.hasModifierProperty(PsiModifier.PROTECTED)) {
return;
}
if (!constructor.hasModifierProperty(PsiModifier.PUBLIC) &&
aClass.hasModifierProperty(PsiModifier.PUBLIC)) {
return;
}
final PsiParameterList parameterList =
constructor.getParameterList();
if (parameterList.getParametersCount() != 0) {
return;
}
if (ignoreAnnotations) {
final PsiModifierList modifierList =
constructor.getModifierList();
final PsiAnnotation[] annotations =
modifierList.getAnnotations();
if (annotations.length > 0) {
return;
}
}
final PsiReferenceList throwsList = constructor.getThrowsList();
final PsiJavaCodeReferenceElement[] elements =
throwsList.getReferenceElements();
if (elements.length != 0) {
return;
}
final PsiCodeBlock body = constructor.getBody();
if (body == null) {
return;
}
final PsiStatement[] statements = body.getStatements();
if (statements.length == 0 || statements.length == 1 && isSuperConstructorInvocationWithoutArguments(statements[0])) {
registerMethodError(constructor);
}
}
private boolean isSuperConstructorInvocationWithoutArguments(PsiStatement statement) {
if (!(statement instanceof PsiExpressionStatement)) {
return false;
}
final PsiExpressionStatement expressionStatement = (PsiExpressionStatement)statement;
final PsiExpression expression = expressionStatement.getExpression();
if (!(expression instanceof PsiMethodCallExpression)) {
return false;
}
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)expression;
final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
if (argumentList.getExpressions().length != 0) {
return false;
}
final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
return PsiKeyword.SUPER.equals(methodExpression.getReferenceName());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy