View Javadoc
1   /*
2   RedundantModifier
3   tokens = (default)METHOD_DEF, VARIABLE_DEF, ANNOTATION_FIELD_DEF, INTERFACE_DEF, \
4            CTOR_DEF, CLASS_DEF, ENUM_DEF, RESOURCE
5   
6   
7   */
8   
9   package com.puppycrawl.tools.checkstyle.checks.modifier.redundantmodifier;
10  
11  /**
12   * Test case for Modifier checks:
13   * - order of modifiers
14   * - use of 'public' in interface definition
15   * @author lkuehne
16   */
17  strictfp final class InputRedundantModifierIt // illegal order of modifiers for class
18  {
19  
20      /** Illegal order of modifiers for variables */
21      static private boolean sModifierOrderVar = false;
22  
23      /**
24       * Illegal order of modifiers for methods. Make sure that the
25       * first and last modifier from the JLS sequence is used.
26       */
27      strictfp private void doStuff()
28      {
29      }
30  
31      /** Single annotation without other modifiers */
32      @MyAnnotation2 void someMethod()
33      {
34      }
35  
36      /** Illegal order of annotation - must come first */
37      private @MyAnnotation2 void someMethod2()
38      {
39      }
40  
41      /** Annotation in middle of other modifiers otherwise in correct order */
42      private @MyAnnotation2 strictfp void someMethod3()
43      {
44      }
45  
46      /** Correct order */
47      @MyAnnotation2 private strictfp void someMethod4()
48      {
49      }
50  
51      /** Annotation in middle of other modifiers otherwise in correct order */
52      @MyAnnotation2 private static @MyAnnotation4 strictfp void someMethod5()
53      {
54      }
55  
56      /** holder for redundant 'public' modifier check. */
57      public static interface InputRedundantPublicModifier // violation
58      {
59          /** redundant 'public' modifier */
60          public void a(); // violation
61  
62          /** all OK */
63          void b();
64  
65          /** redundant abstract modifier */
66          abstract void c(); // violation
67  
68          /** redundant 'public' modifier */
69          public float PI_PUBLIC = (float) 3.14; // violation
70  
71          /** redundant 'abstract' modifier (field can not be abstract) */
72  //        abstract float PI_ABSTRACT = (float) 3.14;
73  
74          /** redundant 'final' modifier */
75          final float PI_FINAL = (float) 3.14; // violation
76  
77          /** all OK */
78          float PI_OK = (float) 3.14;
79      }
80  
81      /** redundant 'final' modifier */
82      private final void method() // violation
83      {
84      }
85  }
86  
87  /** Holder for redundant 'final' check. */
88  final class RedundantFinalClass
89  {
90      /** redundant 'final' modifier */
91      public final void finalMethod() // violation
92      {
93      }
94  
95      /** OK */
96      public void method()
97      {
98      }
99  }
100 
101 /** Holder for redundant modifiers of inner implementation */
102 abstract interface InnerImplementation // violation
103 {
104     InnerImplementation inner =
105             new InnerImplementation()
106             {
107                 /** compiler requires 'public' modifier */
108                 public void method()
109                 {
110                 }
111             };
112 
113     void method();
114 }
115 
116 /** Holder for redundant modifiers of annotation fields/variables */
117 @interface Annotation
118 {
119     public String s1 = ""; // violation
120     final String s2 = ""; // violation
121     static String s3 = ""; // violation
122     String s4 = "";
123     public String blah(); // violation
124     abstract String blah2(); // violation
125 }
126 
127 @interface MyAnnotation2 {
128 }
129 
130 @interface MyAnnotation4 {
131 }
132 
133 class SafeVarargsUsage {
134     @Deprecated
135     @SafeVarargs
136     private final void foo(int... k) {}
137 
138     @Deprecated
139     @SafeVarargs
140     @SuppressWarnings("")
141     private final void foo1(Object... obj) {}
142 }
143 
144 enum TestEnum {
145     ;
146 
147     public void method() {
148     }
149 }
150 
151 /** holder for interface specific modifier check. */
152 interface InputDefaultPublicModifier
153 {
154     /** correct order */
155     default strictfp void a()
156     {
157     }
158 
159     /** wrong order */
160     strictfp default void b()
161     {
162     }
163 }