View Javadoc
1   /*
2   AvoidEscapedUnicodeCharacters
3   allowEscapesForControlCharacters = (default)false
4   allowByTailComment = true
5   allowIfAllCharactersEscaped = (default)false
6   allowNonPrintableEscapes = (default)false
7   
8   
9   */
10  
11  package com.puppycrawl.tools.checkstyle.checks.avoidescapedunicodecharacters;
12  
13  import java.util.concurrent.TimeUnit;
14  
15  public class InputAvoidEscapedUnicodeCharacters2 {
16           // violation below
17          private String unitAbbrev2 = "\u03bcs";
18  
19          private String unitAbbrev3 = "\u03bcs"; // Greek letter mu
20  
21          private String unitAbbrev4 = "\u03bcs"; // Greek letter mu
22  
23          public Object fooString() {
24                  String unitAbbrev = "μs"; // violation below
25                  String unitAbbrev2 = "\u03bcs";
26                  String unitAbbrev3 = "\u03bcs"; // Greek letter mu, "s"
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() { // violation below
34                  char unitAbbrev2 = '\u03bc';
35                  char unitAbbrev3 = '\u03bc'; // Greek letter mu, "s"
36                  char content = 0;
37                  return '\ufeff' + content; // byte order mark
38          }
39  
40          public void multiplyString() { // violation below
41                  String unitAbbrev2 = "asd\u03bcsasd";
42                  String unitAbbrev3 = "aBc\u03bcssdf\u03bc"; /* mu, "s" */ // violation below
43                  String unitAbbrev4 = "\u03bcaBc\u03bcssdf\u03bc";
44                  String allCharactersEscaped = "\u03bc\u03bc";
45          } // violation above
46  
47          private static String abbreviate(TimeUnit unit) {
48                  switch (unit) {
49                  case NANOSECONDS:
50                          return "ns";
51                  case MICROSECONDS:
52                          return "\u03bcs"; // μs
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 = "" // violation below
69                                  + "\u2002\u3000\r\u0085\u200A\u2005\u2000\u3000\\"
70                           // violation below
71                                  + "\u2029\u000B\u3000\u2008\u2003\u205F\u3000\u1680"
72                           // violation below
73                                  + "\u0009\u0020\u2006\u2001\u202F\u00A0\u000C\u2009"
74                           // violation below
75                                  + "\u3000\u2004\u3000\u3000\u2028\n\u2007\u3000";
76  
77                public boolean matches(char c) {
78                  switch (c) {
79                    case '\t':
80                    case '\n':
81                    case '\013':
82                    case '\f':
83                    case '\r':
84                    case ' ':
85                    case '\u0085': // some comment // violation below
86                    case '\u1680':
87                         // violation below
88                    case '\u2028':
89                         // violation below
90                    case '\u2029':
91                         // violation below
92                    case '\u205f':
93                         // violation below
94                    case '\u3000':
95                      return true;
96                       // violation below
97                    case '\u2007':
98                      return false;
99                    default:
100                        // 2 violations below
101                   return c >= '\u2000' && c <= '\u200a';
102               }
103          }
104 
105         private String unitAbbrev5 = "\u03bcs";         // comment is separated by space + tab
106         private String unitAbbrev6 = "\u03bcs";        // comment is separated by tab
107         private String unitAbbrev7 = "\u03bcs";        /* comment is separated by tab */
108         private String unitAbbrev8 = "\u03bcs"; /* comment
109                                                    has 2 lines */
110         void foo() {
111                 for (char c = '\u0000'; c < '\uffff'; c++) {
112                         if (c == '\u001b' ||     // 2 violations above
113                                         c == '\u2014')   // Em-Dash?
114                                 continue;
115                 }
116         } // violation below
117         private String unitAbbrev9 = "\u03bcs"; /* comment */ int i;
118 
119         private String notAUnicodeEscaped1 = "\\u1234";
120 
121         private String notAUnicodeEscaped2 = "\\\\u1234";
122         // violation below
123         private String onlyEscaped = "\\\u1234";
124          // violation below
125         private String sumilarToEscapedByB = "b\u1234";
126         private String sumilarToEscapedCommentedByB = "b\u1234"; // comment
127          // violation below
128         private String sumilarToEscapedByF = "f\u1234";
129         private String sumilarToEscapedCommentedByF = "f\u1234"; // comment
130          // violation below
131         private String sumilarToEscapedByR = "r\u1234";
132         private String sumilarToEscapedCommentedByR = "r\u1234"; // comment
133          // violation below
134         private String sumilarToEscapedByN = "n\u1234";
135         private String sumilarToEscapedCommentedByN = "n\u1234"; // comment
136          // violation below
137         private String sumilarToEscapedByT = "t\u1234";
138         private String sumilarToEscapedCommentedByT = "t\u1234"; // comment
139         // violation below
140         private String validEscapeWithManyUs = "t\uuuuuuuuu1234";
141         private String validEscapeWithManyUsCommented = "t\uuuuuuuuu1234"; // comment
142 }