8294431: jshell reports error on initialisation of static final field of anonymous class

Reviewed-by: sundar
This commit is contained in:
Jan Lahoda 2022-10-03 07:23:42 +00:00
parent 6e8f0387d6
commit 8e9cfeb17c
2 changed files with 9 additions and 4 deletions

View File

@ -523,8 +523,9 @@ class Eval {
if (member.getKind() == Tree.Kind.VARIABLE) { if (member.getKind() == Tree.Kind.VARIABLE) {
VariableTree vt = (VariableTree) member; VariableTree vt = (VariableTree) member;
if (vt.getInitializer() != null) { if (vt.getInitializer() != null &&
//for variables with initializer, explicitly move the initializer !vt.getModifiers().getFlags().contains(Modifier.STATIC)) {
//for instance variables with initializer, explicitly move the initializer
//to the constructor after the captured variables as assigned //to the constructor after the captured variables as assigned
//(the initializers would otherwise run too early): //(the initializers would otherwise run too early):
Range wholeVar = dis.treeToRange(vt); Range wholeVar = dis.treeToRange(vt);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 8144903 8177466 8191842 8211694 8213725 8239536 8257236 8252409 * @bug 8144903 8177466 8191842 8211694 8213725 8239536 8257236 8252409 8294431
* @summary Tests for EvaluationState.variables * @summary Tests for EvaluationState.variables
* @library /tools/lib * @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api * @modules jdk.compiler/com.sun.tools.javac.api
@ -617,4 +617,8 @@ public class VariablesTest extends KullaTesting {
assertEval("v.run()"); assertEval("v.run()");
} }
public void varAnonymousClassAndStaticField() { //JDK-8294431
assertEval("var obj = new Object() { public static final String msg = \"hello\"; };");
}
} }