View Javadoc
1   /*
2   DeclarationOrder
3   ignoreConstructors = (default)false
4   ignoreModifiers = (default)false
5   
6   
7   */
8   
9   package com.puppycrawl.tools.checkstyle.checks.coding.declarationorder;
10  
11  public class InputDeclarationOrder
12  {
13      static final int FOO2 = 3;
14  
15      // public before package
16      public static final int FOO = 3; // violation 'Variable.*access.*wrong.*order'
17  
18      private static final int FOO3 = 3;
19  
20      // public before package and private
21      public static final int FOO4 = 3; // violation 'Variable.*access.*wrong.*order'
22  
23      private static final String ERROR = "error";
24  
25      // protected before private
26      protected static final String ERROR1 = "error"; // violation 'Variable.*access.*wrong.*order'
27  
28      // public before private
29      public static final String WARNING = "warning"; // violation 'Variable.*access.*wrong.*order'
30  
31      private int mMaxInitVars = 3;
32  
33      // statics should be before instance members
34      // publics before private
35      public static final int MAX_ITER_VARS = 3; // violation 'Static.*variable.*wrong.*order'
36  
37      private class InnerClass
38      {
39          private static final int INNER_FOO = 2;
40  
41          // public before private
42          public static final int INNER_FOO2 = 2; // violation 'Variable.*access.*wrong.*order'
43  
44          public InnerClass()
45          {
46              int foo = INNER_FOO;
47              foo += INNER_FOO2;
48              foo += INNER_FOO3;
49          }
50  
51          public InnerClass(int start)
52          {
53              int foo = start;
54              foo += INNER_FOO2;
55              foo += INNER_FOO3;
56          }
57  
58          // member variables should be before methods or ctors
59          // public before private
60          public static final int INNER_FOO3 = 2; // violation 'Static.*variable.*wrong.*order'
61      }
62  
63      public int getFoo1()
64      {
65          return mFoo;
66      }
67  
68      //  ctors before methods
69      public InputDeclarationOrder() // violation 'Constructor.*in.*wrong.*order'
70      {
71          String foo = ERROR;
72          foo += ERROR1;
73          foo += WARNING;
74          int fooInt = mMaxInitVars;
75          fooInt += MAX_ITER_VARS;
76          fooInt += mFoo;
77      }
78  
79      public static int getFoo2()
80      {
81          return 13;
82      }
83  
84      public int getFoo()
85      {
86          return mFoo;
87      }
88  
89      private static int getFoo21()
90      {
91          return 14;
92      }
93  
94      // member variables should be before methods or ctors
95      private int mFoo = 0; // violation 'Instance variable definition in wrong order.'
96  }
97  
98  enum InputDeclarationOrderEnum
99  {
100     ENUM_VALUE_1,
101     ENUM_VALUE_2,
102     ENUM_VALUE_3
103     {
104         private static final int INNER_FOO = 2;
105 
106         // public before private
107         public static final int INNER_FOO2 = 2; // violation 'Variable.*access.*wrong.*order'
108 
109         public void doIt()
110         {
111         }
112 
113         // member variables should be before methods or ctors
114         // public before private
115         public static final int INNER_FOO3 = 2; // violation 'Static.*variable.*wrong.*order'
116     };
117 
118     static final int FOO2 = 3;
119 
120     // public before package
121     public static final int FOO = 3; // violation 'Variable.*access.*wrong.*order'
122 
123     private static final int FOO3 = 3;
124 
125     // public before package and private
126     public static final int FOO4 = 3; // violation 'Variable.*access.*wrong.*order'
127 
128     private static final String ERROR = "error";
129 
130     // protected before private
131     protected static final String ERROR1 = "error"; // violation 'Variable.*access.*wrong.*order'
132 
133     // public before private
134     public static final String WARNING = "warning"; // violation 'Variable.*access.*wrong.*order'
135 
136     private int mMaxInitVars = 3;
137 
138     // statics should be before instance members
139     // publics before private
140     public static final int MAX_ITER_VARS = 3; // violation 'Static.*variable.*wrong.*order'
141 
142     private class InnerClass
143     {
144         private static final int INNER_FOO = 2;
145 
146         // public before private
147         public static final int INNER_FOO2 = 2; // violation 'Variable.*access.*wrong.*order'
148 
149         public InnerClass()
150         {
151             int foo = INNER_FOO;
152             foo += INNER_FOO2;
153             foo += INNER_FOO3;
154         }
155 
156         // member variables should be before methods or ctors
157         // public before private
158         public static final int INNER_FOO3 = 2; // violation 'Static.*variable.*wrong.*order'
159     }
160 
161     public int getFoo1()
162     {
163         return mFoo;
164     }
165 
166     //  ctors before methods
167     InputDeclarationOrderEnum() // violation 'Constructor.*in.*wrong.*order'
168     {
169         String foo = ERROR;
170         foo += ERROR1;
171         foo += WARNING;
172         int fooInt = mMaxInitVars;
173         fooInt += MAX_ITER_VARS;
174         fooInt += mFoo;
175     }
176 
177     public static int getFoo2()
178     {
179         return 2;
180     }
181 
182     public int getFoo()
183     {
184         return mFoo;
185     }
186 
187     private static int getFoo21()
188     {
189         return 1;
190     }
191 
192     // member variables should be before methods or ctors
193     private int mFoo = 0; // violation 'Instance variable definition in wrong order.'
194 
195     class AsyncProcess {
196         private final int startLogErrorsCnt = 0;
197 
198         protected final int maxTotalConcurrentTasks=0; // violation 'Variable.*access.*wrong.*order'
199     }
200 }