View Javadoc
1   /*
2   FinalParameters
3   ignorePrimitiveTypes = (default)false
4   ignoreUnnamedParameters = (default)true
5   tokens = CTOR_DEF
6   
7   
8   */
9   
10  package com.puppycrawl.tools.checkstyle.checks.finalparameters;
11  
12  import java.awt.event.ActionEvent;
13  
14  import javax.swing.AbstractAction;
15  import javax.swing.Action;
16  
17  /**
18   * Test case for detecting missing final parameters.
19   * @author Lars Kühne
20   **/
21  class InputFinalParameters2
22  {
23      /** no param constructor */
24      InputFinalParameters2()
25      {
26      }
27  
28      /** non final param constructor */
29      InputFinalParameters2(String s) // violation, 's' should be final
30      {
31      }
32  
33      /** non final param constructor */
34      InputFinalParameters2(final Integer i)
35      {
36      }
37  
38      /** final param constructor with annotation */
39      InputFinalParameters2(final @MyAnnotation33 Class<Object> i)
40      {
41      }
42  
43      /** non-final param constructor with annotation*/
44      InputFinalParameters2(@MyAnnotation33 Boolean i) // violation, 'i' should be final
45      {
46      }
47  
48      /** mixed */
49      InputFinalParameters2(String s, final Integer i) // violation, 's' should be final
50      {
51      }
52  
53      /** no param method */
54      void method()
55      {
56      }
57  
58      /** non final param method */
59      void method(String s)
60      {
61      }
62  
63      /** final param method */
64      void method(final Integer i)
65      {
66      }
67  
68      /** final param method with annotation **/
69      void method(@MyAnnotation33 final Object s)
70      {
71  
72      }
73  
74      /** non-final param method with annotation **/
75      void method(@MyAnnotation33 Class<Object> s)
76      {
77  
78      }
79  
80      /** mixed */
81      void method(String s, final Integer i)
82      {
83      }
84  
85      /** interface methods should not be flagged. */
86      interface TestInterface
87      {
88          void method(String s);
89      }
90  
91      /** methods in anonymous inner classes */
92      void holder()
93      {
94          Action a = new AbstractAction()
95              {
96                  public void actionPerformed(ActionEvent e)
97                  {
98                  }
99                  void somethingElse(@MyAnnotation33 ActionEvent e)
100                 {
101                 }
102             };
103 
104         Action b = new AbstractAction()
105             {
106                 public void actionPerformed(final ActionEvent e)
107                 {
108                 }
109                 void somethingElse(@MyAnnotation33 final ActionEvent e)
110                 {
111                 }
112             };
113     }
114 
115     /** methods with complicated types of the parameters. */
116     void methodA(String aParam) {
117     }
118 
119     void methodB(String[] args) {
120     }
121 
122     void methodC(String[] args) {
123     }
124 
125     /** some catch blocks */
126     void method1()
127     {
128         try {
129             String.CASE_INSENSITIVE_ORDER.equals("");
130         }
131         catch (NullPointerException npe) {
132             npe.getMessage();
133         }
134         catch (@MyAnnotation32 final ClassCastException e) {
135             e.getMessage();
136         }
137         catch (RuntimeException e) {
138             e.getMessage();
139         }
140         catch (@MyAnnotation32 NoClassDefFoundError e) {
141             e.getMessage();
142         }
143     }
144 
145     native void method(int i);
146 }
147 
148 abstract class AbstractClass2
149 {
150     public abstract void abstractMethod(int aParam);
151 }
152 
153 class Foo2
154 {
155     /* Some for-each clauses */
156     public void Bar()
157     {
158         for(String s : someExpression())
159         {
160 
161         }
162         for(final String s : someExpression())
163         {
164 
165         }
166         for(@MyAnnotation32 String s : someExpression())
167         {
168 
169         }
170         for(@MyAnnotation32 final String s : someExpression())
171         {
172 
173         }
174     }
175 
176     private String[] someExpression()
177     {
178         return null;
179     }
180 }
181 
182 @interface MyAnnotation32 {
183 }