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

Annotations with lambda expressions has parameter result in wrong behavior.

XMLWordPrintable

    • b123
    • generic
    • generic
    • Verified

        FULL PRODUCT VERSION :
        Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Linux 3.2.0-97-generic #137-Ubuntu SMP Thu Dec 17 18:11:47 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux


        A DESCRIPTION OF THE PROBLEM :
        Having static fields in an annotation type initialized by a lambda expression results in the annotations not being available in runtime.
        This seems to happen in specific cases only. The following static fields work without problems:
        Consumer<Integer> f2 = new B(); //with B a class implementing Consumer<Integer>
        Consumer<Integer> f3 = C::eat; //with C::eat begin a static method
        int f4 = 5;
        Supplier<Integer> f5 = ()->5;
        Consumer<Integer> f6 = new Consumer<Integer>() {
            public void accept(Integer t) {}
        };

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Compile the unit test, execute.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The test succeeds. The annotation can be retrieved using reflection.
        ACTUAL -
         The test fails. the annotation is not available.

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.lang.annotation.Retention;
        import java.lang.annotation.RetentionPolicy;
        import java.util.function.Consumer;

        import junit.framework.TestCase;

        @i
        public class AnnotationWithStaticLambda extends TestCase {
        public void testAnnotationWithStaticLambda() throws Exception {
        assertEquals(1, myAnnotMinimal2.class.getAnnotations().length);
        }

        }
        @Retention(RetentionPolicy.RUNTIME)
        @interface i {
        Consumer<Integer> f1 = a -> {return;}; // -> 0
        }

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

        CUSTOMER SUBMITTED WORKAROUND :
        Using an instance of a class instead of a lambda works.

              srastogi Shilpi Rastogi (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: