Java Lambda +通用+自定义注释

时间:2018-12-17 23:51:43

标签: java generics lambda annotations variadic-functions

我正在尝试为lambda创建通用包装器-消费者可以在自定义批注中声明参数的默认值,如下所示。 是否可以像下面的示例一样处理来自lambda表达式参数的注释?

还有其他可能用注解中声明的默认参数值创建函数吗?

非常感谢!

// interface
interface TestInterf {
    void apply(Object ...obj);
}

// functional interface
@FunctionalInterface
public interface FourParameterConsumer<T, U, V, K> {
    public void apply(T te, U u, V v, K k);
}

// custom annotation
@Target(ElementType.PARAMETER) // is that OK in that case?
@Retention(RetentionPolicy.RUNTIME)
@interface def{
    String value() default "no-default";
}

// test class   
class Test{

    public static void main(String[] args) {

        // register new consumer with default parameters

        TestInterf myConsumer = registerFuncctionNew((Integer a, Integer b, @def("stroing hehe") String c, @def("x") Character d) -> {

            // ... body

            System.out.println("a : " + a.toString());
            System.out.println("b : " + b.toString());
            System.out.println("c : " + c);
            System.out.println("d : " + d.toString());

        });

        TestInterf myConsumer2 = registerFuncctionNew((Integer a, Integer b, String c, @def("z") Character d) -> {

            // ... body

        });
    }

    // function/consumer factory
    public static <T, U, V, K> TestInterf registerFuncction(FourParameterConsumer<T, U, V, K> fun){

       return new TestInterf(){

            // initial block
            {

                /* ----- here I would need to know what are def.class annotation values for parameters ------ */
                /*
                    e.g. I would need to process below annotations 

                    1. for myConsumer
                        @def("stroing hehe")
                        @def("x") 

                    2. for myConsumer2
                        @def("z")

                    is that possible?

                */
            }

            // ...

            // other things...
        };

     }
}

0 个答案:

没有答案