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