Test whether a string is a Java identifier. Note that the behaviour of this method depend on the system property org.apache.el.parser.SKIP_IDENTIFIER_CHECK @param key The string to test @return true if the provided String should be treated as a Java identifier, otherwise false
(String key)
| 49 | * @return {@code true} if the provided String should be treated as a Java identifier, otherwise false |
| 50 | */ |
| 51 | public static boolean isIdentifier(String key) { |
| 52 | |
| 53 | if (SKIP_IDENTIFIER_CHECK) { |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | // Should not be the case but check to be sure |
| 58 | if (key == null || key.isEmpty()) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | // Check the list of known invalid values |
| 63 | int i = 0; |
| 64 | int j = invalidIdentifiers.length; |
| 65 | while (i < j) { |
| 66 | int k = (i + j) >>> 1; // Avoid overflow |
| 67 | int result = invalidIdentifiers[k].compareTo(key); |
| 68 | if (result == 0) { |
| 69 | return false; |
| 70 | } |
| 71 | if (result < 0) { |
| 72 | i = k + 1; |
| 73 | } else { |
| 74 | j = k; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * The parser checks Character.isJavaIdentifierStart() and Character.isJavaIdentifierPart() so no need to check |
| 80 | * them again here. However, we do need to check that '#' hasn't been used at the start of the identifier. |
| 81 | */ |
| 82 | return key.charAt(0) != '#'; |
| 83 | } |
| 84 | } |