View Javadoc
1   package com.google.checkstyle.test.chapter4formatting.rule487modifiers;
2   
3   // violation below ''abstract' modifier out of order with the JLS suggestions.'
4   strictfp abstract class InputModifierOrder {
5     transient private String dontSaveMe;
6     // violation above ''private' modifier out of order with the JLS suggestions.'
7   
8     volatile public int whatImReading;
9     // violation above ''public' modifier out of order with the JLS suggestions.'
10  
11    public volatile boolean ssModifierOrderVar = false;
12  
13    /**
14     * Illegal order of modifiers for methods. Make sure that the first and last modifier from the JLS
15     * sequence is used.
16     */
17    strictfp private void doStuff() {}
18    // violation above ''private' modifier out of order with the JLS suggestions.'
19  
20    /** Single annotation without other modifiers. */
21    @MyAnnotation2 void someMethod() {}
22  
23    /** Illegal order of annotation - must come first. */
24    private @MyAnnotation2 void someMethod2() {}
25    // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
26  
27    /** Annotation in middle of other modifiers otherwise in correct order. */
28    private @MyAnnotation2 strictfp void someMethod3() {}
29    // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
30  
31    /** Correct order. */
32    @MyAnnotation2 private strictfp void someMethod4() {}
33  
34    /** Annotation in middle of other modifiers otherwise in correct order. */
35    @MyAnnotation2 public static @MyAnnotation4 strictfp void someMethod5() {}
36    // violation above ''@MyAnnotation4' .* does not precede non-annotation modifiers.'
37  
38    @MyAnnotation2 public static final synchronized strictfp void fooMethod() {}
39  
40    // violation below ''protected' modifier out of order with the JLS suggestions.'
41    strictfp protected final @MyAnnotation2 static synchronized void fooMethod1() {}
42  
43    // violation below ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
44    synchronized @MyAnnotation2 strictfp private final static void fooMethod2() {}
45  
46    // violation below ''final' modifier out of order with the JLS suggestions.'
47    @MyAnnotation2 static synchronized final strictfp protected void fooMethod3() {}
48  
49    // violation below ''static' modifier out of order with the JLS suggestions.'
50    @MyAnnotation2 strictfp static final synchronized private void fooMethod4() {}
51  
52  
53    // violation below ''final' modifier out of order with the JLS suggestions.'
54    synchronized final strictfp @MyAnnotation2 static public void fooMethod5() {}
55  
56    // violation below ''private' modifier out of order with the JLS suggestions.'
57    @MyAnnotation2 static synchronized strictfp private final void fooMethod6() {}
58  
59    // violation below ''synchronized' modifier out of order with the JLS suggestions.'
60    final strictfp synchronized static protected @MyAnnotation2 void fooMethod7() {}
61  
62    // violation below ''protected' modifier out of order with the JLS suggestions.'
63    @MyAnnotation2 abstract protected void fooMet();
64  
65    // violation below ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
66    abstract @MyAnnotation2 public void fooMet1();
67  
68    /** holder for redundant 'public' modifier check. */
69    public static interface InputRedundantPublicModifier {
70      public void abc1();
71  
72      void abc2();
73  
74      abstract void abc3();
75  
76      public float PI_PUBLIC = (float) 3.14;
77  
78      final float PI_FINAL = (float) 3.14;
79  
80      /** all OK. */
81      float PI_OK = (float) 3.14;
82    }
83  
84    // violation below ''private' modifier out of order with the JLS suggestions.'
85    final private void method() {}
86  }
87  
88  // violation below 'Top-level class RedundantFinalClass has to reside in its own source file.'
89  final class RedundantFinalClass {
90    public final void finalMethod() {}
91  
92    public void method() {}
93  
94    protected @MyAnnotation2 static synchronized native void fooMethod();
95    // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
96  
97    static protected @MyAnnotation2 synchronized native void fooMethod1();
98    // violation above ''protected' modifier out of order with the JLS suggestions.'
99  
100   @MyAnnotation2 protected synchronized native void fooMethod2();
101 
102   native synchronized protected static @MyAnnotation2 void fooMethod3();
103   // violation above ''synchronized' modifier out of order with the JLS suggestions.'
104 
105   native @MyAnnotation2 protected static synchronized void fooMethod4();
106   // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
107 
108   public static @MyAnnotation2 synchronized native void fooMethod5();
109   // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
110 
111   synchronized static native @MyAnnotation2 public void fooMethod6();
112   // violation above ''static' modifier out of order with the JLS suggestions.'
113 
114   static synchronized private native @MyAnnotation2 void fooMethod7();
115   // violation above ''private' modifier out of order with the JLS suggestions.'
116 }
117 
118 // violation 2 lines below 'Top-level class .* has to reside in its own source file.'
119 /** Holder for redundant modifiers of inner implementation. */
120 interface InnerImplementation {
121   InnerImplementation inner =
122           new InnerImplementation() {
123             /** compiler requires 'public' modifier. */
124             public void method() {}
125           };
126 
127   void method();
128 }
129 
130 // violation below 'Top-level class WithInner has to reside in its own source file.'
131 class WithInner {
132   /**
133    * Inner class.
134    *
135    * @author max
136    */
137   class Inner {
138     transient private String dontSaveMe;
139     // violation above ''private' modifier out of order with the JLS suggestions.'
140 
141     volatile public int whatImReading;
142     // violation above ''public' modifier out of order with the JLS suggestions.'
143 
144     @MyAnnotation2 protected synchronized native void fooMethod();
145 
146     protected @MyAnnotation2 synchronized native void fooMethod1();
147     // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
148 
149     synchronized protected @MyAnnotation2 native void fooMethod2();
150     // violation above ''protected' modifier out of order with the JLS suggestions.'
151 
152     native synchronized protected @MyAnnotation2 void fooMethod3();
153     // violation above ''synchronized' modifier out of order with the JLS suggestions.'
154 
155     native @MyAnnotation2 protected synchronized void fooMethod4();
156     // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
157 
158     public @MyAnnotation2 synchronized native void fooMethod5();
159     // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
160 
161     synchronized native @MyAnnotation2 public void fooMethod6();
162     // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
163 
164     synchronized private native @MyAnnotation2 void fooMethod7();
165     // violation above ''private' modifier out of order with the JLS suggestions.'
166 
167     /** Anonymous class. */
168     InnerImplementation foo =
169             new InnerImplementation() {
170 
171               public void method() {
172                 // OOOO Auto-generated method stub
173 
174               }
175 
176               transient private String dontSaveMe;
177               // violation above ''private' modifier out of order with the JLS suggestions.'
178 
179               volatile public int whatImReading;
180               // violation above ''public' modifier out of order with the JLS suggestions.'
181 
182               protected @MyAnnotation2 synchronized native void fooMethod();
183               // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
184 
185               protected @MyAnnotation2 synchronized native void fooMethod1();
186               // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
187 
188               synchronized protected @MyAnnotation2 native void fooMethod2();
189               // violation above ''protected' modifier out of order with the JLS suggestions.'
190 
191               native synchronized protected @MyAnnotation2 void fooMethod3();
192               // violation above ''synchronized' modifier out of order with the JLS suggestions.'
193 
194               @MyAnnotation2 protected synchronized native void fooMethod4();
195 
196               public @MyAnnotation2 synchronized native void fooMethod5();
197               // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
198 
199               synchronized native @MyAnnotation2 public void fooMethod6();
200               // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
201 
202               synchronized private native @MyAnnotation2 void fooMethod7();
203               // violation above ''private' modifier out of order with the JLS suggestions.'
204             };
205   }
206 
207   /**
208    * Inner abstract class.
209    *
210    * @author max
211    */
212   abstract class AbsInner {
213     transient private String dontSaveMe;
214     // violation above ''private' modifier out of order with the JLS suggestions.'
215 
216     volatile public int whatImReading;
217     // violation above ''public' modifier out of order with the JLS suggestions.'
218 
219     @MyAnnotation2 public final synchronized strictfp void fooMethod() {}
220 
221     // violation below ''protected' modifier out of order with the JLS suggestions.'
222     strictfp protected final @MyAnnotation2 synchronized void fooMethod1() {}
223 
224     // violation below ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
225     synchronized @MyAnnotation2 strictfp private final void fooMethod2() {}
226 
227     // violation below ''final' modifier out of order with the JLS suggestions.'
228     @MyAnnotation2 synchronized final strictfp protected void fooMethod3() {}
229 
230     // violation below ''final' modifier out of order with the JLS suggestions.'
231     @MyAnnotation2 strictfp final synchronized private void fooMethod4() {}
232 
233     // violation below ''final' modifier out of order with the JLS suggestions.'
234     synchronized final strictfp @MyAnnotation2 public void fooMethod5() {}
235 
236     // violation below ''private' modifier out of order with the JLS suggestions.'
237     @MyAnnotation2 synchronized strictfp private final void fooMethod6() {}
238 
239     // violation below ''synchronized' modifier out of order with the JLS suggestions.'
240     final strictfp synchronized protected @MyAnnotation2 void fooMethod7() {}
241 
242     @MyAnnotation2 abstract protected void fooMet();
243     // violation above ''protected' modifier out of order with the JLS suggestions.'
244 
245     abstract @MyAnnotation2 public void fooMet1();
246     // violation above ''@MyAnnotation2' .* does not precede non-annotation modifiers.'
247   }
248 }
249 
250 // violation 2 lines below 'Top-level class Annotation has to reside in its own source file.'
251 /** Holder for redundant modifiers of annotation fields/variables. */
252 @interface Annotation {
253   public String s1 = "";
254   final String s2 = "";
255   static String s3 = "";
256   String s4 = "";
257 
258   public String blah();
259 
260   abstract String blah2();
261 }
262 
263 // violation below 'Top-level class MyAnnotation2 has to reside in its own source file.'
264 @interface MyAnnotation2 {}
265 
266 // violation below 'Top-level class MyAnnotation4 has to reside in its own source file.'
267 @interface MyAnnotation4 {}
268 
269 // violation 2 lines below 'Top-level class .* has to reside in its own source file.'
270 /** Illegal order of modifiers for interface methods. */
271 interface InputModifierOrderInterface {
272   default strictfp void abc1() {}
273 
274   strictfp default void abc2() {}
275   // violation above ''default' modifier out of order with the JLS suggestions.'
276 }