Consider the following scenario:
public void someMethod() { }
I want to annotate someMethod() with a custom annotation say @CustomAnnotation
. I want to take take dynamic parameter into CustomAnnotation from the method parameter and induce a Compile time error if that parameter is not declared in the method signature.
Eg:
@CustomAnnotation(mandatorySample = "customId")
public void someMethod(String customId) {
}
For the above snippet, I want to throw Compile time error , if someMethod doesnot pass the "customId" parameter. The main goal is to take the dynamic id parameter from the method arguments or use a single parameter for the method and the annotation.
My Research: One solution, I have researched is to use injection through Argument position and getting the value from reflections. But still I can't induce compile time error in that.
I am open for #Java or #Spring based solution.
* More Explanation:
Lets say my annotation is
Public @interface Logger {
String mandatoryLogParam();
}
Let a method someLogMethod(...)
use my annotation. Consider the below cases.
** Invalid Usage **
@Logger(mandatoryLogParam = ‘myMandatoryGeneratedId’)
public void someLogMethod(String logMessage) {
}
MyExpectation: To throw compile error as
-> Method signature should includeparameter named “myMandatoryGeneratedId”
** Valid Usage: **
@Logger(mandatoryLogParam = ‘myMandatoryGeneratedId’)
public void someLogMethod(String logMessage, String myMandatoryGeneratedId) {
}
MyExpectation: No compile error is thrown.
My Goal here is to use the parameter that the method defines to be used as my internal annotation logic. At the same time inducing compile time check for developer using the annotation.
Observe the * 'myMandatoryGeneratedId' * usage in the above code.
Is this possible? Else any workarounds ?