Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8f7132c

Browse files
committed
Adds the ability to use the word 'assert' prior to Java version 1.4
1 parent2e035be commit8f7132c

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

‎javaparser-core-testing/src/test/java/com/github/javaparser/ast/validator/Java1_3ValidatorTest.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
importstaticcom.github.javaparser.ParseStart.STATEMENT;
2525
importstaticcom.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_1_3;
2626
importstaticcom.github.javaparser.Providers.provider;
27+
importstaticcom.github.javaparser.utils.TestUtils.assertNoProblems;
2728
importstaticcom.github.javaparser.utils.TestUtils.assertProblems;
2829

2930
importcom.github.javaparser.JavaParser;
@@ -42,4 +43,10 @@ void noAssert() {
4243
result,
4344
"(line 1,col 1) 'assert' keyword is not supported. Pay attention that this feature is supported starting from 'JAVA_1_4' language level. If you need that feature the language level must be configured in the configuration before parsing the source files.");
4445
}
46+
47+
@Test
48+
voidassertIsValideIdentifierBeforeJAVA_1_4() {
49+
ParseResult<Statement>result =javaParser.parse(STATEMENT,provider("String assert;"));
50+
assertNoProblems(result);
51+
}
4552
}

‎javaparser-core-testing/src/test/java/com/github/javaparser/ast/validator/Java1_4ValidatorTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ void yesAssert() {
4444
assertNoProblems(result);
4545
}
4646

47+
@Test
48+
voidassertIsNotValideIdentifierSinceJAVA_1_4() {
49+
ParseResult<Statement>result =javaParser.parse(STATEMENT,provider("String assert;"));
50+
assertProblems(
51+
result,
52+
"(line 1,col 8) 'assert' identifier is not supported. Pay attention that this feature is no longer supported since 'JAVA_1_4' language level. If you need that feature the language level must be configured in the configuration before parsing the source files.");
53+
}
54+
4755
@Test
4856
voidnoGenerics() {
4957
ParseResult<CompilationUnit>result =

‎javaparser-core/src/main/java/com/github/javaparser/ast/validator/language_level_validations/Java1_0Validator.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
importcom.github.javaparser.ast.body.*;
2828
importcom.github.javaparser.ast.expr.*;
2929
importcom.github.javaparser.ast.modules.ModuleDeclaration;
30+
importcom.github.javaparser.ast.nodeTypes.NodeWithIdentifier;
3031
importcom.github.javaparser.ast.nodeTypes.NodeWithTypeArguments;
3132
importcom.github.javaparser.ast.nodeTypes.NodeWithTypeParameters;
3233
importcom.github.javaparser.ast.stmt.*;
@@ -53,6 +54,15 @@ public class Java1_0Validator extends Validators {
5354
newUpgradeJavaMessage(
5455
"'assert' keyword is not supported.",ParserConfiguration.LanguageLevel.JAVA_1_4)));
5556

57+
finalValidatornoAssertIdentifer =newTreeVisitorValidator((node,reporter) -> {
58+
if (nodeinstanceofNodeWithIdentifier && ((NodeWithIdentifier)node).getIdentifier().equals("assert")) {
59+
reporter.report(
60+
node,
61+
newUpgradeJavaMessage(
62+
"'assert' identifier is not supported.",ParserConfiguration.LanguageLevel.JAVA_1_4,false));
63+
}
64+
});
65+
5666
finalValidatornoInnerClasses =newSimpleValidator<>(
5767
ClassOrInterfaceDeclaration.class,
5868
n -> !n.isTopLevelType(),

‎javaparser-core/src/main/java/com/github/javaparser/ast/validator/language_level_validations/Java1_4Validator.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ public class Java1_4Validator extends Java1_3Validator {
2828
publicJava1_4Validator() {
2929
super();
3030
remove(noAssertKeyword);
31+
add(noAssertIdentifer);
3132
}
3233
}

‎javaparser-core/src/main/javacc/java.jj‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3066,7 +3066,7 @@ String Identifier():
30663066
// Make sure the module info keywords don't interfere with normal Java parsing by matching them as normal identifiers.
30673067
<MODULE> | <REQUIRES> | <TO> | <WITH> | <OPEN> | <OPENS> | <USES> | <EXPORTS> | <PROVIDES> | <TRANSITIVE> |
30683068
// Make sure older Java versions parse
3069-
<ENUM> | <STRICTFP> | <YIELD> | <RECORD> | <PERMITS> | <SEALED> | <WHEN> | "_" |
3069+
<ENUM> | <STRICTFP> | <YIELD> | <RECORD> | <PERMITS> | <SEALED> | <WHEN> | "_" | <ASSERT> |
30703070
// An actual plain old identifier
30713071
<IDENTIFIER>
30723072
) { ret = token.image; setTokenKind(IDENTIFIER);}
@@ -4559,6 +4559,10 @@ Statement BlockStatement():
45594559
// just a restricted identifier and a yield statement can be confused with VariableDeclarationExpression sometimes
45604560
LOOKAHEAD( YieldStatement() )
45614561
ret = YieldStatement()
4562+
|
4563+
// try assert statement separate from more general Statement() because assert can be confused with VariableDeclarationExpression sometimes
4564+
LOOKAHEAD( AssertStatement() )
4565+
ret = AssertStatement()
45624566
|
45634567
LOOKAHEAD( VariableDeclarationExpression() )
45644568
expr = VariableDeclarationExpression()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp