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 !
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With