0

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 ?

Pramod
  • 11
  • 5
  • Is this the same idea as the `@NotNull` annotation? https://stackoverflow.com/questions/34094039/using-notnull-annotation-in-method-argument – markspace Aug 24 '18 at 19:15
  • `@NotNull` annotation makes sure the Value of the method Parameter is NotNull. But I need to mandate the method signature itself with the desired parameter. – Pramod Aug 24 '18 at 19:42
  • 1
    I'm not sure what "method signature" means in this context. Java is not a dynamic language where method parameters may be omitted. If you don't supply the String parameter you get a syntax error. Maybe this is an XY problem and you're thinking of something else? – markspace Aug 24 '18 at 19:47
  • I want the developers who is annotating with my annotation to make sure that particular parameter is declared part of the method signature. Hope it added you more clarity. – Pramod Aug 24 '18 at 19:51
  • Unfortunately it is not clear. Can you provide example of the 'correct code' that should not cause compile error and the code that should cause the error? – Roman-Stop RU aggression in UA Aug 24 '18 at 21:01
  • What it sounds like you're trying to do is ensure that a developer extends a type. For example an interface which defines the method `someMethod(String id)` and developers extend this type. I think if you explained the context of your problem a bit more we might be able to help you better. – markspace Aug 24 '18 at 22:18
  • @markspace and Roman , I have added some more explanation to the question. Please check *More explanation section in the question. – Pramod Aug 25 '18 at 00:07

0 Answers0