DefaultAnnotationParamInspection

This commit is contained in:
Dmitry Avdeev
2015-12-03 11:09:02 +03:00
parent 21d1b968b0
commit ad9d9397fc
7 changed files with 137 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2000-2015 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.intellij.codeInspection;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
/**
* @author Dmitry Avdeev
*/
public class DefaultAnnotationParamInspection extends BaseJavaBatchLocalInspectionTool {
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new JavaElementVisitor() {
@Override
public void visitNameValuePair(final PsiNameValuePair pair) {
PsiAnnotationMemberValue value = pair.getValue();
if (!(value instanceof PsiLiteralExpression)) return;
PsiReference reference = pair.getReference();
if (reference == null) return;
PsiElement element = reference.resolve();
if (!(element instanceof PsiAnnotationMethod)) return;
PsiAnnotationMemberValue defaultValue = ((PsiAnnotationMethod)element).getDefaultValue();
if (defaultValue == null) return;
if (value.getText().equals(defaultValue.getText())) {
holder.registerProblem(value, "Redundant default parameter value assignment", ProblemHighlightType.LIKE_UNUSED_SYMBOL, new LocalQuickFix() {
@Nls
@NotNull
@Override
public String getName() {
return "Remove redundant parameter";
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return getName();
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement parent = descriptor.getPsiElement().getParent();
parent.delete();
}
});
}
}
};
}
}

View File

@@ -0,0 +1,10 @@
// "Remove redundant parameter" "true"
@interface Anno {
boolean foo() default true;
}
@Anno()
class Foo {
}

View File

@@ -0,0 +1,10 @@
// "Remove redundant parameter" "true"
@interface Anno {
boolean foo() default true;
}
@Anno(foo = tr<caret>ue)
class Foo {
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2000-2015 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.intellij.codeInsight.daemon.quickFix;
import com.intellij.codeInspection.DefaultAnnotationParamInspection;
import com.intellij.codeInspection.LocalInspectionTool;
import org.jetbrains.annotations.NotNull;
/**
* @author Dmitry Avdeev
*/
public class DefaultAnnotationParamTest extends LightQuickFixParameterizedTestCase {
public void test() throws Exception { doAllTests(); }
@NotNull
protected LocalInspectionTool[] configureLocalInspectionTools() {
return new LocalInspectionTool[] { new DefaultAnnotationParamInspection()};
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/defaultAnnotationParamValue";
}
}

View File

@@ -701,3 +701,5 @@ problematic.whitespace.show.whitespaces.quickfix=Toggle show whitespace in the e
todo.comment.display.name=TODO comment
todo.comment.problem.descriptor=TODO comment <code>#ref</code> #loc
long.line.display.name=Line is longer than allowed by code style
inspection.default.annotation.param=Default annotation parameter value

View File

@@ -0,0 +1,5 @@
<html>
<body>
Reports explicit assigning default value to an annotation parameter.
</body>
</html>

View File

@@ -657,6 +657,9 @@
<localInspection groupPath="Java" language="JAVA" shortName="SillyAssignment" bundle="messages.InspectionsBundle" key="inspection.variable.assigned.to.itself.display.name" groupName="Declaration redundancy"
enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.codeInspection.sillyAssignment.SillyAssignmentInspection" />
<localInspection groupPath="Java" language="JAVA" shortName="DefaultAnnotationParam" bundle="messages.InspectionsBundle" key="inspection.default.annotation.param" groupName="Declaration redundancy"
enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.codeInspection.DefaultAnnotationParamInspection" />
<localInspection groupPath="Java" language="JAVA" shortName="RedundantThrowsDeclaration" bundle="messages.InspectionsBundle" key="redundant.throws.declaration"
groupName="Declaration redundancy" enabledByDefault="false" level="WARNING" cleanupTool="true"
implementationClass="com.intellij.codeInspection.unneededThrows.RedundantThrowsDeclaration" />