Skip to content
This repository was archived by the owner on May 9, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2717,7 +2717,7 @@ public JCExpression variableInitializer() {

public JCExpression variableInitializer(JCExpression type) {
if (token.kind == LBRACE) {
if (type instanceof JCIdent && ((JCIdent) type).name.equals(names.fromString("Map"))) {
if (isMapType(type)) {
return mapInitializer(token.pos);
} else {
return arrayInitializer(token.pos, null);
Expand All @@ -2726,6 +2726,16 @@ public JCExpression variableInitializer(JCExpression type) {
return parseExpression();
}

private boolean isMapType(JCExpression type) {
final Object localType;
if (type instanceof JCTypeApply) {
localType = ((JCTypeApply) type).getType();
} else {
localType = type;
}
return (localType instanceof JCIdent && ((JCIdent) localType).name.equals(names.fromString("Map")));
}

private JCExpression mapInitializer(int pos) {
List<JCExpression> entries = mapEntries(pos);
return F.at(pos).NewMap(entries);
Expand All @@ -2734,8 +2744,11 @@ private JCExpression mapInitializer(int pos) {
private List<JCExpression> mapEntries(int pos) {
accept(LBRACE);
ListBuffer<JCExpression> entries = new ListBuffer<>();
if (token.kind != RBRACE) {
while (token.kind != RBRACE) {
entries.append(mapEntry(pos));
if (token.kind != RBRACE) {
accept(COMMA);
}
}
accept(RBRACE);
return entries.toList();
Expand Down
13 changes: 12 additions & 1 deletion test/jdk/java/util/Map/ViqueenSyntaxSugarMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ public void testEmptyMap() {
public void testSingletonMap() {
Map singleton = {
"sweden": "stockholm"
};
};
assertEquals(singleton.size(), 1);
}

@Test
public void testFullMapWithStringKeyValuePairs() {
Map<String, String> capitals = {
"norway": "oslo",
"australia": "canberra",
"morocco": "rabat",
"sweden": "stockholm"
};
assertEquals(capitals.size(), 4);
}
}