View Javadoc
1   /*
2   AvoidEscapedUnicodeCharacters
3   allowEscapesForControlCharacters = (default)false
4   allowByTailComment = (default)false
5   allowIfAllCharactersEscaped = (default)false
6   allowNonPrintableEscapes = true
7   
8   
9   */
10  
11  package com.puppycrawl.tools.checkstyle.checks.avoidescapedunicodecharacters;
12  
13  import java.util.concurrent.TimeUnit;
14  
15  public class InputAvoidEscapedUnicodeCharacters4One {
16  
17          private String unitAbbrev2 = "\u03bcs"; // violation
18  
19          private String unitAbbrev3 = "\u03bcs"; // violation
20  
21          private String unitAbbrev4 = "\u03bcs"; // violation
22  
23          public Object fooString() {
24                  String unitAbbrev = "μs";
25                  String unitAbbrev2 = "\u03bcs"; // violation
26                  String unitAbbrev3 = "\u03bcs"; // violation
27                  String fakeUnicode = "asd\tsasd";
28                  String fakeUnicode2 = "\\u23\\u123i\\u";
29                  String content = null;
30                  return "\ufeff" + content; // byte order mark
31          }
32  
33          public Object fooChar() {
34                  char unitAbbrev2 = '\u03bc'; // violation
35                  char unitAbbrev3 = '\u03bc'; // violation
36                  char content = 0;
37                  return '\ufeff' + content; // byte order mark
38          }
39  
40          public void multiplyString() {
41                  String unitAbbrev2 = "asd\u03bcsasd"; // violation
42                  String unitAbbrev3 = "aBc\u03bcssdf\u03bc"; /* Greek letter mu, "s" */ // violation
43                  String unitAbbrev4 = "\u03bcaBc\u03bcssdf\u03bc"; // violation
44                  String allCharactersEscaped = "\u03bc\u03bc"; // violation
45          }
46  
47          private static String abbreviate(TimeUnit unit) {
48                  switch (unit) {
49                  case NANOSECONDS:
50                          return "ns";
51                  case MICROSECONDS:
52                          return "\u03bcs"; // violation
53                  case MILLISECONDS:
54                          return "ms";
55                  case SECONDS:
56                          return "s";
57                  case MINUTES:
58                          return "min";
59                  case HOURS:
60                          return "h";
61                  case DAYS:
62                          return "d";
63                  default:
64                          throw new AssertionError();
65                  }
66          }
67  
68                  static final String WHITESPACE_TABLE = ""
69                                  + "\u2002\u3000\r\u0085\u200A\u2005\u2000\u3000\\"
70                                  + "\u2029\u000B\u3000\u2008\u2003\u205F\u3000\u1680"
71                                  + "\u0009\u0020\u2006\u2001\u202F\u00A0\u000C\u2009"
72                                  + "\u3000\u2004\u3000\u3000\u2028\n\u2007\u3000";
73  
74                public boolean matches(char c) {
75                  switch (c) {
76                    case '\t':
77                    case '\n':
78                    case '\013':
79                    case '\f':
80                    case '\r':
81                    case ' ':
82                    case '\u0085': // some comment
83                    case '\u1680':
84                    case '\u2028':
85                    case '\u2029':
86                    case '\u205f':
87                    case '\u3000':
88                      return true;
89                    case '\u2007':
90                      return false;
91                    default:
92                    return c >= '\u2000' && c <= '\u200a';
93                }
94           }
95  
96  }