Skip to content

Commit

Permalink
8294431: jshell reports error on initialisation of static final field…
Browse files Browse the repository at this point in the history
… of anonymous class

Reviewed-by: sundar
  • Loading branch information
lahodaj committed Oct 3, 2022
1 parent 6e8f038 commit 8e9cfeb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/jdk.jshell/share/classes/jdk/jshell/Eval.java
Expand Up @@ -523,8 +523,9 @@ private Pair<Wrap, Wrap> anonymous2Member(ExpressionInfo ei,
if (member.getKind() == Tree.Kind.VARIABLE) {
VariableTree vt = (VariableTree) member;

if (vt.getInitializer() != null) {
//for variables with initializer, explicitly move the initializer
if (vt.getInitializer() != null &&
!vt.getModifiers().getFlags().contains(Modifier.STATIC)) {
//for instance variables with initializer, explicitly move the initializer
//to the constructor after the captured variables as assigned
//(the initializers would otherwise run too early):
Range wholeVar = dis.treeToRange(vt);
Expand Down
8 changes: 6 additions & 2 deletions test/langtools/jdk/jshell/VariablesTest.java
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,7 +23,7 @@

/*
* @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
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
Expand Down Expand Up @@ -617,4 +617,8 @@ public void varIntersection() {
assertEval("v.run()");
}

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

}

0 comments on commit 8e9cfeb

Please sign in to comment.