mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
Report default annotation parameters in more cases
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2015 JetBrains s.r.o.
|
||||
* Copyright 2000-2016 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.
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.codeInspection;
|
||||
|
||||
import com.intellij.codeInsight.AnnotationUtil;
|
||||
import com.intellij.codeInsight.FileModificationService;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
@@ -33,14 +34,13 @@ public class DefaultAnnotationParamInspection extends BaseJavaBatchLocalInspecti
|
||||
@Override
|
||||
public void visitNameValuePair(final PsiNameValuePair pair) {
|
||||
PsiAnnotationMemberValue value = pair.getValue();
|
||||
if (!(value instanceof PsiLiteralExpression) && !(value instanceof PsiArrayInitializerMemberValue)) 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 (areEqual(value, defaultValue)) {
|
||||
if (AnnotationUtil.equal(value, defaultValue)) {
|
||||
holder.registerProblem(value, "Redundant default parameter value assignment", ProblemHighlightType.LIKE_UNUSED_SYMBOL, new LocalQuickFix() {
|
||||
@Nls
|
||||
@NotNull
|
||||
@@ -60,25 +60,4 @@ public class DefaultAnnotationParamInspection extends BaseJavaBatchLocalInspecti
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static boolean areEqual(PsiAnnotationMemberValue value, PsiAnnotationMemberValue defaultValue) {
|
||||
if (value instanceof PsiLiteralExpression && defaultValue instanceof PsiLiteralExpression) {
|
||||
return value.getText().equals(defaultValue.getText());
|
||||
}
|
||||
|
||||
if (value instanceof PsiArrayInitializerMemberValue && defaultValue instanceof PsiArrayInitializerMemberValue) {
|
||||
PsiAnnotationMemberValue[] initializers = ((PsiArrayInitializerMemberValue)value).getInitializers();
|
||||
PsiAnnotationMemberValue[] defaults = ((PsiArrayInitializerMemberValue)defaultValue).getInitializers();
|
||||
if (initializers.length != defaults.length) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < initializers.length; i++) {
|
||||
if (!areEqual(initializers[i], defaults[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Remove redundant parameter" "true"
|
||||
|
||||
@interface Anno {
|
||||
String foo() default "hello world";
|
||||
}
|
||||
|
||||
@Anno()
|
||||
class Foo {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Remove redundant parameter" "true"
|
||||
|
||||
@interface Anno {
|
||||
String foo() default "hello world";
|
||||
}
|
||||
|
||||
@Anno(foo = "hello " +<caret>
|
||||
"world")
|
||||
class Foo {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user