Skip to content

Commit 94a948e

Browse files
committed
Check live options when determine what java level is used for parsing
Currently the Parser determines what java version is parsed once in the constructor and store them into (modifiable) fields. This causes issues when the options are modified as everything else works but this particular fields are not updated. Instead of having a way to update those fields and as they do not cache any complex computations, this fields are now replaced with appropriate getters like it is already done in some cases. Fix #4849
1 parent 853b240 commit 94a948e

File tree

2 files changed

+48
-35
lines changed
  • org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser
  • org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/complete

2 files changed

+48
-35
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Parser.java

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -915,17 +915,6 @@ protected int actFromTokenOrSynthetic(int previousAct) {
915915
// used for recovery
916916
protected int lastJavadocEnd;
917917
public org.eclipse.jdt.internal.compiler.ReadManager readManager;
918-
final protected boolean parsingJava8Plus = true;
919-
protected boolean parsingJava9Plus;
920-
protected boolean parsingJava10Plus;
921-
protected boolean parsingJava14Plus;
922-
protected boolean parsingJava15Plus;
923-
protected boolean parsingJava17Plus;
924-
protected boolean parsingJava18Plus;
925-
protected boolean parsingJava21Plus;
926-
protected boolean parsingJava22Plus;
927-
protected boolean previewEnabled;
928-
protected boolean parsingJava11Plus;
929918
protected int unstackedAct = ERROR_ACTION;
930919
private boolean haltOnSyntaxError = false;
931920
private boolean tolerateDefaultClassMethods = false;
@@ -944,16 +933,6 @@ public Parser(ProblemReporter problemReporter, boolean optimizeStringLiterals) {
944933
this.options = problemReporter.options;
945934
this.optimizeStringLiterals = optimizeStringLiterals;
946935
initializeScanner();
947-
this.parsingJava9Plus = this.options.sourceLevel >= ClassFileConstants.JDK9;
948-
this.parsingJava10Plus = this.options.sourceLevel >= ClassFileConstants.JDK10;
949-
this.parsingJava11Plus = this.options.sourceLevel >= ClassFileConstants.JDK11;
950-
this.parsingJava14Plus = this.options.sourceLevel >= ClassFileConstants.JDK14;
951-
this.parsingJava15Plus = this.options.sourceLevel >= ClassFileConstants.JDK15;
952-
this.parsingJava17Plus = this.options.sourceLevel >= ClassFileConstants.JDK17;
953-
this.parsingJava18Plus = this.options.sourceLevel >= ClassFileConstants.JDK18;
954-
this.parsingJava21Plus = this.options.sourceLevel >= ClassFileConstants.JDK21;
955-
this.parsingJava22Plus = this.options.sourceLevel >= ClassFileConstants.JDK22;
956-
this.previewEnabled = this.options.sourceLevel == ClassFileConstants.getLatestJDKLevel() && this.options.enablePreviewFeatures;
957936
this.astLengthStack = new int[50];
958937
this.expressionLengthStack = new int[30];
959938
this.typeAnnotationLengthStack = new int[30];
@@ -1154,7 +1133,7 @@ protected void checkAndSetModifiers(int flag){
11541133
of a list of several modifiers. The startPosition
11551134
is zeroed when a copy of modifiers-buffer is push
11561135
onto the this.astStack. */
1157-
if (flag == ClassFileConstants.AccStrictfp && this.parsingJava17Plus) {
1136+
if (flag == ClassFileConstants.AccStrictfp && isParsingJava17Plus()) {
11581137
problemReporter().StrictfpNotRequired(this.scanner.startPosition, this.scanner.currentPosition - 1);
11591138
}
11601139

@@ -4825,7 +4804,7 @@ protected void consumeInterfaceMethodDeclaration(boolean hasSemicolonBody) {
48254804
boolean isDefault = (md.modifiers & ExtraCompilerModifiers.AccDefaultMethod) != 0;
48264805
boolean isStatic = (md.modifiers & ClassFileConstants.AccStatic) != 0;
48274806
boolean isPrivate = (md.modifiers & ClassFileConstants.AccPrivate) != 0;
4828-
boolean bodyAllowed = (this.parsingJava9Plus && isPrivate) || isDefault || isStatic;
4807+
boolean bodyAllowed = (isParsingJava9Plus() && isPrivate) || isDefault || isStatic;
48294808
if (bodyAllowed && hasSemicolonBody) {
48304809
md.modifiers |= ExtraCompilerModifiers.AccSemicolonBody; // avoid complaints regarding undocumented empty body
48314810
}
@@ -8277,7 +8256,7 @@ protected void consumeLambdaHeader() {
82778256
if (argument.isReceiver()) {
82788257
problemReporter().illegalThis(argument);
82798258
}
8280-
if (this.parsingJava8Plus && !JavaFeature.UNNAMMED_PATTERNS_AND_VARS.isSupported(this.options) && argument.name.length == 1 && argument.name[0] == '_')
8259+
if (isParsingJava8Plus() && !JavaFeature.UNNAMMED_PATTERNS_AND_VARS.isSupported(this.options) && argument.name.length == 1 && argument.name[0] == '_')
82818260
problemReporter().illegalUseOfUnderscoreAsAnIdentifier(argument.sourceStart, argument.sourceEnd, true, false); // true == lambdaParameter
82828261
}
82838262
LambdaExpression lexp = (LambdaExpression) this.astStack[this.astPtr];
@@ -8932,7 +8911,7 @@ protected void consumeSwitchStatementOrExpression(boolean isStmt) {
89328911
if (isStmt)
89338912
pushOnAstStack(switchStatement);
89348913
else {
8935-
if (!this.parsingJava14Plus)
8914+
if (!isParsingJava14Plus())
89368915
problemReporter().switchExpressionsNotSupported(switchStatement);
89378916
pushOnExpressionStack(switchStatement);
89388917
}
@@ -9174,9 +9153,9 @@ protected void consumeSwitchLabels(boolean shouldConcat, boolean isSwitchRule) {
91749153

91759154
CaseStatement caseStatement = new CaseStatement(labelExpressions, sourceStart, sourceEnd);
91769155
caseStatement.isSwitchRule = isSwitchRule;
9177-
if (labelExpressions.length > 1 && !this.parsingJava14Plus)
9156+
if (labelExpressions.length > 1 && !isParsingJava14Plus())
91789157
problemReporter().multiConstantCaseLabelsNotSupported(caseStatement);
9179-
if (isSwitchRule && !this.parsingJava14Plus)
9158+
if (isSwitchRule && !isParsingJava14Plus())
91809159
problemReporter().arrowInCaseStatementsNotSupported(caseStatement);
91819160

91829161
// Look for $fall-through$ tag in leading comment for case statement
@@ -10286,7 +10265,7 @@ public MethodDeclaration convertToMethodDeclaration(ConstructorDeclaration c, Co
1028610265
}
1028710266

1028810267
protected TypeReference augmentTypeWithAdditionalDimensions(TypeReference typeReference, int additionalDimensions, Annotation[][] additionalAnnotations, boolean isVarargs) {
10289-
if (this.parsingJava10Plus && typeReference instanceof SingleTypeReference singleTypeRef && CharOperation.equals(singleTypeRef.token, TypeConstants.VAR))
10268+
if (isParsingJava10Plus() && typeReference instanceof SingleTypeReference singleTypeRef && CharOperation.equals(singleTypeRef.token, TypeConstants.VAR))
1029010269
problemReporter().varLocalCannotBeArray(singleTypeRef);
1029110270
return typeReference.augmentTypeWithAdditionalDimensions(additionalDimensions, additionalAnnotations, isVarargs);
1029210271
}
@@ -10795,7 +10774,7 @@ protected void annotateTypeReference(Wildcard ref) {
1079510774
}
1079610775
protected final TypeReference getTypeReference(int dim) {
1079710776
TypeReference typeRef = constructTypeReference(dim);
10798-
if (this.parsingJava10Plus && typeRef instanceof ArrayTypeReference singleTypeRef && CharOperation.equals(singleTypeRef.token, TypeConstants.VAR)) {
10777+
if (isParsingJava10Plus() && typeRef instanceof ArrayTypeReference singleTypeRef && CharOperation.equals(singleTypeRef.token, TypeConstants.VAR)) {
1079910778
if (singleTypeRef.isParameterizedTypeReference())
1080010779
problemReporter().varCannotBeUsedWithTypeArguments(singleTypeRef);
1080110780
if (singleTypeRef.dimensions() > 0)
@@ -12163,7 +12142,7 @@ public ASTNode[] parseClassBodyDeclarations(char[] source, int offset, int lengt
1216312142
try {
1216412143
this.diet = true;
1216512144
this.dietInt = 0;
12166-
this.tolerateDefaultClassMethods = this.parsingJava8Plus;
12145+
this.tolerateDefaultClassMethods = isParsingJava8Plus();
1216712146
parse();
1216812147
} catch (AbortCompilation ex) {
1216912148
this.lastAct = ERROR_ACTION;
@@ -12442,8 +12421,8 @@ protected void pushIdentifier(char [] identifier, long position) {
1244212421
stackLength);
1244312422
}
1244412423
this.identifierLengthStack[this.identifierLengthPtr] = 1;
12445-
if (this.parsingJava8Plus && !JavaFeature.UNNAMMED_PATTERNS_AND_VARS.isSupported(this.options) && identifier.length == 1 && identifier[0] == '_' && !this.processingLambdaParameterList)
12446-
problemReporter().illegalUseOfUnderscoreAsAnIdentifier((int) (position >>> 32), (int) position, this.parsingJava9Plus, false);
12424+
if (isParsingJava8Plus() && !JavaFeature.UNNAMMED_PATTERNS_AND_VARS.isSupported(this.options) && identifier.length == 1 && identifier[0] == '_' && !this.processingLambdaParameterList)
12425+
problemReporter().illegalUseOfUnderscoreAsAnIdentifier((int) (position >>> 32), (int) position, isParsingJava9Plus(), false);
1244712426
}
1244812427
protected void pushIdentifier() {
1244912428
/*push the consumeToken on the identifier stack.
@@ -13309,13 +13288,47 @@ public boolean automatonWillShift(TerminalToken token) {
1330913288

1331013289
@Override
1331113290
public boolean isParsingJava14() {
13312-
return this.parsingJava14Plus;
13291+
return isParsingJava14Plus();
1331313292
}
1331413293
@Override
1331513294
public boolean isParsingModuleDeclaration() {
1331613295
// It can be a null in case of a Vanguard parser, which means no module to be dealt with.
13317-
return (this.parsingJava9Plus && this.compilationUnit != null && this.compilationUnit.isModuleInfo());
13296+
return (isParsingJava9Plus() && this.compilationUnit != null && this.compilationUnit.isModuleInfo());
1331813297
}
13298+
13299+
protected boolean isParsingJava8Plus() {
13300+
return true;
13301+
}
13302+
protected boolean isParsingJava9Plus() {
13303+
return this.options!=null && this.options.sourceLevel >= ClassFileConstants.JDK9;
13304+
}
13305+
protected boolean isParsingJava10Plus() {
13306+
return this.options!=null && this.options.sourceLevel >= ClassFileConstants.JDK10;
13307+
}
13308+
protected boolean isParsingJava11Plus() {
13309+
return this.options!=null && this.options.sourceLevel >= ClassFileConstants.JDK11;
13310+
}
13311+
protected boolean isParsingJava14Plus() {
13312+
return this.options!=null && this.options.sourceLevel >= ClassFileConstants.JDK14;
13313+
}
13314+
protected boolean isParsingJava15Plus() {
13315+
return this.options!=null && this.options.sourceLevel >= ClassFileConstants.JDK15;
13316+
}
13317+
protected boolean isParsingJava17Plus() {
13318+
return this.options!=null && this.options.sourceLevel >= ClassFileConstants.JDK17;
13319+
}
13320+
protected boolean isParsingJava18Plus() {
13321+
return this.options!=null && this.options.sourceLevel >= ClassFileConstants.JDK18;
13322+
}
13323+
protected boolean isParsingJava21Plus() {
13324+
return this.options!=null && this.options.sourceLevel >= ClassFileConstants.JDK21;
13325+
}
13326+
protected boolean isParsingJava22Plus() {
13327+
return this.options!=null && this.options.sourceLevel >= ClassFileConstants.JDK22;
1331913328
}
1332013329

13330+
protected boolean isPreviewEnabled() {
13331+
return this.options!=null && this.options.sourceLevel == ClassFileConstants.getLatestJDKLevel() && this.options.enablePreviewFeatures;
13332+
}
13333+
}
1332113334

org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4299,7 +4299,7 @@ && isIndirectlyInsideFieldInitialization()) { // enum initializers indeed need m
42994299
case TokenNameIdentifier:
43004300
if (this.inReferenceExpression)
43014301
break;
4302-
if (JavaFeature.SWITCH_EXPRESSIONS.isSupported(this.scanner.complianceLevel, this.previewEnabled)
4302+
if (JavaFeature.SWITCH_EXPRESSIONS.isSupported(this.scanner.complianceLevel, isPreviewEnabled())
43034303
&& isInsideSwitch() && checkYieldKeyword()) {
43044304
pushOnElementStack(K_YIELD_KEYWORD);
43054305
// Take the short cut here.

0 commit comments

Comments
 (0)