Kanagavelu Sugumar
Kanagavelu Sugumar

Reputation: 19260

Force annotated field to contain annotated class in java

This is just opposite to Force annotated Class to contain annotated Field.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ContainsNonEditableFields {}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NonEditable {}

If any of the class field is annotated with @NonEditable; then how to force that class should be annotated with @ContainsNonEditableFields ?

Upvotes: 1

Views: 321

Answers (1)

Ralph
Ralph

Reputation: 120801

This can be done with AspectJ.

But the Key Idea is to use AspectJs declare warning or errors function, to declare a error for each field with @NonEditable annotation in a class that does not have a @ContainsNonEditableFields annotation.

(unfortunately I can not provide a working example out of the box, but I thing somebody at stackoverflow can help you if you ask the right question)

declare error : @annotation(NonEditable) &&
                   ! @annotation(ContainsNonEditableFields)      
                : "the class need ContainsNonEditableFields annotation";

This code is only some kind of scribble, it is not working aspectj syntax!

Upvotes: 1

Related Questions