How to retain annotation of default method of interface after implementing and overriding said method

When I am overriding a default method of an interface which is annotated, the annotation is removed from the child method. I want to retain the annotation in the child even when the method is overridden. What is the reason for it getting removed?

MyAnnotation.java

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation {     public String config(); } 

MyInterface,java

public interface MyInterface {          @MyAnnotation(config="myConfig")     public default String getMyVal() {         return null;     }          @MyAnnotation(config="myConfig")     public default String getMyVal2() {         return null;     } } 

MyModel.java

public class MyModel implements MyInterface {     @Override     public String getMyVal() {         return "MyVal";     } } 

MainRunner.java

public class MainRunner {     public static void main(String[] args) {         Arrays.stream(MyModel.class.getMethods()).forEach(                 method -> Arrays.stream(method.getAnnotations()).forEach(ann-> System.out.println(method + " --- " + ann.annotationType()))                 );     } } 

Output

public native int java.lang.Object.hashCode() — interface jdk.internal.HotSpotIntrinsicCandidate public final native java.lang.Class java.lang.Object.getClass() — interface jdk.internal.HotSpotIntrinsicCandidate public final native void java.lang.Object.notify() — interface jdk.internal.HotSpotIntrinsicCandidate public final native void java.lang.Object.notifyAll() — interface jdk.internal.HotSpotIntrinsicCandidate public default java.lang.String com.javaconcepts.annotations.MyInterface.getMyVal2() — interface com.javaconcepts.annotations.MyAnnotation

Appreciate the help. Thanks.

PS: I tried searching but having overridden and annotations in the search gave results for overridden annotation.

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.