Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to solve ambiguous by annotations in Java [duplicate]

I have two methods that in some cases have ambiguous call. For example:

void bla(Integer a);
void bla(String a);

Basically when I call bla(null) I will get ambiguous call error. Is it possible to write or use some annotation that will solve this issue before the compiler phase?

Can I block a null option from in bla(String a) before the compiler kicks in? I tried to wrote a NotNull annotation (but it kicks in after the compile phase).

See code below:

@Documented
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
public @interface NotNull1 {
    String value() default "";
}

void bla(@NotNull1 String a);
like image 292
Avi Levin Avatar asked Feb 07 '23 21:02

Avi Levin


1 Answers

Cast null to the type of the parameter of the method.

bla((Integer) null);
bla((String) null);
like image 149
SOFe Avatar answered Feb 10 '23 09:02

SOFe