diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java index b4dfb04766c..d90c0f1e32a 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java @@ -3434,9 +3434,17 @@ public class JavacParser implements Parser { case GT: typeDepth--; if (typeDepth == 0 && !peekToken(lookahead, DOT)) { - return peekToken(lookahead, LAX_IDENTIFIER) || - peekToken(lookahead, tk -> tk == LPAREN) ? PatternResult.PATTERN - : PatternResult.EXPRESSION; + if (peekToken(lookahead, LAX_IDENTIFIER) || + peekToken(lookahead, tk -> tk == LPAREN)) { + return PatternResult.PATTERN; + } + else if (peekToken(lookahead, LBRACKET) || + peekToken(lookahead, MONKEYS_AT)) { + break; + } + else { + return PatternResult.EXPRESSION; + } } else if (typeDepth < 0) return PatternResult.EXPRESSION; break; case MONKEYS_AT: diff --git a/test/langtools/tools/javac/patterns/DisambiguatePatterns.java b/test/langtools/tools/javac/patterns/DisambiguatePatterns.java index d6180c1810f..82a2e91d946 100644 --- a/test/langtools/tools/javac/patterns/DisambiguatePatterns.java +++ b/test/langtools/tools/javac/patterns/DisambiguatePatterns.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -113,6 +113,26 @@ public class DisambiguatePatterns { ExpressionType.PATTERN); test.disambiguationTest("R(int x) when (x > 0)", ExpressionType.PATTERN); + test.disambiguationTest("java.util.List[] p", + ExpressionType.PATTERN); + test.disambiguationTest("java.util.List[][] p", + ExpressionType.PATTERN); + test.disambiguationTest("a>[] d", + ExpressionType.PATTERN); + test.disambiguationTest("java.util.List[] p when true", + ExpressionType.PATTERN); + test.disambiguationTest("java.util.List @Ann [] p", + ExpressionType.PATTERN); + test.disambiguationTest("a> @Ann [] d", + ExpressionType.PATTERN); + test.disambiguationTest("(java.util.List[]) o", + ExpressionType.EXPRESSION); + test.disambiguationTest("String[].class", + ExpressionType.EXPRESSION); + test.disambiguationTest("new int[1][]", + ExpressionType.EXPRESSION); + test.disambiguationTest("new java.util.List[1][]", + ExpressionType.EXPRESSION); } private final ParserFactory factory;