mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
check hard coded literals in annotation attributes (IDEA-188023)
convert from InspectionTestCase to simplify tests
This commit is contained in:
@@ -341,11 +341,19 @@ public class I18nInspection extends AbstractBaseJavaLocalInspectionTool implemen
|
||||
if (containingClass == null || isClassNonNls(containingClass)) {
|
||||
return null;
|
||||
}
|
||||
List<ProblemDescriptor> results = new ArrayList<>();
|
||||
final PsiCodeBlock body = method.getBody();
|
||||
if (body != null) {
|
||||
return checkElement(body, manager, isOnTheFly);
|
||||
ProblemDescriptor[] descriptors = checkElement(body, manager, isOnTheFly);
|
||||
if (descriptors != null) {
|
||||
ContainerUtil.addAll(results, descriptors);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
checkAnnotations(method, manager, isOnTheFly, results);
|
||||
for (PsiParameter parameter : method.getParameterList().getParameters()) {
|
||||
checkAnnotations(parameter, manager, isOnTheFly, results);
|
||||
}
|
||||
return results.isEmpty() ? null : results.toArray(ProblemDescriptor.EMPTY_ARRAY);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -362,10 +370,23 @@ public class I18nInspection extends AbstractBaseJavaLocalInspectionTool implemen
|
||||
ContainerUtil.addAll(result, descriptors);
|
||||
}
|
||||
}
|
||||
checkAnnotations(aClass, manager, isOnTheFly, result);
|
||||
|
||||
|
||||
return result.isEmpty() ? null : result.toArray(ProblemDescriptor.EMPTY_ARRAY);
|
||||
}
|
||||
|
||||
private void checkAnnotations(PsiModifierListOwner member,
|
||||
@NotNull InspectionManager manager,
|
||||
boolean isOnTheFly, List<ProblemDescriptor> result) {
|
||||
for (PsiAnnotation annotation : member.getAnnotations()) {
|
||||
final ProblemDescriptor[] descriptors = checkElement(annotation, manager, isOnTheFly);
|
||||
if (descriptors != null) {
|
||||
ContainerUtil.addAll(result, descriptors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public ProblemDescriptor[] checkField(@NotNull PsiField field, @NotNull InspectionManager manager, boolean isOnTheFly) {
|
||||
@@ -376,13 +397,21 @@ public class I18nInspection extends AbstractBaseJavaLocalInspectionTool implemen
|
||||
if (AnnotationUtil.isAnnotated(field, AnnotationUtil.NON_NLS, CHECK_EXTERNAL)) {
|
||||
return null;
|
||||
}
|
||||
List<ProblemDescriptor> result = new ArrayList<>();
|
||||
final PsiExpression initializer = field.getInitializer();
|
||||
if (initializer != null) return checkElement(initializer, manager, isOnTheFly);
|
||||
|
||||
if (field instanceof PsiEnumConstant) {
|
||||
return checkElement(((PsiEnumConstant)field).getArgumentList(), manager, isOnTheFly);
|
||||
if (initializer != null) {
|
||||
ProblemDescriptor[] descriptors = checkElement(initializer, manager, isOnTheFly);
|
||||
if (descriptors != null) {
|
||||
ContainerUtil.addAll(result, descriptors);
|
||||
}
|
||||
} else if (field instanceof PsiEnumConstant) {
|
||||
ProblemDescriptor[] descriptors = checkElement(((PsiEnumConstant)field).getArgumentList(), manager, isOnTheFly);
|
||||
if (descriptors != null) {
|
||||
ContainerUtil.addAll(result, descriptors);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
checkAnnotations(field, manager, isOnTheFly, result);
|
||||
return result.isEmpty() ? null : result.toArray(ProblemDescriptor.EMPTY_ARRAY);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
class Test {
|
||||
private abstract class InnerTest {
|
||||
public InnerTest(String s) { }
|
||||
public abstract void run();
|
||||
}
|
||||
|
||||
public void foo(String s) {
|
||||
bar(new InnerTest(<warning descr="Hard coded string literal: \"Literal\"">"Literal"</warning>) { public void run() { } });
|
||||
}
|
||||
|
||||
public void bar(InnerTest t) {
|
||||
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
enum Test {
|
||||
CHECKIN("Text1"),
|
||||
ADD("Rext2");
|
||||
CHECKIN(<warning descr="Hard coded string literal: \"Text1\"">"Text1"</warning>),
|
||||
ADD(<warning descr="Hard coded string literal: \"Rext2\"">"Rext2"</warning>);
|
||||
|
||||
Test(final String id) {
|
||||
myId = id;
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
void foo(String s) {
|
||||
foo(<warning descr="Hard coded string literal: \"text\"">"text"</warning>);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
@interface Anno {
|
||||
String value();
|
||||
}
|
||||
|
||||
@Anno(<warning descr="Hard coded string literal: \"abcd\"">"abcd"</warning>)
|
||||
class Test {
|
||||
@Anno(<warning descr="Hard coded string literal: \"abcd\"">"abcd"</warning>)
|
||||
int field;
|
||||
|
||||
@Anno(<warning descr="Hard coded string literal: \"abcd\"">"abcd"</warning>)
|
||||
void m(@Anno(<warning descr="Hard coded string literal: \"abcd\"">"abcd"</warning>) int i) {}
|
||||
}
|
||||
+2
-2
@@ -1,10 +1,10 @@
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ActionEvent;
|
||||
public class Test {
|
||||
class Test {
|
||||
public static void main(String[] args){
|
||||
ActionListener listener = new ActionListener(){
|
||||
{
|
||||
final String test = "problem reported twice";
|
||||
final String test = <warning descr="Hard coded string literal: \"problem reported twice\"">"problem reported twice"</warning>;
|
||||
}
|
||||
public void actionPerformed(final ActionEvent e) {
|
||||
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
class Foo {
|
||||
void foo() {
|
||||
String v1 = "text";
|
||||
String v1 = <warning descr="Hard coded string literal: \"text\"">"text"</warning>;
|
||||
@org.jetbrains.annotations.NonNls String v2 = "text";
|
||||
String v3;
|
||||
@org.jetbrains.annotations.NonNls String v4;
|
||||
|
||||
v3 = "text";
|
||||
v3 = <warning descr="Hard coded string literal: \"text\"">"text"</warning>;
|
||||
v4 = "text";
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
class Foo {
|
||||
@org.jetbrains.annotations.NonNls String[] myArray = new String[] {"text1", "text2"};
|
||||
@org.jetbrains.annotations.NonNls Stirng[] foo() {
|
||||
@org.jetbrains.annotations.NonNls String[] foo() {
|
||||
myArray = new String[] {"text3", "text4"};
|
||||
myArray[0] = "text5";
|
||||
|
||||
+2
-2
@@ -1,12 +1,12 @@
|
||||
class Foo {
|
||||
void foo(String... s) {
|
||||
foo("literal", "literal"); // MYNON-NLS0
|
||||
String d = "xxxxx"; // NON-NLS
|
||||
String d = <warning descr="Hard coded string literal: \"xxxxx\"">"xxxxx"</warning>; // NON-NLS
|
||||
String d0 = "xxxxx", d01="sssss"; //MYNON-NLS
|
||||
String d1 = "xxxxx"; // MYNON-NLS?
|
||||
|
||||
String d2 = "xxxxx"; /* MYNON-NLS ??? */
|
||||
String wtf="MYNON-NLS";
|
||||
String wtf=<warning descr="Hard coded string literal: \"MYNON-NLS\"">"MYNON-NLS"</warning>;
|
||||
String dw2 = "xxxxx"; String wtw="MYNON-NLS"; /* MYNON-NLS ??? */
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
class Test {
|
||||
abstract class Test {
|
||||
public static final Test TEST = new Test("text") {
|
||||
public void foo() {}
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
<error descr="Cyclic inheritance involving 'A'">class A extends C</error>{
|
||||
String foo(String p){ return <warning descr="Hard coded string literal: \"text\"">"text"</warning>;}
|
||||
}
|
||||
|
||||
<error descr="Cyclic inheritance involving 'A'">class B extends A</error>{
|
||||
String foo(String p){ return <warning descr="Hard coded string literal: \"text\"">"text"</warning>;}
|
||||
}
|
||||
|
||||
<error descr="Cyclic inheritance involving 'C'">class C extends A</error>{
|
||||
String foo(String p){
|
||||
foo<error descr="Ambiguous method call: both 'C.foo(String)' and 'A.foo(String)' match">(<warning descr="Hard coded string literal: \"text\"">"text"</warning>)</error>;
|
||||
return <warning descr="Hard coded string literal: \"text\"">"text"</warning>;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
</problems>
|
||||
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>8</line>
|
||||
<description>Hard coded string literal</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
class Test {
|
||||
private class InnerTest {
|
||||
public InnerTest(String s) { }
|
||||
public abstract void run();
|
||||
}
|
||||
|
||||
public void foo(String s) {
|
||||
bar(new InnerTest("Literal") { public void run() { } });
|
||||
}
|
||||
|
||||
public void bar(InnerTest t) {
|
||||
|
||||
}
|
||||
}
|
||||
-4
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
</problems>
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>2</line>
|
||||
<description>Hard coded string literal</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>3</line>
|
||||
<description>Hard coded string literal</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
</problems>
|
||||
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Foo.java</file>
|
||||
<line>3</line>
|
||||
<description>Hard coded string literal</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
class Foo {
|
||||
void foo(String s) {
|
||||
foo("text");
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>7</line>
|
||||
<description>Hard coded string literal</description>
|
||||
</problem>
|
||||
</problems>
|
||||
@@ -1,14 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>3</line>
|
||||
<description>Hard coded string literal</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>8</line>
|
||||
<description>Hard coded string literal</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
</problems>
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Foo.java</file>
|
||||
<line>4</line>
|
||||
<problem_class>Hard coded strings</problem_class>
|
||||
<description>Hard coded string literal: 'xxxxx'</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Foo.java</file>
|
||||
<line>9</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Hard coded strings</problem_class>
|
||||
<description>Hard coded string literal: 'MYNON-NLS'</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
</problems>
|
||||
|
||||
-4
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
</problems>
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>2</line>
|
||||
<description>Hard coded string literal</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>6</line>
|
||||
<description>Hard coded string literal</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>11</line>
|
||||
<description>Hard coded string literal</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>12</line>
|
||||
<description>Hard coded string literal</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
class A extends C{
|
||||
String foo(String p){ return "text";}
|
||||
}
|
||||
|
||||
class B extends A{
|
||||
String foo(String p){ return "text";}
|
||||
}
|
||||
|
||||
class C extends A{
|
||||
String foo(String p){
|
||||
foo("text");
|
||||
return "text";
|
||||
}
|
||||
}
|
||||
-4
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
</problems>
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
</problems>
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
</problems>
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
</problems>
|
||||
|
||||
+13
-13
@@ -7,17 +7,17 @@ import com.intellij.openapi.application.PluginPathManager;
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.JavaPsiFacade;
|
||||
import com.intellij.testFramework.InspectionTestCase;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
|
||||
/**
|
||||
* @author lesya
|
||||
*/
|
||||
public class I18NInspectionTest extends InspectionTestCase {
|
||||
import static com.intellij.testFramework.LightCodeInsightTestCase.getJavaFacade;
|
||||
|
||||
public class I18NInspectionTest extends LightCodeInsightFixtureTestCase {
|
||||
|
||||
I18nInspection myTool = new I18nInspection();
|
||||
|
||||
private void doTest() {
|
||||
doTest(new I18nInspection());
|
||||
}
|
||||
private void doTest(I18nInspection tool) {
|
||||
doTest("i18n/" + getTestName(true), tool);
|
||||
myFixture.enableInspections(myTool);
|
||||
myFixture.testHighlighting("i18n/" + getTestName(false) + ".java");
|
||||
}
|
||||
|
||||
public void testHardCodedStringLiteralAsParameter() { doTest(); }
|
||||
@@ -26,6 +26,7 @@ public class I18NInspectionTest extends InspectionTestCase {
|
||||
public void testParameterInheritsNonNlsAnnotationFromSuper() { doTest(); }
|
||||
public void testLocalVariables() { doTest(); }
|
||||
public void testFields() { doTest(); }
|
||||
public void testInAnnotationArguments() { doTest(); }
|
||||
public void testAnonymousClassConstructorParameter() { doTest(); }
|
||||
public void testStringBufferNonNls() { doTest(); }
|
||||
public void testEnum() {
|
||||
@@ -47,10 +48,9 @@ public class I18NInspectionTest extends InspectionTestCase {
|
||||
public void testConstructorCallOfNonNlsVariable() { doTest(); }
|
||||
public void testSwitchOnNonNlsString() { doTest(); }
|
||||
public void testNonNlsComment() {
|
||||
I18nInspection inspection = new I18nInspection();
|
||||
inspection.nonNlsCommentPattern = "MYNON-NLS";
|
||||
inspection.cacheNonNlsCommentPattern();
|
||||
doTest(inspection);
|
||||
myTool.nonNlsCommentPattern = "MYNON-NLS";
|
||||
myTool.cacheNonNlsCommentPattern();
|
||||
doTest();
|
||||
}
|
||||
public void testAnnotationArgument() { doTest(); }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user