View Javadoc
1   /*
2   VariableDeclarationUsageDistance
3   allowedDistance = 1
4   ignoreVariablePattern = (default)
5   validateBetweenScopes = true
6   ignoreFinal = false
7   
8   
9   */
10  
11  package com.puppycrawl.tools.checkstyle.checks.coding.variabledeclarationusagedistance;
12  
13  import java.io.*;
14  import java.nio.file.Files;
15  import java.util.*;
16  
17  public class InputVariableDeclarationUsageDistanceTryResources {
18      public int methodTry() {
19          String a = ""; // violation 'Distance .* is 2.'
20          String b = "abc"; // violation 'Distance .* is 2.'
21          System.out.println();
22          try (AutoCloseable j = new java.io.StringReader(b);
23               final AutoCloseable i = new java.io.StringReader(a);
24               final AutoCloseable k = new java.io.StringReader(b);) {
25              b.replace(a.charAt(0), 'b');
26              String c = b.toString();
27          } catch (Exception e) {
28              System.out.println(a);
29              throw new RuntimeException(e);
30          }
31          return 0;
32      }
33      String method2() throws Exception {
34          String b = "";
35          FileReader fr = new FileReader(b);
36          BufferedReader br = new BufferedReader(fr);
37          try {
38              return br.readLine();
39          } finally {
40              br.close();
41              fr.close();
42          }
43      }
44      void method() {
45          byte[] buf = new byte[10];
46          File b = new File("");
47          try (AutoCloseable j = new java.io.StringReader(b.toString());
48               InputStream finstr = Files.newInputStream(b.toPath());
49               BufferedInputStream in = new BufferedInputStream(finstr, buf.length)) {
50              int length;
51              while ((length = in.read(buf)) != -1) {
52                  System.out.println(buf.toString());
53              }
54          } catch (IOException e) {
55              throw new RuntimeException(e);
56          } catch (Exception e) {
57              throw new RuntimeException(e);
58          }
59      }
60      String method3() throws Exception {
61          String b = "";
62          FileReader fr = new FileReader("");
63          BufferedReader br = new BufferedReader(fr);
64          try {
65              return br.readLine();
66          } catch (Exception e) {
67              FileReader t = new FileReader(b);
68          } finally {
69              br.close();
70              fr.close();
71              return b;
72          }
73      }
74      class myClass {
75          String m, n;
76          myClass(String a, String b) {
77              this.m = a;
78              this.n = b;
79          }
80          AutoCloseable getAutoCloseable(int m) {
81              return new AutoCloseable() {
82                  @Override
83                  public void close() throws Exception {
84  
85                  }
86              };
87          }
88      }
89      void method4() throws Exception {
90          String b = "abc";
91          myClass a = new myClass("", b);
92          Scanner scanner = new Scanner(System.in);
93          scanner.nextBigInteger(100);
94          try (final AutoCloseable t = a.getAutoCloseable(3);) {
95              Integer number = 10;
96              for (int i = 0; i < 10; i++) {
97                  System.out.println(i);
98              }
99              b.substring(0, a.toString().length());
100         }
101     }
102     private void method5(OutputStream out, HashMap<String, myClass> fieldStatistics)
103             throws IOException {
104         out.write(fieldStatistics.size());
105         final boolean[] states = {fieldStatistics.isEmpty(), fieldStatistics.isEmpty()};
106         Object[] keys = fieldStatistics.keySet().toArray(new String[0]);
107         Object[] values = fieldStatistics.values().toArray(new myClass[0]);
108         for (int i = 0; i < states.length; i++) {
109             if (states[i]) {
110                 out.write(((String) keys[i]).getBytes());
111                 myClass statistics = (myClass) values[i];
112                 assert statistics.hashCode() >= 0;
113                 out.write(statistics.hashCode());
114                 out.write(statistics.m.getBytes());
115             }
116         }
117     }
118 }