Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting StackOverflowError while initializing a static variable

Tags:

java

java-8

Below is the Minimal Verifiable Example of a piece of code which i stumbled upon.

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;
  }
}

when i am trying to compile it its giving me

Error:java: java.lang.StackOverflowError

can anyone point me where i am going wrong in it ?

I am getting below error when trying to compile javac test_package/TestClass.java

The system is out of resources. Consult the following stack trace for details. java.lang.StackOverflowError at com.sun.tools.javac.code.Scope.dupUnshared(Scope.java:144) at com.sun.tools.javac.comp.Attr.lambdaEnv(Attr.java:2639) at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2305) at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576) at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435) at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576) at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435) at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576) at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435) at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576) at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435) at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576) at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435) at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576) at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435) at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576) at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435) at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576) at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435) at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576) at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435) at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576) at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2435) at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624)...

and my version is javac 1.8.0_112

Thanks in Anticipation !

like image 502
sumit Avatar asked Jan 17 '18 11:01

sumit


1 Answers

It's bug, fixed in javac-9. I tried the latest javac-9 and it would not reproduce.

To get around that you could do:

public static final String CONST_STR;

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

which seems to compile just fine...

like image 195
Eugene Avatar answered Sep 22 '22 17:09

Eugene