Java лямбда + родовой + пользовательская аннотация - PullRequest
0 голосов
/ 18 декабря 2018

Я пытаюсь создать универсальную оболочку для лямбда-потребителя с возможностью объявить значение по умолчанию для параметров в пользовательской аннотации, как показано ниже.Можно ли обработать аннотацию из параметра лямбда-выражения, как в примере ниже?

Есть ли другая возможность создать функцию со значением параметров по умолчанию, объявленным в аннотации?

Большое спасибо!

// 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...
        };

     }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...