Skip to content

Commit

Permalink
Improving error message for attributes with default value.
Browse files Browse the repository at this point in the history
  • Loading branch information
lahodaj committed Jul 3, 2024
1 parent ad11674 commit 7a0c286
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5018,10 +5018,11 @@ protected JCTree methodDeclaratorRest(int pos,
if (token.kind == DEFAULT) {
accept(DEFAULT);
defaultValue = annotationValue();
accept(SEMI);
} else {
defaultValue = null;
accept(SEMI, tk -> Errors.Expected2(LBRACE, SEMI));
}
accept(SEMI, tk -> Errors.Expected2(LBRACE, SEMI));
if (token.pos <= endPosTable.errorEndPos) {
// error recovery
// look if there is a probable missing opening brace,
Expand Down
39 changes: 39 additions & 0 deletions test/langtools/tools/javac/parser/JavacParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2661,6 +2661,45 @@ public class TestB {
}""");
}

@Test //JDK-8324859
void testImplicitlyDeclaredClassesConfusion7() throws IOException {
//after 'default' attribute value, only semicolon (';') is expected,
//not left brace ('{'):
String code = """
package tests;
public @interface A {
public String value() default ""
}
""";
DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll,
List.of("--enable-preview", "--source", SOURCE_VERSION),
null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();

List<String> codes = new LinkedList<>();

for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
codes.add(d.getLineNumber() + ":" + d.getColumnNumber() + ":" + d.getCode());
}

assertEquals("testImplicitlyDeclaredClassesConfusion5: " + codes,
List.of("3:37:compiler.err.expected"),
codes);
String result = toStringWithErrors(cut).replaceAll("\\R", "\n");
System.out.println("RESULT\n" + result);
assertEquals("incorrect AST",
result,
"""
package tests;
\n\
public @interface A {
\n\
public String value() default "";
}""");
}

void run(String[] args) throws Exception {
int passed = 0, failed = 0;
final Pattern p = (args != null && args.length > 0)
Expand Down

0 comments on commit 7a0c286

Please sign in to comment.