brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Aled Sage (JIRA)" <>
Subject [jira] [Created] (BROOKLYN-448) Support java 8 lambdas as config key values
Date Mon, 06 Mar 2017 18:03:32 GMT
Aled Sage created BROOKLYN-448:

             Summary: Support java 8 lambdas as config key values
                 Key: BROOKLYN-448
             Project: Brooklyn
          Issue Type: Improvement
    Affects Versions: 0.10.0
            Reporter: Aled Sage
            Priority: Minor

It would be nice if users could write Java 8 lambda expressions, and use these as config values
when writing/using Java-based entities.

However, the persistence (via xstream version 1.4.8) does not work correctly for it.

According to, xstream could not support lambdas in 1.4.7
but this was fixed in 1.4.8 (i.e. fixed in the version we *are* using). I haven't investigated
further as to why it's not serializing as we'd expect.

Below is the persisted state file that was written by my test entity. Note the "predicate"
config value is null.

        <typeToken class="$SimpleTypeToken">
          <runtimeType class="$ParameterizedTypeImpl">
        <constraint class="$ObjectPredicate">ALWAYS_TRUE</constraint>

Below is my Java 8 test to illustrate this: {{testLambdaConfigValue}} fails because the retrieved
config key value is null after rebind. For comparison, the tests {{testAnonymousInnerClassConfigValue}}
and {{testNonStaticInnerClassConfigValue}} fail at rebind-time with {{ConversionException:
Could not call : null}}.

public class LambdaConfigTest extends RebindTestFixtureWithApp {

	public static final ConfigKey<Predicate<? super String>> PREDICATE = ConfigKeys.newConfigKey(
			new TypeToken<Predicate<? super String>>() {},

	public void testLambdaConfigValue() throws Exception {
		Predicate<? super String> val = p -> p.startsWith("val");

	// Assumes the predicate matches "val1", but not "different1"
	protected void runPredicateConfigValue(Predicate<? super String> val) throws Exception
		Entity entity = app().addChild(EntitySpec.create(TestEntity.class)
				.configure(PREDICATE, val));
		List<String> all = ImmutableList.of("val1", "different1");
		List<String> expectedFiltered = ImmutableList.of("val1");
		List<String> filtered = filter(all, entity.config().get(PREDICATE));
		assertEquals(filtered, expectedFiltered);
		Entity newEntity = Iterables.getOnlyElement(app().getChildren());
		List<String> newFiltered = filter(all, newEntity.config().get(PREDICATE));
		assertEquals(newFiltered, expectedFiltered);

	private List<String> filter(List<String> vals, Predicate<? super String>
tester) {
		List<String> result = Lists.newArrayList();
		for (String val : vals) {
			if (tester.test(val)) {
		return result;
	public void testAnonymousInnerClassConfigValue() throws Exception {
		Predicate<? super String> val = new Predicate<String>() {
			@Override public boolean test(String input) {
				return input.startsWith("val");

	public void testNonStaticInnerClassConfigValue() throws Exception {
		class MyNontStaticPredicate implements Predicate<String> {
			@Override public boolean test(String input) {
				return input.startsWith("val");
		runPredicateConfigValue(new MyNontStaticPredicate());

	public void testStaticInnerClassConfigValue() throws Exception {
		runPredicateConfigValue(new MyStaticPredicate());
	static class MyStaticPredicate implements Predicate<String> {
		@Override public boolean test(String input) {
			return input.startsWith("val");

This message was sent by Atlassian JIRA

View raw message