1 /* 2 OneStatementPerLine 3 treatTryResourcesAsStatement = true 4 5 6 */ 7 8 package com.puppycrawl.tools.checkstyle.checks.coding.onestatementperline; 9 import java.io.StringReader; 10 /* 11 This class provides test input for OneStatementPerLineCheck with different 12 types of multiline statements. 13 A Java statement is the smallest unit that is a complete instruction. 14 Statements must end with a semicolon. 15 Statements generally contain expressions (expressions have a value). 16 One of the simplest is the Assignment Statement. 17 */ 18 19 public class InputOneStatementPerLineMultilineInLoopsAndTryWithResources { 20 int var1 = 1,var2 = 2; 21 22 /** 23 * Multiline for loop statement is legal. 24 */ 25 private void foo3() { 26 for(int n = 0, 27 k = 1 28 ; n<5 29 ; 30 n++, k--) {} 31 } 32 33 /** 34 * One statement inside multiline for loop block is legal. 35 */ 36 private void foo4() { 37 for(int n = 0, 38 k = 1 39 ; n<5 40 ; ) { int a = 5, 41 b = 2;} 42 } 43 44 /** 45 * Two statements on the same lne 46 * inside multiline for loop block are illegal. 47 */ 48 private void foo5() { 49 for(int n = 0, 50 k = 1 51 ; n<5 52 ; 53 n++, k--) { var1++; var2++; } // violation 'Only one statement per line allowed.' 54 } 55 56 /** 57 * Multiple statements within try-with-resource on a separate line is legal. 58 * @see <a href="https://github.com/checkstyle/checkstyle/issues/2211">false match</a> 59 */ 60 private void issue2211pass() { 61 try( 62 AutoCloseable i = new java.io.StringReader(""); 63 AutoCloseable k = new java.io.StringReader(""); 64 ) { 65 } catch (Exception e1) { 66 } 67 } 68 69 /** 70 * Multiple statements within try-with-resource on a separate line is legal. Per PR comment: 71 * @see <a href="https://github.com/checkstyle/checkstyle/pull/2750#issuecomment-166032327"/> 72 */ 73 private void issue2211pass2() { 74 try( AutoCloseable i = new java.io.StringReader(""); 75 AutoCloseable k = new java.io.StringReader("");) { 76 } catch (Exception e1) { 77 } 78 } 79 80 /** 81 * Multiple statements within try-with-resource on next line after try is illegal. 82 * @see <a href="https://github.com/checkstyle/checkstyle/issues/2211">false match</a> 83 */ 84 private void issue2211fail() { 85 try( 86 // violation below 'Only one statement per line allowed.' 87 AutoCloseable i=new java.io.PipedReader();AutoCloseable k=new java.io.PipedReader(); 88 ) { 89 } catch (Exception e1) { 90 } 91 } 92 93 /** 94 * Multiple statements within try-with-resource on a same line as try is illegal. PR comment: 95 * @see <a href="https://github.com/checkstyle/checkstyle/pull/2750#issuecomment-166032327"/> 96 */ 97 private void issue2211fail2() { 98 // violation below 'Only one statement per line allowed.' 99 try(AutoCloseable i=new StringReader("");AutoCloseable k=new StringReader("");) { 100 } catch (Exception e1) { 101 } 102 } 103 104 }