如果使用lambda表达式,则注释为null

时间:2019-06-28 16:32:15

标签: java lambda annotations

如果我在由注解签名的方法中使用lambda表达式,则注解为null(没有lambda就可以了)。

@Data
public class User {

    @NonNull
    private String fgder;
}

带有注释和lambda表达式的方法。

public class test1 {
    @Person(name = "gftrje", surname = "dgbfekft")
    public boolean perform() {
        User user = new User("efggf");
        Optional.ofNullable(user).ifPresent(user1 -> System.out.println("test")); //with this or any other lambda "null.." without lambda all it's fine "test2"
        System.out.println("test2");
        return true;

    }
} 

注释类..

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Person {

    String name();

    String surname();

    int phone() default 1;
}

主要内容并获取注释。

public class main {
    public static void main(String[] args) {
        test2.perform(new test1());
    }
}

public class test2 {
    public static void perform(test1 test1) {
        for (Method method : test1.getClass().getDeclaredMethods()) {
            method.setAccessible(true);

            Person annotation = method.getDeclaredAnnotation(Person.class);
            if (annotation == null) System.out.println("null.."); //this if lambda exist 
            //code...
        }
    }
} 

为什么会这样?

0 个答案:

没有答案