From 672270193d9dce8afc56608920ef4a13979295d9 Mon Sep 17 00:00:00 2001 From: Steven Goldfeder Date: Fri, 23 Aug 2013 06:34:28 -0700 Subject: [PATCH] removed prefix from suppression stirngs and added suppression tests --- ...ctInvalidTargetingOnScopingAnnotation.java | 2 +- .../InjectMoreThanOneQualifier.java | 2 +- ...jectMoreThanOneScopeAnnotationOnClass.java | 2 +- ...eAnnotationOnInterfaceOrAbstractClass.java | 2 +- ...ctScopeOrQualifierAnnotationRetention.java | 2 +- .../GuiceAssistedParametersNegativeCases.java | 10 ++++ ...etingOnScopingAnnotationNegativeCases.java | 11 ++++ ...jectMoreThanOneQualifierNegativeCases.java | 59 +++++++++++++++---- ...jectMoreThanOneQualifierPositiveCases.java | 6 +- ...neScopeAnnotationOnClassNegativeCases.java | 9 +++ ...InterfaceOrAbstractClassNegativeCases.java | 16 +++++ ...ifierAnnotationRetentionNegativeCases.java | 11 ++++ 12 files changed, 113 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/InjectInvalidTargetingOnScopingAnnotation.java b/core/src/main/java/com/google/errorprone/bugpatterns/InjectInvalidTargetingOnScopingAnnotation.java index 38f4025..cd7489a 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/InjectInvalidTargetingOnScopingAnnotation.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/InjectInvalidTargetingOnScopingAnnotation.java @@ -40,7 +40,7 @@ /** * @author sgoldfeder@google.com (Steven Goldfeder) */ -@BugPattern(name = "InjectInvalidTargetingOnScopingAnnotation", +@BugPattern(name = "InvalidTargetingOnScopingAnnotation", summary = "The target of a scoping annotation must be set to METHOD and/or TYPE.", explanation = "Scoping annotations are only appropriate for provision and therefore are only " + "appropriate on @Provides methods and classes that will be provided just-in-time.", diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/InjectMoreThanOneQualifier.java b/core/src/main/java/com/google/errorprone/bugpatterns/InjectMoreThanOneQualifier.java index 8f7d8fd..593adf8 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/InjectMoreThanOneQualifier.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/InjectMoreThanOneQualifier.java @@ -36,7 +36,7 @@ /** * @author sgoldfeder@google.com (Steven Goldfeder) */ -@BugPattern(name = "InjectMoreThanOneQualifier", +@BugPattern(name = "MoreThanOneQualifier", summary = "Using more than one qualifier annotation on the same element is not allowed.", explanation = "An element can be qualified by at most one qualifier.", category = INJECT, severity = ERROR, maturity = EXPERIMENTAL) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/InjectMoreThanOneScopeAnnotationOnClass.java b/core/src/main/java/com/google/errorprone/bugpatterns/InjectMoreThanOneScopeAnnotationOnClass.java index d668bd4..24abb71 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/InjectMoreThanOneScopeAnnotationOnClass.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/InjectMoreThanOneScopeAnnotationOnClass.java @@ -39,7 +39,7 @@ * @author sgoldfeder@google.com (Steven Goldfeder) */ -@BugPattern(name = "InjectMoreThanOneScopeAnnotationOnClass", +@BugPattern(name = "MoreThanOneScopeAnnotationOnClass", summary = "A class can be annotated with at most one scope annotation", explanation = "Annotating a class with more than one scope annotation is " + "invalid according to the JSR-330 specification. ", category = INJECT, severity = ERROR, diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/InjectScopeAnnotationOnInterfaceOrAbstractClass.java b/core/src/main/java/com/google/errorprone/bugpatterns/InjectScopeAnnotationOnInterfaceOrAbstractClass.java index 3fcd104..70dd1e4 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/InjectScopeAnnotationOnInterfaceOrAbstractClass.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/InjectScopeAnnotationOnInterfaceOrAbstractClass.java @@ -38,7 +38,7 @@ /** * @author sgoldfeder@google.com (Steven Goldfeder) */ -@BugPattern(name = "InjectScopeAnnotationOnInterfaceOrAbstractClass", +@BugPattern(name = "ScopeAnnotationOnInterfaceOrAbstractClass", summary = "Scope annotation on an interface or abstact class is not allowed", explanation = "Scoping annotations are not allowed on abstract types.", category = INJECT, severity = ERROR, maturity = EXPERIMENTAL) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/InjectScopeOrQualifierAnnotationRetention.java b/core/src/main/java/com/google/errorprone/bugpatterns/InjectScopeOrQualifierAnnotationRetention.java index 70728ed..4c33361 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/InjectScopeOrQualifierAnnotationRetention.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/InjectScopeOrQualifierAnnotationRetention.java @@ -38,7 +38,7 @@ /** * @author sgoldfeder@google.com (Steven Goldfeder) */ -@BugPattern(name = "InjectScopeOrQualifierAnnotationRetention", +@BugPattern(name = "ScopeOrQualifierAnnotationRetention", summary = "Scoping and qualifier annotations must have runtime retention.", explanation = "The JSR-330 spec allows use of reflection. Not having runtime " + "retention on scoping or qualifer annotations will cause unexpected " diff --git a/core/src/test/resources/com/google/errorprone/bugpatterns/GuiceAssistedParametersNegativeCases.java b/core/src/test/resources/com/google/errorprone/bugpatterns/GuiceAssistedParametersNegativeCases.java index b488b18..3bf57eb 100644 --- a/core/src/test/resources/com/google/errorprone/bugpatterns/GuiceAssistedParametersNegativeCases.java +++ b/core/src/test/resources/com/google/errorprone/bugpatterns/GuiceAssistedParametersNegativeCases.java @@ -66,4 +66,14 @@ public class TestClass5 { @Inject public TestClass5(@Assisted("foo") List x, @Assisted("foo") List y, String z) {} } + + /** + * Class has constructor with two @Assisted parameters of the same type. + * Error is suppressed. + */ + public class TestClass6 { + @SuppressWarnings("GuiceAssistedParameters") + @Inject + public TestClass6(int n, @Assisted String x, @Assisted String y, int z) {} + } } diff --git a/core/src/test/resources/com/google/errorprone/bugpatterns/InjectInvalidTargetingOnScopingAnnotationNegativeCases.java b/core/src/test/resources/com/google/errorprone/bugpatterns/InjectInvalidTargetingOnScopingAnnotationNegativeCases.java index 3187127..2924314 100644 --- a/core/src/test/resources/com/google/errorprone/bugpatterns/InjectInvalidTargetingOnScopingAnnotationNegativeCases.java +++ b/core/src/test/resources/com/google/errorprone/bugpatterns/InjectInvalidTargetingOnScopingAnnotationNegativeCases.java @@ -59,4 +59,15 @@ public class InjectInvalidTargetingOnScopingAnnotationNegativeCases { @Target(PARAMETER) public @interface TestAnnotation4 { } + + /** + * A scoping annotation with taeget TYPE, METHOD, and (illegal) PARAMETER. + * Error is suppressed. + */ + @SuppressWarnings("InvalidTargetingOnScopingAnnotation") + @Target({TYPE, METHOD, PARAMETER}) + @Scope + public @interface TestAnnotation5 { + } + } diff --git a/core/src/test/resources/com/google/errorprone/bugpatterns/InjectMoreThanOneQualifierNegativeCases.java b/core/src/test/resources/com/google/errorprone/bugpatterns/InjectMoreThanOneQualifierNegativeCases.java index 377bfdb..3576ee3 100644 --- a/core/src/test/resources/com/google/errorprone/bugpatterns/InjectMoreThanOneQualifierNegativeCases.java +++ b/core/src/test/resources/com/google/errorprone/bugpatterns/InjectMoreThanOneQualifierNegativeCases.java @@ -19,6 +19,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; import com.google.inject.BindingAnnotation; + import javax.inject.Qualifier; import java.lang.annotation.Retention; @@ -41,39 +42,73 @@ public void setN(int n) {} * A class in which a single javax.inject.Qualifier annotation is on the class, on a constructor, * on a field, on a method, and on a method parameter. */ - @Foo + @Foo1 public class TestClass2 { - @Foo + @Foo1 private int n; - @Foo + @Foo1 public TestClass2() {} - @Foo - public void setN(@Foo int n) {} + @Foo1 + public void setN(@Foo1 int n) {} } /** * A class in which a single com.google.inject.BindingAnnotation annotation is on the class, on a * constructor, on a field, on a method, and on a method parameter. */ - @Bar + @Bar1 public class TestClass3 { - @Bar + @Bar1 private int n; - @Bar + @Bar1 public TestClass3() {} - @Bar - public void setN(@Bar int n) {} + @Bar1 + public void setN(@Bar1 int n) {} + } + + /** + * A suppression test for a class in which the class, a constructor, a field, + * a method, and a method parameter each have two com.google.inject.BindingAnnotation + * annotations + * + * TODO(sgoldfeder) Change so that the error only shows up on elements where qualifiers are + * allowed--i.e. methods and classes. + */ + @SuppressWarnings("MoreThanOneQualifier") + @Foo1 + @Foo2 + public class TestClass4 { + @Foo1 + @Foo2 + private int n; + + @Foo1 + @Foo2 + public TestClass4() {} + + @Foo1 + @Foo2 + public void setN(@Foo1 @Foo2 int n) {} } + + @Qualifier + @Retention(RUNTIME) + public @interface Foo1 {} + @Qualifier @Retention(RUNTIME) - public @interface Foo {} + public @interface Foo2 {} + + @BindingAnnotation + @Retention(RUNTIME) + public @interface Bar1 {} @BindingAnnotation @Retention(RUNTIME) - public @interface Bar {} + public @interface Bar2 {} } diff --git a/core/src/test/resources/com/google/errorprone/bugpatterns/InjectMoreThanOneQualifierPositiveCases.java b/core/src/test/resources/com/google/errorprone/bugpatterns/InjectMoreThanOneQualifierPositiveCases.java index ed04dea..9cb3697 100644 --- a/core/src/test/resources/com/google/errorprone/bugpatterns/InjectMoreThanOneQualifierPositiveCases.java +++ b/core/src/test/resources/com/google/errorprone/bugpatterns/InjectMoreThanOneQualifierPositiveCases.java @@ -27,10 +27,12 @@ */ public class InjectMoreThanOneQualifierPositiveCases { - /** * A class in which the class, a constructor, a field, a method, and a method parameter each have - * two com.google.inject.BindingAnnotation annotations. + * two com.google.inject.BindingAnnotation annotations. + * + * TODO(sgoldfeder) Change so that the error only shows up on elements where qualifiers are + * allowed--i.e. methods and classes. */ //BUG: Suggestion includes "remove" @Foo1 diff --git a/core/src/test/resources/com/google/errorprone/bugpatterns/InjectMoreThanOneScopeAnnotationOnClassNegativeCases.java b/core/src/test/resources/com/google/errorprone/bugpatterns/InjectMoreThanOneScopeAnnotationOnClassNegativeCases.java index 4fb9211..0e3c211 100644 --- a/core/src/test/resources/com/google/errorprone/bugpatterns/InjectMoreThanOneScopeAnnotationOnClassNegativeCases.java +++ b/core/src/test/resources/com/google/errorprone/bugpatterns/InjectMoreThanOneScopeAnnotationOnClassNegativeCases.java @@ -18,6 +18,7 @@ import com.google.inject.Singleton; import com.google.inject.Provides; +import com.google.inject.servlet.SessionScoped; /** * @author sgoldfeder@google.com(Steven Goldfeder) */ @@ -54,4 +55,12 @@ public class TestClass5 { @Singleton @Provides public void foo(){} } + + /** + * Class has two scoping annotations. The error is suppressed. + */ + @SuppressWarnings("MoreThanOneScopeAnnotationOnClass") + @Singleton + @SessionScoped + public class TestClass6 {} } \ No newline at end of file diff --git a/core/src/test/resources/com/google/errorprone/bugpatterns/InjectScopeAnnotationOnInterfaceOrAbstractClassNegativeCases.java b/core/src/test/resources/com/google/errorprone/bugpatterns/InjectScopeAnnotationOnInterfaceOrAbstractClassNegativeCases.java index 15d639f..a1fb397 100644 --- a/core/src/test/resources/com/google/errorprone/bugpatterns/InjectScopeAnnotationOnInterfaceOrAbstractClassNegativeCases.java +++ b/core/src/test/resources/com/google/errorprone/bugpatterns/InjectScopeAnnotationOnInterfaceOrAbstractClassNegativeCases.java @@ -49,4 +49,20 @@ public interface TestClass3 { @Singleton public class TestClass4 { } + + /** + * An abstract class that has scoping annotation. The error is suppressed. + */ + @SuppressWarnings("ScopeAnnotationOnInterfaceOrAbstractClass") + @Singleton + public abstract class TestClass5 { + } + + /** + * An interface interface has scoping annotation. The error is suppressed. + */ + @SuppressWarnings("ScopeAnnotationOnInterfaceOrAbstractClass") + @Singleton + public interface TestClass6 { + } } diff --git a/core/src/test/resources/com/google/errorprone/bugpatterns/InjectScopeOrQualifierAnnotationRetentionNegativeCases.java b/core/src/test/resources/com/google/errorprone/bugpatterns/InjectScopeOrQualifierAnnotationRetentionNegativeCases.java index 68305e3..f2071ed 100644 --- a/core/src/test/resources/com/google/errorprone/bugpatterns/InjectScopeOrQualifierAnnotationRetentionNegativeCases.java +++ b/core/src/test/resources/com/google/errorprone/bugpatterns/InjectScopeOrQualifierAnnotationRetentionNegativeCases.java @@ -76,4 +76,15 @@ public class InjectScopeOrQualifierAnnotationRetentionNegativeCases { @Retention(SOURCE) public @interface TestAnnotation5 { } + + /** + * A qualifer(@Qualifier) annotation with SOURCE retention. The error is suppressed. + */ + @SuppressWarnings("ScopeOrQualifierAnnotationRetention") + @Qualifier + @Target({TYPE, METHOD}) + @Retention(SOURCE) + public @interface TestAnnotation6 { + } + }