Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

Changing Java annotation parameter default

I use a library that provides an annotation @LibraryAnnotation with a Boolean parameter parameter. The default is set to true by the authors of the library.

Unfortunately, in almost all my use cases, I prefer the annotation with parameter = false. To achieve the desired behavior, I use @LibraryAnnotation(parameter=false) instead of @LibraryAnnotation every time. This is error prone since one easily forgets to set the parameter.

I am wondering if there is a way to simplify things by changing the default value for the parameter of @LibraryAnnotation or by defining a custom annotation that behaves like @LibraryAnnotation(parameter=false), but does not require setting a parameter.

(In this particular case, the library is lombok, but the question could be relevant for parametrized annotations provided by any library. Update: Lombok offers default parameter configuration via a lombok.config file, but I am still interested in a general solution.)

question from:https://stackoverflow.com/questions/65857605/changing-java-annotation-parameter-default

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can define your own annotation and use that instead. Something like:

@LibraryAnnotation(parameter = false)
public @interface MyLibraryAnnotationFalse {
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...