Embedding five conditional statements

Is it possible to embed more than 5 "Conditional statements" in Java. I ask because it seems that I am throwing a compiler exception when I try to compile this code:

public Object getValue() { return number != null ? number : string != null ? string : bool != null ? bool : date != null ? date : list != null ? list : null; } 

I narrowed it down to this code, because if I comment on the last line, it seems to compile fine.

 public Object getValue() { return number != null ? number : string != null ? string : bool != null ? bool : date != null ? date : // list != null ? list : null; } 

Does anyone else know if this is a limitation of the java compiler or if I turn to false conclusions, it would be great if someone else tried to reproduce this. If anyone is interested, I reproduced and posted Stack Trace from the compiler here https://gist.github.com/919284 .

Please note that this is a very likely error in the compiler, not my code, as the output says “Please File a Bug on the Java Developer Connect website” (or something similar). I ask here because I'm not sure what this error report will contain.


EDIT: Chris L reproduced this, see his answer

+4
source share
6 answers

I reproduced your error (using Sun JDK 1.6.0_24 on Mac). I simplified your class a bit:

 import java.util.ArrayList; import java.util.Date; public class Test3 { private Number number; private String string; private Boolean bool; // Replace Boolean with Object, and it compiles! private Date date; private ArrayList<String> list; // Replace ArrayList with List, and it // compiles! public Object getValue() { return number != null ? number : string != null ? string : bool != null ? bool : date != null ? date : list != null ? list : null; } } 

My stack trace is basically the same as yours. (By the way, this has nothing to do with GWT.)

 An exception has occurred in the compiler (1.6.0_24). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you. java.lang.AssertionError at com.sun.tools.javac.jvm.Code$State.forceStackTop(Code.java:1688) at com.sun.tools.javac.jvm.Gen.visitConditional(Gen.java:1679) at com.sun.tools.javac.tree.JCTree$JCConditional.accept(JCTree.java:1021) at com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:818) at com.sun.tools.javac.jvm.Gen.visitConditional(Gen.java:1678) at com.sun.tools.javac.tree.JCTree$JCConditional.accept(JCTree.java:1021) at com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:818) at com.sun.tools.javac.jvm.Gen.visitConditional(Gen.java:1678) at com.sun.tools.javac.tree.JCTree$JCConditional.accept(JCTree.java:1021) at com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:818) at com.sun.tools.javac.jvm.Gen.visitReturn(Gen.java:1626) at com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1138) at com.sun.tools.javac.jvm.Gen.genDef(Gen.java:665) at com.sun.tools.javac.jvm.Gen.genStat(Gen.java:700) at com.sun.tools.javac.jvm.Gen.genStat(Gen.java:686) at com.sun.tools.javac.jvm.Gen.genStats(Gen.java:737) at com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1013) at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:739) at com.sun.tools.javac.jvm.Gen.genDef(Gen.java:665) at com.sun.tools.javac.jvm.Gen.genStat(Gen.java:700) at com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:893) at com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:866) at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:639) at com.sun.tools.javac.jvm.Gen.genDef(Gen.java:665) at com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2198) at com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:617) at com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1289) at com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1259) at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:765) at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:730) at com.sun.tools.javac.main.Main.compile(Main.java:353) at com.sun.tools.javac.main.Main.compile(Main.java:279) at com.sun.tools.javac.main.Main.compile(Main.java:270) at com.sun.tools.javac.Main.compile(Main.java:69) at com.sun.tools.javac.Main.main(Main.java:54) 
+1
source

I can only confirm that this compiles without errors for me in both eclipse 3.5 and javac 1.6.0_u24:

 public class Test { Object number=null, string=null, bool=null, date=null, list=null; public Object getValue() { return number != null ? number : string != null ? string : bool != null ? bool : date != null ? date : list != null ? list : null; } } 
+1
source

There is no limit. The method must be less than 64 KB of byte code.

I compiled your example in order. Is there a reason you don't have just one field?

EDIT: Added setters to validate types.

 public class Holder implements Serializable { Serializable value; public void setValue(Number value) { this.value = value; } public void setValue(String value) { this.value = value; } public void setValue(Boolean value) { this.value = value; } public void setValue(Date value) { this.value = value; } public <L extends List & Serializable> void setValue(L value) { this.value = value; } public Serializable getValue() { return value; } } 
+1
source

This compiles perfectly on ideone:

  public static void main (String[] args) throws java.lang.Exception { Object number = null; Object string = null; Object list = null; Object bool = null; Object date = null; Object o = number != null ? number : string != null ? string : bool != null ? bool : date != null ? date : list != null ? list : null; } 

Double check that list declared so that it is accessible inside the method.


May be a bug in your java compiler. I suggest you upgrade your Java to the latest and largest (if any) and play. You can install as many different versions of Java as you want.

+1
source

I do not think there is a limitation, although it is syntactically correct. I would suggest that the java compiler will simply expand its parsing tree, as for deep if / else if-nesting.

+1
source

I know this is an old post, but my recent experiment may shed light on this topic for those of you who are interested. This is what you need to know.

Basically, I “broke” some existing code by running Comparable in one of my other classes. Here is a stripped-down version that generates the same "Exception occurred in the compiler ..."

If the nested conditional expression has less than 5 expressions, or if the USDollars class does not implement Comparable, this code compiles.

 public class TestHit { protected final String fSymbol; protected final long fTime; protected final USDollars fBasePrice; public TestHit(String aSymbol, long aTime, int aBasePrice) { fSymbol = aSymbol; fTime = aTime; fBasePrice = new USDollars(aBasePrice); } public Object field(int aIndex) { return (aIndex == 0)? fSymbol : (aIndex == 1)? fTime : (aIndex == 2)? fBasePrice : (aIndex == 3)? new Integer(4) // comment out this line and it compiles : "?"; } } final class USDollars implements Comparable<USDollars> // comment out this line and it compiles { private int cents; public USDollars() { this(0); } public USDollars(int cents) { this.cents = cents; } public USDollars(int dollars, int cents) { this(cents + 100*dollars); } public int cents() { return cents; } // @Override public int compareTo(USDollars other) { return this.cents - other.cents; } } 

By the way, a quick fix was to change the code as follows (ugly, but it works):

 public Object field(int aIndex) { if (aIndex == 2) return fBasePrice; return (aIndex == 0)? fSymbol : (aIndex == 1)? fTime : (aIndex == 3)? new Integer(4) // comment out this line and it compiles : "?"; } 
+1
source

Source: https://habr.com/ru/post/1348071/


All Articles