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

Cannot assign a value to final variable in lambda

    Details

    • Subcomponent:
    • Resolved In Build:
      b26
    • CPU:
      x86_64
    • OS:
      windows_7
    • Verification:
      Verified

      Backports

        Description

        FULL PRODUCT VERSION :
        java version "1.8.0_20-ea"
        Java(TM) SE Runtime Environment (build 1.8.0_20-ea-b23)
        Java HotSpot(TM) 64-Bit Server VM (build 25.20-b22, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [versie 6.1.7601]

        EXTRA RELEVANT SYSTEM CONFIGURATION :
        Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)
        Maven home: D:\apache-maven-3.0.5\bin\..
        Java version: 1.8.0_20-ea, vendor: Oracle Corporation
        Java home: c:\Program Files\Java\jdk1.8.0_20\jre
        Default locale: nl_NL, platform encoding: Cp1252
        OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"

        A DESCRIPTION OF THE PROBLEM :
        As registered at https://jira.codehaus.org/browse/MCOMPILER-228
        Code example compiles in Eclipse, but not with Maven Compiler Plugin. Code is like below (inside a lamda expression)

        final x;
        if (some condition)
        x = a;
        else if (some other condition)
        x = b;
        else
        x = c;



        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project MediaIndexer: Compilation failure: Compilation failure:
        [ERROR] /C:/MAPS/MediaIndexer/src/main/java/example/FinalExample.java:[11,13] cannot assign a value to final variable compareTo
        [ERROR] /C:/MAPS/MediaIndexer/src/main/java/example/FinalExample.java:[14,13] cannot assign a value to final variable compareTo
        [ERROR] /C:/MAPS/MediaIndexer/src/main/java/example/FinalExample.java:[17,13] cannot assign a value to final variable compareTo

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package example;

        import java.util.Comparator;

        public class FinalExample {
            public static final Comparator<String> comparator = (o1, o2) -> {
                final String desc1 = o1;
                final String desc2 = o2;
                final int compareTo;
                if (desc1 == null) {
                    compareTo = -1;
                }
                else if (desc2 == null) {
                    compareTo = 1;
                }
                else {
                    compareTo = desc1.compareTo(desc2);
                }
                if (compareTo == 0) {
                    return 3;
                }
                return compareTo;
            };
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        The workaround is to remove final for the variable and just ensure that the value is assigned in each code path.

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  mcimadamore Maurizio Cimadamore
                  Reporter:
                  webbuggrp Webbug Group
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  7 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: