31
31
public abstract class CtReferenceImpl extends CtElementImpl implements CtReference , Serializable {
32
32
33
33
private static final long serialVersionUID = 1L ;
34
- private static Collection <String > keywords = fillWithKeywords ();
34
+
35
+ // See isKeyword for more information on keywords
36
+ private static final Collection <String > baseKeywords = fillWithBaseKeywords ();
37
+ private static final Collection <String > java2Keywords = Stream .of ("strictfp" ).collect (Collectors .toCollection (HashSet ::new ));
38
+ private static final Collection <String > java4Keywords = Stream .of ("assert" ).collect (Collectors .toCollection (HashSet ::new ));
39
+ private static final Collection <String > java5Keywords = Stream .of ("enum" ).collect (Collectors .toCollection (HashSet ::new ));
40
+ private static final Collection <String > java9Keywords = Stream .of ("_" ).collect (Collectors .toCollection (HashSet ::new ));
35
41
36
42
@ MetamodelPropertyField (role = NAME )
37
43
protected String simplename = "" ;
@@ -109,9 +115,25 @@ private void checkIdentiferForJLSCorrectness(String simplename) {
109
115
}
110
116
}
111
117
}
118
+
119
+ /**
120
+ * Keywords list and history selected according to:
121
+ * https://docs.oracle.com/en/java/javase/15/docs/specs/sealed-classes-jls.html#jls-3.9
122
+ * https://en.wikipedia.org/wiki/List_of_Java_keywords (contains history of revisions)
123
+ * and https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html (history up to java 8)
124
+ *
125
+ * @param simplename
126
+ * @return true if simplename is a keyword in the current setting (compliance level), false if not
127
+ */
112
128
private boolean isKeyword (String simplename ) {
113
- return keywords .contains (simplename );
129
+ int complianceLevel = getFactory ().getEnvironment ().getComplianceLevel ();
130
+ return (baseKeywords .contains (simplename )
131
+ || (complianceLevel >= 2 && java2Keywords .contains (simplename ))
132
+ || (complianceLevel >= 4 && java4Keywords .contains (simplename ))
133
+ || (complianceLevel >= 5 && java5Keywords .contains (simplename ))
134
+ || (complianceLevel >= 9 && java9Keywords .contains (simplename )));
114
135
}
136
+
115
137
private boolean checkAllParts (String [] simplenameParts ) {
116
138
for (String simpleName :simplenameParts ) {
117
139
//because arrays use e.g. int[] and @Number is used for instances of an object e.g. foo@1
@@ -135,13 +157,14 @@ private boolean checkIdentifierChars(String simplename) {
135
157
);
136
158
}
137
159
138
- private static Collection <String > fillWithKeywords () {
139
- //removed types because needed as ref: "int","short", "char", "void", "byte","float", "true","false","boolean","double","long","class", "null"
140
- return Stream .of ("abstract" , "continue" , "for" , "new" , "switch" , "assert" , "default" , "if" , "package" , "synchronized" , "do" , "goto" , "private" ,
141
- "this" , "break" , "implements" , "protected" , "throw" , "else" , "import" , "public" , "throws" , "case" , "enum" , "instanceof" , "return" ,
142
- "transient" , "catch" , "extends" , "try" , "final" , "interface" , "static" , "finally" , "strictfp" , "volatile" ,
143
- "const" , "native" , "super" , "while" , "_" )
144
- .collect (Collectors .toCollection (HashSet ::new ));
160
+ private static Collection <String > fillWithBaseKeywords () {
161
+ // removed types because needed as ref: "int","short", "char", "void", "byte","float", "true","false","boolean","double","long","class", "null"
162
+ // in the method isKeyword, more keywords are added to the checks based on the compliance level
163
+ return Stream .of ("abstract" , "continue" , "for" , "new" , "switch" , "default" , "if" , "package" , "synchronized" , "do" , "goto" , "private" ,
164
+ "this" , "break" , "implements" , "protected" , "throw" , "else" , "import" , "public" , "throws" , "case" , "instanceof" , "return" ,
165
+ "transient" , "catch" , "extends" , "try" , "final" , "interface" , "static" , "finally" , "volatile" ,
166
+ "const" , "native" , "super" , "while" )
167
+ .collect (Collectors .toCollection (HashSet ::new ));
145
168
}
146
169
147
170
/**
0 commit comments