1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.google.api.generator.engine.lexicon;
16 
17 import com.google.common.base.Strings;
18 import com.google.common.collect.ImmutableList;
19 
20 public class Keyword {
21   // This is a valid field for all objects, so handle particular keyword differently.
22   private static final String CLASS_KEYWORD = "class";
23 
24   private static final ImmutableList<String> KEYWORDS =
25       ImmutableList.of(
26           "abstract",
27           "continue",
28           "for",
29           "new",
30           "switch",
31           "assert",
32           "default",
33           "if",
34           "package",
35           "synchronized",
36           "boolean",
37           "do",
38           "goto",
39           "private",
40           "this",
41           "break",
42           "double",
43           "implements",
44           "protected",
45           "throw",
46           "byte",
47           "else",
48           "import",
49           "public",
50           "throws",
51           "case",
52           "enum",
53           "instanceof",
54           "return",
55           "transient",
56           "catch",
57           "extends",
58           "int",
59           "short",
60           "try",
61           "char",
62           "final",
63           "interface",
64           "static",
65           "void",
66           "finally",
67           "long",
68           "strictfp",
69           "volatile",
70           "const",
71           "float",
72           "native",
73           "super",
74           "while");
75   private static final String ESCAPE_CHAR = "_";
76 
isKeyword(String s)77   public static boolean isKeyword(String s) {
78     return s.equals(CLASS_KEYWORD) || KEYWORDS.contains(s);
79   }
80 
unescapeKeyword(String str)81   public static String unescapeKeyword(String str) {
82     if (Strings.isNullOrEmpty(str)) {
83       return str;
84     }
85     if (!str.endsWith(ESCAPE_CHAR)) {
86       return str;
87     }
88     String strWithoutEscapeChar = str.substring(0, str.lastIndexOf(ESCAPE_CHAR));
89     return isKeyword(strWithoutEscapeChar) ? strWithoutEscapeChar : str;
90   }
91 
escapeKeyword(String str)92   public static String escapeKeyword(String str) {
93     return Keyword.isKeyword(str) ? str + ESCAPE_CHAR : str;
94   }
95 
isInvalidFieldName(String s)96   public static boolean isInvalidFieldName(String s) {
97     return KEYWORDS.contains(s);
98   }
99 }
100