views:

39

answers:

1

Is it possible to force (at compile-time) an annotated Class to have a an annotated Field?

I have this annotations:

@Target(value = ElementType.TYPE)
@interface MyClass {}

@Target(value = ElementType.FIELD)
@interface MyField {}

and now I would like that the compilation of a Class like this fails:

@MyClass
class Customer {

}

whereas this should work:

@MyClass
class Customer {
   @MyField
   String text;
}
+1  A: 

Yes, using apt (annotation processing tool).

But for such simply tasks it will be an overkill.

You can make some listener that is triggered on application startup and checks this requirement.

Finally, you can avoid this by assuming reasonable defaults - i.e. if the class is annotated, then consider all fields annotated as well, with the default values.

Bozho