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 @@ -2712,7 +2712,24 @@ List<JCExpression> arrayInitializerElements(int newpos, JCExpression t) {
/** VariableInitializer = ArrayInitializer | Expression
*/
public JCExpression variableInitializer() {
return token.kind == LBRACE ? arrayInitializer(token.pos, null) : parseExpression();
return this.variableInitializer(null);
}

public JCExpression variableInitializer(JCExpression type) {
if (token.kind == LBRACE) {
if (type instanceof JCIdent && ((JCIdent) type).name.equals(names.fromString("Map"))) {
return mapInitializer(token.pos);
} else {
return arrayInitializer(token.pos, null);
}
}
return parseExpression();
}

private JCExpression mapInitializer(int pos) {
accept(LBRACE);
accept(RBRACE);
return F.at(pos).NewMap();
}

/** ParExpression = "(" Expression ")"
Expand Down Expand Up @@ -3699,7 +3716,7 @@ JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression ty

if (token.kind == EQ) {
nextToken();
init = variableInitializer();
init = variableInitializer(type);
}
else if (reqInit) syntaxError(token.pos, Errors.Expected(EQ));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,17 @@ public JCNewArray NewArray(JCExpression elemtype,
return tree;
}

public JCExpression NewMap() {
JCFieldAccess util = new JCFieldAccess(Ident(names.fromString("java")), names.fromString("util"), null);
JCFieldAccess map = new JCFieldAccess(util, names.fromString("Map"), null);
JCFieldAccess of = new JCFieldAccess(map, names.fromString("of"), null);

JCMethodInvocation invocation = new JCMethodInvocation(List.nil(), of, List.nil());
invocation.pos = pos;

return invocation;
}

public JCLambda Lambda(List<JCVariableDecl> params,
JCTree body)
{
Expand Down
18 changes: 18 additions & 0 deletions test/jdk/java/util/Map/ViqueenSyntaxSugarMapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* @test
* @run testng ViqueenSyntaxSugarMapTest
* @author Hasnae R.
*/
import java.util.Map;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class ViqueenSyntaxSugarMapTest {
@Test
public void testEmptyMap() {
Map empty = {};
assertTrue(empty.isEmpty());
}
}