Lambdas not integrated with annotations

Millies, Sebastian Sebastian.Millies at softwareag.com
Wed Jul 30 11:37:44 UTC 2014


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