View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package integration.tests;
7   
8   /**
9    * The Class IfElseStatements.
10   */
11  @SuppressWarnings("ControlFlowStatementWithoutBraces")
12  public final class IfElseStatements {
13  
14      /**
15       * Simple if.
16       *
17       * @param b
18       *            the b
19       */
20      void simpleIf(boolean b) {
21          if (b) {
22              System.gc();
23              System.runFinalization();
24          }
25      }
26  
27      /**
28       * If and else.
29       *
30       * @param b
31       *            the b
32       */
33      void ifAndElse(boolean b) {
34          if (b) {
35              System.gc();
36          } else {
37              System.runFinalization();
38          }
39      }
40  
41      /**
42       * Single line if.
43       *
44       * @param b
45       *            the b
46       */
47      void singleLineIf(boolean b) {
48          // @formatter:off
49          if (b) { System.gc(); }
50          // @formatter:on
51      }
52  
53      /**
54       * Single line if and else.
55       *
56       * @param b
57       *            the b
58       */
59      void singleLineIfAndElse(boolean b) {
60          // @formatter:off
61          if (b) { System.gc(); } else { System.runFinalization(); }
62          // @formatter:on
63      }
64  
65      /**
66       * Method with four different paths and simple lines.
67       *
68       * @param b
69       *            the b
70       * @param i
71       *            the i
72       */
73      void methodWithFourDifferentPathsAndSimpleLines(boolean b, int i) {
74          if (b) {
75              System.gc();
76          } else {
77              System.runFinalization();
78          }
79  
80          if (i > 0) {
81              System.gc();
82          }
83      }
84  
85      /**
86       * Method with four different paths and segmented lines.
87       *
88       * @param b
89       *            the b
90       * @param i
91       *            the i
92       */
93      void methodWithFourDifferentPathsAndSegmentedLines(boolean b, int i) {
94          if (b) {
95              System.gc();
96          } else {
97              System.runFinalization();
98          }
99  
100         if (i > 0) {
101             System.gc();
102         } else {
103             System.runFinalization();
104         }
105     }
106 
107     /**
108      * If else with complex boolean condition.
109      *
110      * @param a
111      *            the a
112      * @param b
113      *            the b
114      *
115      * @return true, if successful
116      */
117     boolean ifElseWithComplexBooleanCondition(boolean a, boolean b) {
118         // noinspection RedundantIfStatement
119         if (a || b) {
120             return true;
121         }
122         return false;
123     }
124 
125     /**
126      * Return input.
127      *
128      * @param x
129      *            the x
130      * @param a
131      *            the a
132      * @param b
133      *            the b
134      * @param c
135      *            the c
136      *
137      * @return the int
138      */
139     // Must return the same value of x as it was called with. Some paths will fail that requirement.
140     @SuppressWarnings({ "AssignmentToMethodParameter" })
141     int returnInput(int x, boolean a, boolean b, boolean c) {
142         if (a) {
143             x++;
144         }
145 
146         if (b) {
147             x--;
148         }
149 
150         if (c) {
151             // noinspection SillyAssignment
152             x = x;
153         }
154 
155         return x;
156     }
157 
158     /**
159      * Nested if.
160      *
161      * @param a
162      *            the a
163      * @param b
164      *            the b
165      *
166      * @return the int
167      */
168     int nestedIf(boolean a, boolean b) {
169         int i = 1;
170 
171         if (a && b) {
172             i = 2;
173         }
174 
175         return i;
176     }
177 
178     /**
179      * If else with nested if.
180      *
181      * @param a
182      *            the a
183      * @param b
184      *            the b
185      *
186      * @return the int
187      */
188     int ifElseWithNestedIf(boolean a, boolean b) {
189         int i = 1;
190 
191         if (!a) {
192             return 3;
193         }
194         if (b) {
195             i = 2;
196         }
197 
198         return i;
199     }
200 
201     /**
202      * Nested if else.
203      *
204      * @param a
205      *            the a
206      * @param b
207      *            the b
208      *
209      * @return the int
210      */
211     int nestedIfElse(boolean a, boolean b) {
212         int i = 1;
213 
214         if (a) {
215             if (b) {
216                 i = 2;
217             } else {
218                 i = 3;
219             }
220         } else if (b) {
221             i = 4;
222         }
223 
224         return i;
225     }
226 
227     /**
228      * Block comment with method signature: infeasiblePaths(boolean a).
229      *
230      * @param a
231      *            the a
232      */
233     void infeasiblePaths(boolean a) {
234         if (a) {
235             System.gc();
236         }
237 
238         if (a) {
239             System.runFinalization();
240         }
241     }
242 
243     /**
244      * Another single line if and else.
245      *
246      * @param b
247      *            the b
248      *
249      * @return the int
250      */
251     int anotherSingleLineIfAndElse(boolean b) {
252         // @formatter:off
253         int r; if (b) { r = 1; } else { r = 2; } return r;
254         // @formatter:on
255     }
256 
257     /**
258      * Yet another single line if and else.
259      *
260      * @param b
261      *            the b
262      *
263      * @return the int
264      */
265     int yetAnotherSingleLineIfAndElse(boolean b) {
266         // @formatter:off
267         if (b) { return 1; } return 2;
268         // @formatter:on
269     }
270 
271     /**
272      * If with boolean and operator.
273      *
274      * @param b1
275      *            the b 1
276      * @param b2
277      *            the b 2
278      */
279     void ifWithBooleanAndOperator(boolean b1, boolean b2) {
280         if (b1 && b2) {
281             System.gc();
282         }
283     }
284 
285     /**
286      * If with boolean or operator.
287      *
288      * @param b1
289      *            the b 1
290      * @param b2
291      *            the b 2
292      */
293     void ifWithBooleanOrOperator(boolean b1, boolean b2) {
294         if (b1 || b2) {
295             System.gc();
296         }
297     }
298 
299     /**
300      * Another if with boolean and operator.
301      *
302      * @param b1
303      *            the b 1
304      * @param b2
305      *            the b 2
306      */
307     void anotherIfWithBooleanAndOperator(boolean b1, boolean b2) {
308         if (b1 && b2) {
309             System.gc();
310         }
311     }
312 
313     /**
314      * If spanning multiple lines.
315      *
316      * @param b
317      *            the b
318      * @param i
319      *            the i
320      */
321     void ifSpanningMultipleLines(boolean b, int i) {
322         // @formatter:off
323         if (
324            b ||
325            i > 0
326         ) {
327             System.gc();
328         }
329         // @formatter:on
330     }
331 
332     /**
333      * Method to be called from custom runner test.
334      *
335      * @param s
336      *            the s
337      *
338      * @return the class loader
339      */
340     ClassLoader methodToBeCalledFromCustomRunnerTest(String s) {
341         instanceField = s;
342 
343         if (s.isEmpty()) {
344             return null;
345         }
346 
347         return getClass().getClassLoader();
348     }
349 
350     /** The instance field. */
351     String instanceField;
352 }