初始化静态变量时获取StackOverflowError

时间:2018-01-17 11:37:53

标签: java java-8

下面是我偶然发现的一段代码的最小可验证示例。

package test_package;

import java.util.function.Predicate;

public class TestClass {

  public static final String CONST_STR = " some_data "+ getMoreData(e -> e.getId()==3);

  private static String getMoreData(Predicate<TestEnum> p){
    StringBuilder sb = new StringBuilder();
    for(TestEnum e : TestEnum.values()){
      if(p.test(e)){
        sb.append(e.name());
      }
    }
    return sb.toString();
  }

  public static void main(String[] args) {
    System.out.println(CONST_STR);
  }
}

enum TestEnum{
  OP1(1),OP2(2),OP3(3);

  private final int id;

  public int getId() {
    return id;
  }

  TestEnum(int id){
    this.id = id;
  }
}

当我试图编译它给我的时候

  

错误:java:java.lang.StackOverflowError

有人能指出我在哪里出错吗?

我在尝试编译javac test_package/TestClass.java

时遇到错误
  

系统资源不足。请参阅以下堆栈跟踪   细节。 java.lang.StackOverflowError at   com.sun.tools.javac.code.Scope.dupUnshared(Scope.java:144)at at   com.sun.tools.javac.comp.Attr.lambdaEnv(Attr.java:2639)at at   com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2305)at at   com.sun.tools.javac.tree.JCTree $ JCLambda.accept(JCTree.java:1624)at at   com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)at at   com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435)at at   com.sun.tools.javac.tree.JCTree $ JCLambda.accept(JCTree.java:1624)at at   com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)at at   com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435)at at   com.sun.tools.javac.tree.JCTree $ JCLambda.accept(JCTree.java:1624)at at   com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)at at   com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435)at at   com.sun.tools.javac.tree.JCTree $ JCLambda.accept(JCTree.java:1624)at at   com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)at at   com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435)at at   com.sun.tools.javac.tree.JCTree $ JCLambda.accept(JCTree.java:1624)at at   com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)at at   com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435)at at   com.sun.tools.javac.tree.JCTree $ JCLambda.accept(JCTree.java:1624)at at   com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)at at   com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435)at at   com.sun.tools.javac.tree.JCTree $ JCLambda.accept(JCTree.java:1624)at at   com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)at at   com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435)at at   com.sun.tools.javac.tree.JCTree $ JCLambda.accept(JCTree.java:1624)at at   com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)at at   com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435)at at   com.sun.tools.javac.tree.JCTree $ JCLambda.accept(JCTree.java:1624)at at   com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)at at   com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435)at at   com.sun.tools.javac.tree.JCTree $ JCLambda.accept(JCTree.java:1624)at at   com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)at at   com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435)at at   com.sun.tools.javac.tree.JCTree $ JCLambda.accept(JCTree.java:1624)...

我的版本是javac 1.8.0_112

感谢预期!

1 个答案:

答案 0 :(得分:10)

It's bug,已在javac-9中修复。我尝试了最新的javac-9,它不会重现。

要解决这个问题:

public static final String CONST_STR;

static {
    CONST_STR = " some_data " + getMoreData(e -> e.getId() == 3);
}

似乎编译得很好......