Skip to content

Commit 8e9cfeb

Browse files
committed
8294431: jshell reports error on initialisation of static final field of anonymous class
Reviewed-by: sundar
1 parent 6e8f038 commit 8e9cfeb

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/jdk.jshell/share/classes/jdk/jshell/Eval.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,9 @@ private Pair<Wrap, Wrap> anonymous2Member(ExpressionInfo ei,
523523
if (member.getKind() == Tree.Kind.VARIABLE) {
524524
VariableTree vt = (VariableTree) member;
525525

526-
if (vt.getInitializer() != null) {
527-
//for variables with initializer, explicitly move the initializer
526+
if (vt.getInitializer() != null &&
527+
!vt.getModifiers().getFlags().contains(Modifier.STATIC)) {
528+
//for instance variables with initializer, explicitly move the initializer
528529
//to the constructor after the captured variables as assigned
529530
//(the initializers would otherwise run too early):
530531
Range wholeVar = dis.treeToRange(vt);

test/langtools/jdk/jshell/VariablesTest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8144903 8177466 8191842 8211694 8213725 8239536 8257236 8252409
26+
* @bug 8144903 8177466 8191842 8211694 8213725 8239536 8257236 8252409 8294431
2727
* @summary Tests for EvaluationState.variables
2828
* @library /tools/lib
2929
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -617,4 +617,8 @@ public void varIntersection() {
617617
assertEval("v.run()");
618618
}
619619

620+
public void varAnonymousClassAndStaticField() { //JDK-8294431
621+
assertEval("var obj = new Object() { public static final String msg = \"hello\"; };");
622+
}
623+
620624
}

0 commit comments

Comments
 (0)