Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8163026

javac generates wrong bytecode for "ternary expression" /w java.lang.Integer

XMLWordPrintable

    • x86_64
    • linux_ubuntu

      FULL PRODUCT VERSION :
      java version "1.8.0_101"
      Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux XXX 4.2.0-42-generic #49-Ubuntu SMP Tue Jun 28 21:26:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      Using the "ternary expression" in conjunction with java.lang.Integer can cause invalid bytecode generation

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Using the "ternary expression" as nullcheck, see test-case

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Every test should run without errors
      ACTUAL -
      testIntegerHashCode and testIntegerBox throwing a NullPointerException

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import org.junit.Test;

      public class Ternary {
      @Test
      public void testObjectHashCode() {
      Object o = null;
      Object r = (null == o) ? o : o.hashCode();
      }

      @Test
      public void testIntegerHashCode() {
      Integer o = null;
      Object r = (null == o) ? o : o.hashCode();
      }

      @Test
      public void testIntegerBox() {
      Integer o = null;
      Object r = (null == o) ? o : -o;
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Just don't use the "ternary expression"

            sadayapalam Srikanth Adayapalam (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: