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 InputAvoidEscapedUnicodeCharacters4 {
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          private String unitAbbrev5 = "\u03bcs";         // violation
97          private String unitAbbrev6 = "\u03bcs";        // violation
98          private String unitAbbrev7 = "\u03bcs";        /* comment separated by tab */ // violation
99          private String unitAbbrev8 = "\u03bcs"; /* comment // violation
100                                                    has 2 lines */
101         void foo() {
102                 for (char c = '\u0000'; c < '\uffff'; c++) {
103                         if (c == '\u001b' ||     // violation
104                                         c == '\u2014')   // violation
105                                 continue;
106                 }
107         }
108         private String unitAbbrev9 = "\u03bcs"; /* comment */ int i; // violation
109 
110         private String notAUnicodeEscaped1 = "\\u1234";
111 
112         private String notAUnicodeEscaped2 = "\\\\u1234";
113 
114         private String onlyEscaped = "\\\u1234"; // violation
115 
116         private String sumilarToEscapedByB = "b\u1234"; // violation
117         private String sumilarToEscapedCommentedByB = "b\u1234"; // violation
118         private String sumilarToEscapedByF = "f\u1234"; // violation
119         private String sumilarToEscapedCommentedByF = "f\u1234"; // violation
120         private String sumilarToEscapedByR = "r\u1234"; // violation
121         private String sumilarToEscapedCommentedByR = "r\u1234"; // violation
122         private String sumilarToEscapedByN = "n\u1234"; // violation
123         private String sumilarToEscapedCommentedByN = "n\u1234"; // violation
124         private String sumilarToEscapedByT = "t\u1234"; // violation
125         private String sumilarToEscapedCommentedByT = "t\u1234"; // violation
126         private String validEscapeWithManyUs = "t\uuuuuuuuu1234"; // violation
127         private String validEscapeWithManyUsCommented = "t\uuuuuuuuu1234"; // violation
128 }