Lambdas not integrated with annotations

Zhong Yu zhong.j.yu at gmail.com
Wed Jul 30 17:30:17 UTC 2014


If annotation values could be expression ASTs, we would just write

    @ConditionalIgnore( condition =
!System.getProperty("os.name").startsWith("Windows") )

therefore the issue is not really about lambda.


The blog article uses a Class to map to a runtime value through
reflection, however, there's no enough static checking to guarantee
that it'll work at runtime. We could as well do something like

    final boolean notWindows =
!System.getProperty("os.name").startsWith("Windows") ;

    @ConditionalIgnore( condition = "notWindows" )  // referencing a
field by name

Of course, we can always add annotation preprocessors to strengthen
static checking, but it's probably not worth it.


Zhong Yu
http://bayou.io  (async http server for java8)

On Wed, Jul 30, 2014 at 6:37 AM, Millies, Sebastian
<Sebastian.Millies at softwareag.com> wrote:
> Hello there,
>
> why has it not been made possible to supply lambdas as annotation parameters? Would it have required too many changes to the language spec? Not deemed worthwhile?
>
> The example I have in mind is a JUnit rule to conditionally ignore tests, see http://www.codeaffine.com/2013/11/18/a-junit-rule-to-conditionally-ignore-tests/
> It would have been nice if instead of having to specify a concrete class implementing an interface and then doing complicated magic in the annotation implementation to instantiate it, like so:
>
> @Rule
> public ConditionalIgnoreRule rule = new ConditionalIgnoreRule();
> @Test
> @ConditionalIgnore( condition = NotRunningUnderWindows.class )
>     public void testFocus() {
>     }
> }
>
> public class NotRunningOnWindows implements BooleanSupplier {
>     public boolean getAsBoolean() {
>         return !System.getProperty( "os.name" ).startsWith( "Windows" );
>     }
> }
>
> one could just have written
>
> @Test
> @ConditionalIgnore( condition = () -> !System.getProperty("os.name").startsWith("Windows") )
>     public void testFocus() {
>     }
> }
>
> This does simplify the client code (the JUnit test) only a bit, but it would greatly simplify the annotation implementation.
>
> Sebastian Millies
> PPM, Saarbrücken C1.67, +49 681 210 3221
>
>
> Software AG – Sitz/Registered office: Uhlandstraße 12, 64297 Darmstadt, Germany – Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. Andreas Bereczky - http://www.softwareag.com
>
>


More information about the lambda-dev mailing list